Skip to content

Commit d87e229

Browse files
committed
[Tests] Add tests for CryptoData and re-organise
1 parent c5fd5fb commit d87e229

File tree

4 files changed

+108
-25
lines changed

4 files changed

+108
-25
lines changed

org.archicontribs.modelrepository.tests/src/org/archicontribs/modelrepository/AllTests.java

+11-13
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,19 @@
66
package org.archicontribs.modelrepository;
77

88

9+
import org.archicontribs.modelrepository.authentication.CryptoDataTests;
910
import org.archicontribs.modelrepository.grafico.ArchiRepositoryTests;
1011
import org.archicontribs.modelrepository.grafico.GraficoUtilsTests;
12+
import org.junit.runner.RunWith;
13+
import org.junit.runners.Suite;
1114

12-
import junit.framework.TestSuite;
15+
@RunWith(Suite.class)
1316

14-
@SuppressWarnings("nls")
15-
public class AllTests {
16-
17-
public static junit.framework.Test suite() {
18-
TestSuite suite = new TestSuite("org.archicontribs.modelrepository");
17+
@Suite.SuiteClasses({
18+
ArchiRepositoryTests.class,
19+
GraficoUtilsTests.class,
20+
CryptoDataTests.class
21+
})
1922

20-
suite.addTest(ArchiRepositoryTests.suite());
21-
suite.addTest(GraficoUtilsTests.suite());
22-
23-
return suite;
24-
}
25-
26-
}
23+
public class AllTests {
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* This program and the accompanying materials
3+
* are made available under the terms of the License
4+
* which accompanies this distribution in the file LICENSE.txt
5+
*/
6+
package org.archicontribs.modelrepository.authentication;
7+
8+
import static org.junit.Assert.assertArrayEquals;
9+
import static org.junit.Assert.assertFalse;
10+
import static org.junit.Assert.assertNull;
11+
import static org.junit.Assert.assertTrue;
12+
13+
import org.junit.Test;
14+
15+
16+
@SuppressWarnings("nls")
17+
public class CryptoDataTests {
18+
19+
char[] secretText = "SecretTextToEncode".toCharArray();;
20+
byte[] secretBytes = CryptoUtils.convertCharsToBytes(secretText); // These would be encrypted with cipher
21+
22+
byte[] salt = CryptoUtils.generateRandomBytes(8);
23+
byte[] iv = CryptoUtils.generateRandomBytes(8);
24+
25+
/**
26+
* No Salt or IV
27+
*/
28+
@Test
29+
public void noSaltIV() {
30+
CryptoData cd = new CryptoData(null, null, secretBytes);
31+
String encoded = cd.getBase64String();
32+
33+
assertFalse(encoded.contains(",")); // no salt sep
34+
assertFalse(encoded.contains(";")); // no iv sep
35+
36+
cd = new CryptoData(encoded);
37+
38+
assertNull(cd.getSalt());
39+
assertNull(cd.getIV());
40+
assertArrayEquals(secretBytes, cd.getEncryptedData());
41+
}
42+
43+
/**
44+
* Just Salt
45+
*/
46+
@Test
47+
public void justSalt() {
48+
CryptoData cd = new CryptoData(salt, null, secretBytes);
49+
String encoded = cd.getBase64String();
50+
51+
assertTrue(encoded.contains(",")); // has salt sep
52+
assertFalse(encoded.contains(";")); // no iv sep
53+
54+
cd = new CryptoData(encoded);
55+
56+
assertArrayEquals(salt, cd.getSalt());
57+
assertNull(cd.getIV());
58+
assertArrayEquals(secretBytes, cd.getEncryptedData());
59+
}
60+
61+
/**
62+
* Just IV
63+
*/
64+
@Test
65+
public void justIV() {
66+
CryptoData cd = new CryptoData(null, iv, secretBytes);
67+
String encoded = cd.getBase64String();
68+
69+
assertFalse(encoded.contains(",")); // no salt sep
70+
assertTrue(encoded.contains(";")); // has iv sep
71+
72+
cd = new CryptoData(encoded);
73+
74+
assertNull(cd.getSalt());
75+
assertArrayEquals(iv, cd.getIV());
76+
assertArrayEquals(secretBytes, cd.getEncryptedData());
77+
}
78+
79+
/**
80+
* Salt and IV
81+
*/
82+
@Test
83+
public void saltIV() {
84+
CryptoData cd = new CryptoData(salt, iv, secretBytes);
85+
String encoded = cd.getBase64String();
86+
87+
assertTrue(encoded.contains(",")); // has salt sep
88+
assertTrue(encoded.contains(";")); // has iv sep
89+
90+
cd = new CryptoData(encoded);
91+
92+
assertArrayEquals(salt, cd.getSalt());
93+
assertArrayEquals(iv, cd.getIV());
94+
assertArrayEquals(secretBytes, cd.getEncryptedData());
95+
}
96+
97+
}

org.archicontribs.modelrepository.tests/src/org/archicontribs/modelrepository/grafico/ArchiRepositoryTests.java

-6
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,10 @@
2929
import com.archimatetool.model.IArchimateFactory;
3030
import com.archimatetool.model.IArchimateModel;
3131

32-
import junit.framework.JUnit4TestAdapter;
33-
3432

3533
@SuppressWarnings("nls")
3634
public class ArchiRepositoryTests {
3735

38-
public static junit.framework.Test suite() {
39-
return new JUnit4TestAdapter(ArchiRepositoryTests.class);
40-
}
41-
4236
@Before
4337
public void runOnceBeforeEachTest() {
4438
}

org.archicontribs.modelrepository.tests/src/org/archicontribs/modelrepository/grafico/GraficoUtilsTests.java

-6
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,10 @@
2222
import com.archimatetool.model.IArchimateFactory;
2323
import com.archimatetool.model.IArchimateModel;
2424

25-
import junit.framework.JUnit4TestAdapter;
26-
2725

2826
@SuppressWarnings("nls")
2927
public class GraficoUtilsTests {
3028

31-
public static junit.framework.Test suite() {
32-
return new JUnit4TestAdapter(GraficoUtilsTests.class);
33-
}
34-
3529
@Before
3630
public void runOnceBeforeEachTest() {
3731
}

0 commit comments

Comments
 (0)