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