aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/001-missing_arguments.sh10
-rw-r--r--tests/001-no_arguments.sh9
-rw-r--r--tests/002-missing_arguments.sh10
-rw-r--r--tests/002-too_many_arguments.sh9
-rw-r--r--tests/003-too_many_arguments.sh10
-rw-r--r--tests/003-unknown_option.sh9
-rw-r--r--tests/004-missing_argument_for_option.sh10
-rw-r--r--tests/004-unknown_option.sh10
-rw-r--r--tests/005-incorrect_buffer_size.sh11
-rw-r--r--tests/005-missing_argument_for_option.sh13
-rw-r--r--tests/006-incorrect_buffer_size.sh15
-rw-r--r--tests/006-incorrect_sector_size.sh11
-rw-r--r--tests/007-incorrect_sector_size.sh15
-rw-r--r--tests/008-help_option.sh11
-rw-r--r--tests/100-cannot_open_files.sh6
-rw-r--r--tests/200-input_and_reference_size_differs.sh2
-rw-r--r--tests/201-input_or_reference_size_is_not_multiple_of_sector_size.sh4
-rw-r--r--tests/300-incorrect_reference_file.sh14
-rw-r--r--tests/400-successful_backup_restore.sh4
-rw-r--r--tests/assert.sh67
20 files changed, 152 insertions, 98 deletions
diff --git a/tests/001-missing_arguments.sh b/tests/001-missing_arguments.sh
deleted file mode 100644
index 8e00bf0..0000000
--- a/tests/001-missing_arguments.sh
+++ /dev/null
@@ -1,10 +0,0 @@
-#!/bin/sh
-
-source ./assert.sh
-
-PROGRAM_EXEC="$1"
-
-assert_error "missing arguments" $PROGRAM_EXEC
-assert_error "missing arguments" $PROGRAM_EXEC arg
-
-exit 0
diff --git a/tests/001-no_arguments.sh b/tests/001-no_arguments.sh
new file mode 100644
index 0000000..150b0df
--- /dev/null
+++ b/tests/001-no_arguments.sh
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+source ./assert.sh
+
+PROGRAM_EXEC="$1"
+
+assert "Usage:" "" 1 $PROGRAM_EXEC
+
+exit 0
diff --git a/tests/002-missing_arguments.sh b/tests/002-missing_arguments.sh
new file mode 100644
index 0000000..4e1d0e3
--- /dev/null
+++ b/tests/002-missing_arguments.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+source ./assert.sh
+
+PROGRAM_EXEC="$1"
+
+assert "Usage" "missing arguments" 1 $PROGRAM_EXEC backup
+assert "Usage" "missing arguments" 1 $PROGRAM_EXEC restore
+
+exit 0
diff --git a/tests/002-too_many_arguments.sh b/tests/002-too_many_arguments.sh
deleted file mode 100644
index b6a1990..0000000
--- a/tests/002-too_many_arguments.sh
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/sh
-
-source ./assert.sh
-
-PROGRAM_EXEC="$1"
-
-assert_error "too many arguments" $PROGRAM_EXEC arg1 arg2 arg3 arg4
-
-exit 0
diff --git a/tests/003-too_many_arguments.sh b/tests/003-too_many_arguments.sh
new file mode 100644
index 0000000..fc51a42
--- /dev/null
+++ b/tests/003-too_many_arguments.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+source ./assert.sh
+
+PROGRAM_EXEC="$1"
+
+assert "Usage" "too many arguments" 1 $PROGRAM_EXEC backup arg1 arg2 arg3 arg4
+assert "Usage" "too many arguments" 1 $PROGRAM_EXEC restore arg1 arg2 arg3
+
+exit 0
diff --git a/tests/003-unknown_option.sh b/tests/003-unknown_option.sh
deleted file mode 100644
index 919f125..0000000
--- a/tests/003-unknown_option.sh
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/sh
-
-source ./assert.sh
-
-PROGRAM_EXEC="$1"
-
-assert_error "unknown option '-x'" $PROGRAM_EXEC -x ref out
-
-exit 0
diff --git a/tests/004-missing_argument_for_option.sh b/tests/004-missing_argument_for_option.sh
deleted file mode 100644
index 071ea07..0000000
--- a/tests/004-missing_argument_for_option.sh
+++ /dev/null
@@ -1,10 +0,0 @@
-#!/bin/sh
-
-source ./assert.sh
-
-PROGRAM_EXEC="$1"
-
-assert_error "missing argument for option '-b'" $PROGRAM_EXEC -b
-assert_error "missing argument for option '-s'" $PROGRAM_EXEC -s
-
-exit 0
diff --git a/tests/004-unknown_option.sh b/tests/004-unknown_option.sh
new file mode 100644
index 0000000..35ba4d6
--- /dev/null
+++ b/tests/004-unknown_option.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+source ./assert.sh
+
+PROGRAM_EXEC="$1"
+
+assert "Usage" "unknown option '-x'" 1 $PROGRAM_EXEC backup -x in ref out
+assert "Usage" "unknown option '-x'" 1 $PROGRAM_EXEC restore -x ref out
+
+exit 0
diff --git a/tests/005-incorrect_buffer_size.sh b/tests/005-incorrect_buffer_size.sh
deleted file mode 100644
index 8c88b54..0000000
--- a/tests/005-incorrect_buffer_size.sh
+++ /dev/null
@@ -1,11 +0,0 @@
-#!/bin/sh
-
-source ./assert.sh
-
-PROGRAM_EXEC="$1"
-
-assert_error "incorrect sector size" $PROGRAM_EXEC -s abc123 ref out
-assert_error "sector size cannot be 0" $PROGRAM_EXEC -s 0 ref out
-assert_error "sector size cannot larger than buffer size" $PROGRAM_EXEC -s 2 -b 1 ref out
-
-exit 0
diff --git a/tests/005-missing_argument_for_option.sh b/tests/005-missing_argument_for_option.sh
new file mode 100644
index 0000000..faccbdd
--- /dev/null
+++ b/tests/005-missing_argument_for_option.sh
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+source ./assert.sh
+
+PROGRAM_EXEC="$1"
+
+assert "Usage" "missing argument for option '-b'" 1 $PROGRAM_EXEC backup -b
+assert "Usage" "missing argument for option '-s'" 1 $PROGRAM_EXEC backup -s
+
+assert "Usage" "missing argument for option '-b'" 1 $PROGRAM_EXEC restore -b
+assert "Usage" "missing argument for option '-s'" 1 $PROGRAM_EXEC restore -s
+
+exit 0
diff --git a/tests/006-incorrect_buffer_size.sh b/tests/006-incorrect_buffer_size.sh
new file mode 100644
index 0000000..476a30e
--- /dev/null
+++ b/tests/006-incorrect_buffer_size.sh
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+source ./assert.sh
+
+PROGRAM_EXEC="$1"
+
+assert "Usage" "incorrect sector size" 1 $PROGRAM_EXEC backup -s abc123 in ref out
+assert "Usage" "sector size cannot be 0" 1 $PROGRAM_EXEC backup -s 0 in ref out
+assert "Usage" "sector size cannot larger than buffer size" 1 $PROGRAM_EXEC backup -s 2 -b 1 in ref out
+
+assert "Usage" "incorrect sector size" 1 $PROGRAM_EXEC restore -s abc123 in ref out
+assert "Usage" "sector size cannot be 0" 1 $PROGRAM_EXEC restore -s 0 in ref out
+assert "Usage" "sector size cannot larger than buffer size" 1 $PROGRAM_EXEC restore -s 2 -b 1 in ref out
+
+exit 0
diff --git a/tests/006-incorrect_sector_size.sh b/tests/006-incorrect_sector_size.sh
deleted file mode 100644
index 484c09e..0000000
--- a/tests/006-incorrect_sector_size.sh
+++ /dev/null
@@ -1,11 +0,0 @@
-#!/bin/sh
-
-source ./assert.sh
-
-PROGRAM_EXEC="$1"
-
-assert_error "incorrect buffer size" $PROGRAM_EXEC -b abc123 ref out
-assert_error "buffer size cannot be 0" $PROGRAM_EXEC -b 0 ref out
-assert_error "buffer size is not multiple of sector size" $PROGRAM_EXEC -b 3 -s 2 ref out
-
-exit 0
diff --git a/tests/007-incorrect_sector_size.sh b/tests/007-incorrect_sector_size.sh
new file mode 100644
index 0000000..3a170c4
--- /dev/null
+++ b/tests/007-incorrect_sector_size.sh
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+source ./assert.sh
+
+PROGRAM_EXEC="$1"
+
+assert "Usage" "incorrect buffer size" 1 $PROGRAM_EXEC backup -b abc123 in ref out
+assert "Usage" "buffer size cannot be 0" 1 $PROGRAM_EXEC backup -b 0 in ref out
+assert "Usage" "buffer size is not multiple of sector size" 1 $PROGRAM_EXEC backup -b 3 -s 2 in ref out
+
+assert "Usage" "incorrect buffer size" 1 $PROGRAM_EXEC restore -b abc123 in ref out
+assert "Usage" "buffer size cannot be 0" 1 $PROGRAM_EXEC restore -b 0 in ref out
+assert "Usage" "buffer size is not multiple of sector size" 1 $PROGRAM_EXEC restore -b 3 -s 2 in ref out
+
+exit 0
diff --git a/tests/008-help_option.sh b/tests/008-help_option.sh
new file mode 100644
index 0000000..d874780
--- /dev/null
+++ b/tests/008-help_option.sh
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+source ./assert.sh
+
+PROGRAM_EXEC="$1"
+
+assert "Usage" "" 0 $PROGRAM_EXEC help
+assert "Usage" "" 0 $PROGRAM_EXEC backup -h
+assert "Usage" "" 0 $PROGRAM_EXEC restore -h
+
+exit 0
diff --git a/tests/100-cannot_open_files.sh b/tests/100-cannot_open_files.sh
index 4b49acd..07eb574 100644
--- a/tests/100-cannot_open_files.sh
+++ b/tests/100-cannot_open_files.sh
@@ -6,18 +6,18 @@ PROGRAM_EXEC="$1"
rm -f input ref out
touch ref out
-assert_error "cannot open input file" $PROGRAM_EXEC input ref out
+assert "" "cannot open input file" 1 $PROGRAM_EXEC backup input ref out
rm -f input ref out
touch input out
-assert_error "cannot open reference file" $PROGRAM_EXEC input ref out
+assert "" "cannot open reference file" 1 $PROGRAM_EXEC backup input ref out
rm -f input ref out
rmdir outdir 2>/dev/null
touch input ref
mkdir outdir
chmod -w outdir
-assert_error "cannot open output file" $PROGRAM_EXEC input ref outdir/out
+assert "" "cannot open output file" 1 $PROGRAM_EXEC backup input ref outdir/out
rm -f input ref out
rmdir outdir
diff --git a/tests/200-input_and_reference_size_differs.sh b/tests/200-input_and_reference_size_differs.sh
index bf6c49f..50e7ff6 100644
--- a/tests/200-input_and_reference_size_differs.sh
+++ b/tests/200-input_and_reference_size_differs.sh
@@ -8,7 +8,7 @@ rm -f input ref
dd if=/dev/zero of=input bs=500 count=1 1>/dev/null 2>&1
dd if=/dev/zero of=ref bs=501 count=1 1>/dev/null 2>&1
-assert_error "input file and reference file differ in size" $PROGRAM_EXEC input ref out
+assert "" "input file and reference file differ in size" 1 $PROGRAM_EXEC backup input ref out
rm -f input ref out
diff --git a/tests/201-input_or_reference_size_is_not_multiple_of_sector_size.sh b/tests/201-input_or_reference_size_is_not_multiple_of_sector_size.sh
index 055df09..0cb8207 100644
--- a/tests/201-input_or_reference_size_is_not_multiple_of_sector_size.sh
+++ b/tests/201-input_or_reference_size_is_not_multiple_of_sector_size.sh
@@ -8,8 +8,8 @@ rm -f input ref
dd if=/dev/zero of=input bs=513 count=1 1>/dev/null 2>&1
dd if=/dev/zero of=ref bs=513 count=1 1>/dev/null 2>&1
-assert_error "size of input file and reference file is not multiple of [0-9]" \
- $PROGRAM_EXEC -s 512 input ref out
+assert "" "size of input file and reference file is not multiple of [0-9]" \
+ 1 $PROGRAM_EXEC backup -s 512 input ref out
rm -f input ref out
diff --git a/tests/300-incorrect_reference_file.sh b/tests/300-incorrect_reference_file.sh
index b3de8c0..5e03c72 100644
--- a/tests/300-incorrect_reference_file.sh
+++ b/tests/300-incorrect_reference_file.sh
@@ -6,11 +6,11 @@ PROGRAM_EXEC="$1"
rm -f ref out
touch ref out
-assert_error "reference file is empty" $PROGRAM_EXEC ref out
+assert "" "input file is empty" 1 $PROGRAM_EXEC restore ref out
dd if=/dev/zero of=ref bs=513 count=1 1>/dev/null 2>&1
-assert_error "reference file has size that cannot contain valid diff data" \
- $PROGRAM_EXEC -s 512 ref out
+assert "" "input file has size that cannot contain valid diff data" \
+ 1 $PROGRAM_EXEC restore -s 512 ref out
rm -f ref out
dd if=/dev/zero of=out bs=512 count=2 1>/dev/null 2>&1
@@ -20,8 +20,8 @@ dd if=/dev/zero of=ref bs=$(( 512 + 8 )) count=2 1>/dev/null 2>&1
printf '\x02' | dd of=ref bs=1 count=1 seek=0 conv=notrunc 1>/dev/null 2>&1
# The second offset will be 1
printf '\x01' | dd of=ref bs=1 count=1 seek=520 conv=notrunc 1>/dev/null 2>&1
-assert_error "a sector offset points behind the previous offset" \
- $PROGRAM_EXEC -s 512 ref out
+assert "" "a sector offset points behind the previous offset" \
+ 1 $PROGRAM_EXEC restore -s 512 ref out
rm -f ref out
dd if=/dev/zero of=out bs=512 count=1 1>/dev/null 2>&1
@@ -29,8 +29,8 @@ dd if=/dev/zero of=out bs=512 count=1 1>/dev/null 2>&1
dd if=/dev/zero of=ref bs=$(( 512 + 8 )) count=2 1>/dev/null 2>&1
# The first offset will be 1
printf '\x01' | dd of=ref bs=1 count=1 seek=0 conv=notrunc 1>/dev/null 2>&1
-assert_error "a sector offset points past the end of the output file" \
- $PROGRAM_EXEC -s 512 ref out
+assert "" "a sector offset points past the end of the output file" \
+ 1 $PROGRAM_EXEC restore -s 512 ref out
rm -f ref out
diff --git a/tests/400-successful_backup_restore.sh b/tests/400-successful_backup_restore.sh
index ce5949a..517a458 100644
--- a/tests/400-successful_backup_restore.sh
+++ b/tests/400-successful_backup_restore.sh
@@ -29,7 +29,7 @@ printf '\xFF' | dd of=input bs=1 count=1 seek=$(( (512 * 3) - 1 )) conv=notrunc
# The fourth sector will have the middle byte changed
printf '\xFF' | dd of=input bs=1 count=1 seek=$(( (512 * 4) - (512 / 2) )) conv=notrunc 1>/dev/null 2>&1
-assert_success $PROGRAM_EXEC -s 512 input ref out
+assert "" "" 0 $PROGRAM_EXEC backup -s 512 input ref out
if ! files_are_the_same out 400-expected_backup_output.bin; then
echo "assert: Backup output file differs from the expected one"
@@ -44,7 +44,7 @@ if ! files_are_the_same ref input; then
exit 1
fi
-assert_success $PROGRAM_EXEC -s 512 out input
+assert "" "" 0 $PROGRAM_EXEC restore -s 512 out input
if ! files_are_the_same input backedup_input; then
echo "assert: Cannot restore the backup"
diff --git a/tests/assert.sh b/tests/assert.sh
index 73b5dd4..cae9924 100644
--- a/tests/assert.sh
+++ b/tests/assert.sh
@@ -1,34 +1,55 @@
-function assert_error()
+test_error=0
+
+function print_assert_out_error()
{
- expected_stderr="$1"
+ echo "assert: $1 does not contain expected string"
+ echo " actual: $2"
+ echo " expected: $3"
+}
- shift 1
- actual_stderr="$("$@" 2>&1 1>/dev/null)"
- retval=$?
- is_stderr_expected="$(echo "$actual_stderr" | grep -i "$expected_stderr")"
+function assert_retval()
+{
+ if [ $1 -ne $2 ]; then
+ echo "assert: Return value is $1, expected $2"
+ test_error=1
+ fi
+}
- if [ $retval -eq 0 ]; then
- echo "assert_error: Return value is $retval, expected != 0"
- exit 1
- elif [ -z "$is_stderr_expected" ]; then
- echo "assert_error: stderr does not contain expected string"
- echo " actual: $actual_stderr"
- echo " expected: $expected_stderr"
- exit 1
+function assert_out()
+{
+ if [ -z "$2" -a -z "$3" ]; then
+ return
+ elif [ -z "$2" -a -n "$3" ]; then
+ print_assert_out_error $1 "$2" "$3"
+ elif [ -n "$2" -a -z "$3" ]; then
+ print_assert_out_error $1 "$2" "$3"
+ else
+ is_stderr_expected="$(echo "$2" | grep -i "$3")"
+ if [ -z "$is_stderr_expected" ]; then
+ print_assert_out_error $1 "$2" "$3"
+ fi
fi
}
-function assert_success()
+function assert()
{
- actual_stderr="$("$@" 2>&1 1>/dev/null)"
- retval=$?
+ expected_stdout="$1"
+ expected_stderr="$2"
+ expected_retval="$3"
+ shift 3
- if [ $retval -ne 0 ]; then
- echo "assert_error: Return value is $retval, expected 0"
- exit 1
- elif [ -n "$actual_stderr" ]; then
- echo "assert_error: stderr is not empty"
- echo " actual: $actual_stderr"
+ "$@" 1>actual_stdout 2>actual_stderr
+ actual_retval=$?
+ actual_stdout="$(cat actual_stdout)"
+ actual_stderr="$(cat actual_stderr)"
+ rm actual_stdout actual_stderr
+
+ test_error=0
+ assert_retval $actual_retval $expected_retval
+ assert_out stdout "$actual_stdout" "$expected_stdout"
+ assert_out stderr "$actual_stderr" "$expected_stderr"
+
+ if [ $test_error -ne 0 ]; then
exit 1
fi
}