+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.onap.aaf.cadi.test;\r
-\r
-import static org.junit.Assert.assertEquals;\r
-import static org.junit.Assert.assertNotSame;\r
-\r
-import java.io.ByteArrayInputStream;\r
-import java.io.ByteArrayOutputStream;\r
-import java.io.IOException;\r
-import java.security.SecureRandom;\r
-import org.junit.Test;\r
-import org.onap.aaf.cadi.Symm;\r
-import org.onap.aaf.cadi.config.Config;\r
-\r
-\r
-public class JU_Base64 {\r
- private static final String encoding = "Man is distinguished, not only by his reason, but by this singular " +\r
- "passion from other animals, which is a lust of the mind, that by a " + \r
- "perseverance of delight in the continued and indefatigable generation of " + \r
- "knowledge, exceeds the short vehemence of any carnal pleasure.";\r
- \r
- private static final String expected = \r
- "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\n" + \r
- "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\n" + \r
- "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\n" +\r
- "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\n" +\r
- "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";\r
-\r
-\r
- @Test\r
- public void test() throws Exception {\r
- // Test with different Padding\r
- encode("leas", "bGVhcw==");\r
- encode("leasu", "bGVhc3U=");\r
- encode("leasur", "bGVhc3Vy");\r
- encode("leasure", "bGVhc3VyZQ==");\r
- encode("leasure.","bGVhc3VyZS4=");\r
-\r
- // Test with line ends\r
- encode(encoding, expected);\r
- \r
- int ITER = 2000;\r
- System.out.println("Priming by Encoding Base64 " + ITER + " times");\r
- long start = System.nanoTime();\r
- for(int i=0;i<ITER;++i) {\r
- Symm.base64.encode(encoding);\r
- }\r
- Float ms = (System.nanoTime()-start)/1000000F;\r
- System.out.println("Total: " + ms + "ms");\r
- System.out.println("Avg: " + ms/ITER + "ms");\r
- \r
- System.out.println("Priming by Decoding Base64 " + ITER + " times");\r
- start = System.nanoTime();\r
- for(int i=0;i<ITER;++i) {\r
- Symm.base64.decode(expected);\r
- }\r
- ms = (System.nanoTime()-start)/1000000F;\r
- System.out.println("Total: " + ms + "ms");\r
- System.out.println("Avg: " + ms/ITER + "ms");\r
-\r
- \r
- ITER=30000;\r
- System.out.println("Encoding Base64 " + ITER + " times");\r
- start = System.nanoTime();\r
- for(int i=0;i<ITER;++i) {\r
- Symm.base64.encode(encoding);\r
- }\r
- ms = (System.nanoTime()-start)/1000000F;\r
- System.out.println("Total: " + ms + "ms");\r
- System.out.println("Avg: " + ms/ITER + "ms");\r
- \r
- System.out.println("Decoding Base64 " + ITER + " times");\r
- start = System.nanoTime();\r
- for(int i=0;i<ITER;++i) {\r
- Symm.base64.decode(expected);\r
- }\r
- ms = (System.nanoTime()-start)/1000000F;\r
- System.out.println("Total: " + ms + "ms");\r
- System.out.println("Avg: " + ms/ITER + "ms");\r
- }\r
- \r
- @Test\r
- public void symmetric() throws IOException {\r
- System.out.println("Validating Generated Key mechanisms works");\r
- String symmetric = new String(Symm.base64.keygen());\r
- System.out.println(symmetric);\r
- Symm bsym = Symm.obtain(symmetric);\r
- String result = bsym.encode(encoding);\r
- System.out.println("\nResult:");\r
- System.out.println(result);\r
- assertEquals(encoding, bsym.decode(result));\r
- \r
- int ITER = 20000;\r
- System.out.println("Generating keys " + ITER + " times");\r
- long start = System.nanoTime();\r
- for(int i=0;i<ITER;++i) {\r
- Symm.base64.keygen();\r
- }\r
- Float ms = (System.nanoTime()-start)/1000000F;\r
- System.out.println("Total: " + ms + "ms");\r
- System.out.println("Avg: " + ms/ITER + "ms");\r
-\r
- char[] manipulate = symmetric.toCharArray();\r
- int spot = new SecureRandom().nextInt(manipulate.length);\r
- manipulate[spot]|=0xFF;\r
- String newsymmetric = new String(manipulate);\r
- assertNotSame(newsymmetric, symmetric);\r
- try {\r
- bsym = Symm.obtain(newsymmetric);\r
- result = bsym.decode(result);\r
- assertEquals(encoding, result);\r
- } catch (IOException e) {\r
- // this is what we want to see if key wrong\r
- System.out.println(e.getMessage() + " (as expected)");\r
- }\r
- }\r
-\r
- private void encode(String toEncode, String expected) throws IOException {\r
- System.out.println("-------------------------------------------------");\r
- System.out.println(toEncode);\r
- System.out.println();\r
- System.out.println(expected);\r
- System.out.println();\r
- String result = Symm.base64.encode(toEncode);\r
- System.out.println(result);\r
- assertEquals(expected,result);\r
- \r
- ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
- Symm.base64.decode(new ByteArrayInputStream(result.getBytes()), baos);\r
- result = baos.toString(Config.UTF_8);\r
- System.out.println(result);\r
- assertEquals(toEncode,result);\r
- \r
- }\r
- \r
- @Test\r
- public void test1() throws Exception {\r
- // Test with different Padding\r
- encode("leas", "bGVhcw==");\r
- encode("leasu", "bGVhc3U=");\r
- encode("leasur", "bGVhc3Vy");\r
- encode("leasure", "bGVhc3VyZQ==");\r
- encode("leasure.","bGVhc3VyZS4=");\r
-\r
- // Test with line ends\r
- encode(encoding, expected);\r
- \r
- int ITER = 2000;\r
- System.out.println("Priming by Encoding Base64 " + ITER + " times");\r
- long start = System.nanoTime();\r
- for(int i=0;i<ITER;++i) {\r
- Symm.base64.encode(encoding);\r
- }\r
- Float ms = (System.nanoTime()-start)/1000000F;\r
- System.out.println("Total: " + ms + "ms");\r
- System.out.println("Avg: " + ms/ITER + "ms");\r
- \r
- System.out.println("Priming by Decoding Base64 " + ITER + " times");\r
- start = System.nanoTime();\r
- for(int i=0;i<ITER;++i) {\r
- Symm.base64.decode(expected);\r
- }\r
- ms = (System.nanoTime()-start)/1000000F;\r
- System.out.println("Total: " + ms + "ms");\r
- System.out.println("Avg: " + ms/ITER + "ms");\r
-\r
- \r
- ITER=30000;\r
- System.out.println("Encoding Base64 " + ITER + " times");\r
- start = System.nanoTime();\r
- for(int i=0;i<ITER;++i) {\r
- Symm.base64.encode(encoding);\r
- }\r
- ms = (System.nanoTime()-start)/1000000F;\r
- System.out.println("Total: " + ms + "ms");\r
- System.out.println("Avg: " + ms/ITER + "ms");\r
- \r
- System.out.println("Decoding Base64 " + ITER + " times");\r
- start = System.nanoTime();\r
- for(int i=0;i<ITER;++i) {\r
- Symm.base64.decode(expected);\r
- }\r
- ms = (System.nanoTime()-start)/1000000F;\r
- System.out.println("Total: " + ms + "ms");\r
- System.out.println("Avg: " + ms/ITER + "ms");\r
- }\r
- \r
- @Test\r
- public void symmetric1() throws IOException {\r
- System.out.println("Validating Generated Key mechanisms works");\r
- String symmetric = new String(Symm.base64.keygen());\r
- System.out.println(symmetric);\r
- Symm bsym = Symm.obtain(symmetric);\r
- String result = bsym.encode(encoding);\r
- System.out.println("\nResult:");\r
- System.out.println(result);\r
- assertEquals(encoding, bsym.decode(result));\r
- \r
- int ITER = 20000;\r
- System.out.println("Generating keys " + ITER + " times");\r
- long start = System.nanoTime();\r
- for(int i=0;i<ITER;++i) {\r
- Symm.base64.keygen();\r
- }\r
- Float ms = (System.nanoTime()-start)/1000000F;\r
- System.out.println("Total: " + ms + "ms");\r
- System.out.println("Avg: " + ms/ITER + "ms");\r
-\r
- char[] manipulate = symmetric.toCharArray();\r
- int spot = new SecureRandom().nextInt(manipulate.length);\r
- manipulate[spot]|=0xFF;\r
- String newsymmetric = new String(manipulate);\r
- assertNotSame(newsymmetric, symmetric);\r
- try {\r
- bsym = Symm.obtain(newsymmetric);\r
- result = bsym.decode(result);\r
- assertEquals(encoding, result);\r
- } catch (IOException e) {\r
- // this is what we want to see if key wrong\r
- System.out.println(e.getMessage() + " (as expected)");\r
- }\r
- }\r
-\r
- private void encode1(String toEncode, String expected) throws IOException {\r
- System.out.println("-------------------------------------------------");\r
- System.out.println(toEncode);\r
- System.out.println();\r
- System.out.println(expected);\r
- System.out.println();\r
- String result = Symm.base64.encode(toEncode);\r
- System.out.println(result);\r
- assertEquals(expected,result);\r
- \r
- ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
- Symm.base64.decode(new ByteArrayInputStream(result.getBytes()), baos);\r
- result = baos.toString(Config.UTF_8);\r
- System.out.println(result);\r
- assertEquals(toEncode,result);\r
- \r
- }\r
- \r
- @Test\r
- public void test2() throws Exception {\r
- // Test with different Padding\r
- encode("leas", "bGVhcw==");\r
- encode("leasu", "bGVhc3U=");\r
- encode("leasur", "bGVhc3Vy");\r
- encode("leasure", "bGVhc3VyZQ==");\r
- encode("leasure.","bGVhc3VyZS4=");\r
-\r
- // Test with line ends\r
- encode(encoding, expected);\r
- \r
- int ITER = 2000;\r
- System.out.println("Priming by Encoding Base64 " + ITER + " times");\r
- long start = System.nanoTime();\r
- for(int i=0;i<ITER;++i) {\r
- Symm.base64.encode(encoding);\r
- }\r
- Float ms = (System.nanoTime()-start)/1000000F;\r
- System.out.println("Total: " + ms + "ms");\r
- System.out.println("Avg: " + ms/ITER + "ms");\r
- \r
- System.out.println("Priming by Decoding Base64 " + ITER + " times");\r
- start = System.nanoTime();\r
- for(int i=0;i<ITER;++i) {\r
- Symm.base64.decode(expected);\r
- }\r
- ms = (System.nanoTime()-start)/1000000F;\r
- System.out.println("Total: " + ms + "ms");\r
- System.out.println("Avg: " + ms/ITER + "ms");\r
-\r
- \r
- ITER=30000;\r
- System.out.println("Encoding Base64 " + ITER + " times");\r
- start = System.nanoTime();\r
- for(int i=0;i<ITER;++i) {\r
- Symm.base64.encode(encoding);\r
- }\r
- ms = (System.nanoTime()-start)/1000000F;\r
- System.out.println("Total: " + ms + "ms");\r
- System.out.println("Avg: " + ms/ITER + "ms");\r
- \r
- System.out.println("Decoding Base64 " + ITER + " times");\r
- start = System.nanoTime();\r
- for(int i=0;i<ITER;++i) {\r
- Symm.base64.decode(expected);\r
- }\r
- ms = (System.nanoTime()-start)/1000000F;\r
- System.out.println("Total: " + ms + "ms");\r
- System.out.println("Avg: " + ms/ITER + "ms");\r
- }\r
- \r
- @Test\r
- public void symmetric2() throws IOException {\r
- System.out.println("Validating Generated Key mechanisms works");\r
- String symmetric = new String(Symm.base64.keygen());\r
- System.out.println(symmetric);\r
- Symm bsym = Symm.obtain(symmetric);\r
- String result = bsym.encode(encoding);\r
- System.out.println("\nResult:");\r
- System.out.println(result);\r
- assertEquals(encoding, bsym.decode(result));\r
- \r
- int ITER = 20000;\r
- System.out.println("Generating keys " + ITER + " times");\r
- long start = System.nanoTime();\r
- for(int i=0;i<ITER;++i) {\r
- Symm.base64.keygen();\r
- }\r
- Float ms = (System.nanoTime()-start)/1000000F;\r
- System.out.println("Total: " + ms + "ms");\r
- System.out.println("Avg: " + ms/ITER + "ms");\r
-\r
- char[] manipulate = symmetric.toCharArray();\r
- int spot = new SecureRandom().nextInt(manipulate.length);\r
- manipulate[spot]|=0xFF;\r
- String newsymmetric = new String(manipulate);\r
- assertNotSame(newsymmetric, symmetric);\r
- try {\r
- bsym = Symm.obtain(newsymmetric);\r
- result = bsym.decode(result);\r
- assertEquals(encoding, result);\r
- } catch (IOException e) {\r
- // this is what we want to see if key wrong\r
- System.out.println(e.getMessage() + " (as expected)");\r
- }\r
- }\r
-\r
- private void encode2(String toEncode, String expected) throws IOException {\r
- System.out.println("-------------------------------------------------");\r
- System.out.println(toEncode);\r
- System.out.println();\r
- System.out.println(expected);\r
- System.out.println();\r
- String result = Symm.base64.encode(toEncode);\r
- System.out.println(result);\r
- assertEquals(expected,result);\r
- \r
- ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
- Symm.base64.decode(new ByteArrayInputStream(result.getBytes()), baos);\r
- result = baos.toString(Config.UTF_8);\r
- System.out.println(result);\r
- assertEquals(toEncode,result);\r
- \r
- }\r
-}\r