From 0d2039a0f5ca8af1486410f3d78f84c4a4c7fc15 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Fri, 18 Jun 2021 09:33:53 +0900 Subject: [PATCH] test/ipc: Add unit tests for message encoding and authentication This commit adds unit tests that ensure that the following function from the ipc module work as intended: - _ipc_encode() - _ipc_decode() - _ipc_sign() - _ipc_verify() --- test/ipc_spec.sh | 185 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 test/ipc_spec.sh diff --git a/test/ipc_spec.sh b/test/ipc_spec.sh new file mode 100644 index 0000000..7531644 --- /dev/null +++ b/test/ipc_spec.sh @@ -0,0 +1,185 @@ +#shellcheck sh=bash + +. toolbox.sh +include "ipc" + +Describe "Encoding" + It "_ipc_encode() outputs base64" + _test_encoding() { + local data + + data=$(dd if=/dev/urandom bs=1024 count=1024 2>/dev/null | + _ipc_encode) + + if ! [[ "$data" =~ ^[a-zA-Z0-9+/]+[=]*$ ]]; then + return 1 + fi + + return 0 + } + + When call _test_encoding + The status should equal 0 + End + + It "_ipc_encode() output has correct length" + _test_encoding_length() { + local data + local block_size + local block_num + local input_bytes + local input_bits + local expected_length + local actual_length + + block_size=1024 + block_num=1024 + input_bytes=$((block_size * block_num)) + input_bits=$((input_bytes * 8)) + + actual_length=$(dd if=/dev/urandom bs="$block_size" count="$block_num" 2>/dev/null | + _ipc_encode | wc -c) + + if (( input_bits % 24 > 0 )); then + # data is padded + (( input_bits += 24 - (input_bits % 24) )) + fi + expected_length=$((input_bits / 6)) + + if (( expected_length != actual_length )); then + return 1 + fi + + return 0 + } + + When call _test_encoding_length + The status should equal 0 + End + + It "_ipc_encode() output does not contain newlines" + _test_encoding_newlines() { + local lines + + lines=$(dd if=/dev/urandom bs=1024 count=1024 2>/dev/null | + _ipc_encode | wc -l) + + if (( lines != 0 )); then + return 1 + fi + + return 0 + } + + When call _test_encoding_newlines + The status should equal 0 + End + + + It "_ipc_decode() reverses _ipc_encode()" + _test_encode_decode() { + local data_before + local data_encoded + local data_after + + data_before=$(dd if=/dev/urandom bs=1024 count=1024 2>/dev/null | base64 -w 0) + data_encoded=$(_ipc_encode <<< "$data_before") + data_after=$(_ipc_decode <<< "$data_encoded") + + if [[ "$data_before" != "$data_after" ]]; then + return 1 + fi + + return 0 + } + + When call _test_encode_decode + The status should equal 0 + End +End + +Describe "Authentication" + setup() { + if ! mkdir "/tmp/test.$$"; then + return 1 + fi + + if ! chmod 700 "/tmp/test.$$"; then + rmdir "/tmp/test.$$" + return 1 + fi + + export GNUPGHOME="/tmp/test.$$" + + cat < "/tmp/test.$$/batch.gpgscript" +%no-protection +Key-Type: RSA +Key-Length: 4096 +Key-Usage: sign,auth +Subkey-Type: RSA +Subkey-Length: 4096A +Name-Real: Toolbox Test +Name-Comment: Test +Name-Email: test@m10k.eu +Expire-Date: 1d +EOF + + if ! gpg --batch --homedir "/tmp/test.$$" \ + --generate-key "/tmp/test.$$/batch.gpgscript" 2>/dev/null; then + return 1 + fi + + return 0 + } + + cleanup() { + rm -rf "/tmp/test.$$" + } + + BeforeAll 'setup' + AfterAll 'cleanup' + + It "_ipc_sign() creates a signature with correct length" + _test_ipc_sign_length() { + local data + + data=$(dd if=/dev/urandom bs=1024 count=1024 2>/dev/null | _ipc_encode) + + if ! signature=$(_ipc_sign <<< "$data"); then + return 1 + fi + + if (( ${#signature} != 756 )); then + return 1 + fi + + return 0 + } + + When call _test_ipc_sign_length + The status should equal 0 + End + + It "_ipc_verify() can verify signatures" + _test_ipc_verify() { + local data + local signature + + data=$(dd if=/dev/urandom bs=1024 count=1024 2>/dev/null | _ipc_encode) + + if ! signature=$(_ipc_sign "$data"); then + return 1 + fi + + if ! _ipc_verify "$data" "$signature"; then + return 1 + fi + + return 0 + } + + When call _test_ipc_verify + The status should equal 0 + The output should start with "gpg: " + End +End -- 2.47.3