AT&T 2.0.19 Code drop, stage 2
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / JU_Symm.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * * ===========================================================================
7  * * Licensed under the Apache License, Version 2.0 (the "License");
8  * * you may not use this file except in compliance with the License.
9  * * You may obtain a copy of the License at
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
13  *  * Unless required by applicable law or agreed to in writing, software
14  * * distributed under the License is distributed on an "AS IS" BASIS,
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * * See the License for the specific language governing permissions and
17  * * limitations under the License.
18  * * ============LICENSE_END====================================================
19  * *
20  * *
21  ******************************************************************************/
22 package org.onap.aaf.cadi.test;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.util.Arrays;
27
28 import java.lang.reflect.*;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.aaf.cadi.CadiException;
32 import org.onap.aaf.cadi.PropAccess;
33 import org.onap.aaf.cadi.Symm;
34
35 import static org.hamcrest.CoreMatchers.*;
36 import static org.junit.Assert.*;
37 import static org.mockito.Mockito.*;
38
39 public class JU_Symm {
40         private Symm defaultSymm;
41
42         @Before
43         public void setup() throws Exception {
44         defaultSymm = new Symm(
45                         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray()
46                         ,76, "Use default!" ,true);
47         }
48
49         @Test
50         public void constructorTest() throws Exception {
51                 Symm myCustomSymm = new Symm(
52                         "ACEGIKMOQSUWYacegikmoqsuwy02468+/".toCharArray(), 76, "Default", true);
53                 Field convert_field = Symm.class.getDeclaredField("convert");
54                 convert_field.setAccessible(true);
55
56                 Class<?> Unordered_class = Class.forName("org.onap.aaf.cadi.Symm$Unordered");
57                 assertThat(convert_field.get(myCustomSymm), instanceOf(Unordered_class));
58         }
59
60         @SuppressWarnings("unused")
61         @Test
62         public void copyTest() throws Exception {
63                 Symm copy = Symm.base64.copy(76);
64         }
65
66         @SuppressWarnings("deprecation")
67         @Test
68         public void deprecatedTest() {
69                 assertEquals(Symm.base64(), Symm.base64);
70                 assertEquals(Symm.base64noSplit(), Symm.base64noSplit);
71                 assertEquals(Symm.base64url(), Symm.base64url);
72                 assertEquals(Symm.baseCrypt(), Symm.encrypt);
73         }
74
75         @Test
76         public void encodeDecodeStringTest() throws Exception {
77                 String orig = "hello";
78                 String b64encrypted = Symm.base64.encode(orig);
79                 assertEquals(Symm.base64.decode(b64encrypted), orig);
80
81                 String defaultEnrypted = defaultSymm.encode(orig);
82                 assertEquals(defaultSymm.decode(defaultEnrypted), orig);
83         }
84
85         @Test
86         public void encodeDecodeByteArrayTest() throws Exception {
87                 String orig = "hello";
88                 byte[] b64encrypted = Symm.base64.encode(orig.getBytes());
89                 assertEquals(new String(Symm.base64.decode(b64encrypted)), orig);
90
91                 byte[] empty = null;
92                 assertTrue(Arrays.equals(Symm.base64.encode(empty), new byte[0]));
93         }
94
95         @Test
96         public void encodeDecodeStringToStreamTest() throws Exception {
97                 String orig = "I'm a password, really";
98                 String b64encrypted;
99                 String output;
100                 
101                 ByteArrayOutputStream baosEncrypt = new ByteArrayOutputStream();
102                 Symm.base64.encode(orig, baosEncrypt);
103                 b64encrypted = new String(baosEncrypt.toByteArray());
104
105                 ByteArrayOutputStream baosDecrypt = new ByteArrayOutputStream();
106                 Symm.base64.decode(b64encrypted, baosDecrypt);
107                 output = new String(baosDecrypt.toByteArray());
108
109                 assertEquals(orig, output);
110         }
111
112         @Test
113         public void encryptDecryptStreamWithPrefixTest() throws Exception {
114                 String orig = "I'm a password, really";
115                 byte[] b64encrypted;
116                 String output;
117
118                 byte[] prefix = "enc:".getBytes();
119                 
120                 ByteArrayInputStream baisEncrypt = new ByteArrayInputStream(orig.getBytes());
121                 ByteArrayOutputStream baosEncrypt = new ByteArrayOutputStream();
122                 Symm.base64.encode(baisEncrypt, baosEncrypt, prefix);
123
124                 b64encrypted = baosEncrypt.toByteArray();
125
126                 ByteArrayInputStream baisDecrypt = new ByteArrayInputStream(b64encrypted);
127                 ByteArrayOutputStream baosDecrypt = new ByteArrayOutputStream();
128                 Symm.base64.decode(baisDecrypt, baosDecrypt, prefix.length);
129
130                 output = new String(baosDecrypt.toByteArray());
131                 assertEquals(orig, output);
132         }
133
134         @Test
135         public void randomGenTest() {
136                 // Ian - There really isn't a great way to test for randomness...
137                 String prev = null;
138                 for (int i = 0; i < 10; i++) {
139                         String current = Symm.randomGen(100);
140                         if (current.equals(prev)) {
141                                 fail("I don't know how, but you generated the exact same random string twice in a row");
142                         }
143                         prev = current;
144                 }
145                 assertTrue(true);
146         }
147
148         @Test
149         public void obtainTest() throws Exception {
150                 Symm symm = Symm.base64.obtain();
151
152                 String orig ="Another Password, please";
153                 String encrypted = symm.enpass(orig);
154                 String decrypted = symm.depass(encrypted);
155                 assertEquals(orig, decrypted);
156         }
157
158         @Test
159         public void InputStreamObtainTest() throws Exception {
160                 byte[] keygen = Symm.keygen();
161
162                 Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));
163
164                 String orig ="Another Password, please";
165                 String encrypted = symm.enpass(orig);
166                 String decrypted = symm.depass(encrypted);
167                 assertEquals(orig, decrypted);
168         }
169
170         @Test
171         public void StringObtainTest() throws Exception {
172                 byte[] keygen = Symm.keygen();
173
174                 Symm symm = Symm.obtain(new String(keygen));
175
176                 String orig ="Another Password, please";
177                 String encrypted = symm.enpass(orig);
178                 String decrypted = symm.depass(encrypted);
179                 assertEquals(orig, decrypted);
180         }
181
182         @Test
183         public void AccessObtainTest() throws Exception {
184                 PropAccess pa = new PropAccess("cadi_keyfile=test/keyfile");
185                 Symm symm = Symm.obtain(pa);
186                 String orig ="Another Password, please";
187                 String encrypted = symm.enpass(orig);
188                 String decrypted = symm.depass(encrypted);
189                 assertEquals(orig, decrypted);
190
191                 try {
192                         PropAccess badPa = mock(PropAccess.class);
193                         when(badPa.getProperty("cadi_keyfile", null)).thenReturn("not_a_real_file.txt");
194                         symm = Symm.obtain(badPa);
195                         fail("Should have thrown an exception");
196                 } catch (CadiException e) {
197                         assertTrue(e.getMessage().contains("ERROR: "));
198                         assertTrue(e.getMessage().contains("not_a_real_file.txt"));
199                         assertTrue(e.getMessage().contains(" does not exist!"));
200                 }
201         }
202         
203 }