logo

utils-std

Collection of commonly available Unix tools

base64 (5217B)


  1. #!/usr/bin/env atf-sh
  2. # SPDX-FileCopyrightText: 2017 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. # Test vectors from RFC4648
  17. atf_test_case rfc4648_encode
  18. rfc4648_encode_body() {
  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 rfc4648_decode
  28. rfc4648_decode_body() {
  29. atf_check -o 'inline:' sh -c 'printf "" | ../cmd/base64 -d'
  30. atf_check -o 'inline:f' sh -c 'printf "Zg==\n" | ../cmd/base64 -d'
  31. atf_check -o 'inline:fo' sh -c 'printf "Zm8=\n" | ../cmd/base64 -d'
  32. atf_check -o 'inline:foo' sh -c 'printf "Zm9v\n" | ../cmd/base64 -d'
  33. atf_check -o 'inline:foob' sh -c 'printf "Zm9vYg==\n" | ../cmd/base64 -d'
  34. atf_check -o 'inline:fooba' sh -c 'printf "Zm9vYmE=\n" | ../cmd/base64 -d'
  35. atf_check -o 'inline:foobar' sh -c 'printf "Zm9vYmFy\n" | ../cmd/base64 -d'
  36. }
  37. atf_test_case multiliner_encode
  38. multiliner_encode_body() {
  39. atf_check -o file:inputs/base64/true_w76 ../cmd/base64 outputs/base64/true
  40. atf_check -o file:inputs/base64/true_w76 ../cmd/base64 <outputs/base64/true
  41. atf_check -o file:inputs/base64/true_w76 ../cmd/base64 - <outputs/base64/true
  42. }
  43. atf_test_case multiliner_decode
  44. multiliner_decode_body() {
  45. atf_check -o file:outputs/base64/true ../cmd/base64 -d inputs/base64/true_w76
  46. atf_check -o file:outputs/base64/true ../cmd/base64 -d <inputs/base64/true_w76
  47. atf_check -o file:outputs/base64/true ../cmd/base64 -d - <inputs/base64/true_w76
  48. }
  49. atf_test_case oneliner_encode
  50. oneliner_encode_body() {
  51. atf_check -o file:inputs/base64/true_w0 ../cmd/base64 -w0 outputs/base64/true
  52. atf_check -o file:inputs/base64/true_w0 ../cmd/base64 -w0 <outputs/base64/true
  53. atf_check -o file:inputs/base64/true_w0 ../cmd/base64 -w0 - <outputs/base64/true
  54. }
  55. atf_test_case oneliner_decode
  56. oneliner_decode_body() {
  57. atf_check -o file:outputs/base64/true ../cmd/base64 -d inputs/base64/true_w0
  58. atf_check -o file:outputs/base64/true ../cmd/base64 -d <inputs/base64/true_w0
  59. atf_check -o file:outputs/base64/true ../cmd/base64 -d - <inputs/base64/true_w0
  60. }
  61. atf_test_case noperm cleanup
  62. noperm_body() {
  63. touch inputs/chmod_000 || atf_fail "touching chmod_000"
  64. chmod 0000 inputs/chmod_000 || atf_fail "chmod 0000 chmod_000"
  65. # shellcheck disable=SC1112
  66. atf_check -s exit:1 -e 'inline:base64: Error opening ‘inputs/chmod_000’: Permission denied\n' ../cmd/base64 inputs/chmod_000
  67. }
  68. noperm_cleanup() {
  69. chmod 0600 inputs/chmod_000 || atf_fail "chmod 0600 chmod_000"
  70. rm inputs/chmod_000 || atf_fail "rm chmod_000"
  71. }
  72. atf_test_case devfull
  73. devfull_body() {
  74. error='inline:base64: Error writing: No space left on device\n'
  75. [ "$(uname -s)" = "NetBSD" ] && error='inline:base64: Error writing: Inappropriate ioctl for device\n'
  76. [ "$(uname -s)" = "FreeBSD" ] && error='inline:base64: Error writing: Inappropriate ioctl for device\n'
  77. atf_check -s exit:1 -e "$error" sh -c '../cmd/base64 inputs/all_bytes >/dev/full'
  78. atf_check -s exit:1 -e "$error" sh -c '../cmd/base64 <inputs/all_bytes >/dev/full'
  79. atf_check -s exit:1 -e "$error" sh -c '../cmd/base64 - <inputs/all_bytes >/dev/full'
  80. }
  81. atf_test_case readslash
  82. readslash_body() {
  83. [ "$(uname -s)" = "NetBSD" ] && atf_skip "NetBSD allows to read directories"
  84. atf_check -s exit:1 -e "inline:base64: Error reading ‘/’: Is a directory\n" ../cmd/base64 /
  85. }
  86. atf_test_case enoent
  87. enoent_body() {
  88. # shellcheck disable=SC1112
  89. 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
  90. }
  91. atf_test_case doubledash
  92. doubledash_body() {
  93. atf_check -o file:outputs/base64/all_bytes -- ../cmd/base64 -- inputs/all_bytes
  94. # shellcheck disable=SC1112
  95. atf_check -s exit:1 -e "inline:base64: Error: Unrecognised option: ‘--’\n" -o empty -- ../cmd/base64 --- inputs/all_bytes
  96. }
  97. atf_init_test_cases() {
  98. cd "$(atf_get_srcdir)" || exit 1
  99. atf_add_test_case rfc4648_encode
  100. atf_add_test_case rfc4648_decode
  101. atf_add_test_case multiliner_encode
  102. atf_add_test_case multiliner_decode
  103. atf_add_test_case oneliner_encode
  104. atf_add_test_case oneliner_decode
  105. atf_add_test_case allbytes
  106. atf_add_test_case devnull
  107. atf_add_test_case noperm
  108. atf_add_test_case devfull
  109. # Somehow you can fopen directories…
  110. #atf_add_test_case readslash
  111. atf_add_test_case enoent
  112. atf_add_test_case doubledash
  113. }