Files
dshash/tests/test.sh
2025-09-02 16:05:30 +12:00

100 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
DSHASH_BIN="$PROJECT_DIR/dshash/dshash"
TEMP_DIR=$(mktemp -d)
trap "rm -rf $TEMP_DIR" EXIT
echo "Building dshash utility..."
cd "$PROJECT_DIR/dshash"
make clean > /dev/null 2>&1
make > /dev/null 2>&1
echo "Building test program..."
cd "$SCRIPT_DIR"
g++ -std=c++17 -o test_lib test_lib.cpp ../src/dshash.cpp -I../src
FAILED=0
PASSED=0
run_test() {
local test_name="$1"
local expected="$2"
local actual="$3"
if [ "$expected" = "$actual" ]; then
echo "$test_name"
PASSED=$((PASSED + 1))
else
echo "$test_name"
echo " Expected: $expected"
echo " Got: $actual"
FAILED=$((FAILED + 1))
fi
}
echo ""
echo "Running library tests..."
./test_lib
if [ $? -eq 0 ]; then
echo "✓ All library tests passed"
PASSED=$((PASSED + 1))
else
echo "✗ Library tests failed"
FAILED=$((FAILED + 1))
fi
echo ""
echo "Running utility tests..."
echo -n "abc" > "$TEMP_DIR/test1.txt"
HASH=$($DSHASH_BIN "$TEMP_DIR/test1.txt")
run_test "Hash of 'abc'" "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" "$HASH"
echo -n "" > "$TEMP_DIR/empty.txt"
HASH=$($DSHASH_BIN "$TEMP_DIR/empty.txt")
run_test "Hash of empty file" "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" "$HASH"
echo -n "The quick brown fox jumps over the lazy dog" > "$TEMP_DIR/fox.txt"
HASH=$($DSHASH_BIN "$TEMP_DIR/fox.txt")
run_test "Hash of 'The quick brown fox...'" "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592" "$HASH"
mkdir -p "$TEMP_DIR/testdir/subdir"
echo -n "file1" > "$TEMP_DIR/testdir/file1.txt"
echo -n "file2" > "$TEMP_DIR/testdir/subdir/file2.txt"
HASH=$($DSHASH_BIN "$TEMP_DIR/testdir")
run_test "Hash of directory" "$(echo -n "$HASH" | grep -E '^[a-f0-9]{64}$' > /dev/null && echo 'valid')" "valid"
echo -n "test" > "$TEMP_DIR/verbose_test.txt"
OUTPUT=$($DSHASH_BIN -v "$TEMP_DIR/verbose_test.txt" 2>&1)
if echo "$OUTPUT" | grep -q "Processing file:"; then
run_test "Verbose mode" "works" "works"
else
run_test "Verbose mode" "works" "failed"
fi
mkdir -p "$TEMP_DIR/verbose_dir"
echo -n "test" > "$TEMP_DIR/verbose_dir/file.txt"
OUTPUT=$($DSHASH_BIN -v "$TEMP_DIR/verbose_dir" 2>&1)
if echo "$OUTPUT" | grep -q "Processing:"; then
run_test "Verbose mode for directory" "works" "works"
else
run_test "Verbose mode for directory" "works" "failed"
fi
echo ""
echo "========================================="
echo "Test Results: $PASSED passed, $FAILED failed"
echo "========================================="
if [ $FAILED -eq 0 ]; then
echo "All tests passed!"
exit 0
else
echo "Some tests failed!"
exit 1
fi