Update DCAE Startup Info
[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
23 package org.onap.aaf.cadi.test;
24
25 import org.junit.Test;
26 import org.onap.aaf.cadi.CadiException;
27 import org.onap.aaf.cadi.Hash;
28
29 import static org.junit.Assert.*;
30
31 import org.junit.BeforeClass;
32
33 import static org.hamcrest.CoreMatchers.*;
34
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";
40
41     private String emptyVector = "";
42     private String emptyMD5 = "0xd41d8cd98f00b204e9800998ecf8427e";
43     private String emptySHA256 = "0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
44
45
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();
50
51
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";
57
58     private String lowersDec = "abcdefghijklmnopqrstuvwxyz";
59     private String lowersHex = "0x6162636465666768696a6b6c6d6e6f707172737475767778797a";
60     private String lowersHexNo0x1 = "6162636465666768696a6b6c6d6e6f707172737475767778797a";
61     private String lowersHexNo0x2 = "6162636465666768696A6B6C6D6E6F707172737475767778797A";
62
63     private String numbersDec = "1234567890";
64     private String numbersHex = "0x31323334353637383930";
65     private String numbersHexNo0x = "31323334353637383930";
66
67     @SuppressWarnings("unused")
68     @BeforeClass
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();
73     }
74
75     @Test
76     public void hashMD5Test() throws Exception {
77         byte[] output = Hash.hashMD5(quickBrownFoxVector.getBytes());
78         assertEquals(quickBrownFoxMD5, new String(Hash.toHex(output)));
79
80         output = Hash.hashMD5(emptyVector.getBytes());
81         assertEquals(emptyMD5, new String(Hash.toHex(output)));
82     }
83
84     @Test
85     public void hashMD5WithOffsetTest() throws Exception {
86         byte[] output = Hash.hashMD5(quickBrownFoxVector.getBytes(), 0, quickBrownFoxVector.length());
87         assertEquals(quickBrownFoxMD5, new String(Hash.toHex(output)));
88
89         output = Hash.hashMD5(emptyVector.getBytes(), 0, emptyVector.length());
90         assertEquals(emptyMD5, new String(Hash.toHex(output)));
91     }
92
93     @Test
94     public void hashMD5AsStringHexTest() throws Exception {
95         String output = Hash.hashMD5asStringHex(quickBrownFoxVector);
96         assertEquals(quickBrownFoxMD5, output);
97
98         output = Hash.hashMD5asStringHex(emptyVector);
99         assertEquals(emptyMD5, output);
100     }
101
102     @Test
103     public void hashSHA256Test() throws Exception {
104         byte[] output = Hash.hashSHA256(quickBrownFoxVector.getBytes());
105         assertEquals(quickBrownFoxSHA256, new String(Hash.toHex(output)));
106
107         output = Hash.hashSHA256(emptyVector.getBytes());
108         assertEquals(emptySHA256, new String(Hash.toHex(output)));
109     }
110
111     @Test
112     public void hashSHA256WithOffsetTest() throws Exception {
113         byte[] output = Hash.hashSHA256(quickBrownFoxVector.getBytes(), 0, quickBrownFoxVector.length());
114         assertEquals(quickBrownFoxSHA256, new String(Hash.toHex(output)));
115
116         output = Hash.hashSHA256(emptyVector.getBytes(), 0, emptyVector.length());
117         assertEquals(emptySHA256, new String(Hash.toHex(output)));
118     }
119
120     @Test
121     public void hashSHA256AsStringHexTest() throws Exception {
122         String output = Hash.hashSHA256asStringHex(quickBrownFoxVector);
123         assertEquals(quickBrownFoxSHA256, output);
124
125         output = Hash.hashSHA256asStringHex(emptyVector);
126         assertEquals(emptySHA256, output);
127     }
128
129     @Test
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);
135
136         assertEquals(hash1, hash2);
137         assertThat(hash1, not(equalTo(hash3)));
138     }
139
140     @Test
141     public void isEqualTest() throws Exception {
142         assertTrue(Hash.isEqual(same1, same2));
143         assertFalse(Hash.isEqual(same1, different1));
144         assertFalse(Hash.isEqual(same1, different2));
145     }
146
147     @Test
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));
153     }
154
155     @Test
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()));
160     }
161
162     @Test
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()));
167     }
168
169     @Test
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()));
174     }
175
176     @Test
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)));
181
182         try {
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());
188         }
189
190         try {
191             // This string has invalid hex characters
192             Hash.fromHex("0xQ");
193             fail("Should have thrown CadiException");
194         } catch (CadiException e) {
195             // 81 is dec(Q)
196             assertEquals("Invalid char '81' in HexString", e.getMessage());
197         }
198     }
199
200     @Test
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("~~"));
210     }
211 //
212 //    @Test
213 //    public void aaf_941() throws Exception {
214 //        // User notes: From reported error "aaf" not coded right
215 //
216 //
217 //    }
218 }