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