Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add tests for injecting large resources #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

4 changes: 4 additions & 0 deletions test.list
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
tests/inject_and_retrieve_data_1KB
tests/inject_and_retrieve_data_1MB
tests/inject_and_retrieve_data_1GB
tests/inject_and_retrieve_data_3GB
24 changes: 24 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions tests/inject_and_retrieve_data_1GB/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <assert.h>
#include <stdio.h>

#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;
}
18 changes: 18 additions & 0 deletions tests/inject_and_retrieve_data_1GB/test.sh
Original file line number Diff line number Diff line change
@@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not head -c 1G /dev/urandom?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@robertgzr doesn't work on macOS unfortunately

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sad


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

output="$TEMPORARY_DIRECTORY/output.txt"

"$bin" > "$output"

diff "$input" "$output"
17 changes: 17 additions & 0 deletions tests/inject_and_retrieve_data_1KB/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <assert.h>
#include <stdio.h>

#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;
}
18 changes: 18 additions & 0 deletions tests/inject_and_retrieve_data_1KB/test.sh
Original file line number Diff line number Diff line change
@@ -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"
17 changes: 17 additions & 0 deletions tests/inject_and_retrieve_data_1MB/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <assert.h>
#include <stdio.h>

#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;
}
18 changes: 18 additions & 0 deletions tests/inject_and_retrieve_data_1MB/test.sh
Original file line number Diff line number Diff line change
@@ -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"
17 changes: 17 additions & 0 deletions tests/inject_and_retrieve_data_3GB/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <assert.h>
#include <stdio.h>

#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;
}
18 changes: 18 additions & 0 deletions tests/inject_and_retrieve_data_3GB/test.sh
Original file line number Diff line number Diff line change
@@ -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 3221225472 /dev/urandom > "$input"

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

output="$TEMPORARY_DIRECTORY/output.txt"

"$bin" > "$output"

diff "$input" "$output"