test: new: CryptUtilsTest
authorNiki Roo <niki@nikiroo.be>
Tue, 23 Apr 2019 06:54:05 +0000 (08:54 +0200)
committerNiki Roo <niki@nikiroo.be>
Tue, 23 Apr 2019 06:54:05 +0000 (08:54 +0200)
src/be/nikiroo/utils/test_code/CryptUtilsTest.java [new file with mode: 0644]
src/be/nikiroo/utils/test_code/Test.java

diff --git a/src/be/nikiroo/utils/test_code/CryptUtilsTest.java b/src/be/nikiroo/utils/test_code/CryptUtilsTest.java
new file mode 100644 (file)
index 0000000..5976504
--- /dev/null
@@ -0,0 +1,136 @@
+package be.nikiroo.utils.test_code;
+
+import be.nikiroo.utils.CryptUtils;
+import be.nikiroo.utils.test.TestCase;
+import be.nikiroo.utils.test.TestLauncher;
+
+class CryptUtilsTest extends TestLauncher {
+       private String key;
+       private CryptUtils crypt;
+
+       public CryptUtilsTest(String[] args) {
+               super("CryptUtils test", args);
+
+               String longKey = "some long string with more than 128 bits (=32 bytes) of data";
+
+               addSeries(new CryptUtilsTest(args, "Manual input wuth NULL key", null,
+                               1));
+               addSeries(new CryptUtilsTest(args, "Streams with NULL key", null, true));
+
+               addSeries(new CryptUtilsTest(args, "Manual input with emptykey", "", 1));
+               addSeries(new CryptUtilsTest(args, "Streams with empty key", "", true));
+
+               addSeries(new CryptUtilsTest(args, "Manual input with long key",
+                               longKey, 1));
+               addSeries(new CryptUtilsTest(args, "Streams with long key", longKey,
+                               true));
+       }
+
+       @Override
+       protected void addTest(final TestCase test) {
+               super.addTest(new TestCase(test.getName()) {
+                       @Override
+                       public void test() throws Exception {
+                               test.test();
+                       }
+
+                       @Override
+                       public void setUp() throws Exception {
+                               crypt = new CryptUtils(key);
+                               test.setUp();
+                       }
+
+                       @Override
+                       public void tearDown() throws Exception {
+                               test.tearDown();
+                               crypt = null;
+                       }
+               });
+       }
+
+       private CryptUtilsTest(String[] args, String title, String key,
+                       @SuppressWarnings("unused") int dummy) {
+               super(title, args);
+               this.key = key;
+
+               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 :/";
+
+               addTest(new TestCase("Short") {
+                       @Override
+                       public void test() throws Exception {
+                               String orig = "data";
+                               byte[] encrypted = crypt.encrypt(orig);
+                               String decrypted = crypt.decrypts(encrypted);
+
+                               assertEquals(orig, decrypted);
+                       }
+               });
+
+               addTest(new TestCase("Short, base64") {
+                       @Override
+                       public void test() throws Exception {
+                               String orig = "data";
+                               String encrypted = crypt.encrypt64(orig, false);
+                               String decrypted = crypt.decrypt64s(encrypted, false);
+
+                               assertEquals(orig, decrypted);
+                       }
+               });
+
+               addTest(new TestCase("Empty") {
+                       @Override
+                       public void test() throws Exception {
+                               String orig = "";
+                               byte[] encrypted = crypt.encrypt(orig);
+                               String decrypted = crypt.decrypts(encrypted);
+
+                               assertEquals(orig, decrypted);
+                       }
+               });
+
+               addTest(new TestCase("Empty, base64") {
+                       @Override
+                       public void test() throws Exception {
+                               String orig = "";
+                               String encrypted = crypt.encrypt64(orig, false);
+                               String decrypted = crypt.decrypt64s(encrypted, false);
+
+                               assertEquals(orig, decrypted);
+                       }
+               });
+
+               addTest(new TestCase("Long") {
+                       @Override
+                       public void test() throws Exception {
+                               String orig = longData;
+                               byte[] encrypted = crypt.encrypt(orig);
+                               String decrypted = crypt.decrypts(encrypted);
+
+                               assertEquals(orig, decrypted);
+                       }
+               });
+
+               addTest(new TestCase("Long, base64") {
+                       @Override
+                       public void test() throws Exception {
+                               String orig = longData;
+                               String encrypted = crypt.encrypt64(orig, false);
+                               String decrypted = crypt.decrypt64s(encrypted, false);
+
+                               assertEquals(orig, decrypted);
+                       }
+               });
+       }
+
+       private CryptUtilsTest(String[] args, String title, String key,
+                       @SuppressWarnings("unused") boolean dummy) {
+               super(title, args);
+               this.key = key;
+
+               addTest(new TestCase("TODO: Make some tests with the Streams") {
+                       @Override
+                       public void test() throws Exception {
+                       }
+               });
+       }
+}
index d6d4f4d6954d01e2e61982271d750594d5e1a349..f25c02db8573a723761819a1b5f403a1344f2c31 100644 (file)
@@ -32,6 +32,7 @@ public class Test extends TestLauncher {
                addSeries(new SerialServerTest(args));
                addSeries(new StringUtilsTest(args));
                addSeries(new TempFilesTest(args));
+               addSeries(new CryptUtilsTest(args));
 
                // TODO: test cache and downloader
                Cache cache = null;