From 9f554a72b01705ebf6be66143c5e69b09b3c1372 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 28 Jan 2022 07:33:30 -0300 Subject: Init project: copy files and skeletons from others --- .../c/resources/a-file-to-be-read-in-the-test.txt | 3 + tests/c/resources/an-empty-file.txt | 0 tests/fuzz/another.c | 17 ++++++ tests/fuzz/hello.c | 17 ++++++ tests/slurp.c | 71 ++++++++++++++++++++++ tests/slurp.h | 2 + 6 files changed, 110 insertions(+) create mode 100644 tests/c/resources/a-file-to-be-read-in-the-test.txt create mode 100644 tests/c/resources/an-empty-file.txt create mode 100644 tests/fuzz/another.c create mode 100644 tests/fuzz/hello.c create mode 100644 tests/slurp.c create mode 100644 tests/slurp.h (limited to 'tests') 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 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 +#include +#include + +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 +#include +#include + +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 +#include + +#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); -- cgit v1.2.3