1 package be
.nikiroo
.utils
.test_code
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.ByteArrayOutputStream
;
5 import java
.io
.InputStream
;
7 import be
.nikiroo
.utils
.CryptUtils
;
8 import be
.nikiroo
.utils
.IOUtils
;
9 import be
.nikiroo
.utils
.test
.TestCase
;
10 import be
.nikiroo
.utils
.test
.TestLauncher
;
12 class CryptUtilsTest
extends TestLauncher
{
14 private CryptUtils crypt
;
16 public CryptUtilsTest(String
[] args
) {
17 super("CryptUtils test", args
);
19 String longKey
= "some long string with more than 128 bits (=32 bytes) of data";
21 addSeries(new CryptUtilsTest(args
, "Manual input wuth NULL key", null,
23 addSeries(new CryptUtilsTest(args
, "Streams with NULL key", null, true));
25 addSeries(new CryptUtilsTest(args
, "Manual input with emptykey", "", 1));
26 addSeries(new CryptUtilsTest(args
, "Streams with empty key", "", true));
28 addSeries(new CryptUtilsTest(args
, "Manual input with long key",
30 addSeries(new CryptUtilsTest(args
, "Streams with long key", longKey
,
35 protected void addTest(final TestCase test
) {
36 super.addTest(new TestCase(test
.getName()) {
38 public void test() throws Exception
{
43 public void setUp() throws Exception
{
44 crypt
= new CryptUtils(key
);
49 public void tearDown() throws Exception
{
56 private CryptUtilsTest(String
[] args
, String title
, String key
,
57 @SuppressWarnings("unused") int dummy
) {
61 final String longData
= "Le premier jour, Le Grand Barbu dans le cloud fit la lumière, et il vit que c'était bien. Ou quelque chose comme ça. Je préfère la Science-Fiction en général, je trouve ça plus sain :/";
63 addTest(new TestCase("Short") {
65 public void test() throws Exception
{
67 byte[] encrypted
= crypt
.encrypt(orig
);
68 String decrypted
= crypt
.decrypts(encrypted
);
70 assertEquals(orig
, decrypted
);
74 addTest(new TestCase("Short, base64") {
76 public void test() throws Exception
{
78 String encrypted
= crypt
.encrypt64(orig
);
79 String decrypted
= crypt
.decrypt64s(encrypted
);
81 assertEquals(orig
, decrypted
);
85 addTest(new TestCase("Empty") {
87 public void test() throws Exception
{
89 byte[] encrypted
= crypt
.encrypt(orig
);
90 String decrypted
= crypt
.decrypts(encrypted
);
92 assertEquals(orig
, decrypted
);
96 addTest(new TestCase("Empty, base64") {
98 public void test() throws Exception
{
100 String encrypted
= crypt
.encrypt64(orig
);
101 String decrypted
= crypt
.decrypt64s(encrypted
);
103 assertEquals(orig
, decrypted
);
107 addTest(new TestCase("Long") {
109 public void test() throws Exception
{
110 String orig
= longData
;
111 byte[] encrypted
= crypt
.encrypt(orig
);
112 String decrypted
= crypt
.decrypts(encrypted
);
114 assertEquals(orig
, decrypted
);
118 addTest(new TestCase("Long, base64") {
120 public void test() throws Exception
{
121 String orig
= longData
;
122 String encrypted
= crypt
.encrypt64(orig
);
123 String decrypted
= crypt
.decrypt64s(encrypted
);
125 assertEquals(orig
, decrypted
);
130 private CryptUtilsTest(String
[] args
, String title
, String key
,
131 @SuppressWarnings("unused") boolean dummy
) {
135 addTest(new TestCase("Simple test") {
137 public void test() throws Exception
{
138 InputStream in
= new ByteArrayInputStream(new byte[] { 42, 127,
141 ByteArrayOutputStream out
= new ByteArrayOutputStream();
142 IOUtils
.write(in
, out
);
143 byte[] result
= out
.toByteArray();
146 "We wrote 3 bytes, we expected 3 bytes back but got: "
147 + result
.length
, result
.length
, result
.length
);
149 assertEquals(42, result
[0]);
150 assertEquals(127, result
[1]);
151 assertEquals(12, result
[2]);