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
158 public void test1() throws Exception {
\r
159 // Test with different Padding
\r
160 encode("leas", "bGVhcw==");
\r
161 encode("leasu", "bGVhc3U=");
\r
162 encode("leasur", "bGVhc3Vy");
\r
163 encode("leasure", "bGVhc3VyZQ==");
\r
164 encode("leasure.","bGVhc3VyZS4=");
\r
166 // Test with line ends
\r
167 encode(encoding, expected);
\r
170 System.out.println("Priming by Encoding Base64 " + ITER + " times");
\r
171 long start = System.nanoTime();
\r
172 for(int i=0;i<ITER;++i) {
\r
173 Symm.base64.encode(encoding);
\r
175 Float ms = (System.nanoTime()-start)/1000000F;
\r
176 System.out.println("Total: " + ms + "ms");
\r
177 System.out.println("Avg: " + ms/ITER + "ms");
\r
179 System.out.println("Priming by Decoding Base64 " + ITER + " times");
\r
180 start = System.nanoTime();
\r
181 for(int i=0;i<ITER;++i) {
\r
182 Symm.base64.decode(expected);
\r
184 ms = (System.nanoTime()-start)/1000000F;
\r
185 System.out.println("Total: " + ms + "ms");
\r
186 System.out.println("Avg: " + ms/ITER + "ms");
\r
190 System.out.println("Encoding Base64 " + ITER + " times");
\r
191 start = System.nanoTime();
\r
192 for(int i=0;i<ITER;++i) {
\r
193 Symm.base64.encode(encoding);
\r
195 ms = (System.nanoTime()-start)/1000000F;
\r
196 System.out.println("Total: " + ms + "ms");
\r
197 System.out.println("Avg: " + ms/ITER + "ms");
\r
199 System.out.println("Decoding Base64 " + ITER + " times");
\r
200 start = System.nanoTime();
\r
201 for(int i=0;i<ITER;++i) {
\r
202 Symm.base64.decode(expected);
\r
204 ms = (System.nanoTime()-start)/1000000F;
\r
205 System.out.println("Total: " + ms + "ms");
\r
206 System.out.println("Avg: " + ms/ITER + "ms");
\r
210 public void symmetric1() throws IOException {
\r
211 System.out.println("Validating Generated Key mechanisms works");
\r
212 String symmetric = new String(Symm.base64.keygen());
\r
213 System.out.println(symmetric);
\r
214 Symm bsym = Symm.obtain(symmetric);
\r
215 String result = bsym.encode(encoding);
\r
216 System.out.println("\nResult:");
\r
217 System.out.println(result);
\r
218 assertEquals(encoding, bsym.decode(result));
\r
221 System.out.println("Generating keys " + ITER + " times");
\r
222 long start = System.nanoTime();
\r
223 for(int i=0;i<ITER;++i) {
\r
224 Symm.base64.keygen();
\r
226 Float ms = (System.nanoTime()-start)/1000000F;
\r
227 System.out.println("Total: " + ms + "ms");
\r
228 System.out.println("Avg: " + ms/ITER + "ms");
\r
230 char[] manipulate = symmetric.toCharArray();
\r
231 int spot = new SecureRandom().nextInt(manipulate.length);
\r
232 manipulate[spot]|=0xFF;
\r
233 String newsymmetric = new String(manipulate);
\r
234 assertNotSame(newsymmetric, symmetric);
\r
236 bsym = Symm.obtain(newsymmetric);
\r
237 result = bsym.decode(result);
\r
238 assertEquals(encoding, result);
\r
239 } catch (IOException e) {
\r
240 // this is what we want to see if key wrong
\r
241 System.out.println(e.getMessage() + " (as expected)");
\r
245 private void encode1(String toEncode, String expected) throws IOException {
\r
246 System.out.println("-------------------------------------------------");
\r
247 System.out.println(toEncode);
\r
248 System.out.println();
\r
249 System.out.println(expected);
\r
250 System.out.println();
\r
251 String result = Symm.base64.encode(toEncode);
\r
252 System.out.println(result);
\r
253 assertEquals(expected,result);
\r
255 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
256 Symm.base64.decode(new ByteArrayInputStream(result.getBytes()), baos);
\r
257 result = baos.toString(Config.UTF_8);
\r
258 System.out.println(result);
\r
259 assertEquals(toEncode,result);
\r
264 public void test2() throws Exception {
\r
265 // Test with different Padding
\r
266 encode("leas", "bGVhcw==");
\r
267 encode("leasu", "bGVhc3U=");
\r
268 encode("leasur", "bGVhc3Vy");
\r
269 encode("leasure", "bGVhc3VyZQ==");
\r
270 encode("leasure.","bGVhc3VyZS4=");
\r
272 // Test with line ends
\r
273 encode(encoding, expected);
\r
276 System.out.println("Priming by Encoding Base64 " + ITER + " times");
\r
277 long start = System.nanoTime();
\r
278 for(int i=0;i<ITER;++i) {
\r
279 Symm.base64.encode(encoding);
\r
281 Float ms = (System.nanoTime()-start)/1000000F;
\r
282 System.out.println("Total: " + ms + "ms");
\r
283 System.out.println("Avg: " + ms/ITER + "ms");
\r
285 System.out.println("Priming by Decoding Base64 " + ITER + " times");
\r
286 start = System.nanoTime();
\r
287 for(int i=0;i<ITER;++i) {
\r
288 Symm.base64.decode(expected);
\r
290 ms = (System.nanoTime()-start)/1000000F;
\r
291 System.out.println("Total: " + ms + "ms");
\r
292 System.out.println("Avg: " + ms/ITER + "ms");
\r
296 System.out.println("Encoding Base64 " + ITER + " times");
\r
297 start = System.nanoTime();
\r
298 for(int i=0;i<ITER;++i) {
\r
299 Symm.base64.encode(encoding);
\r
301 ms = (System.nanoTime()-start)/1000000F;
\r
302 System.out.println("Total: " + ms + "ms");
\r
303 System.out.println("Avg: " + ms/ITER + "ms");
\r
305 System.out.println("Decoding Base64 " + ITER + " times");
\r
306 start = System.nanoTime();
\r
307 for(int i=0;i<ITER;++i) {
\r
308 Symm.base64.decode(expected);
\r
310 ms = (System.nanoTime()-start)/1000000F;
\r
311 System.out.println("Total: " + ms + "ms");
\r
312 System.out.println("Avg: " + ms/ITER + "ms");
\r
316 public void symmetric2() throws IOException {
\r
317 System.out.println("Validating Generated Key mechanisms works");
\r
318 String symmetric = new String(Symm.base64.keygen());
\r
319 System.out.println(symmetric);
\r
320 Symm bsym = Symm.obtain(symmetric);
\r
321 String result = bsym.encode(encoding);
\r
322 System.out.println("\nResult:");
\r
323 System.out.println(result);
\r
324 assertEquals(encoding, bsym.decode(result));
\r
327 System.out.println("Generating keys " + ITER + " times");
\r
328 long start = System.nanoTime();
\r
329 for(int i=0;i<ITER;++i) {
\r
330 Symm.base64.keygen();
\r
332 Float ms = (System.nanoTime()-start)/1000000F;
\r
333 System.out.println("Total: " + ms + "ms");
\r
334 System.out.println("Avg: " + ms/ITER + "ms");
\r
336 char[] manipulate = symmetric.toCharArray();
\r
337 int spot = new SecureRandom().nextInt(manipulate.length);
\r
338 manipulate[spot]|=0xFF;
\r
339 String newsymmetric = new String(manipulate);
\r
340 assertNotSame(newsymmetric, symmetric);
\r
342 bsym = Symm.obtain(newsymmetric);
\r
343 result = bsym.decode(result);
\r
344 assertEquals(encoding, result);
\r
345 } catch (IOException e) {
\r
346 // this is what we want to see if key wrong
\r
347 System.out.println(e.getMessage() + " (as expected)");
\r
351 private void encode2(String toEncode, String expected) throws IOException {
\r
352 System.out.println("-------------------------------------------------");
\r
353 System.out.println(toEncode);
\r
354 System.out.println();
\r
355 System.out.println(expected);
\r
356 System.out.println();
\r
357 String result = Symm.base64.encode(toEncode);
\r
358 System.out.println(result);
\r
359 assertEquals(expected,result);
\r
361 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
362 Symm.base64.decode(new ByteArrayInputStream(result.getBytes()), baos);
\r
363 result = baos.toString(Config.UTF_8);
\r
364 System.out.println(result);
\r
365 assertEquals(toEncode,result);
\r