1 /*******************************************************************************
2 * ============LICENSE_START====================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
21 ******************************************************************************/
22 package org.onap.aaf.cadi.test;
24 import org.junit.Test;
25 import org.onap.aaf.cadi.CadiException;
26 import org.onap.aaf.cadi.Hash;
28 import static org.junit.Assert.*;
30 import org.junit.BeforeClass;
32 import static org.hamcrest.CoreMatchers.*;
34 public class JU_Hash {
35 // Some common test vectors
36 private String quickBrownFoxVector = "The quick brown fox jumps over the lazy dog";
37 private String quickBrownFoxMD5 = "0x9e107d9d372bb6826bd81d3542a419d6";
38 private String quickBrownFoxSHA256 = "0xd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592";
40 private String emptyVector = "";
41 private String emptyMD5 = "0xd41d8cd98f00b204e9800998ecf8427e";
42 private String emptySHA256 = "0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
45 private byte[] same1 = "this is a twin".getBytes();
46 private byte[] same2 = "this is a twin".getBytes();
47 private byte[] different1 = "guvf vf n gjva".getBytes();
48 private byte[] different2 = "this is an only child".getBytes();
51 private String uppersDec = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
52 private String uppersHex1 = "0x4142434445464748494A4B4C4D4E4F505152535455565758595A";
53 private String uppersHex2 = "0x4142434445464748494a4b4c4d4e4f505152535455565758595a";
54 private String uppersHexNo0x1 = "4142434445464748494a4b4c4d4e4f505152535455565758595a";
55 private String uppersHexNo0x2 = "4142434445464748494A4B4C4D4E4F505152535455565758595A";
57 private String lowersDec = "abcdefghijklmnopqrstuvwxyz";
58 private String lowersHex = "0x6162636465666768696a6b6c6d6e6f707172737475767778797a";
59 private String lowersHexNo0x1 = "6162636465666768696a6b6c6d6e6f707172737475767778797a";
60 private String lowersHexNo0x2 = "6162636465666768696A6B6C6D6E6F707172737475767778797A";
62 private String numbersDec = "1234567890";
63 private String numbersHex = "0x31323334353637383930";
64 private String numbersHexNo0x = "31323334353637383930";
66 @SuppressWarnings("unused")
68 public static void getCoverage() {
69 // All of this class's methods are static, so we never need to instantiate an object.
70 // That said, we can't get 100% coverage unless we instantiate one
71 Hash hash = new Hash();
75 public void hashMD5Test() throws Exception {
76 byte[] output = Hash.hashMD5(quickBrownFoxVector.getBytes());
77 assertEquals(quickBrownFoxMD5, new String(Hash.toHex(output)));
79 output = Hash.hashMD5(emptyVector.getBytes());
80 assertEquals(emptyMD5, new String(Hash.toHex(output)));
84 public void hashMD5WithOffsetTest() throws Exception {
85 byte[] output = Hash.hashMD5(quickBrownFoxVector.getBytes(), 0, quickBrownFoxVector.length());
86 assertEquals(quickBrownFoxMD5, new String(Hash.toHex(output)));
88 output = Hash.hashMD5(emptyVector.getBytes(), 0, emptyVector.length());
89 assertEquals(emptyMD5, new String(Hash.toHex(output)));
93 public void hashMD5AsStringHexTest() throws Exception {
94 String output = Hash.hashMD5asStringHex(quickBrownFoxVector);
95 assertEquals(quickBrownFoxMD5, output);
97 output = Hash.hashMD5asStringHex(emptyVector);
98 assertEquals(emptyMD5, output);
102 public void hashSHA256Test() throws Exception {
103 byte[] output = Hash.hashSHA256(quickBrownFoxVector.getBytes());
104 assertEquals(quickBrownFoxSHA256, new String(Hash.toHex(output)));
106 output = Hash.hashSHA256(emptyVector.getBytes());
107 assertEquals(emptySHA256, new String(Hash.toHex(output)));
111 public void hashSHA256WithOffsetTest() throws Exception {
112 byte[] output = Hash.hashSHA256(quickBrownFoxVector.getBytes(), 0, quickBrownFoxVector.length());
113 assertEquals(quickBrownFoxSHA256, new String(Hash.toHex(output)));
115 output = Hash.hashSHA256(emptyVector.getBytes(), 0, emptyVector.length());
116 assertEquals(emptySHA256, new String(Hash.toHex(output)));
120 public void hashSHA256AsStringHexTest() throws Exception {
121 String output = Hash.hashSHA256asStringHex(quickBrownFoxVector);
122 assertEquals(quickBrownFoxSHA256, output);
124 output = Hash.hashSHA256asStringHex(emptyVector);
125 assertEquals(emptySHA256, output);
129 public void hashSaltySHA256AsStringHexTest() throws Exception {
130 String input = "password";
131 String hash1 = Hash.hashSHA256asStringHex(input, 10);
132 String hash2 = Hash.hashSHA256asStringHex(input, 10);
133 String hash3 = Hash.hashSHA256asStringHex(input, 11);
135 assertEquals(hash1, hash2);
136 assertThat(hash1, not(equalTo(hash3)));
140 public void isEqualTest() throws Exception {
141 assertTrue(Hash.isEqual(same1, same2));
142 assertFalse(Hash.isEqual(same1, different1));
143 assertFalse(Hash.isEqual(same1, different2));
147 public void compareToTest() throws Exception {
148 assertEquals(0, Hash.compareTo(same1, same2));
149 // different1 is rot13(same1), so the difference should be 13
150 assertEquals(13, Hash.compareTo(same1, different1));
151 assertEquals(-78, Hash.compareTo(same1, different2));
155 public void toHexNo0xTest() throws Exception {
156 assertEquals(uppersHexNo0x1, Hash.toHexNo0x(uppersDec.getBytes()));
157 assertEquals(lowersHexNo0x1, Hash.toHexNo0x(lowersDec.getBytes()));
158 assertEquals(numbersHexNo0x, Hash.toHexNo0x(numbersDec.getBytes()));
162 public void toHexTest() throws Exception {
163 assertEquals(uppersHex2, Hash.toHex(uppersDec.getBytes()));
164 assertEquals(lowersHex, Hash.toHex(lowersDec.getBytes()));
165 assertEquals(numbersHex, Hash.toHex(numbersDec.getBytes()));
169 public void toHexWithOffset() throws Exception {
170 assertEquals(uppersHex2, Hash.toHex(uppersDec.getBytes(), 0, uppersDec.length()));
171 assertEquals(lowersHex, Hash.toHex(lowersDec.getBytes(), 0, lowersDec.length()));
172 assertEquals(numbersHex, Hash.toHex(numbersDec.getBytes(), 0, numbersDec.length()));
176 public void fromHexTest() throws Exception {
177 assertEquals(uppersDec, new String(Hash.fromHex(uppersHex1)));
178 assertEquals(lowersDec, new String(Hash.fromHex(lowersHex)));
179 assertEquals(numbersDec, new String(Hash.fromHex(numbersHex)));
182 // This string doesn't begin with "0x"
183 Hash.fromHex("0X65");
184 fail("Should have thrown CadiException");
185 } catch (CadiException e) {
186 assertEquals("HexString must start with \"0x\"", e.getMessage());
190 // This string has invalid hex characters
192 fail("Should have thrown CadiException");
193 } catch (CadiException e) {
195 assertEquals("Invalid char '81' in HexString", e.getMessage());
200 public void fromHexNo0xTest() throws Exception {
201 assertEquals(uppersDec, new String(Hash.fromHexNo0x(uppersHexNo0x1)));
202 assertEquals(lowersDec, new String(Hash.fromHexNo0x(lowersHexNo0x1)));
203 assertEquals(uppersDec, new String(Hash.fromHexNo0x(uppersHexNo0x2)));
204 assertEquals(lowersDec, new String(Hash.fromHexNo0x(lowersHexNo0x2)));
205 assertEquals(numbersDec, new String(Hash.fromHexNo0x(numbersHexNo0x)));
206 byte[] output = Hash.fromHexNo0x("ABC");
207 assertEquals(new String(new byte[] {(byte)0x0A, (byte)0xB0}), new String(output));
208 assertNull(Hash.fromHexNo0x("~~"));