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