Fix Hash.toHex for odd character Strings
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / JU_Base64.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.junit.Assert.*;
26 import static org.hamcrest.CoreMatchers.*;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.security.SecureRandom;
32
33 import org.junit.Test;
34 import org.onap.aaf.cadi.Symm;
35 import org.onap.aaf.cadi.config.Config;
36
37 public class JU_Base64 {
38     private static final String encoding = "Man is distinguished, not only by his reason, but by this singular " +
39             "passion from other animals, which is a lust of the mind, that by a " +
40             "perseverance of delight in the continued and indefatigable generation of " +
41             "knowledge, exceeds the short vehemence of any carnal pleasure.";
42
43     private static final String expected =
44             "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\n" +
45             "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\n" +
46             "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\n" +
47             "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\n" +
48             "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";
49
50     @Test
51     public void test() throws Exception {
52         // Test with different Padding
53         assertEncoded("leas",     "bGVhcw==");
54         assertEncoded("leasu",    "bGVhc3U=");
55         assertEncoded("leasur",   "bGVhc3Vy");
56         assertEncoded("leasure",  "bGVhc3VyZQ==");
57         assertEncoded("leasure.", "bGVhc3VyZS4=");
58
59         // Test with line ends
60         assertEncoded(encoding, expected);
61     }
62
63     @Test
64     public void symmetric() throws IOException {
65         String symmetric = new String(Symm.keygen());
66         Symm bsym = Symm.obtain(symmetric);
67         String result = bsym.encode(encoding);
68         assertThat(bsym.decode(result), is(encoding));
69
70         char[] manipulate = symmetric.toCharArray();
71         int spot = new SecureRandom().nextInt(manipulate.length);
72         manipulate[spot]|=0xFF;
73         String newsymmetric = new String(manipulate);
74         assertThat(symmetric, is(not(newsymmetric)));
75         try {
76             bsym = Symm.obtain(newsymmetric);
77             result = bsym.decode(result);
78             assertThat(result, is(encoding));
79         } catch (IOException e) {
80             // this is what we want to see if key wrong
81         }
82     }
83
84     private void assertEncoded(String toEncode, String expected) throws IOException {
85         String result = Symm.base64.encode(toEncode);
86         assertThat(result, is(expected));
87         ByteArrayOutputStream baos = new ByteArrayOutputStream();
88         Symm.base64.decode(new ByteArrayInputStream(result.getBytes()), baos);
89         result = baos.toString(Config.UTF_8);
90         assertThat(result, is(toEncode));
91     }
92
93 }