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.assertNotSame;
22 import static org.junit.Assert.assertTrue;
23
24 import org.apache.commons.codec.Charsets;
25 import org.junit.Test;
26
27 public class UnixCryptTest {
28
29 @Test
30 public void testCtor() {
31 assertNotNull(new UnixCrypt());
32 }
33
34 @Test
35 public void testUnixCryptStrings() {
36
37 assertEquals("xxWAum7tHdIUw", Crypt.crypt("secret", "xx"));
38
39 assertEquals("12UFlHxel6uMM", Crypt.crypt("", "12"));
40
41 assertEquals("12FJgqDtVOg7Q", Crypt.crypt("secret", "12"));
42 assertEquals("12FJgqDtVOg7Q", Crypt.crypt("secret", "12345678"));
43 }
44
45 @Test
46 public void testUnixCryptBytes() {
47
48 assertEquals("12UFlHxel6uMM", Crypt.crypt(new byte[0], "12"));
49
50 assertEquals("./287bds2PjVw", Crypt.crypt("t\u00e4st", "./"));
51
52 assertEquals("./bLIFNqo9XKQ", Crypt.crypt("t\u00e4st".getBytes(Charsets.ISO_8859_1), "./"));
53 assertEquals("./bLIFNqo9XKQ", Crypt.crypt(new byte[]{(byte) 0x74, (byte) 0xe4, (byte) 0x73, (byte) 0x74}, "./"));
54 }
55
56
57
58
59 @Test
60 public void testUnixCryptExplicitCall() {
61
62
63 assertTrue(UnixCrypt.crypt("secret".getBytes()).matches("^[a-zA-Z0-9./]{13}$"));
64 assertTrue(UnixCrypt.crypt("secret".getBytes(), null).matches("^[a-zA-Z0-9./]{13}$"));
65 }
66
67
68
69
70
71
72 @Test(expected = IllegalArgumentException.class)
73 public void testUnixCryptWithHalfSalt() {
74 UnixCrypt.crypt("secret", "x");
75 }
76
77
78
79
80 @Test(expected = IllegalArgumentException.class)
81 public void testUnicCryptInvalidSalt() {
82 UnixCrypt.crypt("secret", "$a");
83 }
84
85 @Test(expected = NullPointerException.class)
86 public void testUnixCryptNullData() {
87 UnixCrypt.crypt((byte[]) null);
88 }
89
90 @Test(expected = IllegalArgumentException.class)
91 public void testUnixCryptWithEmptySalt() {
92 UnixCrypt.crypt("secret", "");
93 }
94
95 @Test
96 public void testUnixCryptWithoutSalt() {
97 final String hash = UnixCrypt.crypt("foo");
98 assertTrue(hash.matches("^[a-zA-Z0-9./]{13}$"));
99 final String hash2 = UnixCrypt.crypt("foo");
100 assertNotSame(hash, hash2);
101 }
102 }