commit: 146de4b710aefadd58adabba35a6c1c6783a8984
parent 4450c9b322c7b5a3857ed0f7e2ff5027edecca5d
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Mon, 8 Jul 2024 01:39:52 +0200
test-cmd/base64: Change from ATF to tap.sh
Diffstat:
3 files changed, 94 insertions(+), 132 deletions(-)
diff --git a/test-cmd/Kyuafile b/test-cmd/Kyuafile
@@ -7,7 +7,6 @@ test_suite("utils-std commands")
basedir = fs.dirname(fs.dirname(current_kyuafile()))
-- 9,$|LC_ALL=C.UTF-8 sort
-atf_test_program{name="base64", required_files=basedir.."/cmd/base64", timeout=1}
atf_test_program{name="cat", required_files=basedir.."/cmd/cat", timeout=1}
atf_test_program{name="df", required_files=basedir.."/cmd/df", timeout=1}
atf_test_program{name="id", required_files=basedir.."/cmd/id", timeout=1}
@@ -18,6 +17,7 @@ atf_test_program{name="tee", required_files=basedir.."/cmd/tee", timeout=1}
atf_test_program{name="touch", required_files=basedir.."/cmd/touch", timeout=3}
atf_test_program{name="tty", required_files=basedir.."/cmd/tty", timeout=1}
tap_test_program{name="arch.sh", required_files=basedir.."/cmd/arch", timeout=1}
+tap_test_program{name="base64.sh", required_files=basedir.."/cmd/base64", timeout=1}
tap_test_program{name="basename.sh", required_files=basedir.."/cmd/basename", timeout=1}
tap_test_program{name="chmod.sh", required_files=basedir.."/cmd/chmod", timeout=1}
tap_test_program{name="date.sh", required_files=basedir.."/cmd/date", timeout=1}
diff --git a/test-cmd/base64 b/test-cmd/base64
@@ -1,131 +0,0 @@
-#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
-atf_test_case allbytes
-allbytes_body() {
- atf_check -o file:outputs/base64/all_bytes ../cmd/base64 inputs/all_bytes
- atf_check -o file:outputs/base64/all_bytes ../cmd/base64 <inputs/all_bytes
- atf_check -o file:outputs/base64/all_bytes ../cmd/base64 - <inputs/all_bytes
-}
-
-atf_test_case devnull
-devnull_body() {
- atf_check ../cmd/base64 /dev/null
- atf_check ../cmd/base64 </dev/null
- atf_check ../cmd/base64 - </dev/null
-}
-
-# Test vectors from RFC4648
-atf_test_case rfc4648_encode
-rfc4648_encode_body() {
- atf_check -o 'inline:' sh -c 'printf "" | ../cmd/base64'
- atf_check -o 'inline:Zg==\n' sh -c 'printf "f" | ../cmd/base64'
- atf_check -o 'inline:Zm8=\n' sh -c 'printf "fo" | ../cmd/base64'
- atf_check -o 'inline:Zm9v\n' sh -c 'printf "foo" | ../cmd/base64'
- atf_check -o 'inline:Zm9vYg==\n' sh -c 'printf "foob" | ../cmd/base64'
- atf_check -o 'inline:Zm9vYmE=\n' sh -c 'printf "fooba" | ../cmd/base64'
- atf_check -o 'inline:Zm9vYmFy\n' sh -c 'printf "foobar" | ../cmd/base64'
-}
-
-atf_test_case rfc4648_decode
-rfc4648_decode_body() {
- atf_check -o 'inline:' sh -c 'printf "" | ../cmd/base64 -d'
- atf_check -o 'inline:f' sh -c 'printf "Zg==\n" | ../cmd/base64 -d'
- atf_check -o 'inline:fo' sh -c 'printf "Zm8=\n" | ../cmd/base64 -d'
- atf_check -o 'inline:foo' sh -c 'printf "Zm9v\n" | ../cmd/base64 -d'
- atf_check -o 'inline:foob' sh -c 'printf "Zm9vYg==\n" | ../cmd/base64 -d'
- atf_check -o 'inline:fooba' sh -c 'printf "Zm9vYmE=\n" | ../cmd/base64 -d'
- atf_check -o 'inline:foobar' sh -c 'printf "Zm9vYmFy\n" | ../cmd/base64 -d'
-}
-
-atf_test_case multiliner_encode
-multiliner_encode_body() {
- atf_check -o file:inputs/base64/true_w76 ../cmd/base64 outputs/base64/true
- atf_check -o file:inputs/base64/true_w76 ../cmd/base64 <outputs/base64/true
- atf_check -o file:inputs/base64/true_w76 ../cmd/base64 - <outputs/base64/true
-}
-
-atf_test_case multiliner_decode
-multiliner_decode_body() {
- atf_check -o file:outputs/base64/true ../cmd/base64 -d inputs/base64/true_w76
- atf_check -o file:outputs/base64/true ../cmd/base64 -d <inputs/base64/true_w76
- atf_check -o file:outputs/base64/true ../cmd/base64 -d - <inputs/base64/true_w76
-}
-
-atf_test_case oneliner_encode
-oneliner_encode_body() {
- atf_check -o file:inputs/base64/true_w0 ../cmd/base64 -w0 outputs/base64/true
- atf_check -o file:inputs/base64/true_w0 ../cmd/base64 -w0 <outputs/base64/true
- atf_check -o file:inputs/base64/true_w0 ../cmd/base64 -w0 - <outputs/base64/true
-}
-
-atf_test_case oneliner_decode
-oneliner_decode_body() {
- atf_check -o file:outputs/base64/true ../cmd/base64 -d inputs/base64/true_w0
- atf_check -o file:outputs/base64/true ../cmd/base64 -d <inputs/base64/true_w0
- atf_check -o file:outputs/base64/true ../cmd/base64 -d - <inputs/base64/true_w0
-}
-
-atf_test_case noperm cleanup
-noperm_body() {
- touch inputs/chmod_000 || atf_fail "touching chmod_000"
- chmod 0000 inputs/chmod_000 || atf_fail "chmod 0000 chmod_000"
- # shellcheck disable=SC1112
- atf_check -s exit:1 -e 'inline:base64: Error opening ‘inputs/chmod_000’: Permission denied\n' ../cmd/base64 inputs/chmod_000
-}
-noperm_cleanup() {
- chmod 0600 inputs/chmod_000 || atf_fail "chmod 0600 chmod_000"
- rm inputs/chmod_000 || atf_fail "rm chmod_000"
-}
-
-atf_test_case devfull
-devfull_body() {
- error='inline:base64: Error writing: No space left on device\n'
- [ "$(uname -s)" = "NetBSD" ] && error='inline:base64: Error writing: Inappropriate ioctl for device\n'
- [ "$(uname -s)" = "FreeBSD" ] && error='inline:base64: Error writing: Inappropriate ioctl for device\n'
-
- atf_check -s exit:1 -e "$error" sh -c '../cmd/base64 inputs/all_bytes >/dev/full'
- atf_check -s exit:1 -e "$error" sh -c '../cmd/base64 <inputs/all_bytes >/dev/full'
- atf_check -s exit:1 -e "$error" sh -c '../cmd/base64 - <inputs/all_bytes >/dev/full'
-}
-
-atf_test_case readslash
-readslash_body() {
- [ "$(uname -s)" = "NetBSD" ] && atf_skip "NetBSD allows to read directories"
-
- atf_check -s exit:1 -e "inline:base64: Error reading ‘/’: Is a directory\n" ../cmd/base64 /
-}
-
-atf_test_case enoent
-enoent_body() {
- # shellcheck disable=SC1112
- atf_check -s exit:1 -e 'inline:base64: Error opening ‘/var/empty/e/no/ent’: No such file or directory\n' ../cmd/base64 /var/empty/e/no/ent
-}
-
-atf_test_case doubledash
-doubledash_body() {
- atf_check -o file:outputs/base64/all_bytes -- ../cmd/base64 -- inputs/all_bytes
- # shellcheck disable=SC1112
- atf_check -s exit:1 -e "inline:base64: Error: Unrecognised option: ‘--’\n" -o empty -- ../cmd/base64 --- inputs/all_bytes
-}
-
-atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
- atf_add_test_case rfc4648_encode
- atf_add_test_case rfc4648_decode
-
- atf_add_test_case multiliner_encode
- atf_add_test_case multiliner_decode
- atf_add_test_case oneliner_encode
- atf_add_test_case oneliner_decode
-
- atf_add_test_case allbytes
- atf_add_test_case devnull
- atf_add_test_case noperm
- atf_add_test_case devfull
- # Somehow you can fopen directories…
- #atf_add_test_case readslash
- atf_add_test_case enoent
- atf_add_test_case doubledash
-}
diff --git a/test-cmd/base64.sh b/test-cmd/base64.sh
@@ -0,0 +1,93 @@
+#!/bin/sh
+# SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+# SPDX-License-Identifier: MPL-2.0
+
+WD="$(dirname "$0")"
+target="${WD}/../cmd/base64"
+plans=22
+. "$(dirname "$0")/tap.sh"
+
+# $1 -> name
+# $2 -> arguments
+# $3 -> file to compare output against
+t_file()
+{
+ exp_ret=0
+
+ count=$((count+1))
+ out="$(mktemp)"
+ "${target?}" $2 2>&1 >"$out"
+ ret="$?"
+
+ if [ "$ret" != "$exp_ret" ]; then
+ printf 'not ok %d - %s\n' "$count" "$1"
+ printf '# Expected exit code %d, got %d\n' "$exp_ret" "$ret"
+ printf "$out" | sed -e 's;^;# ;'
+ err=1
+ elif ! cmp -s "$out" "$3"; then
+ printf 'not ok %d - %s\n' "$count" "$1"
+ diff -u "$out" "$3" | sed -e 's;^;# ;'
+ err=1
+ else
+ printf 'ok %d - %s\n' "$count" "$1"
+ fi
+}
+
+t devnull '/dev/null' ''
+
+t_file all_byte "$WD/inputs/all_bytes" "$WD/outputs/base64/all_bytes"
+
+# Test vectors from RFC4648
+t --input='' 'rfc4648:' '' ''
+t --input='f' 'rfc4648:f' '' 'Zg==
+'
+t --input='fo' 'rfc4648:fo' '' 'Zm8=
+'
+t --input='foo' 'rfc4648:foo' '' 'Zm9v
+'
+t --input='foob' 'rfc4648:foob' '' 'Zm9vYg==
+'
+t --input='fooba' 'rfc4648:fooba' '' 'Zm9vYmE=
+'
+t --input='foobar' 'rfc4648:foobar' '' 'Zm9vYmFy
+'
+
+t --input='' 'rfc4648_decode:' '' ''
+t --input='Zg==\n' 'rfc4648_decode:Zg==\n' '-d' 'f'
+t --input='Zm8=\n' 'rfc4648_decode:Zm8=\n' '-d' 'fo'
+t --input='Zm9v\n' 'rfc4648_decode:Zm9v\n' '-d' 'foo'
+t --input='Zm9vYg==\n' 'rfc4648_decode:Zm9vYg==\n' '-d' 'foob'
+t --input='Zm9vYmE=\n' 'rfc4648_decode:Zm9vYmE=\n' '-d' 'fooba'
+t --input='Zm9vYmFy\n' 'rfc4648_decode:Zm9vYmFy\n' '-d' 'foobar'
+
+t_file multiliner_encode "$WD/outputs/base64/true" "$WD/inputs/base64/true_w76"
+
+t_file multiliner_decode "-d $WD/inputs/base64/true_w76" "$WD/outputs/base64/true"
+
+t_file oneliner_encode "-w0 $WD/outputs/base64/true" "$WD/inputs/base64/true_w0"
+
+t_file oneliner_decode "-d -w0 $WD/inputs/base64/true_w0" "$WD/outputs/base64/true"
+
+touch "$WD/inputs/chmod_000"
+chmod 0000 "$WD/inputs/chmod_000"
+t --exit=1 'noperm' "$WD/inputs/chmod_000" "base64: Error opening ‘$WD/inputs/chmod_000’: Permission denied
+"
+chmod 0600 "$WD/inputs/chmod_000"
+rm "$WD/inputs/chmod_000"
+
+#atf_test_case devfull
+#devfull_body() {
+# error='inline:base64: Error writing: No space left on device\n'
+# [ "$(uname -s)" = "NetBSD" ] && error='inline:base64: Error writing: Inappropriate ioctl for device\n'
+# [ "$(uname -s)" = "FreeBSD" ] && error='inline:base64: Error writing: Inappropriate ioctl for device\n'
+#
+# atf_check -s exit:1 -e "$error" sh -c '../cmd/base64 inputs/all_bytes >/dev/full'
+# atf_check -s exit:1 -e "$error" sh -c '../cmd/base64 <inputs/all_bytes >/dev/full'
+# atf_check -s exit:1 -e "$error" sh -c '../cmd/base64 - <inputs/all_bytes >/dev/full'
+#}
+
+#t --exit=1 'readslash' '/' 'base64: Error reading ‘/’: Is a directory
+#'
+
+t --exit=1 'enoent' '/var/empty/e/no/ent' 'base64: Error opening ‘/var/empty/e/no/ent’: No such file or directory
+'