From 581d0ce62c08630f315e3598c44ba7f35cabe633 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Fri, 9 Sep 2022 13:47:33 +0530 Subject: [PATCH] tests: add tests for injecting large resources Fixes: https://github.com/postmanlabs/postject/issues/12 Signed-off-by: Darshan Sen --- .circleci/config.yml | 2 +- Makefile | 8 ++++---- README.markdown | 2 +- examples/Makefile | 22 -------------------- examples/test.S | 21 ------------------- examples/test.c | 21 ------------------- examples/test.cpp | 19 ----------------- examples/test.sh | 15 -------------- test.list | 3 +++ test.sh | 24 ++++++++++++++++++++++ tests/inject_and_retrieve_data_1GB/test.c | 17 +++++++++++++++ tests/inject_and_retrieve_data_1GB/test.sh | 18 ++++++++++++++++ tests/inject_and_retrieve_data_1KB/test.c | 17 +++++++++++++++ tests/inject_and_retrieve_data_1KB/test.sh | 18 ++++++++++++++++ tests/inject_and_retrieve_data_1MB/test.c | 17 +++++++++++++++ tests/inject_and_retrieve_data_1MB/test.sh | 18 ++++++++++++++++ 16 files changed, 138 insertions(+), 104 deletions(-) delete mode 100644 examples/Makefile delete mode 100644 examples/test.S delete mode 100644 examples/test.c delete mode 100644 examples/test.cpp delete mode 100755 examples/test.sh create mode 100644 test.list create mode 100755 test.sh create mode 100644 tests/inject_and_retrieve_data_1GB/test.c create mode 100755 tests/inject_and_retrieve_data_1GB/test.sh create mode 100644 tests/inject_and_retrieve_data_1KB/test.c create mode 100755 tests/inject_and_retrieve_data_1KB/test.sh create mode 100644 tests/inject_and_retrieve_data_1MB/test.c create mode 100755 tests/inject_and_retrieve_data_1MB/test.sh diff --git a/.circleci/config.yml b/.circleci/config.yml index 7c0d791..3b00a18 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -110,7 +110,7 @@ commands: - attach_workspace: at: . - - run: make check + - run: make test ## JOBS ## diff --git a/Makefile b/Makefile index fb54a6d..ccf001b 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ include vendor/vendorpull/targets.mk include build/system.mk include build/deps.mk -all: vendor compile patch check +all: vendor compile patch test .PHONY: lief lief: dist/lief @@ -15,6 +15,6 @@ dist/lief: cd vendor/lief && python3 ./setup.py $(BUILD_OPTS) build_ext -b ../../$@ -j $(JOBS) -.PHONY: check -check: - $(MAKE) -C examples/ +.PHONY: test +test: + ./test.sh diff --git a/README.markdown b/README.markdown index a048e8f..bcb44b5 100644 --- a/README.markdown +++ b/README.markdown @@ -29,7 +29,7 @@ $ ./postject.py --macho-segment-name __ELECTRON /Users/dsanders/electron/src/out ### Testing ```sh -$ make check +$ make test ``` ## Design diff --git a/examples/Makefile b/examples/Makefile deleted file mode 100644 index 142fbfb..0000000 --- a/examples/Makefile +++ /dev/null @@ -1,22 +0,0 @@ - -all: clean patch run - -.PHONY: run -run: patch - ./test.sh - -.PHONY: patch -patch: test data.txt - ../postject.py --overwrite $< "foobar" data.txt - -test: CFLAGS=-g -test: test.o - -data.txt: - @echo "hello world @ $$(date)" > $@ - - -.PHONY: compile -clean: RM ?= rm -clean: - $(RM) test *.o data.txt || true diff --git a/examples/test.S b/examples/test.S deleted file mode 100644 index 50210f0..0000000 --- a/examples/test.S +++ /dev/null @@ -1,21 +0,0 @@ -// This is modeled after -// https://source.chromium.org/chromium/chromium/src/+/main:third_party/crashpad/crashpad/client/crashpad_info_note.S;drc=8371af273f35eb989520360435fb81a730d6a593 - - .section .note.postject.resource,"a",%note - .balign 4 -POSTJECT_NOTE: - .long name_end - name // namesz - .long desc_end - desc // descsz - .long 0x5f464f49 // type -name: - .asciz "postject" -name_end: - .balign 4 -desc: - .long rname_end - rname // rnamesz -rname: - .asciz "foobar" // Customize this to what you want -rname_end: - .incbin "foo.txt" // resource content -desc_end: - .size POSTJECT_NOTE, .-POSTJECT_NOTE diff --git a/examples/test.c b/examples/test.c deleted file mode 100644 index f7629de..0000000 --- a/examples/test.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include - -#include "../postject-api.h" - -int main() -{ - size_t size; - const void *ptr = postject_find_resource("foobar", &size, NULL); - - if (ptr && size > 0) { - char *str = (char *)malloc(size + 1); - memset(str, 0, size + 1); - strncpy(str, ptr, size); - printf("%s\n", str); - } else { - printf("Hello world\n"); - } - - return 0; -} diff --git a/examples/test.cpp b/examples/test.cpp deleted file mode 100644 index c4341df..0000000 --- a/examples/test.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include - -#include "../postject-api.h" - -int main() -{ - size_t size; - const void *ptr = postject_find_resource("foobar", &size, nullptr); - - if (ptr && size > 0) { - std::cout << std::string(static_cast(ptr), size) - << std::endl; - } else { - std::cout << "Hello world" << std::endl; - } - - return 0; -} diff --git a/examples/test.sh b/examples/test.sh deleted file mode 100755 index 5c36e96..0000000 --- a/examples/test.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh - -set -o errexit -set -o nounset - -want=$(cat ./data.txt) -have=$(./test) - -if test "$have" = "$want"; then - exit 0 -else - printf "have: \"%s\"\n" "$have" - printf "want: \"%s\"\n" "$want" - exit 1 -fi diff --git a/test.list b/test.list new file mode 100644 index 0000000..8d41981 --- /dev/null +++ b/test.list @@ -0,0 +1,3 @@ +tests/inject_and_retrieve_data_1KB +tests/inject_and_retrieve_data_1MB +tests/inject_and_retrieve_data_1GB diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..1b92155 --- /dev/null +++ b/test.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +set -o errexit +set -o nounset + +while IFS= read -r test_directory +do + echo "---- Running $test_directory" 1>&2 + TEMPORARY_DIRECTORY="$(mktemp -d)" + pwd="$PWD" + cd "$test_directory" + TEMPORARY_DIRECTORY="$TEMPORARY_DIRECTORY" ./test.sh \ + && EXIT_CODE="$?" || EXIT_CODE="$?" + cd "$pwd" + rm -rf "$TEMPORARY_DIRECTORY" + + if [ "$EXIT_CODE" = "0" ] + then + echo "\033[33;32m✓ PASS\x1b[0m $test_directory" 1>&2 + else + echo "\033[33;31m× FAIL\x1b[0m $test_directory" 1>&2 + exit "$EXIT_CODE" + fi +done < test.list diff --git a/tests/inject_and_retrieve_data_1GB/test.c b/tests/inject_and_retrieve_data_1GB/test.c new file mode 100644 index 0000000..5da181a --- /dev/null +++ b/tests/inject_and_retrieve_data_1GB/test.c @@ -0,0 +1,17 @@ +#include +#include + +#include "../../postject-api.h" + +int main() +{ + size_t size; + const void *ptr = postject_find_resource("foobar", &size, NULL); + + assert(ptr != NULL); + assert(size > 0); + + fwrite(ptr, size, 1, stdout); + + return 0; +} diff --git a/tests/inject_and_retrieve_data_1GB/test.sh b/tests/inject_and_retrieve_data_1GB/test.sh new file mode 100755 index 0000000..eaf3324 --- /dev/null +++ b/tests/inject_and_retrieve_data_1GB/test.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +set -o errexit +set -o nounset + +bin="$TEMPORARY_DIRECTORY/a.out" +cc test.c -o "$bin" + +input="$TEMPORARY_DIRECTORY/input.txt" +head -c 1073741824 /dev/urandom > "$input" + +../../postject.py --overwrite "$bin" "foobar" "$input" + +output="$TEMPORARY_DIRECTORY/output.txt" + +"$bin" > "$output" + +diff "$input" "$output" diff --git a/tests/inject_and_retrieve_data_1KB/test.c b/tests/inject_and_retrieve_data_1KB/test.c new file mode 100644 index 0000000..5da181a --- /dev/null +++ b/tests/inject_and_retrieve_data_1KB/test.c @@ -0,0 +1,17 @@ +#include +#include + +#include "../../postject-api.h" + +int main() +{ + size_t size; + const void *ptr = postject_find_resource("foobar", &size, NULL); + + assert(ptr != NULL); + assert(size > 0); + + fwrite(ptr, size, 1, stdout); + + return 0; +} diff --git a/tests/inject_and_retrieve_data_1KB/test.sh b/tests/inject_and_retrieve_data_1KB/test.sh new file mode 100755 index 0000000..b626575 --- /dev/null +++ b/tests/inject_and_retrieve_data_1KB/test.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +set -o errexit +set -o nounset + +bin="$TEMPORARY_DIRECTORY/a.out" +cc test.c -o "$bin" + +input="$TEMPORARY_DIRECTORY/input.txt" +head -c 1024 /dev/urandom > "$input" + +../../postject.py --overwrite "$bin" "foobar" "$input" + +output="$TEMPORARY_DIRECTORY/output.txt" + +"$bin" > "$output" + +diff "$input" "$output" diff --git a/tests/inject_and_retrieve_data_1MB/test.c b/tests/inject_and_retrieve_data_1MB/test.c new file mode 100644 index 0000000..5da181a --- /dev/null +++ b/tests/inject_and_retrieve_data_1MB/test.c @@ -0,0 +1,17 @@ +#include +#include + +#include "../../postject-api.h" + +int main() +{ + size_t size; + const void *ptr = postject_find_resource("foobar", &size, NULL); + + assert(ptr != NULL); + assert(size > 0); + + fwrite(ptr, size, 1, stdout); + + return 0; +} diff --git a/tests/inject_and_retrieve_data_1MB/test.sh b/tests/inject_and_retrieve_data_1MB/test.sh new file mode 100755 index 0000000..a701e5b --- /dev/null +++ b/tests/inject_and_retrieve_data_1MB/test.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +set -o errexit +set -o nounset + +bin="$TEMPORARY_DIRECTORY/a.out" +cc test.c -o "$bin" + +input="$TEMPORARY_DIRECTORY/input.txt" +head -c 1048576 /dev/urandom > "$input" + +../../postject.py --overwrite "$bin" "foobar" "$input" + +output="$TEMPORARY_DIRECTORY/output.txt" + +"$bin" > "$output" + +diff "$input" "$output"