Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
RaisinTen committed Sep 9, 2022
1 parent 90a8f03 commit 71e8008
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ commands:
- attach_workspace:
at: .

- run: make check
- run: make test

## JOBS ##

Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ $ ./postject.py --macho-segment-name __ELECTRON /Users/dsanders/electron/src/out
### Testing

```sh
$ make check
$ make test
```

## Design
Expand Down
22 changes: 0 additions & 22 deletions examples/Makefile

This file was deleted.

21 changes: 0 additions & 21 deletions examples/test.S

This file was deleted.

21 changes: 0 additions & 21 deletions examples/test.c

This file was deleted.

19 changes: 0 additions & 19 deletions examples/test.cpp

This file was deleted.

15 changes: 0 additions & 15 deletions examples/test.sh

This file was deleted.

27 changes: 27 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh

set -o errexit
set -o nounset

for test_directory in tests/*
do
if [ -d "$test_directory" ]
then
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
fi
done
21 changes: 21 additions & 0 deletions tests/inject_and_retrieve_data_1000b/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>
#include <string.h>

#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;
}
23 changes: 23 additions & 0 deletions tests/inject_and_retrieve_data_1000b/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh

set -o errexit
set -o nounset

bin="$TEMPORARY_DIRECTORY/a.out"
clang test.c -o "$bin"

data="$(head -c 1000 /dev/urandom)"
file="$TEMPORARY_DIRECTORY/data.txt"
echo "$data" > "$file"

../../postject.py --overwrite "$bin" "foobar" "$file"

want="$data"
have="$("$bin")"

if [ "$have" != "$want" ]
then
echo "have: \""$have"\"" 1>&2
echo "want: \""$want"\"" 1>&2
exit 1
fi
21 changes: 21 additions & 0 deletions tests/inject_and_retrieve_data_basic/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>
#include <string.h>

#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;
}
23 changes: 23 additions & 0 deletions tests/inject_and_retrieve_data_basic/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh

set -o errexit
set -o nounset

bin="$TEMPORARY_DIRECTORY/a.out"
clang test.c -o "$bin"

data="hello world @ $(date)"
file="$TEMPORARY_DIRECTORY/data.txt"
echo "$data" > "$file"

../../postject.py --overwrite "$bin" "foobar" "$file"

want="$data"
have="$("$bin")"

if [ "$have" != "$want" ]
then
echo "have: \""$have"\"" 1>&2
echo "want: \""$want"\"" 1>&2
exit 1
fi

0 comments on commit 71e8008

Please sign in to comment.