View Javadoc
1   /*
2    * Copyright (C) 2020-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.validation.values.test;
5   
6   
7   import static org.junit.jupiter.api.Assertions.assertAll;
8   import static org.junit.jupiter.api.Assertions.assertEquals;
9   import static org.junit.jupiter.api.Assertions.assertFalse;
10  import static org.junit.jupiter.api.Assertions.assertNotEquals;
11  import static org.junit.jupiter.api.Assertions.assertThrows;
12  import static org.junit.jupiter.api.Assertions.assertTrue;
13  
14  import org.junit.jupiter.api.Test;
15  import org.junit.jupiter.params.ParameterizedTest;
16  import org.junit.jupiter.params.provider.ValueSource;
17  
18  import de.powerstat.validation.values.Password;
19  import de.powerstat.validation.values.strategies.IPasswordStrategy;
20  import de.powerstat.validation.values.strategies.PasswordConfigurableStrategy;
21  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22  
23  
24  /**
25   * Password tests.
26   */
27  @SuppressFBWarnings({"EC_NULL_ARG", "RV_NEGATING_RESULT_OF_COMPARETO", "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", "SPP_USE_ZERO_WITH_COMPARATOR"})
28  final class PasswordTests
29   {
30    /**
31     * Password.
32     */
33    private static final String PASSWORD = "password"; //$NON-NLS-1$
34  
35    /**
36     * Password 2.
37     */
38    private static final String PASSWORD2 = "password2"; //$NON-NLS-1$
39  
40    /**
41     * Password 3 - only for stringValueNoRead() test.
42     */
43    private static final String PASSWORD3 = "password4"; //$NON-NLS-1$
44  
45    /**
46     * Illegal argument exception expected constant.
47     */
48    private static final String ILLEGAL_ARGUMENT = "Illegal argument exception expected"; //$NON-NLS-1$
49  
50    /**
51     * Password not as expected constant.
52     */
53    private static final String PASSWORD_NOT_AS_EXPECTED = "Password not as expected"; //$NON-NLS-1$
54  
55    /**
56     * Hidden password constant.
57     */
58    private static final String SECRET_PASSWORD = "********"; //$NON-NLS-1$
59  
60  
61    /**
62     * Default constructor.
63     */
64    /* default */ PasswordTests()
65     {
66      super();
67     }
68  
69  
70    /**
71     * Test Password with valid values.
72     *
73     * @param password Password
74     */
75    @ParameterizedTest
76    @ValueSource(strings = {"username", "username@example.com", "a2345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234"})
77    /* default */ void testPasswordOk0(final String password)
78     {
79      final Password cleanPassword = Password.of(password);
80      assertTrue(cleanPassword.verifyPassword(password), PasswordTests.PASSWORD_NOT_AS_EXPECTED);
81     }
82  
83  
84    /**
85     * Test Password with wrong vaidation.
86     */
87    @Test
88    /* default */ void testPasswordWrongValidation()
89     {
90      final Password cleanPassword = Password.of(PasswordTests.PASSWORD);
91      assertFalse(cleanPassword.verifyPassword("wrongPassword"), "Password verification not as expected"); //$NON-NLS-1$ //$NON-NLS-2$
92     }
93  
94  
95    /**
96     * Test Password with password to short or long.
97     *
98     * @param password Password
99     */
100   @ParameterizedTest
101   @ValueSource(strings = {"", "a234567", "a23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"})
102   /* default */ void testPasswordLength(final String password)
103    {
104     assertThrows(IllegalArgumentException.class, () ->
105      {
106       /* final Password cleanPassword = */ Password.of(password);
107      }, PasswordTests.ILLEGAL_ARGUMENT
108     );
109    }
110 
111 
112   /**
113    * Test Password with illegal characters.
114    */
115   @Test
116   /* default */ void testPasswordWithIllegalCharacters0()
117    {
118     final IPasswordStrategy strategy = PasswordConfigurableStrategy.of(2, 254, "^[!§$%&/()=?öäüÖÄÜ,.:;_@0-9a-zA-Z-]+$", 0, 0, 0, 0, 0, 0); //$NON-NLS-1$
119     assertThrows(IllegalArgumentException.class, () ->
120      {
121       /* final Password cleanPassword = */ Password.of(strategy, "\"€"); //$NON-NLS-1$
122      }, PasswordTests.ILLEGAL_ARGUMENT
123     );
124    }
125 
126 
127   /**
128    * Test get password.
129    */
130   @Test
131   /* default */ void testStringValueRead()
132    {
133     final Password password = Password.of(PasswordTests.PASSWORD);
134     assertEquals(PasswordTests.PASSWORD, password.stringValue(), PasswordTests.PASSWORD_NOT_AS_EXPECTED);
135    }
136 
137 
138   /**
139    * Test get password.
140    */
141   @Test
142   /* default */ void testStringValueNoRead()
143    {
144     final Password password = Password.of(PasswordTests.PASSWORD3, true);
145     assertEquals(SECRET_PASSWORD, password.stringValue(), PasswordTests.PASSWORD_NOT_AS_EXPECTED);
146    }
147 
148 
149   /**
150    * Test hash code.
151    */
152   @Test
153   /* default */ void testHashCode()
154    {
155     final Password password1 = Password.of(PasswordTests.PASSWORD);
156     final Password password2 = Password.of(PasswordTests.PASSWORD);
157     final Password password3 = Password.of(PasswordTests.PASSWORD2);
158     assertAll("testHashCode", //$NON-NLS-1$
159       () -> assertEquals(password1.hashCode(), password2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
160       () -> assertNotEquals(password1.hashCode(), password3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
161     );
162    }
163 
164 
165   /**
166    * Test equals.
167    */
168   @Test
169   @SuppressWarnings("java:S5785")
170   /* default */ void testEquals()
171    {
172     final Password password1 = Password.of(PasswordTests.PASSWORD);
173     final Password password2 = Password.of(PasswordTests.PASSWORD);
174     final Password password3 = Password.of(PasswordTests.PASSWORD2);
175     final Password password4 = Password.of(PasswordTests.PASSWORD);
176     assertAll("testEquals", //$NON-NLS-1$
177       () -> assertTrue(password1.equals(password1), "password11 is not equal"), //$NON-NLS-1$
178       () -> assertTrue(password1.equals(password2), "password12 are not equal"), //$NON-NLS-1$
179       () -> assertTrue(password2.equals(password1), "password21 are not equal"), //$NON-NLS-1$
180       () -> assertTrue(password2.equals(password4), "password24 are not equal"), //$NON-NLS-1$
181       () -> assertTrue(password1.equals(password4), "password14 are not equal"), //$NON-NLS-1$
182       () -> assertFalse(password1.equals(password3), "password13 are equal"), //$NON-NLS-1$
183       () -> assertFalse(password3.equals(password1), "password31 are equal"), //$NON-NLS-1$
184       () -> assertFalse(password1.equals(null), "password10 is equal") //$NON-NLS-1$
185     );
186    }
187 
188 
189   /**
190    * Test toString.
191    */
192   @Test
193   /* default */ void testToString()
194    {
195     final Password password = Password.of(PasswordTests.PASSWORD);
196     assertEquals("Password[password=********]", password.toString(), "toString not equal"); //$NON-NLS-1$ //$NON-NLS-2$
197    }
198 
199 
200   /**
201    * Test compareTo.
202    */
203   @Test
204   @SuppressWarnings("java:S5785")
205   /* default */ void testCompareTo()
206    {
207     final Password password1 = Password.of(PasswordTests.PASSWORD);
208     final Password password2 = Password.of(PasswordTests.PASSWORD);
209     final Password password3 = Password.of(PasswordTests.PASSWORD2);
210     final Password password4 = Password.of("password3"); //$NON-NLS-1$
211     final Password password5 = Password.of(PasswordTests.PASSWORD);
212     assertAll("testCompareTo", //$NON-NLS-1$
213       () -> assertTrue(password1.compareTo(password2) == -password2.compareTo(password1), "reflexive1"), //$NON-NLS-1$
214       () -> assertTrue(password1.compareTo(password3) == -password3.compareTo(password1), "reflexive2"), //$NON-NLS-1$
215       () -> assertTrue((password4.compareTo(password3) > 0) && (password3.compareTo(password1) > 0) && (password4.compareTo(password1) > 0), "transitive1"), //$NON-NLS-1$
216       () -> assertTrue((password1.compareTo(password2) == 0) && (Math.abs(password1.compareTo(password5)) == Math.abs(password2.compareTo(password5))), "sgn1"), //$NON-NLS-1$
217       () -> assertTrue((password1.compareTo(password2) == 0) && password1.equals(password2), "equals") //$NON-NLS-1$
218     );
219    }
220 
221  }