Mass removal of all Tabs (Style Warnings)
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / JU_Hash.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
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
10  * * 
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * * 
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====================================================
19  * *
20  * *
21  ******************************************************************************/
22 package org.onap.aaf.cadi.test;
23
24 import org.junit.Test;
25 import org.onap.aaf.cadi.CadiException;
26 import org.onap.aaf.cadi.Hash;
27
28 import static org.junit.Assert.*;
29
30 import org.junit.BeforeClass;
31
32 import static org.hamcrest.CoreMatchers.*;
33
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";
39
40     private String emptyVector = "";
41     private String emptyMD5 = "0xd41d8cd98f00b204e9800998ecf8427e";
42     private String emptySHA256 = "0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
43
44
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();
49
50
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";
56
57     private String lowersDec = "abcdefghijklmnopqrstuvwxyz";
58     private String lowersHex = "0x6162636465666768696a6b6c6d6e6f707172737475767778797a";
59     private String lowersHexNo0x1 = "6162636465666768696a6b6c6d6e6f707172737475767778797a";
60     private String lowersHexNo0x2 = "6162636465666768696A6B6C6D6E6F707172737475767778797A";
61
62     private String numbersDec = "1234567890";
63     private String numbersHex = "0x31323334353637383930";
64     private String numbersHexNo0x = "31323334353637383930";
65
66     @SuppressWarnings("unused")
67     @BeforeClass
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();
72     }
73
74     @Test
75     public void hashMD5Test() throws Exception {
76         byte[] output = Hash.hashMD5(quickBrownFoxVector.getBytes());
77         assertEquals(quickBrownFoxMD5, new String(Hash.toHex(output)));
78
79         output = Hash.hashMD5(emptyVector.getBytes());
80         assertEquals(emptyMD5, new String(Hash.toHex(output)));
81     }
82
83     @Test
84     public void hashMD5WithOffsetTest() throws Exception {
85         byte[] output = Hash.hashMD5(quickBrownFoxVector.getBytes(), 0, quickBrownFoxVector.length());
86         assertEquals(quickBrownFoxMD5, new String(Hash.toHex(output)));
87
88         output = Hash.hashMD5(emptyVector.getBytes(), 0, emptyVector.length());
89         assertEquals(emptyMD5, new String(Hash.toHex(output)));
90     }
91
92     @Test
93     public void hashMD5AsStringHexTest() throws Exception {
94         String output = Hash.hashMD5asStringHex(quickBrownFoxVector);
95         assertEquals(quickBrownFoxMD5, output);
96
97         output = Hash.hashMD5asStringHex(emptyVector);
98         assertEquals(emptyMD5, output);
99     }
100
101     @Test
102     public void hashSHA256Test() throws Exception {
103         byte[] output = Hash.hashSHA256(quickBrownFoxVector.getBytes());
104         assertEquals(quickBrownFoxSHA256, new String(Hash.toHex(output)));
105
106         output = Hash.hashSHA256(emptyVector.getBytes());
107         assertEquals(emptySHA256, new String(Hash.toHex(output)));
108     }
109
110     @Test
111     public void hashSHA256WithOffsetTest() throws Exception {
112         byte[] output = Hash.hashSHA256(quickBrownFoxVector.getBytes(), 0, quickBrownFoxVector.length());
113         assertEquals(quickBrownFoxSHA256, new String(Hash.toHex(output)));
114
115         output = Hash.hashSHA256(emptyVector.getBytes(), 0, emptyVector.length());
116         assertEquals(emptySHA256, new String(Hash.toHex(output)));
117     }
118
119     @Test
120     public void hashSHA256AsStringHexTest() throws Exception {
121         String output = Hash.hashSHA256asStringHex(quickBrownFoxVector);
122         assertEquals(quickBrownFoxSHA256, output);
123
124         output = Hash.hashSHA256asStringHex(emptyVector);
125         assertEquals(emptySHA256, output);
126     }
127
128     @Test
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);
134
135         assertEquals(hash1, hash2);
136         assertThat(hash1, not(equalTo(hash3)));
137     }
138
139     @Test
140     public void isEqualTest() throws Exception {
141         assertTrue(Hash.isEqual(same1, same2));
142         assertFalse(Hash.isEqual(same1, different1));
143         assertFalse(Hash.isEqual(same1, different2));
144     }
145
146     @Test
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));
152     }
153
154     @Test
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()));
159     }
160
161     @Test
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()));
166     }
167
168     @Test
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()));
173     }
174
175     @Test
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)));
180
181         try {
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());
187         }
188
189         try {
190             // This string has invalid hex characters
191             Hash.fromHex("0xQ");
192             fail("Should have thrown CadiException");
193         } catch (CadiException e) {
194             // 81 is dec(Q)
195             assertEquals("Invalid char '81' in HexString", e.getMessage());
196         }
197     }
198
199     @Test
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("~~"));
209     }
210 }