| 1 | package be.nikiroo.utils.test_code; |
| 2 | |
| 3 | import be.nikiroo.utils.CryptUtils; |
| 4 | import be.nikiroo.utils.test.TestCase; |
| 5 | import be.nikiroo.utils.test.TestLauncher; |
| 6 | |
| 7 | class CryptUtilsTest extends TestLauncher { |
| 8 | private String key; |
| 9 | private CryptUtils crypt; |
| 10 | |
| 11 | public CryptUtilsTest(String[] args) { |
| 12 | super("CryptUtils test", args); |
| 13 | |
| 14 | String longKey = "some long string with more than 128 bits (=32 bytes) of data"; |
| 15 | |
| 16 | addSeries(new CryptUtilsTest(args, "Manual input wuth NULL key", null, |
| 17 | 1)); |
| 18 | addSeries(new CryptUtilsTest(args, "Streams with NULL key", null, true)); |
| 19 | |
| 20 | addSeries(new CryptUtilsTest(args, "Manual input with emptykey", "", 1)); |
| 21 | addSeries(new CryptUtilsTest(args, "Streams with empty key", "", true)); |
| 22 | |
| 23 | addSeries(new CryptUtilsTest(args, "Manual input with long key", |
| 24 | longKey, 1)); |
| 25 | addSeries(new CryptUtilsTest(args, "Streams with long key", longKey, |
| 26 | true)); |
| 27 | } |
| 28 | |
| 29 | @Override |
| 30 | protected void addTest(final TestCase test) { |
| 31 | super.addTest(new TestCase(test.getName()) { |
| 32 | @Override |
| 33 | public void test() throws Exception { |
| 34 | test.test(); |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public void setUp() throws Exception { |
| 39 | crypt = new CryptUtils(key); |
| 40 | test.setUp(); |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public void tearDown() throws Exception { |
| 45 | test.tearDown(); |
| 46 | crypt = null; |
| 47 | } |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | private CryptUtilsTest(String[] args, String title, String key, |
| 52 | @SuppressWarnings("unused") int dummy) { |
| 53 | super(title, args); |
| 54 | this.key = key; |
| 55 | |
| 56 | 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 :/"; |
| 57 | |
| 58 | addTest(new TestCase("Short") { |
| 59 | @Override |
| 60 | public void test() throws Exception { |
| 61 | String orig = "data"; |
| 62 | byte[] encrypted = crypt.encrypt(orig); |
| 63 | String decrypted = crypt.decrypts(encrypted); |
| 64 | |
| 65 | assertEquals(orig, decrypted); |
| 66 | } |
| 67 | }); |
| 68 | |
| 69 | addTest(new TestCase("Short, base64") { |
| 70 | @Override |
| 71 | public void test() throws Exception { |
| 72 | String orig = "data"; |
| 73 | String encrypted = crypt.encrypt64(orig, false); |
| 74 | String decrypted = crypt.decrypt64s(encrypted, false); |
| 75 | |
| 76 | assertEquals(orig, decrypted); |
| 77 | } |
| 78 | }); |
| 79 | |
| 80 | addTest(new TestCase("Empty") { |
| 81 | @Override |
| 82 | public void test() throws Exception { |
| 83 | String orig = ""; |
| 84 | byte[] encrypted = crypt.encrypt(orig); |
| 85 | String decrypted = crypt.decrypts(encrypted); |
| 86 | |
| 87 | assertEquals(orig, decrypted); |
| 88 | } |
| 89 | }); |
| 90 | |
| 91 | addTest(new TestCase("Empty, base64") { |
| 92 | @Override |
| 93 | public void test() throws Exception { |
| 94 | String orig = ""; |
| 95 | String encrypted = crypt.encrypt64(orig, false); |
| 96 | String decrypted = crypt.decrypt64s(encrypted, false); |
| 97 | |
| 98 | assertEquals(orig, decrypted); |
| 99 | } |
| 100 | }); |
| 101 | |
| 102 | addTest(new TestCase("Long") { |
| 103 | @Override |
| 104 | public void test() throws Exception { |
| 105 | String orig = longData; |
| 106 | byte[] encrypted = crypt.encrypt(orig); |
| 107 | String decrypted = crypt.decrypts(encrypted); |
| 108 | |
| 109 | assertEquals(orig, decrypted); |
| 110 | } |
| 111 | }); |
| 112 | |
| 113 | addTest(new TestCase("Long, base64") { |
| 114 | @Override |
| 115 | public void test() throws Exception { |
| 116 | String orig = longData; |
| 117 | String encrypted = crypt.encrypt64(orig, false); |
| 118 | String decrypted = crypt.decrypt64s(encrypted, false); |
| 119 | |
| 120 | assertEquals(orig, decrypted); |
| 121 | } |
| 122 | }); |
| 123 | } |
| 124 | |
| 125 | private CryptUtilsTest(String[] args, String title, String key, |
| 126 | @SuppressWarnings("unused") boolean dummy) { |
| 127 | super(title, args); |
| 128 | this.key = key; |
| 129 | |
| 130 | addTest(new TestCase("TODO: Make some tests with the Streams") { |
| 131 | @Override |
| 132 | public void test() throws Exception { |
| 133 | } |
| 134 | }); |
| 135 | } |
| 136 | } |