diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/CMakeLists.txt | 12 | ||||
-rw-r--r-- | test/myassert.h | 11 | ||||
-rw-r--r-- | test/test_reloc_match.c | 90 | ||||
-rw-r--r-- | test/test_reloc_read.c | 38 |
4 files changed, 151 insertions, 0 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..6b79d98 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,12 @@ +project(tests) + +include_directories("${CMAKE_SOURCE_DIR}") +include_directories("${CMAKE_BINARY_DIR}") +add_executable(test_reloc_match test_reloc_match.c) +add_executable(test_reloc_read test_reloc_read.c) + +target_link_libraries(test_reloc_match relocate) +target_link_libraries(test_reloc_read relocate) + +add_test(test_reloc_match test_reloc_match) +add_test(test_reloc_read test_reloc_read) diff --git a/test/myassert.h b/test/myassert.h new file mode 100644 index 0000000..01e567e --- /dev/null +++ b/test/myassert.h @@ -0,0 +1,11 @@ +#define myassert(message, condition) \ + do { \ + if (!(condition)) { \ + fprintf(stderr, "%s:%d:%s :: %s\n", \ + __FILE__, \ + __LINE__, \ + __FUNCTION__, \ + message); \ + return 1; \ + } \ + } while (0); diff --git a/test/test_reloc_match.c b/test/test_reloc_match.c new file mode 100644 index 0000000..bb9d6b0 --- /dev/null +++ b/test/test_reloc_match.c @@ -0,0 +1,90 @@ +#include "reloc.h" +#include "myassert.h" + +char *test_cases[] = { + "I have a yellow pencil\x00", + "/one/two/three/four/five\x00\xAB\xCD\x00", + "\x09\x09\x09 1234567890\x00", + "123456789/0\x00", + NULL, +}; + +const char *test_patterns[] = { + "yellow", + "three", + "1234567890", + "/", + NULL, +}; + +const char *test_solution_begin[] = { + "yellow pencil", + "three/four/five", + "1234567890", + "/0", + NULL, +}; + +const char *test_solution_post[] = { + " pencil", + "/four/five", + "", + "0", + NULL, +}; + +const size_t test_solution_length[] = { + 6, + 5, + 10, + 1, +}; + +const size_t test_solution_post_length[] = { + 7, + 10, + 0, + 1, +}; + +const size_t test_solution_total_length[] = { + 13, + 15, + 10, + 2, +}; + +int test_reloc_match() { + for (int i = 0; test_cases[i] != NULL; i++) { + RelocMatch *match = NULL; + char *ptr = test_cases[i]; + + printf("case %d:\n", i); + while (*ptr != '\0') { + if (match) break; + printf("\tpattern=\"%s\", data=\"%s\"\n", test_patterns[i], ptr); + match = reloc_match(ptr, test_patterns[i]); + ptr++; + } + myassert("Failed to find pattern", match != NULL); + myassert("Invalid match->begin", !strcmp(match->begin, test_solution_begin[i])); + printf("\tend=\"%s\", data=\"%s\"\n", match->end, match->begin); + myassert("Invalid match->end", match->end != NULL); + printf("\tpost=\"%s\", data=\"%s\"\n", match->post, match->begin); + myassert("Invalid match->post", !strcmp(match->post, test_solution_post[i])); + printf("\tlength=%lu\n", match->length); + myassert("Invalid match->length", match->length == test_solution_length[i]); + printf("\tpost_length=%lu\n", match->post_length); + myassert("Invalid match->post_length", match->post_length == test_solution_post_length[i]); + printf("\ttotal_length=%lu\n", match->total_length); + myassert("Invalid match->total_length", match->total_length == test_solution_total_length[i]); + if (match) { + free(match); + } + } + return 0; +} + +int main() { + return test_reloc_match(); +} diff --git a/test/test_reloc_read.c b/test/test_reloc_read.c new file mode 100644 index 0000000..310370d --- /dev/null +++ b/test/test_reloc_read.c @@ -0,0 +1,38 @@ +#include "reloc.h" +#include "myassert.h" + +const char *input_file = "input.bin"; +const char *output_file = "output.bin"; +char test_case[] = + "\xAB\xCD\xEF the quick brown fox\n\x00" + "jumps over\n\x00 \xEE\x09" + "\xAB\xCD\xEF the quick brown fox\n\x00" + "jumps over\n\x00 \xEE\x09" + "\xBBthe lazy\n\x00 dog\n" + "\xAB\xCD\xEF the quick brown fox\n\x00" + "\xBBthe lazy\n\x00 dog\n"; + + +int test_reloc_read() { + RelocData *info = NULL; + if (!(info = reloc_read(input_file))) { + fprintf(stderr, "Failed to open '%s' for writing\n", input_file); + return 1; + } + myassert("info->size is incorrect", info->size == sizeof(test_case)); + myassert("info->path is incorrect", !strcmp(info->path, input_file)); + myassert("info->data is uninitialized", info->data); + return 0; +} + +int main() { + FILE *fp; + if (!(fp = fopen(input_file, "w+b"))) { + fprintf(stderr, "Failed to open '%s' for writing\n", input_file); + return 1; + } + fwrite(test_case, sizeof(char), sizeof(test_case), fp); + fclose(fp); + + return test_reloc_read(); +} |