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.assertNotNull;
021    import static org.junit.Assert.assertTrue;
022    
023    import org.apache.commons.codec.Charsets;
024    import org.junit.Test;
025    
026    public class Md5CryptTest {
027    
028        @Test
029        public void testCtor() {
030            assertNotNull(new Md5Crypt()); // for code-coverage
031        }
032    
033        @Test
034        public void testMd5CryptStrings() {
035            // empty data
036            assertEquals("$1$foo$9mS5ExwgIECGE5YKlD5o91", Crypt.crypt("", "$1$foo"));
037            // salt gets cut at dollar sign
038            assertEquals("$1$1234$ImZYBLmYC.rbBKg9ERxX70", Crypt.crypt("secret", "$1$1234"));
039            assertEquals("$1$1234$ImZYBLmYC.rbBKg9ERxX70", Crypt.crypt("secret", "$1$1234$567"));
040            assertEquals("$1$1234$ImZYBLmYC.rbBKg9ERxX70", Crypt.crypt("secret", "$1$1234$567$890"));
041            // salt gets cut at maximum length
042            assertEquals("$1$12345678$hj0uLpdidjPhbMMZeno8X/", Crypt.crypt("secret", "$1$1234567890123456"));
043            assertEquals("$1$12345678$hj0uLpdidjPhbMMZeno8X/", Crypt.crypt("secret", "$1$123456789012345678"));
044        }
045    
046        @Test
047        public void testMd5CryptBytes() {
048            // An empty Bytearray equals an empty String
049            assertEquals("$1$foo$9mS5ExwgIECGE5YKlD5o91", Crypt.crypt(new byte[0], "$1$foo"));
050            // UTF-8 stores \u00e4 "a with diaeresis" as two bytes 0xc3 0xa4.
051            assertEquals("$1$./$52agTEQZs877L9jyJnCNZ1", Crypt.crypt("t\u00e4st", "$1$./$"));
052            // ISO-8859-1 stores "a with diaeresis" as single byte 0xe4.
053            assertEquals("$1$./$J2UbKzGe0Cpe63WZAt6p//", Crypt.crypt("t\u00e4st".getBytes(Charsets.ISO_8859_1), "$1$./$"));
054        }
055    
056        @Test
057        public void testMd5CryptExplicitCall() {
058            assertTrue(Md5Crypt.md5Crypt("secret".getBytes()).matches("^\\$1\\$[a-zA-Z0-9./]{0,8}\\$.{1,}$"));
059            assertTrue(Md5Crypt.md5Crypt("secret".getBytes(), null).matches("^\\$1\\$[a-zA-Z0-9./]{0,8}\\$.{1,}$"));
060        }
061    
062        @Test
063        public void testMd5CryptLongInput() {
064            assertEquals("$1$1234$MoxekaNNUgfPRVqoeYjCD/", Crypt.crypt("12345678901234567890", "$1$1234"));
065        }
066    
067        @Test(expected = NullPointerException.class)
068        public void testMd5CryptNullData() {
069            Md5Crypt.md5Crypt((byte[]) null);
070        }
071    
072        @Test(expected = IllegalArgumentException.class)
073        public void testMd5CryptWithEmptySalt() {
074            Md5Crypt.md5Crypt("secret".getBytes(), "");
075        }
076    }