summaryrefslogtreecommitdiff
path: root/tests/test_helper.c
diff options
context:
space:
mode:
authorJustin Husted <sigstop@gmail.com>2019-11-03 00:35:03 -0700
committerKent Overstreet <kent.overstreet@gmail.com>2019-11-03 23:17:43 -0500
commit61bc316a4da4831d8812eb5051732cca27652d8d (patch)
tree3a7309c6010e5b918839319e7fc8140b2968687e /tests/test_helper.c
parentd79d57ef89000f857158875055b977dbc54354da (diff)
Initial version of bcachefs tests.
So far, these tests just test basic format, fsck, and list functions under valgrind, as well as a few self-validation tests. Signed-off-by: Justin Husted <sigstop@gmail.com>
Diffstat (limited to 'tests/test_helper.c')
-rw-r--r--tests/test_helper.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/tests/test_helper.c b/tests/test_helper.c
new file mode 100644
index 00000000..c7604f0c
--- /dev/null
+++ b/tests/test_helper.c
@@ -0,0 +1,103 @@
+#include <assert.h>
+#include <malloc.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void trick_compiler(int *x);
+
+static void test_abort(void)
+{
+ abort();
+}
+
+static void test_segfault(void)
+{
+ raise(SIGSEGV);
+}
+
+static void test_leak(void)
+{
+ int *p = malloc(sizeof *p);
+ trick_compiler(p);
+}
+
+static void test_undefined(void)
+{
+ int *p = malloc(1);
+ printf("%d\n", *p);
+}
+
+static void test_undefined_branch(void)
+{
+ int x;
+ trick_compiler(&x);
+
+ if (x)
+ printf("1\n");
+ else
+ printf("0\n");
+}
+
+static void test_read_after_free(void)
+{
+ int *p = malloc(sizeof *p);
+ free(p);
+
+ printf("%d\n", *p);
+}
+
+static void test_write_after_free(void)
+{
+ int *p = malloc(sizeof *p);
+ free(p);
+
+ printf("%d\n", *p);
+}
+
+typedef void (*test_fun)(void);
+
+struct test {
+ const char *name;
+ test_fun fun;
+};
+
+#define TEST(f) { .name = #f, .fun = test_##f, }
+static struct test tests[] = {
+ TEST(abort),
+ TEST(segfault),
+ TEST(leak),
+ TEST(undefined),
+ TEST(undefined_branch),
+ TEST(read_after_free),
+ TEST(write_after_free),
+};
+#define ntests (sizeof tests / sizeof *tests)
+
+int main(int argc, char *argv[])
+{
+ int i;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: test_helper <test>\n");
+ exit(1);
+ }
+
+ bool found = false;
+ for (i = 0; i < ntests; ++i)
+ if (!strcmp(argv[1], tests[i].name)) {
+ found = true;
+ printf("Running test: %s\n", tests[i].name);
+ tests[i].fun();
+ break;
+ }
+
+ if (!found) {
+ fprintf(stderr, "Unable to find test: %s\n", argv[1]);
+ exit(1);
+ }
+
+ return 0;
+}