summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/c/resources/a-file-to-be-read-in-the-test.txt3
-rw-r--r--tests/c/resources/an-empty-file.txt0
-rw-r--r--tests/fuzz/another.c17
-rw-r--r--tests/fuzz/hello.c17
-rw-r--r--tests/slurp.c71
-rw-r--r--tests/slurp.h2
6 files changed, 110 insertions, 0 deletions
diff --git a/tests/c/resources/a-file-to-be-read-in-the-test.txt b/tests/c/resources/a-file-to-be-read-in-the-test.txt
new file mode 100644
index 0000000..762cf0f
--- /dev/null
+++ b/tests/c/resources/a-file-to-be-read-in-the-test.txt
@@ -0,0 +1,3 @@
+A file with some content
+that should be read when
+running the unit tests.
diff --git a/tests/c/resources/an-empty-file.txt b/tests/c/resources/an-empty-file.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/c/resources/an-empty-file.txt
diff --git a/tests/fuzz/another.c b/tests/fuzz/another.c
new file mode 100644
index 0000000..7fbfe07
--- /dev/null
+++ b/tests/fuzz/another.c
@@ -0,0 +1,17 @@
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+static bool
+fuzz_me(const uint8_t *const data, const size_t size) {
+ (void)data;
+ (void)size;
+ return true;
+}
+
+int
+LLVMFuzzerTestOneInput(const uint8_t *const data, const size_t size) {
+ int rc = 0;
+ fuzz_me(data, size);
+ return rc;
+}
diff --git a/tests/fuzz/hello.c b/tests/fuzz/hello.c
new file mode 100644
index 0000000..7fbfe07
--- /dev/null
+++ b/tests/fuzz/hello.c
@@ -0,0 +1,17 @@
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+static bool
+fuzz_me(const uint8_t *const data, const size_t size) {
+ (void)data;
+ (void)size;
+ return true;
+}
+
+int
+LLVMFuzzerTestOneInput(const uint8_t *const data, const size_t size) {
+ int rc = 0;
+ fuzz_me(data, size);
+ return rc;
+}
diff --git a/tests/slurp.c b/tests/slurp.c
new file mode 100644
index 0000000..7b5936f
--- /dev/null
+++ b/tests/slurp.c
@@ -0,0 +1,71 @@
+#include "../src/config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "slurp.h"
+
+int
+slurp_for_tests(const char *const FNAME, char **strref) {
+ int rc = 0;
+
+ FILE *file = NULL;
+ char *str = NULL;
+
+ file = fopen(FNAME, "r");
+ if (!file) {
+ perror("fopen(FNAME, \"r\")");
+ rc = -1;
+ goto out;
+ }
+
+ if (fseek(file, 0L, SEEK_END)) {
+ perror("fseek(file, 0L, SEEK_END)");
+ rc = -1;
+ goto out;
+ }
+
+ const long lsize = ftell(file);
+ if (lsize == -1) {
+ perror("ftell(file)");
+ rc = -1;
+ goto out;
+ }
+ const size_t size = (size_t)lsize + sizeof(char);
+
+ if (fseek(file, 0L, SEEK_SET)) {
+ perror("fseek(file, 0L, SEEK_SET)");
+ rc = -1;
+ goto out;
+ }
+
+ str = malloc(size);
+ if (!str) {
+ perror("malloc(...)");
+ rc = -1;
+ goto out;
+ }
+
+ if (fread(str, sizeof(char), size - 1, file) != size - 1) {
+ perror("fread(...)");
+ rc = -1;
+ goto out;
+ }
+ str[size - 1] = '\0';
+ *strref = str;
+
+out:
+ if (file) {
+ if (fclose(file)) {
+ perror("flcose(file");
+ rc = -1;
+ }
+ }
+
+ if (rc) {
+ if (str) {
+ free(str);
+ }
+ }
+ return rc;
+}
diff --git a/tests/slurp.h b/tests/slurp.h
new file mode 100644
index 0000000..fd6c082
--- /dev/null
+++ b/tests/slurp.h
@@ -0,0 +1,2 @@
+int
+slurp_for_tests(const char *const FNAME, char **strref);