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