Update AAF Version 1.0.0
[aaf/cadi.git] / core / src / test / java / com / att / cadi / test / JU_Base64.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\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
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cadi.test;\r
24 \r
25 import static org.junit.Assert.assertEquals;\r
26 import static org.junit.Assert.assertNotSame;\r
27 \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 \r
34 import com.att.cadi.Symm;\r
35 import com.att.cadi.config.Config;\r
36 \r
37 \r
38 public class JU_Base64 {\r
39         private static final String encoding = "Man is distinguished, not only by his reason, but by this singular " +\r
40                         "passion from other animals, which is a lust of the mind, that by a " + \r
41                         "perseverance of delight in the continued and indefatigable generation of " + \r
42                         "knowledge, exceeds the short vehemence of any carnal pleasure.";\r
43                  \r
44         private static final String expected = \r
45                         "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\n" + \r
46                         "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\n" + \r
47                         "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\n" +\r
48                         "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\n" +\r
49                         "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";\r
50 \r
51 \r
52         @Test\r
53         public void test() throws Exception {\r
54                 // Test with different Padding\r
55                 encode("leas",    "bGVhcw==");\r
56                 encode("leasu",   "bGVhc3U=");\r
57                 encode("leasur",  "bGVhc3Vy");\r
58                 encode("leasure", "bGVhc3VyZQ==");\r
59                 encode("leasure.","bGVhc3VyZS4=");\r
60 \r
61                 // Test with line ends\r
62                 encode(encoding, expected);\r
63                 \r
64                 int ITER = 2000;\r
65                 System.out.println("Priming by Encoding Base64 " + ITER + " times");\r
66                 long start = System.nanoTime();\r
67                 for(int i=0;i<ITER;++i) {\r
68                         Symm.base64.encode(encoding);\r
69                 }\r
70                 Float ms = (System.nanoTime()-start)/1000000F;\r
71                 System.out.println("Total: " + ms + "ms");\r
72                 System.out.println("Avg:   " + ms/ITER + "ms");\r
73                 \r
74                 System.out.println("Priming by Decoding Base64 " + ITER + " times");\r
75                 start = System.nanoTime();\r
76                 for(int i=0;i<ITER;++i) {\r
77                         Symm.base64.decode(expected);\r
78                 }\r
79                 ms = (System.nanoTime()-start)/1000000F;\r
80                 System.out.println("Total: " + ms + "ms");\r
81                 System.out.println("Avg:   " + ms/ITER + "ms");\r
82 \r
83                 \r
84                 ITER=30000;\r
85                 System.out.println("Encoding Base64 " + ITER + " times");\r
86                 start = System.nanoTime();\r
87                 for(int i=0;i<ITER;++i) {\r
88                         Symm.base64.encode(encoding);\r
89                 }\r
90                 ms = (System.nanoTime()-start)/1000000F;\r
91                 System.out.println("Total: " + ms + "ms");\r
92                 System.out.println("Avg:   " + ms/ITER + "ms");\r
93                 \r
94                 System.out.println("Decoding Base64 " + ITER + " times");\r
95                 start = System.nanoTime();\r
96                 for(int i=0;i<ITER;++i) {\r
97                         Symm.base64.decode(expected);\r
98                 }\r
99                 ms = (System.nanoTime()-start)/1000000F;\r
100                 System.out.println("Total: " + ms + "ms");\r
101                 System.out.println("Avg:   " + ms/ITER + "ms");\r
102         }\r
103         \r
104         @Test\r
105         public void symmetric() throws IOException {\r
106                 System.out.println("Validating Generated Key mechanisms works");\r
107                 String symmetric = new String(Symm.base64.keygen());\r
108                 System.out.println(symmetric);\r
109                 Symm bsym = Symm.obtain(symmetric);\r
110                 String result = bsym.encode(encoding);\r
111                 System.out.println("\nResult:");\r
112                 System.out.println(result);\r
113                 assertEquals(encoding, bsym.decode(result));\r
114                 \r
115                 int ITER = 20000;\r
116                 System.out.println("Generating keys " + ITER + " times");\r
117                 long start = System.nanoTime();\r
118                 for(int i=0;i<ITER;++i) {\r
119                         Symm.base64.keygen();\r
120                 }\r
121                 Float ms = (System.nanoTime()-start)/1000000F;\r
122                 System.out.println("Total: " + ms + "ms");\r
123                 System.out.println("Avg:   " + ms/ITER + "ms");\r
124 \r
125                 char[] manipulate = symmetric.toCharArray();\r
126                 int spot = new SecureRandom().nextInt(manipulate.length);\r
127                 manipulate[spot]|=0xFF;\r
128                 String newsymmetric = new String(manipulate);\r
129                 assertNotSame(newsymmetric, symmetric);\r
130                 try {\r
131                         bsym = Symm.obtain(newsymmetric);\r
132                         result = bsym.decode(result);\r
133                         assertEquals(encoding, result);\r
134                 } catch (IOException e) {\r
135                         // this is what we want to see if key wrong\r
136                         System.out.println(e.getMessage() + " (as expected)");\r
137                 }\r
138         }\r
139 \r
140         private void encode(String toEncode, String expected) throws IOException {\r
141                 System.out.println("-------------------------------------------------");\r
142                 System.out.println(toEncode);\r
143                 System.out.println();\r
144                 System.out.println(expected);\r
145                 System.out.println();\r
146                 String result = Symm.base64.encode(toEncode);\r
147                 System.out.println(result);\r
148                 assertEquals(expected,result);\r
149                 \r
150                 ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
151                 Symm.base64.decode(new ByteArrayInputStream(result.getBytes()), baos);\r
152                 result = baos.toString(Config.UTF_8);\r
153                 System.out.println(result);\r
154                 assertEquals(toEncode,result);\r
155                 \r
156         }\r
157 }\r