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 ******************************************************************************/
23 package org.onap.aaf.cadi.test;
25 import org.junit.Test;
26 import org.onap.aaf.cadi.CadiException;
27 import org.onap.aaf.cadi.Hash;
29 import static org.junit.Assert.*;
31 import org.junit.BeforeClass;
33 import static org.hamcrest.CoreMatchers.*;
35 public class JU_Hash {
36 // Some common test vectors
37 private String quickBrownFoxVector = "The quick brown fox jumps over the lazy dog";
38 private String quickBrownFoxMD5 = "0x9e107d9d372bb6826bd81d3542a419d6";
39 private String quickBrownFoxSHA256 = "0xd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592";
41 private String emptyVector = "";
42 private String emptyMD5 = "0xd41d8cd98f00b204e9800998ecf8427e";
43 private String emptySHA256 = "0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
46 private byte[] same1 = "this is a twin".getBytes();
47 private byte[] same2 = "this is a twin".getBytes();
48 private byte[] different1 = "guvf vf n gjva".getBytes();
49 private byte[] different2 = "this is an only child".getBytes();
52 private String uppersDec = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
53 private String uppersHex1 = "0x4142434445464748494A4B4C4D4E4F505152535455565758595A";
54 private String uppersHex2 = "0x4142434445464748494a4b4c4d4e4f505152535455565758595a";
55 private String uppersHexNo0x1 = "4142434445464748494a4b4c4d4e4f505152535455565758595a";
56 private String uppersHexNo0x2 = "4142434445464748494A4B4C4D4E4F505152535455565758595A";
58 private String lowersDec = "abcdefghijklmnopqrstuvwxyz";
59 private String lowersHex = "0x6162636465666768696a6b6c6d6e6f707172737475767778797a";
60 private String lowersHexNo0x1 = "6162636465666768696a6b6c6d6e6f707172737475767778797a";
61 private String lowersHexNo0x2 = "6162636465666768696A6B6C6D6E6F707172737475767778797A";
63 private String numbersDec = "1234567890";
64 private String numbersHex = "0x31323334353637383930";
65 private String numbersHexNo0x = "31323334353637383930";
67 @SuppressWarnings("unused")
69 public static void getCoverage() {
70 // All of this class's methods are static, so we never need to instantiate an object.
71 // That said, we can't get 100% coverage unless we instantiate one
72 Hash hash = new Hash();
76 public void hashMD5Test() throws Exception {
77 byte[] output = Hash.hashMD5(quickBrownFoxVector.getBytes());
78 assertEquals(quickBrownFoxMD5, new String(Hash.toHex(output)));
80 output = Hash.hashMD5(emptyVector.getBytes());
81 assertEquals(emptyMD5, new String(Hash.toHex(output)));
85 public void hashMD5WithOffsetTest() throws Exception {
86 byte[] output = Hash.hashMD5(quickBrownFoxVector.getBytes(), 0, quickBrownFoxVector.length());
87 assertEquals(quickBrownFoxMD5, new String(Hash.toHex(output)));
89 output = Hash.hashMD5(emptyVector.getBytes(), 0, emptyVector.length());
90 assertEquals(emptyMD5, new String(Hash.toHex(output)));
94 public void hashMD5AsStringHexTest() throws Exception {
95 String output = Hash.hashMD5asStringHex(quickBrownFoxVector);
96 assertEquals(quickBrownFoxMD5, output);
98 output = Hash.hashMD5asStringHex(emptyVector);
99 assertEquals(emptyMD5, output);
103 public void hashSHA256Test() throws Exception {
104 byte[] output = Hash.hashSHA256(quickBrownFoxVector.getBytes());
105 assertEquals(quickBrownFoxSHA256, new String(Hash.toHex(output)));
107 output = Hash.hashSHA256(emptyVector.getBytes());
108 assertEquals(emptySHA256, new String(Hash.toHex(output)));
112 public void hashSHA256WithOffsetTest() throws Exception {
113 byte[] output = Hash.hashSHA256(quickBrownFoxVector.getBytes(), 0, quickBrownFoxVector.length());
114 assertEquals(quickBrownFoxSHA256, new String(Hash.toHex(output)));
116 output = Hash.hashSHA256(emptyVector.getBytes(), 0, emptyVector.length());
117 assertEquals(emptySHA256, new String(Hash.toHex(output)));
121 public void hashSHA256AsStringHexTest() throws Exception {
122 String output = Hash.hashSHA256asStringHex(quickBrownFoxVector);
123 assertEquals(quickBrownFoxSHA256, output);
125 output = Hash.hashSHA256asStringHex(emptyVector);
126 assertEquals(emptySHA256, output);
130 public void hashSaltySHA256AsStringHexTest() throws Exception {
131 String input = "password";
132 String hash1 = Hash.hashSHA256asStringHex(input, 10);
133 String hash2 = Hash.hashSHA256asStringHex(input, 10);
134 String hash3 = Hash.hashSHA256asStringHex(input, 11);
136 assertEquals(hash1, hash2);
137 assertThat(hash1, not(equalTo(hash3)));
141 public void isEqualTest() throws Exception {
142 assertTrue(Hash.isEqual(same1, same2));
143 assertFalse(Hash.isEqual(same1, different1));
144 assertFalse(Hash.isEqual(same1, different2));
148 public void compareToTest() throws Exception {
149 assertEquals(0, Hash.compareTo(same1, same2));
150 // different1 is rot13(same1), so the difference should be 13
151 assertEquals(13, Hash.compareTo(same1, different1));
152 assertEquals(-78, Hash.compareTo(same1, different2));
156 public void toHexNo0xTest() throws Exception {
157 assertEquals(uppersHexNo0x1, Hash.toHexNo0x(uppersDec.getBytes()));
158 assertEquals(lowersHexNo0x1, Hash.toHexNo0x(lowersDec.getBytes()));
159 assertEquals(numbersHexNo0x, Hash.toHexNo0x(numbersDec.getBytes()));
163 public void toHexTest() throws Exception {
164 assertEquals(uppersHex2, Hash.toHex(uppersDec.getBytes()));
165 assertEquals(lowersHex, Hash.toHex(lowersDec.getBytes()));
166 assertEquals(numbersHex, Hash.toHex(numbersDec.getBytes()));
170 public void toHexWithOffset() throws Exception {
171 assertEquals(uppersHex2, Hash.toHex(uppersDec.getBytes(), 0, uppersDec.length()));
172 assertEquals(lowersHex, Hash.toHex(lowersDec.getBytes(), 0, lowersDec.length()));
173 assertEquals(numbersHex, Hash.toHex(numbersDec.getBytes(), 0, numbersDec.length()));
177 public void fromHexTest() throws Exception {
178 assertEquals(uppersDec, new String(Hash.fromHex(uppersHex1)));
179 assertEquals(lowersDec, new String(Hash.fromHex(lowersHex)));
180 assertEquals(numbersDec, new String(Hash.fromHex(numbersHex)));
183 // This string doesn't begin with "0x"
184 Hash.fromHex("0X65");
185 fail("Should have thrown CadiException");
186 } catch (CadiException e) {
187 assertEquals("HexString must start with \"0x\"", e.getMessage());
191 // This string has invalid hex characters
193 fail("Should have thrown CadiException");
194 } catch (CadiException e) {
196 assertEquals("Invalid char '81' in HexString", e.getMessage());
201 public void fromHexNo0xTest() throws Exception {
202 assertEquals(uppersDec, new String(Hash.fromHexNo0x(uppersHexNo0x1)));
203 assertEquals(lowersDec, new String(Hash.fromHexNo0x(lowersHexNo0x1)));
204 assertEquals(uppersDec, new String(Hash.fromHexNo0x(uppersHexNo0x2)));
205 assertEquals(lowersDec, new String(Hash.fromHexNo0x(lowersHexNo0x2)));
206 assertEquals(numbersDec, new String(Hash.fromHexNo0x(numbersHexNo0x)));
207 byte[] output = Hash.fromHexNo0x("ABC");
208 assertEquals(new String(new byte[] {(byte)0x0A, (byte)0xB0}), new String(output));
209 assertNull(Hash.fromHexNo0x("~~"));
213 // public void aaf_941() throws Exception {
214 // // User notes: From reported error "aaf" not coded right