001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.codec.digest;
018    
019    import static org.junit.Assert.assertEquals;
020    import static org.junit.Assert.assertNotSame;
021    import static org.junit.Assert.assertTrue;
022    
023    import org.apache.commons.codec.Charsets;
024    import org.junit.Test;
025    
026    public class Apr1CryptTest {
027    
028        @Test
029        public void testApr1CryptStrings() {
030            // A random example using htpasswd
031            assertEquals("$apr1$TqI9WECO$LHZB2DqRlk9nObiB6vJG9.", Md5Crypt.apr1Crypt("secret", "$apr1$TqI9WECO"));
032            // empty data
033            assertEquals("$apr1$foo$P27KyD1htb4EllIPEYhqi0", Md5Crypt.apr1Crypt("", "$apr1$foo"));
034            // salt gets cut at dollar sign
035            assertEquals("$apr1$1234$mAlH7FRST6FiRZ.kcYL.j1", Md5Crypt.apr1Crypt("secret", "$apr1$1234"));
036            assertEquals("$apr1$1234$mAlH7FRST6FiRZ.kcYL.j1", Md5Crypt.apr1Crypt("secret", "$apr1$1234$567"));
037            assertEquals("$apr1$1234$mAlH7FRST6FiRZ.kcYL.j1", Md5Crypt.apr1Crypt("secret", "$apr1$1234$567$890"));
038            // salt gets cut at maximum length
039            assertEquals("$apr1$12345678$0lqb/6VUFP8JY/s/jTrIk0", Md5Crypt.apr1Crypt("secret", "$apr1$1234567890123456"));
040            assertEquals("$apr1$12345678$0lqb/6VUFP8JY/s/jTrIk0", Md5Crypt.apr1Crypt("secret", "$apr1$123456789012345678"));
041        }
042    
043        @Test
044        public void testApr1CryptBytes() {
045            // random salt
046            final byte[] keyBytes = new byte[] { '!', 'b', 'c', '.' };
047            final String hash = Md5Crypt.apr1Crypt(keyBytes);
048            assertEquals(hash, Md5Crypt.apr1Crypt("!bc.", hash));
049    
050            // An empty Bytearray equals an empty String
051            assertEquals("$apr1$foo$P27KyD1htb4EllIPEYhqi0", Md5Crypt.apr1Crypt(new byte[0], "$apr1$foo"));
052            // UTF-8 stores \u00e4 "a with diaeresis" as two bytes 0xc3 0xa4.
053            assertEquals("$apr1$./$EeFrYzWWbmTyGdf4xULYc.", Md5Crypt.apr1Crypt("t\u00e4st", "$apr1$./$"));
054            // ISO-8859-1 stores "a with diaeresis" as single byte 0xe4.
055            assertEquals("$apr1$./$kCwT1pY9qXAJElYG9q1QE1", Md5Crypt.apr1Crypt("t\u00e4st".getBytes(Charsets.ISO_8859_1), "$apr1$./$"));
056        }
057    
058        @Test
059        public void testApr1CryptExplicitCall() {
060            // When explicitly called the prefix is optional
061            assertEquals("$apr1$1234$mAlH7FRST6FiRZ.kcYL.j1", Md5Crypt.apr1Crypt("secret", "1234"));
062            // When explicitly called without salt, a random one will be used.
063            assertTrue(Md5Crypt.apr1Crypt("secret".getBytes()).matches("^\\$apr1\\$[a-zA-Z0-9./]{0,8}\\$.{1,}$"));
064            assertTrue(Md5Crypt.apr1Crypt("secret".getBytes(), null).matches("^\\$apr1\\$[a-zA-Z0-9./]{0,8}\\$.{1,}$"));
065        }
066    
067        @Test
068        public void testApr1LongSalt() {
069            assertEquals("$apr1$12345678$0lqb/6VUFP8JY/s/jTrIk0", Md5Crypt.apr1Crypt("secret", "12345678901234567890"));
070        }
071    
072        @Test(expected = NullPointerException.class)
073        public void testApr1CryptNullData() {
074            Md5Crypt.apr1Crypt((byte[]) null);
075        }
076    
077        @Test(expected = IllegalArgumentException.class)
078        public void testApr1CryptWithEmptySalt() {
079            Md5Crypt.apr1Crypt("secret".getBytes(), "");
080        }
081    
082        @Test
083        public void testApr1CryptWithoutSalt() {
084            // Without salt, a random is generated
085            final String hash = Md5Crypt.apr1Crypt("secret");
086            assertTrue(hash.matches("^\\$apr1\\$[a-zA-Z0-9\\./]{8}\\$[a-zA-Z0-9\\./]{22}$"));
087            final String hash2 = Md5Crypt.apr1Crypt("secret");
088            assertNotSame(hash, hash2);
089        }
090    
091        @Test(expected = IllegalArgumentException.class)
092        public void testApr1CryptWithInvalidSalt() {
093            Md5Crypt.apr1Crypt(new byte[0], "!");
094        }
095    }