1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.codec.digest;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22
23 import org.apache.commons.codec.Charsets;
24 import org.junit.Test;
25
26 public class Md5CryptTest {
27
28 @Test
29 public void testCtor() {
30 assertNotNull(new Md5Crypt());
31 }
32
33 @Test
34 public void testMd5CryptStrings() {
35
36 assertEquals("$1$foo$9mS5ExwgIECGE5YKlD5o91", Crypt.crypt("", "$1$foo"));
37
38 assertEquals("$1$1234$ImZYBLmYC.rbBKg9ERxX70", Crypt.crypt("secret", "$1$1234"));
39 assertEquals("$1$1234$ImZYBLmYC.rbBKg9ERxX70", Crypt.crypt("secret", "$1$1234$567"));
40 assertEquals("$1$1234$ImZYBLmYC.rbBKg9ERxX70", Crypt.crypt("secret", "$1$1234$567$890"));
41
42 assertEquals("$1$12345678$hj0uLpdidjPhbMMZeno8X/", Crypt.crypt("secret", "$1$1234567890123456"));
43 assertEquals("$1$12345678$hj0uLpdidjPhbMMZeno8X/", Crypt.crypt("secret", "$1$123456789012345678"));
44 }
45
46 @Test
47 public void testMd5CryptBytes() {
48
49 assertEquals("$1$foo$9mS5ExwgIECGE5YKlD5o91", Crypt.crypt(new byte[0], "$1$foo"));
50
51 assertEquals("$1$./$52agTEQZs877L9jyJnCNZ1", Crypt.crypt("t\u00e4st", "$1$./$"));
52
53 assertEquals("$1$./$J2UbKzGe0Cpe63WZAt6p//", Crypt.crypt("t\u00e4st".getBytes(Charsets.ISO_8859_1), "$1$./$"));
54 }
55
56 @Test
57 public void testMd5CryptExplicitCall() {
58 assertTrue(Md5Crypt.md5Crypt("secret".getBytes()).matches("^\\$1\\$[a-zA-Z0-9./]{0,8}\\$.{1,}$"));
59 assertTrue(Md5Crypt.md5Crypt("secret".getBytes(), null).matches("^\\$1\\$[a-zA-Z0-9./]{0,8}\\$.{1,}$"));
60 }
61
62 @Test
63 public void testMd5CryptLongInput() {
64 assertEquals("$1$1234$MoxekaNNUgfPRVqoeYjCD/", Crypt.crypt("12345678901234567890", "$1$1234"));
65 }
66
67 @Test(expected = NullPointerException.class)
68 public void testMd5CryptNullData() {
69 Md5Crypt.md5Crypt((byte[]) null);
70 }
71
72 @Test(expected = IllegalArgumentException.class)
73 public void testMd5CryptWithEmptySalt() {
74 Md5Crypt.md5Crypt("secret".getBytes(), "");
75 }
76 }