View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests {@link UnixCrypt}.
31   */
32  class UnixCryptTest {
33  
34      @Test
35      void testCtor() {
36          assertNotNull(new UnixCrypt());
37      }
38  
39      @Test
40      void testUnixCryptBytes() {
41          // An empty Bytearray equals an empty String
42          assertEquals("12UFlHxel6uMM", Crypt.crypt(new byte[0], "12"));
43          // UTF-8 stores \u00e4 "a with dieresis" as two bytes 0xc3 0xa4.
44          assertEquals("./287bds2PjVw", Crypt.crypt("t\u00e4st", "./"));
45          // ISO-8859-1 stores "a with dieresis" as single byte 0xe4.
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       * Some salts are invalid for crypt(3) but not for unixCrypt().
52       */
53      @Test
54      void testUnixCryptExplicitCall() {
55          // A call to crypt() with an empty salt would result in a "$6$" hash.
56          // Using unixCrypt() explicitly results in a random salt.
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       * Unimplemented "$foo$" salt prefixes would be treated as UnixCrypt salt.
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          // trivial test
77          assertEquals("xxWAum7tHdIUw", Crypt.crypt("secret", "xx"));
78          // empty data
79          assertEquals("12UFlHxel6uMM", Crypt.crypt("", "12"));
80          // salt gets cut at maximum length
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       * Single character salts are illegal!
92       * E.g. with glibc 2.13, crypt("secret", "x") = "xxZREZpkHZpkI" but
93       * crypt("secret", "xx") = "xxWAum7tHdIUw" which makes it unverifiable.
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 }