summaryrefslogtreecommitdiffstats
path: root/core/src/test/java/org/onap/aaf/cadi/test
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/test/java/org/onap/aaf/cadi/test')
-rw-r--r--core/src/test/java/org/onap/aaf/cadi/test/JU_Base64.java212
-rw-r--r--core/src/test/java/org/onap/aaf/cadi/test/JU_Hash.java55
2 files changed, 267 insertions, 0 deletions
diff --git a/core/src/test/java/org/onap/aaf/cadi/test/JU_Base64.java b/core/src/test/java/org/onap/aaf/cadi/test/JU_Base64.java
index 4d49c92..3596619 100644
--- a/core/src/test/java/org/onap/aaf/cadi/test/JU_Base64.java
+++ b/core/src/test/java/org/onap/aaf/cadi/test/JU_Base64.java
@@ -153,4 +153,216 @@ public class JU_Base64 {
assertEquals(toEncode,result);
}
+
+ @Test
+ public void test1() throws Exception {
+ // Test with different Padding
+ encode("leas", "bGVhcw==");
+ encode("leasu", "bGVhc3U=");
+ encode("leasur", "bGVhc3Vy");
+ encode("leasure", "bGVhc3VyZQ==");
+ encode("leasure.","bGVhc3VyZS4=");
+
+ // Test with line ends
+ encode(encoding, expected);
+
+ int ITER = 2000;
+ System.out.println("Priming by Encoding Base64 " + ITER + " times");
+ long start = System.nanoTime();
+ for(int i=0;i<ITER;++i) {
+ Symm.base64.encode(encoding);
+ }
+ Float ms = (System.nanoTime()-start)/1000000F;
+ System.out.println("Total: " + ms + "ms");
+ System.out.println("Avg: " + ms/ITER + "ms");
+
+ System.out.println("Priming by Decoding Base64 " + ITER + " times");
+ start = System.nanoTime();
+ for(int i=0;i<ITER;++i) {
+ Symm.base64.decode(expected);
+ }
+ ms = (System.nanoTime()-start)/1000000F;
+ System.out.println("Total: " + ms + "ms");
+ System.out.println("Avg: " + ms/ITER + "ms");
+
+
+ ITER=30000;
+ System.out.println("Encoding Base64 " + ITER + " times");
+ start = System.nanoTime();
+ for(int i=0;i<ITER;++i) {
+ Symm.base64.encode(encoding);
+ }
+ ms = (System.nanoTime()-start)/1000000F;
+ System.out.println("Total: " + ms + "ms");
+ System.out.println("Avg: " + ms/ITER + "ms");
+
+ System.out.println("Decoding Base64 " + ITER + " times");
+ start = System.nanoTime();
+ for(int i=0;i<ITER;++i) {
+ Symm.base64.decode(expected);
+ }
+ ms = (System.nanoTime()-start)/1000000F;
+ System.out.println("Total: " + ms + "ms");
+ System.out.println("Avg: " + ms/ITER + "ms");
+ }
+
+ @Test
+ public void symmetric1() throws IOException {
+ System.out.println("Validating Generated Key mechanisms works");
+ String symmetric = new String(Symm.base64.keygen());
+ System.out.println(symmetric);
+ Symm bsym = Symm.obtain(symmetric);
+ String result = bsym.encode(encoding);
+ System.out.println("\nResult:");
+ System.out.println(result);
+ assertEquals(encoding, bsym.decode(result));
+
+ int ITER = 20000;
+ System.out.println("Generating keys " + ITER + " times");
+ long start = System.nanoTime();
+ for(int i=0;i<ITER;++i) {
+ Symm.base64.keygen();
+ }
+ Float ms = (System.nanoTime()-start)/1000000F;
+ System.out.println("Total: " + ms + "ms");
+ System.out.println("Avg: " + ms/ITER + "ms");
+
+ char[] manipulate = symmetric.toCharArray();
+ int spot = new SecureRandom().nextInt(manipulate.length);
+ manipulate[spot]|=0xFF;
+ String newsymmetric = new String(manipulate);
+ assertNotSame(newsymmetric, symmetric);
+ try {
+ bsym = Symm.obtain(newsymmetric);
+ result = bsym.decode(result);
+ assertEquals(encoding, result);
+ } catch (IOException e) {
+ // this is what we want to see if key wrong
+ System.out.println(e.getMessage() + " (as expected)");
+ }
+ }
+
+ private void encode1(String toEncode, String expected) throws IOException {
+ System.out.println("-------------------------------------------------");
+ System.out.println(toEncode);
+ System.out.println();
+ System.out.println(expected);
+ System.out.println();
+ String result = Symm.base64.encode(toEncode);
+ System.out.println(result);
+ assertEquals(expected,result);
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ Symm.base64.decode(new ByteArrayInputStream(result.getBytes()), baos);
+ result = baos.toString(Config.UTF_8);
+ System.out.println(result);
+ assertEquals(toEncode,result);
+
+ }
+
+ @Test
+ public void test2() throws Exception {
+ // Test with different Padding
+ encode("leas", "bGVhcw==");
+ encode("leasu", "bGVhc3U=");
+ encode("leasur", "bGVhc3Vy");
+ encode("leasure", "bGVhc3VyZQ==");
+ encode("leasure.","bGVhc3VyZS4=");
+
+ // Test with line ends
+ encode(encoding, expected);
+
+ int ITER = 2000;
+ System.out.println("Priming by Encoding Base64 " + ITER + " times");
+ long start = System.nanoTime();
+ for(int i=0;i<ITER;++i) {
+ Symm.base64.encode(encoding);
+ }
+ Float ms = (System.nanoTime()-start)/1000000F;
+ System.out.println("Total: " + ms + "ms");
+ System.out.println("Avg: " + ms/ITER + "ms");
+
+ System.out.println("Priming by Decoding Base64 " + ITER + " times");
+ start = System.nanoTime();
+ for(int i=0;i<ITER;++i) {
+ Symm.base64.decode(expected);
+ }
+ ms = (System.nanoTime()-start)/1000000F;
+ System.out.println("Total: " + ms + "ms");
+ System.out.println("Avg: " + ms/ITER + "ms");
+
+
+ ITER=30000;
+ System.out.println("Encoding Base64 " + ITER + " times");
+ start = System.nanoTime();
+ for(int i=0;i<ITER;++i) {
+ Symm.base64.encode(encoding);
+ }
+ ms = (System.nanoTime()-start)/1000000F;
+ System.out.println("Total: " + ms + "ms");
+ System.out.println("Avg: " + ms/ITER + "ms");
+
+ System.out.println("Decoding Base64 " + ITER + " times");
+ start = System.nanoTime();
+ for(int i=0;i<ITER;++i) {
+ Symm.base64.decode(expected);
+ }
+ ms = (System.nanoTime()-start)/1000000F;
+ System.out.println("Total: " + ms + "ms");
+ System.out.println("Avg: " + ms/ITER + "ms");
+ }
+
+ @Test
+ public void symmetric2() throws IOException {
+ System.out.println("Validating Generated Key mechanisms works");
+ String symmetric = new String(Symm.base64.keygen());
+ System.out.println(symmetric);
+ Symm bsym = Symm.obtain(symmetric);
+ String result = bsym.encode(encoding);
+ System.out.println("\nResult:");
+ System.out.println(result);
+ assertEquals(encoding, bsym.decode(result));
+
+ int ITER = 20000;
+ System.out.println("Generating keys " + ITER + " times");
+ long start = System.nanoTime();
+ for(int i=0;i<ITER;++i) {
+ Symm.base64.keygen();
+ }
+ Float ms = (System.nanoTime()-start)/1000000F;
+ System.out.println("Total: " + ms + "ms");
+ System.out.println("Avg: " + ms/ITER + "ms");
+
+ char[] manipulate = symmetric.toCharArray();
+ int spot = new SecureRandom().nextInt(manipulate.length);
+ manipulate[spot]|=0xFF;
+ String newsymmetric = new String(manipulate);
+ assertNotSame(newsymmetric, symmetric);
+ try {
+ bsym = Symm.obtain(newsymmetric);
+ result = bsym.decode(result);
+ assertEquals(encoding, result);
+ } catch (IOException e) {
+ // this is what we want to see if key wrong
+ System.out.println(e.getMessage() + " (as expected)");
+ }
+ }
+
+ private void encode2(String toEncode, String expected) throws IOException {
+ System.out.println("-------------------------------------------------");
+ System.out.println(toEncode);
+ System.out.println();
+ System.out.println(expected);
+ System.out.println();
+ String result = Symm.base64.encode(toEncode);
+ System.out.println(result);
+ assertEquals(expected,result);
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ Symm.base64.decode(new ByteArrayInputStream(result.getBytes()), baos);
+ result = baos.toString(Config.UTF_8);
+ System.out.println(result);
+ assertEquals(toEncode,result);
+
+ }
}
diff --git a/core/src/test/java/org/onap/aaf/cadi/test/JU_Hash.java b/core/src/test/java/org/onap/aaf/cadi/test/JU_Hash.java
index 92dd297..f79564d 100644
--- a/core/src/test/java/org/onap/aaf/cadi/test/JU_Hash.java
+++ b/core/src/test/java/org/onap/aaf/cadi/test/JU_Hash.java
@@ -57,4 +57,59 @@ public class JU_Hash {
}
}
+ @Test
+ public void test1() throws CadiException {
+ String init = "m8234337@csp.att.com:kumquat8rie@#Tomatos3";
+ String hashed = Hash.toHex(init.getBytes());
+ System.out.println(hashed);
+ byte[] ba = Hash.fromHex(hashed);
+ String recon = new String(ba);
+ System.out.println(recon);
+ Assert.assertEquals(init, recon);
+
+ init =hashed.substring(1);
+ try {
+ hashed = Hash.fromHex(init).toString();
+ Assert.fail("Should have thrown Exception");
+ } catch (CadiException e) {
+
+ }
+
+ init = hashed.replace('1', '~');
+ try {
+ hashed = Hash.fromHex(init).toString();
+ Assert.fail("Should have thrown Exception");
+ } catch (CadiException e) {
+
+ }
+ }
+
+
+ @Test
+ public void test2() throws CadiException {
+ String init = "m823475@csp.att.com:kumquat8rie@#Tomatos3";
+ String hashed = Hash.toHex(init.getBytes());
+ System.out.println(hashed);
+ byte[] ba = Hash.fromHex(hashed);
+ String recon = new String(ba);
+ System.out.println(recon);
+ Assert.assertEquals(init, recon);
+
+ init =hashed.substring(1);
+ try {
+ hashed = Hash.fromHex(init).toString();
+ Assert.fail("Should have thrown Exception");
+ } catch (CadiException e) {
+
+ }
+
+ init = hashed.replace('1', '~');
+ try {
+ hashed = Hash.fromHex(init).toString();
+ Assert.fail("Should have thrown Exception");
+ } catch (CadiException e) {
+
+ }
+ }
+
}