Skip to content

Commit 73f4ed8

Browse files
Copilotdevondragon
andcommitted
Phase 5: Add char[] password handling tests and verification
Co-authored-by: devondragon <1254537+devondragon@users.noreply.github.com>
1 parent 5ca3064 commit 73f4ed8

1 file changed

Lines changed: 223 additions & 0 deletions

File tree

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
package com.digitalsanctuary.spring.user.dto;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import com.digitalsanctuary.spring.user.util.PasswordSecurityUtil;
8+
9+
/**
10+
* Unit tests for DTO password char[] handling.
11+
*/
12+
class PasswordDtoCharArrayTest {
13+
14+
@Test
15+
void userDto_getPasswordChars_returnsCharArray() {
16+
UserDto dto = new UserDto();
17+
dto.setPassword("TestP@ssw0rd");
18+
19+
char[] passwordChars = dto.getPasswordChars();
20+
assertNotNull(passwordChars);
21+
assertArrayEquals("TestP@ssw0rd".toCharArray(), passwordChars);
22+
}
23+
24+
@Test
25+
void userDto_setPasswordChars_updatesStringPassword() {
26+
UserDto dto = new UserDto();
27+
char[] password = "TestP@ssw0rd".toCharArray();
28+
dto.setPasswordChars(password);
29+
30+
assertEquals("TestP@ssw0rd", dto.getPassword());
31+
}
32+
33+
@Test
34+
void userDto_clearPasswords_clearsArrays() {
35+
UserDto dto = new UserDto();
36+
char[] password = "TestP@ssw0rd".toCharArray();
37+
char[] matching = "TestP@ssw0rd".toCharArray();
38+
39+
dto.setPasswordChars(password);
40+
dto.setMatchingPasswordChars(matching);
41+
42+
dto.clearPasswords();
43+
44+
// Verify arrays are cleared
45+
for (char c : password) {
46+
assertEquals('\0', c);
47+
}
48+
for (char c : matching) {
49+
assertEquals('\0', c);
50+
}
51+
}
52+
53+
@Test
54+
void userDto_autoCloseable_clearsPasswordsOnClose() {
55+
UserDto dto = new UserDto();
56+
char[] password = "TestP@ssw0rd".toCharArray();
57+
dto.setPasswordChars(password);
58+
59+
// Try-with-resources should call close()
60+
try (UserDto closeable = dto) {
61+
assertNotNull(closeable.getPassword());
62+
}
63+
64+
// Verify password is cleared
65+
for (char c : password) {
66+
assertEquals('\0', c);
67+
}
68+
}
69+
70+
@Test
71+
void passwordDto_getOldPasswordChars_returnsCharArray() {
72+
PasswordDto dto = new PasswordDto();
73+
dto.setOldPassword("OldP@ssw0rd");
74+
75+
char[] passwordChars = dto.getOldPasswordChars();
76+
assertNotNull(passwordChars);
77+
assertArrayEquals("OldP@ssw0rd".toCharArray(), passwordChars);
78+
}
79+
80+
@Test
81+
void passwordDto_setOldPasswordChars_updatesStringPassword() {
82+
PasswordDto dto = new PasswordDto();
83+
char[] password = "OldP@ssw0rd".toCharArray();
84+
dto.setOldPasswordChars(password);
85+
86+
assertEquals("OldP@ssw0rd", dto.getOldPassword());
87+
}
88+
89+
@Test
90+
void passwordDto_clearPasswords_clearsArrays() {
91+
PasswordDto dto = new PasswordDto();
92+
char[] oldPassword = "OldP@ssw0rd".toCharArray();
93+
char[] newPassword = "NewP@ssw0rd".toCharArray();
94+
95+
dto.setOldPasswordChars(oldPassword);
96+
dto.setNewPasswordChars(newPassword);
97+
98+
dto.clearPasswords();
99+
100+
// Verify arrays are cleared
101+
for (char c : oldPassword) {
102+
assertEquals('\0', c);
103+
}
104+
for (char c : newPassword) {
105+
assertEquals('\0', c);
106+
}
107+
}
108+
109+
@Test
110+
void passwordDto_autoCloseable_clearsPasswordsOnClose() {
111+
PasswordDto dto = new PasswordDto();
112+
char[] oldPassword = "OldP@ssw0rd".toCharArray();
113+
char[] newPassword = "NewP@ssw0rd".toCharArray();
114+
115+
dto.setOldPasswordChars(oldPassword);
116+
dto.setNewPasswordChars(newPassword);
117+
118+
// Try-with-resources should call close()
119+
try (PasswordDto closeable = dto) {
120+
assertNotNull(closeable.getOldPassword());
121+
assertNotNull(closeable.getNewPassword());
122+
}
123+
124+
// Verify passwords are cleared
125+
for (char c : oldPassword) {
126+
assertEquals('\0', c);
127+
}
128+
for (char c : newPassword) {
129+
assertEquals('\0', c);
130+
}
131+
}
132+
133+
@Test
134+
void savePasswordDto_getNewPasswordChars_returnsCharArray() {
135+
SavePasswordDto dto = new SavePasswordDto();
136+
dto.setNewPassword("NewP@ssw0rd");
137+
138+
char[] passwordChars = dto.getNewPasswordChars();
139+
assertNotNull(passwordChars);
140+
assertArrayEquals("NewP@ssw0rd".toCharArray(), passwordChars);
141+
}
142+
143+
@Test
144+
void savePasswordDto_setNewPasswordChars_updatesStringPassword() {
145+
SavePasswordDto dto = new SavePasswordDto();
146+
char[] password = "NewP@ssw0rd".toCharArray();
147+
dto.setNewPasswordChars(password);
148+
149+
assertEquals("NewP@ssw0rd", dto.getNewPassword());
150+
}
151+
152+
@Test
153+
void savePasswordDto_clearPasswords_clearsArrays() {
154+
SavePasswordDto dto = new SavePasswordDto();
155+
char[] newPassword = "NewP@ssw0rd".toCharArray();
156+
char[] confirmPassword = "NewP@ssw0rd".toCharArray();
157+
158+
dto.setNewPasswordChars(newPassword);
159+
dto.setConfirmPasswordChars(confirmPassword);
160+
161+
dto.clearPasswords();
162+
163+
// Verify arrays are cleared
164+
for (char c : newPassword) {
165+
assertEquals('\0', c);
166+
}
167+
for (char c : confirmPassword) {
168+
assertEquals('\0', c);
169+
}
170+
}
171+
172+
@Test
173+
void savePasswordDto_autoCloseable_clearsPasswordsOnClose() {
174+
SavePasswordDto dto = new SavePasswordDto();
175+
char[] newPassword = "NewP@ssw0rd".toCharArray();
176+
char[] confirmPassword = "NewP@ssw0rd".toCharArray();
177+
178+
dto.setNewPasswordChars(newPassword);
179+
dto.setConfirmPasswordChars(confirmPassword);
180+
181+
// Try-with-resources should call close()
182+
try (SavePasswordDto closeable = dto) {
183+
assertNotNull(closeable.getNewPassword());
184+
assertNotNull(closeable.getConfirmPassword());
185+
}
186+
187+
// Verify passwords are cleared
188+
for (char c : newPassword) {
189+
assertEquals('\0', c);
190+
}
191+
for (char c : confirmPassword) {
192+
assertEquals('\0', c);
193+
}
194+
}
195+
196+
@Test
197+
void integrationTest_fullPasswordFlow() {
198+
// Simulate a secure password handling flow
199+
UserDto userDto = new UserDto();
200+
201+
// 1. Set password using char array
202+
char[] password = "SecureP@ssw0rd123".toCharArray();
203+
char[] matching = "SecureP@ssw0rd123".toCharArray();
204+
205+
userDto.setPasswordChars(password);
206+
userDto.setMatchingPasswordChars(matching);
207+
208+
// 2. Verify String fields are updated
209+
assertEquals("SecureP@ssw0rd123", userDto.getPassword());
210+
assertEquals("SecureP@ssw0rd123", userDto.getMatchingPassword());
211+
212+
// 3. Clear passwords
213+
userDto.clearPasswords();
214+
215+
// 4. Verify char arrays are cleared
216+
for (char c : password) {
217+
assertEquals('\0', c);
218+
}
219+
for (char c : matching) {
220+
assertEquals('\0', c);
221+
}
222+
}
223+
}

0 commit comments

Comments
 (0)