android: Add some (older) unit tests
authorTobias Brunner <tobias@strongswan.org>
Wed, 11 Nov 2015 18:35:33 +0000 (19:35 +0100)
committerTobias Brunner <tobias@strongswan.org>
Thu, 12 Nov 2015 13:12:43 +0000 (14:12 +0100)
src/frontends/android/app/build.gradle
src/frontends/android/app/src/test/java/org/strongswan/android/test/BufferedByteWriterTest.java [new file with mode: 0644]
src/frontends/android/app/src/test/java/org/strongswan/android/test/SettingsWriterTest.java [new file with mode: 0644]

index 8280f50..46927ea 100644 (file)
@@ -40,3 +40,7 @@ android {
         }
     }
 }
+
+dependencies {
+    testCompile 'junit:junit:4.12'
+}
diff --git a/src/frontends/android/app/src/test/java/org/strongswan/android/test/BufferedByteWriterTest.java b/src/frontends/android/app/src/test/java/org/strongswan/android/test/BufferedByteWriterTest.java
new file mode 100644 (file)
index 0000000..56ba560
--- /dev/null
@@ -0,0 +1,257 @@
+/*
+ * Copyright (C) 2013-2015 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+package org.strongswan.android.test;
+
+import org.junit.Test;
+import org.strongswan.android.utils.BufferedByteWriter;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+public class BufferedByteWriterTest
+{
+       byte[] testArray = {0, 1, 2, 3, 4, 5, 6, 7};
+
+       private void testArray(byte[] array, int offset)
+       {
+               for (int i = offset; i < array.length; i++)
+               {
+                       assertEquals("value " + i, array[i], (i - offset) % 8);
+               }
+       }
+
+       private void testArray(byte[] array)
+       {
+               testArray(array, 0);
+       }
+
+       @Test
+       public void testPutByteArray()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               byte[] array = writer.toByteArray();
+               assertEquals("length", array.length, 0);
+               writer.put(testArray);
+               array = writer.toByteArray();
+               assertEquals("length", array.length, testArray.length);
+               testArray(array);
+       }
+
+       @Test
+       public void testResize()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put(testArray);
+               writer.put(testArray);
+               writer.put(testArray);
+               writer.put(testArray);
+               byte[] array = writer.toByteArray();
+               assertEquals("length", array.length, testArray.length * 4);
+               testArray(array);
+               writer.put(testArray);
+               array = writer.toByteArray();
+               assertEquals("length", array.length, testArray.length * 5);
+               testArray(array);
+       }
+
+       @Test
+       public void testResizeCapacity()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter(4);
+               writer.put(testArray);
+               writer.put(testArray);
+               writer.put(testArray);
+               writer.put(testArray);
+               byte[] array = writer.toByteArray();
+               assertEquals("length", array.length, testArray.length * 4);
+               testArray(array);
+               writer.put(testArray);
+               array = writer.toByteArray();
+               assertEquals("length", array.length, testArray.length * 5);
+               testArray(array);
+       }
+
+       @Test
+       public void testPutByte()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put((byte)1);
+               byte[] array = writer.toByteArray();
+               assertEquals("length", array.length, 1);
+               assertEquals("value", array[0], 1);
+       }
+
+       @Test
+       public void testPutLen8()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.putLen8(testArray);
+               byte[] array = writer.toByteArray();
+               assertEquals("length", array.length, testArray.length + 1);
+               assertEquals("value", array[0], testArray.length);
+               testArray(array, 1);
+       }
+
+       @Test
+       public void testPutLen16()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.putLen16(testArray);
+               byte[] array = writer.toByteArray();
+               assertEquals("length", array.length, testArray.length + 2);
+               assertEquals("value 0", array[0], 0);
+               assertEquals("value 1", array[1], testArray.length);
+               testArray(array, 2);
+       }
+
+       @Test
+       public void testPut16()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put16((short)0xbeef);
+               byte[] exp = new byte[]{(byte)0xbe, (byte)0xef};
+               assertArrayEquals(exp, writer.toByteArray());
+               writer.put16((short)0xfeed);
+               exp = new byte[]{(byte)0xbe, (byte)0xef, (byte)0xfe, (byte)0xed};
+               assertArrayEquals(exp, writer.toByteArray());
+       }
+
+       @Test
+       public void testPut16Byte()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put16((byte)0xfe);
+               byte[] exp = new byte[]{0, (byte)0xfe};
+               assertArrayEquals(exp, writer.toByteArray());
+       }
+
+       @Test
+       public void testPut24Half()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put24(0xbeef);
+               byte[] exp = new byte[]{0, (byte)0xbe, (byte)0xef};
+               assertArrayEquals(exp, writer.toByteArray());
+       }
+
+       @Test
+       public void testPut24Full()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put24(0xbeeffe);
+               byte[] exp = new byte[]{(byte)0xbe, (byte)0xef, (byte)0xfe};
+               assertArrayEquals(exp, writer.toByteArray());
+       }
+
+       @Test
+       public void testPut24Int()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put24(0xcafebeef);
+               byte[] exp = new byte[]{(byte)0xfe, (byte)0xbe, (byte)0xef};
+               assertArrayEquals(exp, writer.toByteArray());
+       }
+
+       @Test
+       public void testPut24Short()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put24((short)0xfeed);
+               byte[] exp = new byte[]{0, (byte)0xfe, (byte)0xed};
+               assertArrayEquals(exp, writer.toByteArray());
+       }
+
+       @Test
+       public void testPut24Byte()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put24((byte)0xfe);
+               byte[] exp = new byte[]{0, 0, (byte)0xfe};
+               assertArrayEquals(exp, writer.toByteArray());
+       }
+
+       @Test
+       public void testPut32()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put32(0xbeeffeed);
+               byte[] exp = new byte[]{(byte)0xbe, (byte)0xef, (byte)0xfe, (byte)0xed};
+               assertArrayEquals(exp, writer.toByteArray());
+       }
+
+       @Test
+       public void testPut32Short()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put32((short)0xbeef);
+               byte[] exp = new byte[]{0, 0, (byte)0xbe, (byte)0xef};
+               assertArrayEquals(exp, writer.toByteArray());
+       }
+
+       @Test
+       public void testPut32Byte()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put32((byte)0xfe);
+               byte[] exp = new byte[]{0, 0, 0, (byte)0xfe};
+               assertArrayEquals(exp, writer.toByteArray());
+       }
+
+       @Test
+       public void testPut64()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put64(0xdeadcafebeeffeedL);
+               byte[] exp = new byte[]{(byte)0xde, (byte)0xad, (byte)0xca, (byte)0xfe, (byte)0xbe, (byte)0xef, (byte)0xfe, (byte)0xed};
+               assertArrayEquals(exp, writer.toByteArray());
+       }
+
+       @Test
+       public void testPut64Half()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put64(0xbeeffeedL);
+               byte[] exp = new byte[]{0, 0, 0, 0, (byte)0xbe, (byte)0xef, (byte)0xfe, (byte)0xed};
+               assertArrayEquals(exp, writer.toByteArray());
+       }
+
+       @Test
+       public void testPut64Int()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put64(0xbeeffeed);
+               byte[] exp = new byte[]{0, 0, 0, 0, (byte)0xbe, (byte)0xef, (byte)0xfe, (byte)0xed};
+               assertArrayEquals(exp, writer.toByteArray());
+       }
+
+       @Test
+       public void testPut64Short()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put64((short)0xfeed);
+               byte[] exp = new byte[]{0, 0, 0, 0, 0, 0, (byte)0xfe, (byte)0xed};
+               assertArrayEquals(exp, writer.toByteArray());
+       }
+
+       @Test
+       public void testPut64Byte()
+       {
+               BufferedByteWriter writer = new BufferedByteWriter();
+               writer.put64((byte)0xfe);
+               byte[] exp = new byte[]{0, 0, 0, 0, 0, 0, 0, (byte)0xfe};
+               assertArrayEquals(exp, writer.toByteArray());
+       }
+}
diff --git a/src/frontends/android/app/src/test/java/org/strongswan/android/test/SettingsWriterTest.java b/src/frontends/android/app/src/test/java/org/strongswan/android/test/SettingsWriterTest.java
new file mode 100644 (file)
index 0000000..aaa880c
--- /dev/null
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2015 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+package org.strongswan.android.test;
+
+import org.junit.Test;
+import org.strongswan.android.utils.SettingsWriter;
+
+import static org.junit.Assert.assertEquals;
+
+public class SettingsWriterTest
+{
+       @Test
+       public void testString()
+       {
+               SettingsWriter writer = new SettingsWriter();
+               assertEquals("serialized", "", writer.serialize());
+               writer.setValue("key", "value");
+               assertEquals("serialized", "key=\"value\"\n", writer.serialize());
+       }
+
+       @Test
+       public void testInt()
+       {
+               SettingsWriter writer = new SettingsWriter();
+               writer.setValue("key", 1234);
+               assertEquals("serialized", "key=\"1234\"\n", writer.serialize());
+               writer.setValue("key", (Integer)null);
+               assertEquals("serialized", "key=\n", writer.serialize());
+       }
+
+       @Test
+       public void testBoolean()
+       {
+               SettingsWriter writer = new SettingsWriter();
+               writer.setValue("key1", true);
+               writer.setValue("key2", false);
+               assertEquals("serialized", "key1=\"1\"\nkey2=\"0\"\n", writer.serialize());
+               writer.setValue("key2", (Boolean)null);
+               assertEquals("serialized", "key1=\"1\"\nkey2=\n", writer.serialize());
+       }
+
+       @Test
+       public void testReplace()
+       {
+               SettingsWriter writer = new SettingsWriter();
+               writer.setValue("key", "value");
+               writer.setValue("key", "other");
+               assertEquals("serialized", "key=\"other\"\n", writer.serialize());
+       }
+
+       @Test
+       public void testUnset()
+       {
+               SettingsWriter writer = new SettingsWriter();
+               writer.setValue("key", (String)null);
+               assertEquals("serialized", "key=\n", writer.serialize());
+       }
+
+       @Test
+       public void testChain()
+       {
+               SettingsWriter writer = new SettingsWriter();
+               writer.setValue("key1", "value").setValue("key2", 1234).setValue("key3", true);
+               assertEquals("serialized", "key1=\"value\"\nkey2=\"1234\"\nkey3=\"1\"\n", writer.serialize());
+       }
+
+       @Test
+       public void testInvalid()
+       {
+               SettingsWriter writer = new SettingsWriter();
+               writer.setValue("", "value");
+               writer.setValue(null, "value");
+               writer.setValue("se{c.key", "value");
+               writer.setValue("sec}.key", "value");
+               writer.setValue("sec\n.key", "value");
+               assertEquals("serialized", "", writer.serialize());
+       }
+
+       @Test
+       public void testEscape()
+       {
+               SettingsWriter writer = new SettingsWriter();
+               writer.setValue("key", "val\"ue");
+               writer.setValue("nl", "val\nue");
+               assertEquals("serialized", "key=\"val\\\"ue\"\nnl=\"val\nue\"\n", writer.serialize());
+       }
+
+       @Test
+       public void testSections()
+       {
+               SettingsWriter writer = new SettingsWriter();
+               writer.setValue("sec.key1", "value");
+               writer.setValue("sec.sub.key", true);
+               writer.setValue("sec.key2", 1234);
+               assertEquals("serialized", "sec {\nkey1=\"value\"\nkey2=\"1234\"\nsub {\nkey=\"1\"\n}\n}\n", writer.serialize());
+       }
+
+       @Test
+       public void testOrder()
+       {
+               SettingsWriter writer = new SettingsWriter();
+               writer.setValue("key1", "value");
+               writer.setValue("key2", 1234);
+               writer.setValue("key1", "other");
+               assertEquals("serialized", "key1=\"other\"\nkey2=\"1234\"\n", writer.serialize());
+       }
+}