Add networking unit tests
authorXiao Jia <stfairy@gmail.com>
Wed, 28 Oct 2015 00:36:41 +0000 (17:36 -0700)
committerBarret Rhoden <brho@cs.berkeley.edu>
Tue, 3 Nov 2015 17:00:38 +0000 (12:00 -0500)
Currently it includes a unit test for ptclbsum, and unit tests for
checksum benchmark.

Signed-off-by: Xiao Jia <stfairy@gmail.com>
Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
kern/src/ktest/Kbuild
kern/src/ktest/Kconfig.kernel
kern/src/ktest/Kconfig.net [new file with mode: 0644]
kern/src/ktest/net_ktests.c [new file with mode: 0644]

index 514bc6e..88e4403 100644 (file)
@@ -1,2 +1,3 @@
 obj-y                                                  += ktest.o
 obj-$(CONFIG_PB_KTESTS)                        += pb_ktests.o
+obj-$(CONFIG_NET_KTESTS)               += net_ktests.o
index ad96486..b2e333c 100644 (file)
@@ -5,4 +5,4 @@ menuconfig KERNEL_TESTING
         Run unit tests for the kernel
 
 source "kern/src/ktest/Kconfig.postboot"
-
+source "kern/src/ktest/Kconfig.net"
diff --git a/kern/src/ktest/Kconfig.net b/kern/src/ktest/Kconfig.net
new file mode 100644 (file)
index 0000000..0b2f593
--- /dev/null
@@ -0,0 +1,19 @@
+menuconfig NET_KTESTS
+    depends on KERNEL_TESTING
+    bool "Networking unit tests"
+    default y
+
+config TEST_ptclbsum
+    depends on NET_KTESTS
+    bool "Unit tests for ptclbsum"
+    default y
+
+config TEST_simplesum_bench
+    depends on NET_KTESTS
+    bool "Checksum benchmark: baseline"
+    default y
+
+config TEST_ptclbsum_bench
+    depends on NET_KTESTS
+    bool "Checksum benchmark: ptclbsum"
+    default y
diff --git a/kern/src/ktest/net_ktests.c b/kern/src/ktest/net_ktests.c
new file mode 100644 (file)
index 0000000..ba4a720
--- /dev/null
@@ -0,0 +1,94 @@
+#include <ip.h>
+#include <ktest.h>
+#include <linker_func.h>
+
+KTEST_SUITE("NET")
+
+static uint16_t simplesum(const uint8_t *buf, int len)
+{
+       uint64_t hi = 0, lo = 0, sum;
+       int i;
+
+       for (i = 0; i < len; i++) {
+               if (i % 2 == 0)
+                       hi += buf[i];
+               else
+                       lo += buf[i];
+       }
+       sum = (hi << 8) + lo;
+       while (sum >> 16)
+               sum = (sum >> 16) + (sum & 0xffff);
+       return sum & 0xffff;
+}
+
+bool test_ptclbsum(void)
+{
+       uint16_t csum, expected;
+       uint8_t buf[100];
+       int i, j, len;
+
+       for (i = 0; i < sizeof(buf); i++)
+               buf[i] = i & 0xff;
+       for (i = 0; i < sizeof(buf); i++) {
+               for (j = i; j < sizeof(buf); j++) {
+                       len = j - i + 1;
+                       csum = ptclbsum(buf + i, len);
+                       expected = simplesum(buf + i, len);
+                       if (csum != expected) {
+                               printk("i %d j %d len %d csum %04x expected %04x\n",
+                                          i, j, len, csum, expected);
+                               return false;
+                       }
+               }
+       }
+       return true;
+}
+
+#define CSUM_BENCH_BUFSIZE 4000
+
+bool test_simplesum_bench(void)
+{
+       uint8_t buf[CSUM_BENCH_BUFSIZE];
+       uint16_t csum = 0;
+       int i, j, len;
+
+       for (i = 0; i < sizeof(buf); i++)
+               buf[i] = i & 0xff;
+       for (i = 0; i < sizeof(buf); i++) {
+               for (j = i; j < sizeof(buf); j++) {
+                       len = j - i + 1;
+                       csum += simplesum(buf + i, len);
+               }
+       }
+       return true;
+}
+
+bool test_ptclbsum_bench(void)
+{
+       uint8_t buf[CSUM_BENCH_BUFSIZE];
+       uint16_t csum = 0;
+       int i, j, len;
+
+       for (i = 0; i < sizeof(buf); i++)
+               buf[i] = i & 0xff;
+       for (i = 0; i < sizeof(buf); i++) {
+               for (j = i; j < sizeof(buf); j++) {
+                       len = j - i + 1;
+                       csum += ptclbsum(buf + i, len);
+               }
+       }
+       return true;
+}
+
+static struct ktest ktests[] = {
+       KTEST_REG(ptclbsum,                             CONFIG_TEST_ptclbsum),
+       KTEST_REG(simplesum_bench,              CONFIG_TEST_simplesum_bench),
+       KTEST_REG(ptclbsum_bench,               CONFIG_TEST_ptclbsum_bench),
+};
+
+static int num_ktests = sizeof(ktests) / sizeof(struct ktest);
+
+linker_func_1(register_net_ktests)
+{
+       REGISTER_KTESTS(ktests, num_ktests);
+}