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