1 /*******************************************************************************
2 * ============LICENSE_START====================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 ******************************************************************************/
23 package org.onap.aaf.cadi.test;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.junit.Assert.*;
27 import static org.mockito.Mockito.*;
28 import java.lang.reflect.*;
31 import java.io.ByteArrayInputStream;
32 import java.io.ByteArrayOutputStream;
33 import java.io.PrintStream;
34 import java.util.Arrays;
36 import org.onap.aaf.cadi.CadiException;
37 import org.onap.aaf.cadi.PropAccess;
38 import org.onap.aaf.cadi.Symm;
40 public class JU_Symm {
41 private Symm defaultSymm;
43 private ByteArrayOutputStream outStream;
46 public void setup() throws Exception {
47 defaultSymm = new Symm(
48 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray()
49 ,76, "Use default!" ,true, "Junit 1");
50 outStream = new ByteArrayOutputStream();
51 System.setOut(new PrintStream(outStream));
55 public void tearDown() {
56 System.setOut(System.out);
60 public void constructorTest() throws Exception {
61 Symm myCustomSymm = new Symm(
62 "ACEGIKMOQSUWYacegikmoqsuwy02468+/".toCharArray(), 76, "Default", true, "Junit 2");
63 Field convert_field = Symm.class.getDeclaredField("convert");
64 convert_field.setAccessible(true);
66 Class<?> Unordered_class = Class.forName("org.onap.aaf.cadi.Symm$Unordered");
67 assertThat(convert_field.get(myCustomSymm), instanceOf(Unordered_class));
70 @SuppressWarnings("unused")
72 public void copyTest() throws Exception {
73 Symm copy = Symm.base64.copy(76);
76 @SuppressWarnings("deprecation")
78 public void deprecatedTest() {
79 assertEquals(Symm.base64(), Symm.base64);
80 assertEquals(Symm.base64noSplit(), Symm.base64noSplit);
81 assertEquals(Symm.base64url(), Symm.base64url);
82 assertEquals(Symm.baseCrypt(), Symm.encrypt);
86 public void encodeDecodeStringTest() throws Exception {
87 String orig = "hello";
88 String b64encrypted = Symm.base64.encode(orig);
89 assertEquals(Symm.base64.decode(b64encrypted), orig);
91 String defaultEnrypted = defaultSymm.encode(orig);
92 assertEquals(defaultSymm.decode(defaultEnrypted), orig);
96 public void encodeDecodeByteArrayTest() throws Exception {
97 String orig = "hello";
98 byte[] b64encrypted = Symm.base64.encode(orig.getBytes());
99 assertEquals(new String(Symm.base64.decode(b64encrypted)), orig);
102 assertTrue(Arrays.equals(Symm.base64.encode(empty), new byte[0]));
106 public void encodeDecodeStringToStreamTest() throws Exception {
107 String orig = "I'm a password, really";
111 ByteArrayOutputStream baosEncrypt = new ByteArrayOutputStream();
112 Symm.base64.encode(orig, baosEncrypt);
113 b64encrypted = new String(baosEncrypt.toByteArray());
115 ByteArrayOutputStream baosDecrypt = new ByteArrayOutputStream();
116 Symm.base64.decode(b64encrypted, baosDecrypt);
117 output = new String(baosDecrypt.toByteArray());
119 assertEquals(orig, output);
123 public void encryptDecryptStreamWithPrefixTest() throws Exception {
124 String orig = "I'm a password, really";
128 byte[] prefix = "enc:".getBytes();
130 ByteArrayInputStream baisEncrypt = new ByteArrayInputStream(orig.getBytes());
131 ByteArrayOutputStream baosEncrypt = new ByteArrayOutputStream();
132 Symm.base64.encode(baisEncrypt, baosEncrypt, prefix);
134 b64encrypted = baosEncrypt.toByteArray();
136 ByteArrayInputStream baisDecrypt = new ByteArrayInputStream(b64encrypted);
137 ByteArrayOutputStream baosDecrypt = new ByteArrayOutputStream();
138 Symm.base64.decode(baisDecrypt, baosDecrypt, prefix.length);
140 output = new String(baosDecrypt.toByteArray());
141 assertEquals(orig, output);
145 public void randomGenTest() {
146 // Ian - There really isn't a great way to test for randomness...
148 for (int i = 0; i < 10; i++) {
149 String current = Symm.randomGen(100);
150 if (current.equals(prev)) {
151 fail("I don't know how, but you generated the exact same random string twice in a row");
159 public void obtainTest() throws Exception {
160 Symm symm = Symm.base64.obtain();
162 String orig ="Another Password, please";
163 String encrypted = symm.enpass(orig);
164 String decrypted = symm.depass(encrypted);
165 assertEquals(orig, decrypted);
169 public void InputStreamObtainTest() throws Exception {
170 byte[] keygen = Symm.keygen();
172 Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));
174 String orig ="Another Password, please";
175 String encrypted = symm.enpass(orig);
176 String decrypted = symm.depass(encrypted);
177 assertEquals(orig, decrypted);
181 public void StringObtainTest() throws Exception {
182 byte[] keygen = Symm.keygen();
184 Symm symm = Symm.obtain(new String(keygen));
186 String orig ="Another Password, please";
187 String encrypted = symm.enpass(orig);
188 String decrypted = symm.depass(encrypted);
189 assertEquals(orig, decrypted);
193 public void AccessObtainTest() throws Exception {
194 PropAccess pa = new PropAccess("cadi_keyfile=src/test/resources/keyfile");
195 Symm symm = Symm.obtain(pa);
196 String orig ="Another Password, please";
197 String encrypted = symm.enpass(orig);
198 String decrypted = symm.depass(encrypted);
199 assertEquals(orig, decrypted);
202 PropAccess badPa = mock(PropAccess.class);
203 when(badPa.getProperty("cadi_keyfile", null)).thenReturn("not_a_real_file.txt");
204 symm = Symm.obtain(badPa);
205 fail("Should have thrown an exception");
206 } catch (CadiException e) {
207 assertTrue(e.getMessage().contains("ERROR: "));
208 assertTrue(e.getMessage().contains("not_a_real_file.txt"));
209 assertTrue(e.getMessage().contains(" does not exist!"));