diff options
Diffstat (limited to 'test')
-rwxr-xr-x | test/test.sh | 77 | ||||
-rw-r--r-- | test/test.txt | 26 |
2 files changed, 103 insertions, 0 deletions
diff --git a/test/test.sh b/test/test.sh new file mode 100755 index 0000000..ddc25ab --- /dev/null +++ b/test/test.sh @@ -0,0 +1,77 @@ +#! /bin/bash + +# wgetpaste test script +# Exit code: number of mismatched downloads or 1 for general failure +# Copyright (C) 2021 xxc3nsoredxx + +# Don't assume the test is being run from the same directory as the script +TEST_DIR="$(dirname "$0")" +TEST_FILE="$TEST_DIR/test.txt" +DL_DIR="$(mktemp -q -d /tmp/wgetpaste_test.XXXXX)" +DL_COUNT=0 +DL_MISMATCH=0 + +# Test that temp directory was created +if [ ! -d "$DL_DIR" ]; then + echo "Failed to create temporary download directory: $DL_DIR" + exit 1 +fi +echo "Using download directory: $DL_DIR" + +# Post test file into each service (if possible) +# Download the resulting paste into /tmp/wgetpaste_test.XXXXX/<service>.txt +for s in $($TEST_DIR/../wgetpaste -S --completions); do + # Ignore codepad (timing out) + if [ "$s" == 'codepad' ]; then + continue + fi + + # Discard stderr output + echo -n "Posting to $s: " + URL="$($TEST_DIR/../wgetpaste -r -s "$s" "$TEST_FILE" 2>/dev/null)" + STATUS="$?" + + # Skip failed posts (eg, not authorized for GitHub or GitLab) + if [ "$STATUS" -ne 0 ]; then + echo "FAILED, skipping..." + continue + fi + echo "SUCCESS!" + + echo -n "Downloading from $s: " + if ! (wget -q "$URL" -O "$DL_DIR/$s.txt" 2>/dev/null); then + echo "FAILED, skipping..." + continue + fi + echo "SUCCESS!" + DL_COUNT=$((DL_COUNT + 1)) +done + +# Test if any files were downloaded +if [ "$DL_COUNT" -eq 0 ]; then + echo "No files downloaded!" + rm -rf "$DL_DIR" + exit 1 +fi + +# Compare downloaded files +for f in $DL_DIR/*; do + echo -n "Testing file $f: " + # Ignore missing trailing newline in downloaded file + if ! (diff -q -Z "$TEST_FILE" "$f" &>/dev/null); then + echo "FAILED!" + DL_MISMATCH=$((DL_MISMATCH + 1)) + else + echo "SUCCESS!" + fi +done + +echo "Total mismatches: $DL_MISMATCH" + +# Delete download directory if all tests succeeded +if [ "$DL_MISMATCH" -eq 0 ]; then + echo "Deleting download directory" + rm -rf "$DL_DIR" +fi + +exit "$DL_MISMATCH" diff --git a/test/test.txt b/test/test.txt new file mode 100644 index 0000000..ad1f2f0 --- /dev/null +++ b/test/test.txt @@ -0,0 +1,26 @@ +wgetpaste test data + +test common string escapes used in programming languages +#include <stdio.h> + +int main (void) { + printf("test bell\a\n"); + printf("test backspace\bE\n"); + printf("test escape\e[31m red text\e[m end red text\n"); + printf("test form feed\f"); + printf("test newline\n"); + printf("test carriage return\rA\n"); + printf("test tab\tend tab\n"); + printf("test vertical tab\vend vertical tab\n"); + printf("test backslash\\\n"); + printf("test single quote\'\n"); + printf("test double quote\"\n"); + printf("test question mark\?\n"); + printf("test octal (A) \101\n"); + printf("test octal (null)\n\0not printed"); + printf("test hex (A) \x41\n"); + printf("test unocpde < 0x1000 (acute A) \u00c1\n"); + printf("test unicode (acute A) \U000000c1\n"); + + printf("test literal tab end tab\n"); +} |