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 java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.util.Arrays;
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;
35 import static org.hamcrest.CoreMatchers.*;
36 import static org.junit.Assert.*;
37 import static org.mockito.Mockito.*;
39 public class JU_Symm {
40 private Symm defaultSymm;
43 public void setup() throws Exception {
44 defaultSymm = new Symm(
45 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray()
46 ,76, "Use default!" ,true);
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);
56 Class<?> Unordered_class = Class.forName("org.onap.aaf.cadi.Symm$Unordered");
57 assertThat(convert_field.get(myCustomSymm), instanceOf(Unordered_class));
60 @SuppressWarnings("unused")
62 public void copyTest() throws Exception {
63 Symm copy = Symm.base64.copy(76);
66 @SuppressWarnings("deprecation")
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);
76 public void encodeDecodeStringTest() throws Exception {
77 String orig = "hello";
78 String b64encrypted = Symm.base64.encode(orig);
79 assertEquals(Symm.base64.decode(b64encrypted), orig);
81 String defaultEnrypted = defaultSymm.encode(orig);
82 assertEquals(defaultSymm.decode(defaultEnrypted), orig);
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);
92 assertTrue(Arrays.equals(Symm.base64.encode(empty), new byte[0]));
96 public void encodeDecodeStringToStreamTest() throws Exception {
97 String orig = "I'm a password, really";
101 ByteArrayOutputStream baosEncrypt = new ByteArrayOutputStream();
102 Symm.base64.encode(orig, baosEncrypt);
103 b64encrypted = new String(baosEncrypt.toByteArray());
105 ByteArrayOutputStream baosDecrypt = new ByteArrayOutputStream();
106 Symm.base64.decode(b64encrypted, baosDecrypt);
107 output = new String(baosDecrypt.toByteArray());
109 assertEquals(orig, output);
113 public void encryptDecryptStreamWithPrefixTest() throws Exception {
114 String orig = "I'm a password, really";
118 byte[] prefix = "enc:".getBytes();
120 ByteArrayInputStream baisEncrypt = new ByteArrayInputStream(orig.getBytes());
121 ByteArrayOutputStream baosEncrypt = new ByteArrayOutputStream();
122 Symm.base64.encode(baisEncrypt, baosEncrypt, prefix);
124 b64encrypted = baosEncrypt.toByteArray();
126 ByteArrayInputStream baisDecrypt = new ByteArrayInputStream(b64encrypted);
127 ByteArrayOutputStream baosDecrypt = new ByteArrayOutputStream();
128 Symm.base64.decode(baisDecrypt, baosDecrypt, prefix.length);
130 output = new String(baosDecrypt.toByteArray());
131 assertEquals(orig, output);
135 public void randomGenTest() {
136 // Ian - There really isn't a great way to test for randomness...
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");
149 public void obtainTest() throws Exception {
150 Symm symm = Symm.base64.obtain();
152 String orig ="Another Password, please";
153 String encrypted = symm.enpass(orig);
154 String decrypted = symm.depass(encrypted);
155 assertEquals(orig, decrypted);
159 public void InputStreamObtainTest() throws Exception {
160 byte[] keygen = Symm.keygen();
162 Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));
164 String orig ="Another Password, please";
165 String encrypted = symm.enpass(orig);
166 String decrypted = symm.depass(encrypted);
167 assertEquals(orig, decrypted);
171 public void StringObtainTest() throws Exception {
172 byte[] keygen = Symm.keygen();
174 Symm symm = Symm.obtain(new String(keygen));
176 String orig ="Another Password, please";
177 String encrypted = symm.enpass(orig);
178 String decrypted = symm.depass(encrypted);
179 assertEquals(orig, decrypted);
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);
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!"));