ConfigItem: assure label placement if taller pane
[fanfix.git] / src / be / nikiroo / utils / test_code / CryptUtilsTest.java
CommitLineData
f3502fbd
NR
1package be.nikiroo.utils.test_code;
2
6fd1a061
NR
3import java.io.ByteArrayInputStream;
4import java.io.ByteArrayOutputStream;
5import java.io.InputStream;
6
f3502fbd 7import be.nikiroo.utils.CryptUtils;
6fd1a061 8import be.nikiroo.utils.IOUtils;
f3502fbd
NR
9import be.nikiroo.utils.test.TestCase;
10import be.nikiroo.utils.test.TestLauncher;
11
12class CryptUtilsTest extends TestLauncher {
13 private String key;
14 private CryptUtils crypt;
15
16 public CryptUtilsTest(String[] args) {
17 super("CryptUtils test", args);
18
19 String longKey = "some long string with more than 128 bits (=32 bytes) of data";
20
21 addSeries(new CryptUtilsTest(args, "Manual input wuth NULL key", null,
22 1));
23 addSeries(new CryptUtilsTest(args, "Streams with NULL key", null, true));
24
25 addSeries(new CryptUtilsTest(args, "Manual input with emptykey", "", 1));
26 addSeries(new CryptUtilsTest(args, "Streams with empty key", "", true));
27
28 addSeries(new CryptUtilsTest(args, "Manual input with long key",
29 longKey, 1));
30 addSeries(new CryptUtilsTest(args, "Streams with long key", longKey,
31 true));
32 }
33
34 @Override
35 protected void addTest(final TestCase test) {
36 super.addTest(new TestCase(test.getName()) {
37 @Override
38 public void test() throws Exception {
39 test.test();
40 }
41
42 @Override
43 public void setUp() throws Exception {
44 crypt = new CryptUtils(key);
45 test.setUp();
46 }
47
48 @Override
49 public void tearDown() throws Exception {
50 test.tearDown();
51 crypt = null;
52 }
53 });
54 }
55
56 private CryptUtilsTest(String[] args, String title, String key,
57 @SuppressWarnings("unused") int dummy) {
58 super(title, args);
59 this.key = key;
60
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 :/";
62
63 addTest(new TestCase("Short") {
64 @Override
65 public void test() throws Exception {
66 String orig = "data";
67 byte[] encrypted = crypt.encrypt(orig);
68 String decrypted = crypt.decrypts(encrypted);
69
70 assertEquals(orig, decrypted);
71 }
72 });
73
74 addTest(new TestCase("Short, base64") {
75 @Override
76 public void test() throws Exception {
77 String orig = "data";
a6a73de3
NR
78 String encrypted = crypt.encrypt64(orig);
79 String decrypted = crypt.decrypt64s(encrypted);
f3502fbd
NR
80
81 assertEquals(orig, decrypted);
82 }
83 });
84
85 addTest(new TestCase("Empty") {
86 @Override
87 public void test() throws Exception {
88 String orig = "";
89 byte[] encrypted = crypt.encrypt(orig);
90 String decrypted = crypt.decrypts(encrypted);
91
92 assertEquals(orig, decrypted);
93 }
94 });
95
96 addTest(new TestCase("Empty, base64") {
97 @Override
98 public void test() throws Exception {
99 String orig = "";
a6a73de3
NR
100 String encrypted = crypt.encrypt64(orig);
101 String decrypted = crypt.decrypt64s(encrypted);
f3502fbd
NR
102
103 assertEquals(orig, decrypted);
104 }
105 });
106
107 addTest(new TestCase("Long") {
108 @Override
109 public void test() throws Exception {
110 String orig = longData;
111 byte[] encrypted = crypt.encrypt(orig);
112 String decrypted = crypt.decrypts(encrypted);
113
114 assertEquals(orig, decrypted);
115 }
116 });
117
118 addTest(new TestCase("Long, base64") {
119 @Override
120 public void test() throws Exception {
121 String orig = longData;
a6a73de3
NR
122 String encrypted = crypt.encrypt64(orig);
123 String decrypted = crypt.decrypt64s(encrypted);
f3502fbd
NR
124
125 assertEquals(orig, decrypted);
126 }
127 });
128 }
129
130 private CryptUtilsTest(String[] args, String title, String key,
131 @SuppressWarnings("unused") boolean dummy) {
132 super(title, args);
133 this.key = key;
134
6fd1a061 135 addTest(new TestCase("Simple test") {
f3502fbd
NR
136 @Override
137 public void test() throws Exception {
08f80ac5
NR
138 InputStream in = new ByteArrayInputStream(new byte[] { 42, 127,
139 12 });
f28a134e 140 crypt.encrypt(in);
6fd1a061
NR
141 ByteArrayOutputStream out = new ByteArrayOutputStream();
142 IOUtils.write(in, out);
143 byte[] result = out.toByteArray();
08f80ac5
NR
144
145 assertEquals(
146 "We wrote 3 bytes, we expected 3 bytes back but got: "
147 + result.length, result.length, result.length);
148
6fd1a061
NR
149 assertEquals(42, result[0]);
150 assertEquals(127, result[1]);
151 assertEquals(12, result[2]);
f3502fbd
NR
152 }
153 });
154 }
155}