Update AAF Version 1.0.0
[aaf/cadi.git] / core / src / test / java / org / onap / aaf / cadi / test / JU_Passcode.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 org.onap.aaf.cadi.test;\r
24 \r
25 import java.io.ByteArrayOutputStream;\r
26 import java.io.File;\r
27 import java.io.FileInputStream;\r
28 \r
29 import org.junit.Test;\r
30 import org.onap.aaf.cadi.CmdLine;\r
31 import org.onap.aaf.cadi.Symm;\r
32 \r
33 import junit.framework.Assert;\r
34 \r
35 public class JU_Passcode {\r
36         @Test\r
37         public void test() throws Exception {\r
38                 int iterations = 0;\r
39                 long start = System.nanoTime();\r
40                 int gens= 800, en_de=2000;\r
41                 for(int j=0;j<gens;++j) {\r
42                         CmdLine.main(new String[] {"keygen","tempkey"});\r
43                         \r
44                         Symm symm;\r
45                         File fi = new File("tempkey");\r
46                         Assert.assertEquals(2074, fi.length());\r
47                         FileInputStream fis = new FileInputStream(fi);\r
48                         \r
49                         try {\r
50                                 symm = Symm.obtain(fis);\r
51                         } finally {\r
52                                 fis.close();\r
53                         }\r
54                         String samples[] = {"activevos","ThisIsATestPassword","I have spaces","I have 's, /s and &s and _s"};\r
55                         ByteArrayOutputStream baos;\r
56                         for(int i=0;i<en_de;++i) {\r
57                                 String password = samples[i%samples.length];\r
58                                 baos = new ByteArrayOutputStream();\r
59                                 symm.enpass(password, baos);\r
60                                 String pass = baos.toString();\r
61                                 byte[] array = baos.toByteArray();\r
62                                 for(int k=0;k<array.length;++k) {\r
63                                         byte ch = array[k];\r
64 //                              for(int k=0;k<pass.length();++k) {\r
65 //                                      char ch = pass.charAt(k);\r
66                                         if(!(Character.isLetter(ch) || Character.isDigit(ch) || ch=='-' || ch=='_' || ch=='=')) {\r
67                                                 throw new Exception("Yikes, have a bad character..." + ch + '(' + (int)ch + ')');\r
68                                         }\r
69                                 }\r
70                                 baos = new ByteArrayOutputStream();\r
71                                 symm.depass(pass, baos);\r
72                                 Assert.assertEquals(password,baos.toString());\r
73                                 Assert.assertEquals(password,symm.depass(pass));\r
74                                 ++iterations;\r
75                         }\r
76                         symm.enpass("activevos", System.out);\r
77                         System.out.println();\r
78                 }\r
79                 float ms = (System.nanoTime()-start)/1000000F;\r
80                 System.out.println("Ran " + iterations + " Encrypt/Decrypt cycles + " + gens + " keygens");\r
81 \r
82                 System.out.println("Total: " + ms + "ms");\r
83                 System.out.println("Avg:   " + ms/iterations + "ms");\r
84                 System.out.println("Avg Gen + " + en_de + " Encrypt/Decrypt cycles:  " + ms/gens + "ms");\r
85 \r
86 \r
87         }\r
88 \r
89 }\r