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    *      http://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.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          // trivial test
37          assertEquals("xxWAum7tHdIUw", Crypt.crypt("secret", "xx"));
38          // empty data
39          assertEquals("12UFlHxel6uMM", Crypt.crypt("", "12"));
40          // salt gets cut at maximum length
41          assertEquals("12FJgqDtVOg7Q", Crypt.crypt("secret", "12"));
42          assertEquals("12FJgqDtVOg7Q", Crypt.crypt("secret", "12345678"));
43      }
44  
45      @Test
46      public void testUnixCryptBytes() {
47          // An empty Bytearray equals an empty String
48          assertEquals("12UFlHxel6uMM", Crypt.crypt(new byte[0], "12"));
49          // UTF-8 stores \u00e4 "a with diaeresis" as two bytes 0xc3 0xa4.
50          assertEquals("./287bds2PjVw", Crypt.crypt("t\u00e4st", "./"));
51          // ISO-8859-1 stores "a with diaeresis" as single byte 0xe4.
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       * Some salts are invalid for crypt(3) but not for unixCrypt().
58       */
59      @Test
60      public void testUnixCryptExplicitCall() {
61          // A call to crypt() with an empty salt would result in a "$6$" hash.
62          // Using unixCrypt() explicitly results in a random salt.
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       * Single character salts are illegal!
69       * E.g. with glibc 2.13, crypt("secret", "x") = "xxZREZpkHZpkI" but
70       * crypt("secret", "xx") = "xxWAum7tHdIUw" which makes it unverifyable.
71       */
72      @Test(expected = IllegalArgumentException.class)
73      public void testUnixCryptWithHalfSalt() {
74          UnixCrypt.crypt("secret", "x");
75      }
76  
77      /**
78       * Unimplemented "$foo$" salt prefixes would be threated as UnixCrypt salt.
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 }