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 ******************************************************************************/
22 package org.onap.aaf.cadi.test;
24 import static org.hamcrest.CoreMatchers.*;
25 import static org.junit.Assert.*;
26 import static org.mockito.Mockito.*;
27 import java.lang.reflect.*;
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.io.PrintStream;
33 import java.util.Arrays;
35 import org.onap.aaf.cadi.CadiException;
36 import org.onap.aaf.cadi.PropAccess;
37 import org.onap.aaf.cadi.Symm;
39 public class JU_Symm {
40 private Symm defaultSymm;
42 private ByteArrayOutputStream outStream;
45 public void setup() throws Exception {
46 defaultSymm = new Symm(
47 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray()
48 ,76, "Use default!" ,true);
49 outStream = new ByteArrayOutputStream();
50 System.setOut(new PrintStream(outStream));
54 public void tearDown() {
55 System.setOut(System.out);
59 public void constructorTest() throws Exception {
60 Symm myCustomSymm = new Symm(
61 "ACEGIKMOQSUWYacegikmoqsuwy02468+/".toCharArray(), 76, "Default", true);
62 Field convert_field = Symm.class.getDeclaredField("convert");
63 convert_field.setAccessible(true);
65 Class<?> Unordered_class = Class.forName("org.onap.aaf.cadi.Symm$Unordered");
66 assertThat(convert_field.get(myCustomSymm), instanceOf(Unordered_class));
69 @SuppressWarnings("unused")
71 public void copyTest() throws Exception {
72 Symm copy = Symm.base64.copy(76);
75 @SuppressWarnings("deprecation")
77 public void deprecatedTest() {
78 assertEquals(Symm.base64(), Symm.base64);
79 assertEquals(Symm.base64noSplit(), Symm.base64noSplit);
80 assertEquals(Symm.base64url(), Symm.base64url);
81 assertEquals(Symm.baseCrypt(), Symm.encrypt);
85 public void encodeDecodeStringTest() throws Exception {
86 String orig = "hello";
87 String b64encrypted = Symm.base64.encode(orig);
88 assertEquals(Symm.base64.decode(b64encrypted), orig);
90 String defaultEnrypted = defaultSymm.encode(orig);
91 assertEquals(defaultSymm.decode(defaultEnrypted), orig);
95 public void encodeDecodeByteArrayTest() throws Exception {
96 String orig = "hello";
97 byte[] b64encrypted = Symm.base64.encode(orig.getBytes());
98 assertEquals(new String(Symm.base64.decode(b64encrypted)), orig);
101 assertTrue(Arrays.equals(Symm.base64.encode(empty), new byte[0]));
105 public void encodeDecodeStringToStreamTest() throws Exception {
106 String orig = "I'm a password, really";
110 ByteArrayOutputStream baosEncrypt = new ByteArrayOutputStream();
111 Symm.base64.encode(orig, baosEncrypt);
112 b64encrypted = new String(baosEncrypt.toByteArray());
114 ByteArrayOutputStream baosDecrypt = new ByteArrayOutputStream();
115 Symm.base64.decode(b64encrypted, baosDecrypt);
116 output = new String(baosDecrypt.toByteArray());
118 assertEquals(orig, output);
122 public void encryptDecryptStreamWithPrefixTest() throws Exception {
123 String orig = "I'm a password, really";
127 byte[] prefix = "enc:".getBytes();
129 ByteArrayInputStream baisEncrypt = new ByteArrayInputStream(orig.getBytes());
130 ByteArrayOutputStream baosEncrypt = new ByteArrayOutputStream();
131 Symm.base64.encode(baisEncrypt, baosEncrypt, prefix);
133 b64encrypted = baosEncrypt.toByteArray();
135 ByteArrayInputStream baisDecrypt = new ByteArrayInputStream(b64encrypted);
136 ByteArrayOutputStream baosDecrypt = new ByteArrayOutputStream();
137 Symm.base64.decode(baisDecrypt, baosDecrypt, prefix.length);
139 output = new String(baosDecrypt.toByteArray());
140 assertEquals(orig, output);
144 public void randomGenTest() {
145 // Ian - There really isn't a great way to test for randomness...
147 for (int i = 0; i < 10; i++) {
148 String current = Symm.randomGen(100);
149 if (current.equals(prev)) {
150 fail("I don't know how, but you generated the exact same random string twice in a row");
158 public void obtainTest() throws Exception {
159 Symm symm = Symm.base64.obtain();
161 String orig ="Another Password, please";
162 String encrypted = symm.enpass(orig);
163 String decrypted = symm.depass(encrypted);
164 assertEquals(orig, decrypted);
168 public void InputStreamObtainTest() throws Exception {
169 byte[] keygen = Symm.keygen();
171 Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));
173 String orig ="Another Password, please";
174 String encrypted = symm.enpass(orig);
175 String decrypted = symm.depass(encrypted);
176 assertEquals(orig, decrypted);
180 public void StringObtainTest() throws Exception {
181 byte[] keygen = Symm.keygen();
183 Symm symm = Symm.obtain(new String(keygen));
185 String orig ="Another Password, please";
186 String encrypted = symm.enpass(orig);
187 String decrypted = symm.depass(encrypted);
188 assertEquals(orig, decrypted);
192 public void AccessObtainTest() throws Exception {
193 PropAccess pa = new PropAccess("cadi_keyfile=src/test/resources/keyfile");
194 Symm symm = Symm.obtain(pa);
195 String orig ="Another Password, please";
196 String encrypted = symm.enpass(orig);
197 String decrypted = symm.depass(encrypted);
198 assertEquals(orig, decrypted);
201 PropAccess badPa = mock(PropAccess.class);
202 when(badPa.getProperty("cadi_keyfile", null)).thenReturn("not_a_real_file.txt");
203 symm = Symm.obtain(badPa);
204 fail("Should have thrown an exception");
205 } catch (CadiException e) {
206 assertTrue(e.getMessage().contains("ERROR: "));
207 assertTrue(e.getMessage().contains("not_a_real_file.txt"));
208 assertTrue(e.getMessage().contains(" does not exist!"));