aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@users.noreply.github.com>2019-11-22 22:54:39 -0500
committerGitHub <noreply@github.com>2019-11-22 22:54:39 -0500
commit46ab9c276afee2fb03304aa53089c6c700c7af6e (patch)
tree59ea6b7a8217c17c0c1e915616f6615cc1813d9c
parent46a3e5a6837929d16670ed78e549882f34800ca4 (diff)
parent2ae80482cd88fb9786d3ff54777b8a462641f29f (diff)
downloadreloc-46ab9c276afee2fb03304aa53089c6c700c7af6e.tar.gz
Merge pull request #3 from jhunkeler/improve-testing
Improve testing
-rw-r--r--.github/workflows/ci.yml2
-rw-r--r--CMakeLists.txt9
-rw-r--r--reloc.h2
-rw-r--r--test/CMakeLists.txt34
-rw-r--r--test/test_data.binbin0 -> 580 bytes
-rw-r--r--test/test_reloc_match.c6
-rw-r--r--test/test_reloc_read.c4
-rw-r--r--test/test_reloc_write.c28
8 files changed, 69 insertions, 16 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..e24cc1d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,13 +2,16 @@ 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")
+ # 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()
+include(CTest)
add_subdirectory(test)
configure_file(version.h.in version.h)
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/CMakeLists.txt b/test/CMakeLists.txt
index 6b79d98..9dab686 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,12 +1,34 @@
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_reloc_test(test_reloc_write test_reloc_write.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_data.bin b/test/test_data.bin
new file mode 100644
index 0000000..a670304
--- /dev/null
+++ b/test/test_data.bin
Binary files differ
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);
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;
}
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 <unistd.h> // 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();
+}