2525
2626public class CryptTest {
2727
28+ // Allow CLI testing
29+ // CLASSPATH=target/classes:target/test-classes/ java org.apache.commons.codec.digest.CryptTest
30+ public static void main (final String [] args ) {
31+ final String hash ;
32+ switch (args .length ) {
33+ case 1 :
34+ hash = Crypt .crypt (args [0 ]);
35+ System .out .println (hash .length () + ": " + hash );
36+ break ;
37+ case 2 :
38+ hash = Crypt .crypt (args [0 ], args [1 ]);
39+ System .out .println (hash .length () + "; " + hash );
40+ break ;
41+ default :
42+ System .out .println ("Enter key [salt (remember to quote this!)]" );
43+ break ;
44+ }
45+ }
46+
47+ // Helper method
48+ private void startsWith (final String string , final String prefix ) {
49+ assertTrue (string .startsWith (prefix ), string + " should start with " + prefix );
50+ }
51+
52+ @ Test
53+ public void testBadSalt () {
54+ // No salt
55+ assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$1$" ));
56+ assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$5$" ));
57+ assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$6$" ));
58+ // wrong char
59+ assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$1$%" ));
60+ assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$5$!" ));
61+ assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$6$_" ));
62+ }
63+
64+ @ Test
65+ public void testBadType () {
66+ assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$2$xxxx" ));
67+ assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$3$xxxx" ));
68+ assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$4$" ));
69+ }
70+
2871 @ Test
2972 public void testCrypt () {
3073 assertNotNull (new Crypt ()); // just for Test Coverage
3174 }
32-
3375 @ Test
3476 public void testCryptWithBytes () {
3577 final byte [] keyBytes = { 'b' , 'y' , 't' , 'e' };
@@ -48,14 +90,12 @@ public void testCryptWithBytes() {
4890 public void testCryptWithEmptySalt () {
4991 assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "" ));
5092 }
51-
5293 @ Test
5394 public void testDefaultCryptVariant () {
5495 // If salt is null or completely omitted, a random "$6$" is used.
5596 assertTrue (Crypt .crypt ("secret" ).startsWith ("$6$" ));
5697 assertTrue (Crypt .crypt ("secret" , null ).startsWith ("$6$" ));
5798 }
58-
5999 @ Test
60100 public void testSamples () { // From Javadoc
61101 assertEquals ("$1$xxxx$aMkevjfEIpa35Bh3G4bAc." , Crypt .crypt ("secret" , "$1$xxxx" ));
@@ -67,51 +107,11 @@ public void testStored() { // From Javadoc
67107 assertEquals ("xxWAum7tHdIUw" , Crypt .crypt ("secret" , "xxWAum7tHdIUw" ));
68108 }
69109
70- // Helper method
71- private void startsWith (final String string , final String prefix ) {
72- assertTrue (string .startsWith (prefix ), string + " should start with " + prefix );
73- }
74110 @ Test
75111 public void testType () {
76112 startsWith (Crypt .crypt ("secret" , "xxxx" ), "xx" );
77113 startsWith (Crypt .crypt ("secret" , "$1$xxxx" ), "$1$xxxx$" );
78114 startsWith (Crypt .crypt ("secret" , "$5$xxxx" ), "$5$xxxx$" );
79115 startsWith (Crypt .crypt ("secret" , "$6$xxxx" ), "$6$xxxx$" );
80116 }
81- @ Test
82- public void testBadType () {
83- assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$2$xxxx" ));
84- assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$3$xxxx" ));
85- assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$4$" ));
86- }
87- @ Test
88- public void testBadSalt () {
89- // No salt
90- assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$1$" ));
91- assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$5$" ));
92- assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$6$" ));
93- // wrong char
94- assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$1$%" ));
95- assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$5$!" ));
96- assertThrowsExactly (IllegalArgumentException .class , () -> Crypt .crypt ("secret" , "$6$_" ));
97- }
98-
99- // Allow CLI testing
100- // CLASSPATH=target/classes:target/test-classes/ java org.apache.commons.codec.digest.CryptTest
101- public static void main (final String [] args ) {
102- final String hash ;
103- switch (args .length ) {
104- case 1 :
105- hash = Crypt .crypt (args [0 ]);
106- System .out .println (hash .length () + ": " + hash );
107- break ;
108- case 2 :
109- hash = Crypt .crypt (args [0 ], args [1 ]);
110- System .out .println (hash .length () + "; " + hash );
111- break ;
112- default :
113- System .out .println ("Enter key [salt (remember to quote this!)]" );
114- break ;
115- }
116- }
117117}
0 commit comments