From cb28447b459fff3a9a154ad4f3c5b51287919658 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 22 Nov 2019 00:31:01 -0500 Subject: Add test_data.bin --- test/test_data.bin | Bin 0 -> 580 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/test_data.bin diff --git a/test/test_data.bin b/test/test_data.bin new file mode 100644 index 0000000..a670304 Binary files /dev/null and b/test/test_data.bin differ -- cgit From 6e877cf4f49f4b396e3a2e8898eef69188df8fdb Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 22 Nov 2019 00:34:32 -0500 Subject: Improve testing --- .github/workflows/ci.yml | 2 +- CMakeLists.txt | 8 +++++--- test/CMakeLists.txt | 33 +++++++++++++++++++++++++++------ test/test_reloc_read.c | 4 ++-- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9723294..fcd07d9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,4 +40,4 @@ jobs: - name: make run: cmake --build . - name: test - run: ctest -V + run: ctest -C Debug -V diff --git a/CMakeLists.txt b/CMakeLists.txt index fef8144..5f17030 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,13 +2,15 @@ cmake_minimum_required(VERSION 2.8.11) project(relocate C) set(VERSION "1.2.2") set(CMAKE_C_STANDARD 99) -if(MSVC) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_SECURE_NO_WARNINGS /Wall") -else() +if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" + OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall -Wextra") +elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_SECURE_NO_WARNINGS /Wall") endif() enable_testing() +include(CTest) add_subdirectory(test) configure_file(version.h.in version.h) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6b79d98..56cdae2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,12 +1,33 @@ project(tests) +set(INPUT_SEARCH + /_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_) +set(INPUT_REPLACE + _ctest_replaced_this) +set(INPUT_FN + test_data.bin) +set(OUTPUT_FN + test_data.out) + +configure_file(${INPUT_FN} ${INPUT_FN} COPYONLY) +file(REMOVE ${OUTPUT_FN}) + 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) +function(add_reloc_test target) + add_executable(${target} ${ARGN}) + target_link_libraries(${target} relocate) + add_test(${target} ${target}) +endfunction() + +function(add_exec_test target) + add_test(NAME ${target} COMMAND ${ARGN}) +endfunction() + +add_reloc_test(test_reloc_match test_reloc_match.c) +add_reloc_test(test_reloc_read test_reloc_read.c) +add_exec_test(test_exec_success reloc "${INPUT_SEARCH}" "${INPUT_REPLACE}" "${INPUT_FN}" "${OUTPUT_FN}") -add_test(test_reloc_match test_reloc_match) -add_test(test_reloc_read test_reloc_read) +# Windows users need grep too. "findstr" and "find" were not reliable here +add_exec_test(test_exec_result grep "${INPUT_REPLACE}" "${OUTPUT_FN}") diff --git a/test/test_reloc_read.c b/test/test_reloc_read.c index 39d5eb8..934bb9f 100644 --- a/test/test_reloc_read.c +++ b/test/test_reloc_read.c @@ -19,7 +19,7 @@ int test_reloc_read() { 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); - free(info); + reloc_deinit_data(info); return 0; } @@ -27,7 +27,7 @@ int test_reloc_read_verify() { RelocData *info = reloc_read(input_file); myassert("failed to populate RelocData struct", info); myassert("info->data != input_data contents", !memcmp(test_case, info->data, info->size)); - free(info); + reloc_deinit_data(info); return 0; } -- cgit From 8ff58f7703bb2854a05acfbbda317b0abad2c0e7 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 22 Nov 2019 16:01:37 -0500 Subject: Add test_reloc_write test --- test/CMakeLists.txt | 1 + test/test_reloc_write.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 test/test_reloc_write.c diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 56cdae2..9dab686 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -27,6 +27,7 @@ endfunction() add_reloc_test(test_reloc_match test_reloc_match.c) add_reloc_test(test_reloc_read test_reloc_read.c) +add_reloc_test(test_reloc_write test_reloc_write.c) add_exec_test(test_exec_success reloc "${INPUT_SEARCH}" "${INPUT_REPLACE}" "${INPUT_FN}" "${OUTPUT_FN}") # Windows users need grep too. "findstr" and "find" were not reliable here diff --git a/test/test_reloc_write.c b/test/test_reloc_write.c new file mode 100644 index 0000000..b2044c7 --- /dev/null +++ b/test/test_reloc_write.c @@ -0,0 +1,28 @@ +#include "reloc.h" +#include "myassert.h" +#if !defined(_MSC_VER) +#include // unlink() +#endif + +const char *input_file = "test_data.bin"; +const char *output_file = "test_reloc_write.out"; + + +int test_reloc_write() { + RelocData *info = reloc_read(input_file); + myassert("failed to populate RelocData struct", info); + size_t size = reloc_write(info, output_file); + myassert("incorrect number of bytes written", size == info->size); + reloc_deinit_data(info); + if (unlink(output_file) != 0) { + perror("Could not delete output file"); + return errno; + } + + return 0; +} + + +int main() { + return test_reloc_write(); +} -- cgit From 2ae80482cd88fb9786d3ff54777b8a462641f29f Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 22 Nov 2019 22:12:03 -0500 Subject: Decrease MSVC chatter --- CMakeLists.txt | 3 ++- reloc.h | 2 +- test/test_reloc_match.c | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f17030..e24cc1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,8 @@ if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall -Wextra") elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_SECURE_NO_WARNINGS /Wall") + # C4996: The POSIX name for this item is deprecated + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_SECURE_NO_WARNINGS /W4 /wd4996") endif() enable_testing() diff --git a/reloc.h b/reloc.h index f978ae5..6cc0641 100644 --- a/reloc.h +++ b/reloc.h @@ -15,7 +15,7 @@ #define SIZE_T_FMT "%I64u" #elif defined(_MSC_VER) #define DIRSEP '\\' -#define SIZE_T_FMT "%lu" +#define SIZE_T_FMT "%zu" #elif defined(__linux__) || defined(__unix__) || defined(__APPLE__) || defined(__MACH__) #define DIRSEP '/' #define SIZE_T_FMT "%lu" diff --git a/test/test_reloc_match.c b/test/test_reloc_match.c index bb9d6b0..ed725cd 100644 --- a/test/test_reloc_match.c +++ b/test/test_reloc_match.c @@ -72,11 +72,11 @@ int test_reloc_match() { 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); + printf("\tlength=" SIZE_T_FMT "\n", match->length); myassert("Invalid match->length", match->length == test_solution_length[i]); - printf("\tpost_length=%lu\n", match->post_length); + printf("\tpost_length=" SIZE_T_FMT "\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); + printf("\ttotal_length=" SIZE_T_FMT "\n", match->total_length); myassert("Invalid match->total_length", match->total_length == test_solution_total_length[i]); if (match) { free(match); -- cgit