Skip to content

Commit

Permalink
Make testing scripts more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
dhinakg committed Jul 8, 2024
1 parent 677290e commit a560180
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 12 deletions.
83 changes: 78 additions & 5 deletions test_extract.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,91 @@
#!/usr/bin/env bash

set -e
set -o pipefail

FAILED=0

fail() {
echo "$1"
echo ""
if [ "$CI" = "true" ]; then
FAILED=1
else
exit 1
fi
}

rm -rf tmp
mkdir tmp
mkdir -p tmp

for i in tests/*; do
i=$(basename "$i")
echo "Testing $i"
mkdir -p "tmp/$i/a" "tmp/$i/b"
./aastuff "tests/$i/encrypted.aea" "tmp/$i/a" "$(cat tests/"$i"/key.txt)"
./aastuff_standalone "tests/$i/encrypted.aea" "tmp/$i/b" "$(cat tests/"$i"/key.txt)"
diff -r "tmp/$i/a" "tmp/$i/b" && echo "Test $i passed" || echo "Test $i failed"
if [ -f "tests/$i/skip_extract" ]; then
echo "Skipping $i"
echo ""
continue
fi

TEST_DIR="tests/$i"
TMP_DIR="tmp/$i"

mkdir -p "$TMP_DIR/a" "$TMP_DIR/b"

ret=0

if [[ ! -f "$TEST_DIR/expected.txt" ]]; then
fail "Missing expected.txt"
continue
fi

if [[ ! -f "$TEST_DIR/flags.txt" ]]; then
fail "Missing flags.txt"
continue
fi

./aastuff -i "tests/$i/encrypted.aea" -o "tmp/$i/a" --key "$(cat "$TEST_DIR"/expected.txt)" || ret=$?
if [ $ret -ne 0 ]; then
fail "aastuff failed with $ret"
continue
fi

./aastuff_standalone -i "tests/$i/encrypted.aea" -o "tmp/$i/b" --key "$(cat "$TEST_DIR"/expected.txt)" || ret=$?
if [ $ret -ne 0 ]; then
fail "aastuff_standalone failed with $ret"
continue
fi

diff -r "$TMP_DIR/a" "$TMP_DIR/b" || ret=$?
if [ $ret -eq 0 ]; then
echo "Diff passed"
else
fail "Diff failed"
continue
fi

aa archive -d "tmp/$i/a" -o "tmp/$i/a.aar" -exclude-field all -include-field "$(cat tests/"$i"/flags.txt)" || ret=$?
if [ $ret -ne 0 ]; then
fail "Archive creation failed with $ret"
continue
fi

aa verify -i "tmp/$i/a.aar" -d "tmp/$i/b" || ret=$?
if [ $ret -eq 0 ]; then
echo "Verify passed"
else
fail "Verify failed"
continue
fi

echo "Test $i passed"
echo ""
done

rm -rf tmp
echo Done

if [ $FAILED -ne 0 ]; then
echo "Some tests failed"
exit 1
fi
73 changes: 66 additions & 7 deletions test_get_key.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
#!/usr/bin/env bash

set -e
set -o pipefail

abort() {
# Use aa list instead of full decryption
FAST=1
FAILED=0

fail() {
echo "$1"
exit 1
echo ""
if [ "$CI" = "true" ]; then
FAILED=1
else
exit 1
fi
}

rm -rf tmp
Expand All @@ -13,18 +23,67 @@ mkdir tmp
for i in tests/*; do
i=$(basename "$i")
echo "Testing $i"
mkdir -p "tmp/$i"
if [ -e "tests/$i/skip_get_key" ]; then
echo "Skipping $i"
echo ""
continue
fi

if [ -e "test/$i/fast_unsupported" ]; then
FAST=0
fi

TEST_DIR="tests/$i"
TMP_DIR="tmp/$i"

mkdir -p "$TMP_DIR"

ret=0

# Ensure expected key is valid first
aea decrypt -i "tests/$i/encrypted.aea" -o "tmp/decrypted" -key-value "base64:$(cat tests/"$i"/expected.txt)" || abort "Failed to decrypt with expected key"
if [ "$FAST" -eq 1 ]; then
aa list -i "$TEST_DIR/encrypted.aea" -key-value "base64:$(cat tests/"$i"/expected.txt)" || ret=$?
else
aea decrypt -i "$TEST_DIR/encrypted.aea" -o "/dev/null" -key-value "base64:$(cat tests/"$i"/expected.txt)" || ret=$?
fi

if [ $ret -ne 0 ]; then
fail "Failed to decrypt with expected key"
continue
fi

# Get the key
python3 get_key.py "tests/$i/encrypted.aea" > "tmp/$i/actual.txt" || abort "Failed to get key"
python3 get_key.py "$TEST_DIR/encrypted.aea" | tr -d '\n' >"$TMP_DIR/actual.txt" || ret=$?
if [ $ret -ne 0 ]; then
fail "Failed to get key"
continue
fi

# Ensure the key is correct
aea decrypt -i "tests/$i/encrypted.aea" -o "tmp/decrypted" -key-value "base64:$(cat tmp/"$i"/actual.txt)" || abort "Failed to decrypt with actual key"
if ! diff "tmp/$i/actual.txt" "tests/$i/expected.txt"; then
if [ "$FAST" -eq 1 ]; then
aa list -i "$TEST_DIR/encrypted.aea" -key-value "base64:$(cat tmp/"$i"/actual.txt)" || ret=$?
else
aea decrypt -i "$TEST_DIR/encrypted.aea" -o "/dev/null" -key-value "base64:$(cat tmp/"$i"/actual.txt)" || ret=$?
fi

if [ $ret -ne 0 ]; then
fail "Failed to decrypt with actual key"
continue
fi

diff "$TMP_DIR/actual.txt" "$TEST_DIR/expected.txt" || ret=$?
if [ $ret -ne 0 ]; then
echo "Warning: key does not match expected key, but decryption was successful"
fi

echo "Test $i passed"
echo ""
done

rm -rf tmp
echo Done

if [ $FAILED -ne 0 ]; then
echo "Some tests failed"
exit 1
fi

0 comments on commit a560180

Please sign in to comment.