Test the Password Policy
Medium About 15 min Free, no account needed
You're given a correct password validator with four rules. Write tests that isolate each rule so a dropped one is caught.
The brief
The validator is already written and correct. Your job is the part that separates a thorough engineer from a careless one: proving each rule is really enforced by writing the tests.
Here is the code under test:
public class PasswordPolicy
{
// At least 8 characters, and at least one digit, one uppercase, and one lowercase letter.
public bool IsValid(string password)
{
if (password is null || password.Length < 8) return false;
bool hasDigit = false, hasUpper = false, hasLower = false;
foreach (char c in password)
{
if (char.IsDigit(c)) hasDigit = true;
else if (char.IsUpper(c)) hasUpper = true;
else if (char.IsLower(c)) hasLower = true;
}
return hasDigit && hasUpper && hasLower;
}
}Write a Tests class. Each public, parameterless method is one test: it passes if it runs
without throwing and fails when an assertion throws. A tiny assertion library is in scope (no
using needed):
Assert.True(condition); Assert.False(condition);
Assert.Equal(expected, actual); Assert.NotEqual(a, b);
Assert.Null(x); Assert.NotNull(x);
Assert.Throws<SomeException>(() => ...);Your suite is run twice over:
- Against the correct
PasswordPolicyabove, every one of your tests must pass. - Against a set of planted-bug "mutants", weakened policies you never see. A mutant is caught when at least one of your tests fails against it.
You pass when your suite is green on the correct code and catches at least 4 of the 5 mutants. Each mutant quietly drops one rule, loosens the length, or accepts a password that satisfies just one class. A single 'this strong password is accepted' test catches none of them. Break exactly one rule per test so the dropped rule has nowhere to hide. Stuck? Reveal the model suite to see what thorough looks like.
Starter code
// Write tests for the `PasswordPolicy` class. Each public, parameterless method is one test; it
// passes if it doesn't throw. Make your suite pass on the correct code AND catch the hidden buggy
// versions, the trick is to break exactly one rule per test, so a dropped rule has nowhere to hide.
public class Tests
{
public void A_password_meeting_every_rule_is_valid()
{
Assert.True(new PasswordPolicy().IsValid("Password1"));
}
// TODO: add a test that breaks only one rule each (too short, no digit, no upper, no lower).
}How it is graded
You write the tests. A suite passes only when it stays green on correct code and turns enough faulty implementations red.
What it practices
- Equivalence partitioning
- Cover each class of input and every branch with one deliberate case.
- Boundary value testing
- Pin behavior exactly at, just below and just above every boundary.
- Mutation testing
- Write tests strong enough to catch planted bugs, not just to pass.
Related katas
- Test the Caesar Cipher Medium Free You're given a correct Caesar cipher. Write the tests that pin wraparound, case, non-letters, and shift normalization.
- Test the Triangle Classifier Medium Free You're given a correct triangle classifier. Write the tests that cover every shape and the invalid cases.
- Test the Clamp Function Easy Free You're given a correct clamp function. Write the tests that cover below, above, inside, and the boundaries.
- Test the Progressive Tax Hard Free You're given a correct marginal income-tax calculator. Write the tests that prove each band is taxed marginally, not flatly.