changed to unmaintained
[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 static org.hamcrest.CoreMatchers.equalTo;
26 import static org.hamcrest.CoreMatchers.not;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertThat;
31 import static org.junit.Assert.assertTrue;
32
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.aaf.cadi.Hash;
36
37 public class JU_Hash {
38     // Some common test vectors
39     private String quickBrownFoxVector = "The quick brown fox jumps over the lazy dog";
40     private String quickBrownFoxMD5 = "0x9e107d9d372bb6826bd81d3542a419d6";
41     private String quickBrownFoxSHA256 = "0xd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592";
42
43     private String emptyVector = "";
44     private String emptyMD5 = "0xd41d8cd98f00b204e9800998ecf8427e";
45     private String emptySHA256 = "0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
46
47
48     private byte[] same1 = "this is a twin".getBytes();
49     private byte[] same2 = "this is a twin".getBytes();
50     private byte[] different1 = "guvf vf n gjva".getBytes();
51     private byte[] different2 = "this is an only child".getBytes();
52
53
54     private String uppersDec = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
55     private String uppersHex1 = "0x4142434445464748494A4B4C4D4E4F505152535455565758595A";
56     private String uppersHex2 = "0x4142434445464748494a4b4c4d4e4f505152535455565758595a";
57     private String uppersHexNo0x1 = "4142434445464748494a4b4c4d4e4f505152535455565758595a";
58     private String uppersHexNo0x2 = "4142434445464748494A4B4C4D4E4F505152535455565758595A";
59
60     private String lowersDec = "abcdefghijklmnopqrstuvwxyz";
61     private String lowersHex = "0x6162636465666768696a6b6c6d6e6f707172737475767778797a";
62     private String lowersHexNo0x1 = "6162636465666768696a6b6c6d6e6f707172737475767778797a";
63     private String lowersHexNo0x2 = "6162636465666768696A6B6C6D6E6F707172737475767778797A";
64
65     private String numbersDec = "1234567890";
66     private String numbersHex = "0x31323334353637383930";
67     private String numbersHexNo0x = "31323334353637383930";
68         
69     @SuppressWarnings("unused")
70     @BeforeClass
71     public static void getCoverage() {
72         // All of this class's methods are static, so we never need to instantiate an object.
73         // That said, we can't get 100% coverage unless we instantiate one
74         Hash hash = new Hash();
75     }
76
77     @Test
78     public void hashMD5Test() throws Exception {
79         byte[] output = Hash.hashMD5(quickBrownFoxVector.getBytes());
80         assertEquals(quickBrownFoxMD5, new String(Hash.toHex(output)));
81
82         output = Hash.hashMD5(emptyVector.getBytes());
83         assertEquals(emptyMD5, new String(Hash.toHex(output)));
84     }
85
86     @Test
87     public void hashMD5WithOffsetTest() throws Exception {
88         byte[] output = Hash.hashMD5(quickBrownFoxVector.getBytes(), 0, quickBrownFoxVector.length());
89         assertEquals(quickBrownFoxMD5, new String(Hash.toHex(output)));
90
91         output = Hash.hashMD5(emptyVector.getBytes(), 0, emptyVector.length());
92         assertEquals(emptyMD5, new String(Hash.toHex(output)));
93     }
94
95     @Test
96     public void hashMD5AsStringHexTest() throws Exception {
97         String output = Hash.hashMD5asStringHex(quickBrownFoxVector);
98         assertEquals(quickBrownFoxMD5, output);
99
100         output = Hash.hashMD5asStringHex(emptyVector);
101         assertEquals(emptyMD5, output);
102     }
103
104     @Test
105     public void hashSHA256Test() throws Exception {
106         byte[] output = Hash.hashSHA256(quickBrownFoxVector.getBytes());
107         assertEquals(quickBrownFoxSHA256, new String(Hash.toHex(output)));
108
109         output = Hash.hashSHA256(emptyVector.getBytes());
110         assertEquals(emptySHA256, new String(Hash.toHex(output)));
111     }
112
113     @Test
114     public void hashSHA256WithOffsetTest() throws Exception {
115         byte[] output = Hash.hashSHA256(quickBrownFoxVector.getBytes(), 0, quickBrownFoxVector.length());
116         assertEquals(quickBrownFoxSHA256, new String(Hash.toHex(output)));
117
118         output = Hash.hashSHA256(emptyVector.getBytes(), 0, emptyVector.length());
119         assertEquals(emptySHA256, new String(Hash.toHex(output)));
120     }
121
122     @Test
123     public void hashSHA256AsStringHexTest() throws Exception {
124         String output = Hash.hashSHA256asStringHex(quickBrownFoxVector);
125         assertEquals(quickBrownFoxSHA256, output);
126
127         output = Hash.hashSHA256asStringHex(emptyVector);
128         assertEquals(emptySHA256, output);
129     }
130
131     @Test
132     public void hashSaltySHA256AsStringHexTest() throws Exception {
133         String input = "password";
134         String hash1 = Hash.hashSHA256asStringHex(input, 10);
135         String hash2 = Hash.hashSHA256asStringHex(input, 10);
136         String hash3 = Hash.hashSHA256asStringHex(input, 11);
137
138         assertEquals(hash1, hash2);
139         assertThat(hash1, not(equalTo(hash3)));
140     }
141
142     @Test
143     public void isEqualTest() throws Exception {
144         assertTrue(Hash.isEqual(same1, same2));
145         assertFalse(Hash.isEqual(same1, different1));
146         assertFalse(Hash.isEqual(same1, different2));
147     }
148
149     @Test
150     public void compareToTest() throws Exception {
151         assertEquals(0, Hash.compareTo(same1, same2));
152         // different1 is rot13(same1), so the difference should be 13
153         assertEquals(13, Hash.compareTo(same1, different1));
154         assertEquals(-78, Hash.compareTo(same1, different2));
155     }
156
157     @Test
158     public void toHexNo0xTest() throws Exception {
159         assertEquals(uppersHexNo0x1, Hash.toHexNo0x(uppersDec.getBytes()));
160         assertEquals(lowersHexNo0x1, Hash.toHexNo0x(lowersDec.getBytes()));
161         assertEquals(numbersHexNo0x, Hash.toHexNo0x(numbersDec.getBytes()));
162     }
163
164     @Test
165     public void toHexTest() throws Exception {
166         assertEquals(uppersHex2, Hash.toHex(uppersDec.getBytes()));
167         assertEquals(lowersHex, Hash.toHex(lowersDec.getBytes()));
168         assertEquals(numbersHex, Hash.toHex(numbersDec.getBytes()));
169     }
170
171     @Test
172     public void toHexWithOffset() throws Exception {
173         assertEquals(uppersHex2, Hash.toHex(uppersDec.getBytes(), 0, uppersDec.length()));
174         assertEquals(lowersHex, Hash.toHex(lowersDec.getBytes(), 0, lowersDec.length()));
175         assertEquals(numbersHex, Hash.toHex(numbersDec.getBytes(), 0, numbersDec.length()));
176     }
177
178     @Test
179     public void fromHexTest() throws Exception {
180         assertEquals(uppersDec, new String(Hash.fromHex(uppersHex1)));
181         assertEquals(lowersDec, new String(Hash.fromHex(lowersHex)));
182         assertEquals(numbersDec, new String(Hash.fromHex(numbersHex)));
183
184         // This string doesn't begin with "0x"
185         assertNull(Hash.fromHex("0X65"));
186
187             // This string has invalid hex characters
188         assertNull(Hash.fromHex("0xQ"));
189     }
190
191     @Test
192     public void fromHexNo0xTest() throws Exception {
193         assertEquals(uppersDec, new String(Hash.fromHexNo0x(uppersHexNo0x1)));
194         assertEquals(lowersDec, new String(Hash.fromHexNo0x(lowersHexNo0x1)));
195         assertEquals(uppersDec, new String(Hash.fromHexNo0x(uppersHexNo0x2)));
196         assertEquals(lowersDec, new String(Hash.fromHexNo0x(lowersHexNo0x2)));
197         byte[] output = Hash.fromHexNo0x("ABC");
198         assertEquals(new String(new byte[] {(byte)0x0A, (byte)0xBC}), new String(output));
199         assertNull(Hash.fromHexNo0x("~~"));
200     }
201     
202     @Test
203     public void aaf_941() throws Exception {
204         // User notes: From reported error "aaf" not coded right for odd digits
205         // Note:  In the original concept, this isn't a valid Hex digit.  It has to do with whether to assume an initial 
206         // char of "0" if left out.
207         
208         String sample = "aaf";
209         byte[] bytes = Hash.fromHexNo0x(sample);
210         String back = Hash.toHexNo0x(bytes);
211         // Note: We don't presume to know that someone left off leading 0 on start.
212         assertEquals("0aaf", back);
213         
214         sample = "0x0aaf";
215         bytes = Hash.fromHex(sample);
216         back = Hash.toHex(bytes);
217         assertEquals(sample, back);
218         
219         // Assumed leading zero.  Note, we ALWAYS translate back with leading zero.  
220         bytes = Hash.fromHex("0xaaf");
221         back = Hash.toHex(bytes);
222         assertEquals(sample, back);
223
224     }
225 }