aboutsummaryrefslogtreecommitdiffstats
path: root/executor/test.h
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2018-07-20 20:26:05 +0200
committerDmitry Vyukov <dvyukov@google.com>2018-07-24 12:04:27 +0200
commit9fe4bdc5f1037a409e82299f36117030114c7b94 (patch)
treed3d73c1f69ded8152436be47684a07baa0e7f6ec /executor/test.h
parentdb7957bc09bf5715d33e4c56b8614579aa94000a (diff)
executor: overhaul
Make as much code as possible shared between all OSes. In particular main is now common across all OSes. Make more code shared between executor and csource (in particular, loop function and threaded execution logic). Also make loop and threaded logic shared across all OSes. Make more posix/unix code shared across OSes (e.g. signal handling, pthread creation, etc). Plus other changes along similar lines. Also support test OS in executor (based on portable posix) and add 4 arches that cover all execution modes (fork server/no fork server, shmem/no shmem). This change paves way for testing of executor code and allows to preserve consistency across OSes and executor/csource.
Diffstat (limited to 'executor/test.h')
-rw-r--r--executor/test.h212
1 files changed, 212 insertions, 0 deletions
diff --git a/executor/test.h b/executor/test.h
new file mode 100644
index 000000000..74133f58b
--- /dev/null
+++ b/executor/test.h
@@ -0,0 +1,212 @@
+// Copyright 2017 syzkaller project authors. All rights reserved.
+// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
+
+#if GOOS_linux && GOARCH_amd64
+#include "test_linux.h"
+#endif
+
+static int test_copyin()
+{
+ unsigned char x[4] = {};
+ STORE_BY_BITMASK(uint16, &x[1], 0x1234, 0, 0);
+ if (x[0] != 0 || x[1] != 0x34 || x[2] != 0x12 || x[3] != 0) {
+ printf("bad result of STORE_BY_BITMASK(0, 0): %x %x %x %x\n", x[0], x[1], x[2], x[3]);
+ return 1;
+ }
+ STORE_BY_BITMASK(uint16, &x[1], 0x555a, 5, 4);
+ if (x[0] != 0 || x[1] != 0x54 || x[2] != 0x13 || x[3] != 0) {
+ printf("bad result of STORE_BY_BITMASK(7, 3): %x %x %x %x\n", x[0], x[1], x[2], x[3]);
+ return 1;
+ }
+ return 0;
+}
+
+static int test_csum_inet()
+{
+ struct csum_inet_test {
+ const char* data;
+ size_t length;
+ uint16 csum;
+ };
+ struct csum_inet_test tests[] = {
+ {// 0
+ "",
+ 0,
+ 0xffff},
+ {
+ // 1
+ "\x00",
+ 1,
+ 0xffff,
+ },
+ {
+ // 2
+ "\x00\x00",
+ 2,
+ 0xffff,
+ },
+ {
+ // 3
+ "\x00\x00\xff\xff",
+ 4,
+ 0x0000,
+ },
+ {
+ // 4
+ "\xfc",
+ 1,
+ 0xff03,
+ },
+ {
+ // 5
+ "\xfc\x12",
+ 2,
+ 0xed03,
+ },
+ {
+ // 6
+ "\xfc\x12\x3e",
+ 3,
+ 0xecc5,
+ },
+ {
+ // 7
+ "\xfc\x12\x3e\x00\xc5\xec",
+ 6,
+ 0x0000,
+ },
+ {
+ // 8
+ "\x42\x00\x00\x43\x44\x00\x00\x00\x45\x00\x00\x00\xba\xaa\xbb\xcc\xdd",
+ 17,
+ 0x43e1,
+ },
+ {
+ // 9
+ "\x42\x00\x00\x43\x44\x00\x00\x00\x45\x00\x00\x00\xba\xaa\xbb\xcc\xdd\x00",
+ 18,
+ 0x43e1,
+ },
+ {
+ // 10
+ "\x00\x00\x42\x00\x00\x43\x44\x00\x00\x00\x45\x00\x00\x00\xba\xaa\xbb\xcc\xdd",
+ 19,
+ 0x43e1,
+ },
+ {
+ // 11
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\xab\xcd",
+ 15,
+ 0x5032,
+ },
+ {
+ // 12
+ "\x00\x00\x12\x34\x56\x78",
+ 6,
+ 0x5397,
+ },
+ {
+ // 13
+ "\x00\x00\x12\x34\x00\x00\x56\x78\x00\x06\x00\x04\xab\xcd",
+ 14,
+ 0x7beb,
+ },
+ {
+ // 14
+ "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\xff\xee\xdd\xcc\xbb\xaa\x99\x88\x77\x66\x55\x44\x33\x22\x11\x00\x00\x00\x00\x04\x00\x00\x00\x06\x00\x00\xab\xcd",
+ 44,
+ 0x2854,
+ },
+ {
+ // 15
+ "\x00\x00\x12\x34\x00\x00\x56\x78\x00\x11\x00\x04\xab\xcd",
+ 14,
+ 0x70eb,
+ },
+ {
+ // 16
+ "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\xff\xee\xdd\xcc\xbb\xaa\x99\x88\x77\x66\x55\x44\x33\x22\x11\x00\x00\x00\x00\x04\x00\x00\x00\x11\x00\x00\xab\xcd",
+ 44,
+ 0x1d54,
+ },
+ {
+ // 17
+ "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\xff\xee\xdd\xcc\xbb\xaa\x99\x88\x77\x66\x55\x44\x33\x22\x11\x00\x00\x00\x00\x04\x00\x00\x00\x3a\x00\x00\xab\xcd",
+ 44,
+ 0xf453,
+ }};
+
+ for (unsigned i = 0; i < ARRAY_SIZE(tests); i++) {
+ struct csum_inet csum;
+ csum_inet_init(&csum);
+ csum_inet_update(&csum, (const uint8*)tests[i].data, tests[i].length);
+ if (csum_inet_digest(&csum) != tests[i].csum) {
+ fprintf(stderr, "bad checksum in test #%u, want: %hx, got: %hx\n", i, tests[i].csum, csum_inet_digest(&csum));
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+static int rand_int_range(int start, int end)
+{
+ return rand() % (end + 1 - start) + start;
+}
+
+static int test_csum_inet_acc()
+{
+ uint8 buffer[128];
+
+ int test;
+ for (test = 0; test < 256; test++) {
+ int size = rand_int_range(1, 128);
+ int step = rand_int_range(1, 8) * 2;
+
+ int i;
+ for (i = 0; i < size; i++)
+ buffer[i] = rand_int_range(0, 255);
+
+ struct csum_inet csum_acc;
+ csum_inet_init(&csum_acc);
+
+ for (i = 0; i < size / step; i++)
+ csum_inet_update(&csum_acc, &buffer[i * step], step);
+ if (size % step != 0)
+ csum_inet_update(&csum_acc, &buffer[size - size % step], size % step);
+
+ struct csum_inet csum;
+ csum_inet_init(&csum);
+ csum_inet_update(&csum, &buffer[0], size);
+
+ if (csum_inet_digest(&csum_acc) != csum_inet_digest(&csum))
+ return 1;
+ }
+ return 0;
+}
+
+static struct {
+ const char* name;
+ int (*f)();
+} tests[] = {
+ {"test_copyin", test_copyin},
+ {"test_csum_inet", test_csum_inet},
+ {"test_csum_inet_acc", test_csum_inet_acc},
+#if GOOS_linux && GOARCH_amd64
+ {"test_kvm", test_kvm},
+#endif
+};
+
+int run_tests()
+{
+ int ret = 0;
+ for (size_t i = 0; i < ARRAY_SIZE(tests); i++) {
+ const char* name = tests[i].name;
+ printf("=== RUN %s\n", name);
+ int res = tests[i].f();
+ ret |= res > 0;
+ const char* strres = res < 0 ? "SKIP" : (res > 0 ? "FAIL" : "OK");
+ printf("--- %-4s %s\n", strres, name);
+ }
+ return ret;
+}