logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git

base64 (3043B)


  1. #!/usr/bin/env atf-sh
  2. # SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. # SPDX-License-Identifier: MPL-2.0
  4. atf_test_case allbytes
  5. allbytes_body() {
  6. atf_check -o file:outputs/base64/all_bytes ../cmd/base64 inputs/all_bytes
  7. atf_check -o file:outputs/base64/all_bytes ../cmd/base64 <inputs/all_bytes
  8. atf_check -o file:outputs/base64/all_bytes ../cmd/base64 - <inputs/all_bytes
  9. }
  10. atf_test_case devnull
  11. devnull_body() {
  12. atf_check ../cmd/base64 /dev/null
  13. atf_check ../cmd/base64 </dev/null
  14. atf_check ../cmd/base64 - </dev/null
  15. }
  16. atf_test_case rfc4648
  17. rfc4648_body() {
  18. # Test vectors from RFC4648
  19. atf_check -o 'inline:' sh -c 'printf "" | ../cmd/base64'
  20. atf_check -o 'inline:Zg==\n' sh -c 'printf "f" | ../cmd/base64'
  21. atf_check -o 'inline:Zm8=\n' sh -c 'printf "fo" | ../cmd/base64'
  22. atf_check -o 'inline:Zm9v\n' sh -c 'printf "foo" | ../cmd/base64'
  23. atf_check -o 'inline:Zm9vYg==\n' sh -c 'printf "foob" | ../cmd/base64'
  24. atf_check -o 'inline:Zm9vYmE=\n' sh -c 'printf "fooba" | ../cmd/base64'
  25. atf_check -o 'inline:Zm9vYmFy\n' sh -c 'printf "foobar" | ../cmd/base64'
  26. }
  27. atf_test_case noperm cleanup
  28. noperm_body() {
  29. touch inputs/chmod_000 || atf_fail "touching chmod_000"
  30. chmod 0000 inputs/chmod_000 || atf_fail "chmod 0000 chmod_000"
  31. # shellcheck disable=SC1112
  32. atf_check -s exit:1 -e 'inline:base64: Error opening ‘inputs/chmod_000’: Permission denied\n' ../cmd/base64 inputs/chmod_000
  33. }
  34. noperm_cleanup() {
  35. chmod 0600 inputs/chmod_000 || atf_fail "chmod 0600 chmod_000"
  36. rm inputs/chmod_000 || atf_fail "rm chmod_000"
  37. }
  38. atf_test_case devfull
  39. devfull_body() {
  40. atf_check -s exit:1 -e 'inline:base64: Error writing: No space left on device\n' sh -c '../cmd/base64 inputs/all_bytes >/dev/full'
  41. atf_check -s exit:1 -e 'inline:base64: Error writing: No space left on device\n' sh -c '../cmd/base64 <inputs/all_bytes >/dev/full'
  42. atf_check -s exit:1 -e 'inline:base64: Error writing: No space left on device\n' sh -c '../cmd/base64 - <inputs/all_bytes >/dev/full'
  43. }
  44. atf_test_case readslash
  45. readslash_body() {
  46. [ "$(uname -s)" = "NetBSD" ] && atf_skip "NetBSD allows to read directories"
  47. atf_check -s exit:1 -e "inline:base64: Error reading ‘/’: Is a directory\n" ../cmd/base64 /
  48. }
  49. atf_test_case enoent
  50. enoent_body() {
  51. # shellcheck disable=SC1112
  52. 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
  53. }
  54. atf_test_case doubledash
  55. doubledash_body() {
  56. atf_check -o file:outputs/base64/all_bytes -- ../cmd/base64 -- inputs/all_bytes
  57. # shellcheck disable=SC1112
  58. atf_check -s exit:1 -e "inline:base64: Error: Unrecognised option: ‘--’\n" -o empty -- ../cmd/base64 --- inputs/all_bytes
  59. }
  60. atf_init_test_cases() {
  61. cd "$(atf_get_srcdir)" || exit 1
  62. atf_add_test_case rfc4648
  63. atf_add_test_case allbytes
  64. atf_add_test_case devnull
  65. atf_add_test_case noperm
  66. atf_add_test_case devfull
  67. atf_add_test_case readslash
  68. atf_add_test_case enoent
  69. atf_add_test_case doubledash
  70. }