-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordValidator.java
More file actions
125 lines (104 loc) · 4.92 KB
/
Copy pathPasswordValidator.java
File metadata and controls
125 lines (104 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.util.ArrayList;
import java.util.List;
public class PasswordValidator {
/**
* Validates a password against the following rules and returns a list of
* unmet requirements. An empty list means the password is valid.
*
* Rules (checked in this order):
* 1. At least 8 characters long
* 2. At least one uppercase letter (A-Z)
* 3. At least one lowercase letter (a-z)
* 4. At least one digit (0-9)
* 5. At least one special character from: !@#$%^&*()-_=+[]{}|;':",.<>?/`~
* 6. No whitespace characters allowed
*
* @param password the password to validate (may be null)
* @return list of violation messages; empty if all rules pass
*/
public static List<String> validate(String password) {
List<String> errors = new ArrayList<>();
if (password == null) {
errors.add("Password must not be null");
return errors;
}
if (password.length() < 8) {
errors.add("Must be at least 8 characters long");
}
if (!password.matches(".*[A-Z].*")) {
errors.add("Must contain at least one uppercase letter");
}
if (!password.matches(".*[a-z].*")) {
errors.add("Must contain at least one lowercase letter");
}
if (!password.matches(".*[0-9].*")) {
errors.add("Must contain at least one digit");
}
if (!password.matches(".*[!@#$%^&*()\\-_=+\\[\\]{}|;':\",.<>?/~].*")) {
errors.add("Must contain at least one special character");
}
if (password.matches(".*\\s+.*")) {
errors.add("Must not contain whitespace");
}
return errors;
}
// -------------------------------------------------------------------------
// Test runner
// -------------------------------------------------------------------------
public static void main(String[] args) {
int passed = 0, failed = 0;
Object[][] tests = {
// { input, expected violations (String[]) }
// valid password
{ "Secure@99", new String[] {} },
// valid - special chars variety
{ "Hello#World1", new String[] {} },
// valid - minimum length exactly 8
{ "Abcde1@x", new String[] {} },
// too short
{ "Ab1@", new String[] { "Must be at least 8 characters long" } },
// missing uppercase
{ "secure@99", new String[] { "Must contain at least one uppercase letter" } },
// missing lowercase
{ "SECURE@99", new String[] { "Must contain at least one lowercase letter" } },
// missing digit
{ "Secure@xx", new String[] { "Must contain at least one digit" } },
// missing special char
{ "Secure999", new String[] { "Must contain at least one special character" } },
// contains whitespace
{ "Secure @99", new String[] { "Must not contain whitespace" } },
// multiple violations: too short, no uppercase, no digit, no special
{ "abc", new String[] {
"Must be at least 8 characters long",
"Must contain at least one uppercase letter",
"Must contain at least one digit",
"Must contain at least one special character"
} },
// null
{ null, new String[] { "Password must not be null" } },
// empty string
{ "", new String[] {
"Must be at least 8 characters long",
"Must contain at least one uppercase letter",
"Must contain at least one lowercase letter",
"Must contain at least one digit",
"Must contain at least one special character"
} },
};
for (Object[] test : tests) {
String input = (String) test[0];
String[] expectedArr = (String[]) test[1];
List<String> actual = validate(input);
List<String> expected = List.of(expectedArr);
boolean ok = actual.size() == expected.size()
&& actual.containsAll(expected)
&& expected.containsAll(actual);
String display = input == null ? "null" : "\"" + input + "\"";
System.out.printf(" %-20s -> %s%n",
display,
ok ? "PASS" : "FAIL\n expected: " + expected + "\n actual: " + actual);
if (ok) passed++; else failed++;
}
System.out.printf("%nResults: %d passed, %d failed%n", passed, failed);
}
}