1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.cadi.test;
\r
25 import static org.junit.Assert.assertEquals;
\r
26 import static org.junit.Assert.assertNotSame;
\r
28 import java.io.ByteArrayInputStream;
\r
29 import java.io.ByteArrayOutputStream;
\r
30 import java.io.IOException;
\r
31 import java.security.SecureRandom;
\r
32 import org.junit.Test;
\r
33 import org.onap.aaf.cadi.Symm;
\r
34 import org.onap.aaf.cadi.config.Config;
\r
37 public class JU_Base64 {
\r
38 private static final String encoding = "Man is distinguished, not only by his reason, but by this singular " +
\r
39 "passion from other animals, which is a lust of the mind, that by a " +
\r
40 "perseverance of delight in the continued and indefatigable generation of " +
\r
41 "knowledge, exceeds the short vehemence of any carnal pleasure.";
\r
43 private static final String expected =
\r
44 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\n" +
\r
45 "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\n" +
\r
46 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\n" +
\r
47 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\n" +
\r
48 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";
\r
52 public void test() throws Exception {
\r
53 // Test with different Padding
\r
54 encode("leas", "bGVhcw==");
\r
55 encode("leasu", "bGVhc3U=");
\r
56 encode("leasur", "bGVhc3Vy");
\r
57 encode("leasure", "bGVhc3VyZQ==");
\r
58 encode("leasure.","bGVhc3VyZS4=");
\r
60 // Test with line ends
\r
61 encode(encoding, expected);
\r
64 System.out.println("Priming by Encoding Base64 " + ITER + " times");
\r
65 long start = System.nanoTime();
\r
66 for(int i=0;i<ITER;++i) {
\r
67 Symm.base64.encode(encoding);
\r
69 Float ms = (System.nanoTime()-start)/1000000F;
\r
70 System.out.println("Total: " + ms + "ms");
\r
71 System.out.println("Avg: " + ms/ITER + "ms");
\r
73 System.out.println("Priming by Decoding Base64 " + ITER + " times");
\r
74 start = System.nanoTime();
\r
75 for(int i=0;i<ITER;++i) {
\r
76 Symm.base64.decode(expected);
\r
78 ms = (System.nanoTime()-start)/1000000F;
\r
79 System.out.println("Total: " + ms + "ms");
\r
80 System.out.println("Avg: " + ms/ITER + "ms");
\r
84 System.out.println("Encoding Base64 " + ITER + " times");
\r
85 start = System.nanoTime();
\r
86 for(int i=0;i<ITER;++i) {
\r
87 Symm.base64.encode(encoding);
\r
89 ms = (System.nanoTime()-start)/1000000F;
\r
90 System.out.println("Total: " + ms + "ms");
\r
91 System.out.println("Avg: " + ms/ITER + "ms");
\r
93 System.out.println("Decoding Base64 " + ITER + " times");
\r
94 start = System.nanoTime();
\r
95 for(int i=0;i<ITER;++i) {
\r
96 Symm.base64.decode(expected);
\r
98 ms = (System.nanoTime()-start)/1000000F;
\r
99 System.out.println("Total: " + ms + "ms");
\r
100 System.out.println("Avg: " + ms/ITER + "ms");
\r
104 public void symmetric() throws IOException {
\r
105 System.out.println("Validating Generated Key mechanisms works");
\r
106 String symmetric = new String(Symm.base64.keygen());
\r
107 System.out.println(symmetric);
\r
108 Symm bsym = Symm.obtain(symmetric);
\r
109 String result = bsym.encode(encoding);
\r
110 System.out.println("\nResult:");
\r
111 System.out.println(result);
\r
112 assertEquals(encoding, bsym.decode(result));
\r
115 System.out.println("Generating keys " + ITER + " times");
\r
116 long start = System.nanoTime();
\r
117 for(int i=0;i<ITER;++i) {
\r
118 Symm.base64.keygen();
\r
120 Float ms = (System.nanoTime()-start)/1000000F;
\r
121 System.out.println("Total: " + ms + "ms");
\r
122 System.out.println("Avg: " + ms/ITER + "ms");
\r
124 char[] manipulate = symmetric.toCharArray();
\r
125 int spot = new SecureRandom().nextInt(manipulate.length);
\r
126 manipulate[spot]|=0xFF;
\r
127 String newsymmetric = new String(manipulate);
\r
128 assertNotSame(newsymmetric, symmetric);
\r
130 bsym = Symm.obtain(newsymmetric);
\r
131 result = bsym.decode(result);
\r
132 assertEquals(encoding, result);
\r
133 } catch (IOException e) {
\r
134 // this is what we want to see if key wrong
\r
135 System.out.println(e.getMessage() + " (as expected)");
\r
139 private void encode(String toEncode, String expected) throws IOException {
\r
140 System.out.println("-------------------------------------------------");
\r
141 System.out.println(toEncode);
\r
142 System.out.println();
\r
143 System.out.println(expected);
\r
144 System.out.println();
\r
145 String result = Symm.base64.encode(toEncode);
\r
146 System.out.println(result);
\r
147 assertEquals(expected,result);
\r
149 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
150 Symm.base64.decode(new ByteArrayInputStream(result.getBytes()), baos);
\r
151 result = baos.toString(Config.UTF_8);
\r
152 System.out.println(result);
\r
153 assertEquals(toEncode,result);
\r