From f3502fbd11d357e1d33e2eb6fc6261df3cf26d09 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Tue, 23 Apr 2019 08:54:05 +0200 Subject: [PATCH] test: new: CryptUtilsTest --- .../utils/test_code/CryptUtilsTest.java | 136 ++++++++++++++++++ src/be/nikiroo/utils/test_code/Test.java | 1 + 2 files changed, 137 insertions(+) create mode 100644 src/be/nikiroo/utils/test_code/CryptUtilsTest.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 index 00000000..59765046 --- /dev/null +++ b/src/be/nikiroo/utils/test_code/CryptUtilsTest.java @@ -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 { + } + }); + } +} diff --git a/src/be/nikiroo/utils/test_code/Test.java b/src/be/nikiroo/utils/test_code/Test.java index d6d4f4d6..f25c02db 100644 --- a/src/be/nikiroo/utils/test_code/Test.java +++ b/src/be/nikiroo/utils/test_code/Test.java @@ -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; -- 2.27.0