logo

utils

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

id (4890B)


  1. #!/usr/bin/env atf-sh
  2. # SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. # SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
  4. atf_test_case noargs cleanup
  5. noargs_body() {
  6. atf_check -o save:noargs.out ../bin/id
  7. atf_check grep -q "uid=$(id -u)($(id -un)) gid=$(id -g)($(id -gn)) groups=" noargs.out
  8. }
  9. noargs_cleanup() {
  10. rm -f noargs.out
  11. }
  12. atf_test_case names cleanup
  13. names_body() {
  14. atf_check -o save:names.out ../bin/id -n
  15. atf_check grep -q "uid=$(id -un) gid=$(id -gn) groups=" names.out
  16. }
  17. names_cleanup() {
  18. rm -f names.out
  19. }
  20. atf_test_case devfull
  21. devfull_body() {
  22. has_glibc && atf_expect_fail "glibc ignoring write errors for puts()"
  23. [ "$(uname -s)" = "NetBSD" ] && atf_expect_fail "NetBSD ignoring write errors for puts()"
  24. [ "$(uname -s)" = "FreeBSD" ] && atf_expect_fail "FreeBSD ignoring write errors for puts()"
  25. atf_check -s exit:1 sh -c '../bin/id >/dev/full'
  26. atf_check -s exit:1 sh -c '../bin/id -n >/dev/full'
  27. atf_check -s exit:1 sh -c '../bin/id -u >/dev/full'
  28. atf_check -s exit:1 sh -c '../bin/id -g >/dev/full'
  29. }
  30. atf_test_case group
  31. group_body() {
  32. atf_check -o "inline:$(id -g)\n" ../bin/id -g
  33. atf_check -o "inline:$(id -gr)\n" ../bin/id -gr
  34. atf_check -o "inline:$(id -gn)\n" ../bin/id -gn
  35. atf_check -o "inline:$(id -gnr)\n" ../bin/id -gnr
  36. }
  37. atf_test_case user
  38. user_body() {
  39. atf_check -o "inline:$(id -u)\n" ../bin/id -u
  40. atf_check -o "inline:$(id -ur)\n" ../bin/id -ur
  41. atf_check -o "inline:$(id -un)\n" ../bin/id -un
  42. atf_check -o "inline:$(id -unr)\n" ../bin/id -unr
  43. }
  44. atf_test_case groups
  45. groups_body() {
  46. # sadly GNU coreutils' id(1) sorts it's grouplist
  47. atf_check -o not-empty ../bin/id -G
  48. atf_check -o not-empty ../bin/id -Gr
  49. atf_check -o not-empty ../bin/id -Gn
  50. atf_check -o not-empty ../bin/id -Gnr
  51. }
  52. atf_test_case noetc
  53. noetc_body() {
  54. bwrap_args="--bind / / --bind /var/empty /etc"
  55. command -v "${BWRAP:-bwrap}" >/dev/null 2>/dev/null || atf_skip "${BWRAP:-bwrap} command not found"
  56. [ -n "${NO_BWRAP}" ] && atf_skip "'NO_BWRAP' set"
  57. set -f
  58. # shellcheck disable=SC2086
  59. atf_check -o "inline:$(id -u)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../bin/id -u
  60. # shellcheck disable=SC2086
  61. atf_check -s exit:1 -e "inline:id: cannot find name for user ID $(id -u)\n" -o "inline:$(id -u)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../bin/id -un
  62. # shellcheck disable=SC2086
  63. atf_check -o "inline:$(id -g)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../bin/id -g
  64. # shellcheck disable=SC2086
  65. atf_check -s exit:1 -e "inline:id: cannot find name for group ID $(id -g)\n" -o "inline:$(id -g)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../bin/id -gn
  66. }
  67. atf_test_case nopasswd
  68. nopasswd_body() {
  69. bwrap_args="--bind / / --bind /dev/null /etc/passwd"
  70. command -v "${BWRAP:-bwrap}" >/dev/null 2>/dev/null || atf_skip "${BWRAP:-bwrap} command not found"
  71. [ -n "${NO_BWRAP}" ] && atf_skip "'NO_BWRAP' set"
  72. set -f
  73. # shellcheck disable=SC2086
  74. atf_check -o "inline:$(id -u)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../bin/id -u
  75. # shellcheck disable=SC2086
  76. atf_check -s exit:1 -e "inline:id: cannot find name for user ID $(id -u)\n" -o "inline:$(id -u)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../bin/id -un
  77. # shellcheck disable=SC2086
  78. atf_check -o "inline:$(id -g)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../bin/id -g
  79. # shellcheck disable=SC2086
  80. atf_check -o "inline:$(id -gn)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../bin/id -gn
  81. }
  82. atf_test_case nogroup
  83. nogroup_body() {
  84. bwrap_args="--bind / / --bind /dev/null /etc/group"
  85. command -v "${BWRAP:-bwrap}" >/dev/null 2>/dev/null || atf_skip "${BWRAP:-bwrap} command not found"
  86. [ -n "${NO_BWRAP}" ] && atf_skip "'NO_BWRAP' set"
  87. set -f
  88. # shellcheck disable=SC2086
  89. atf_check -o "inline:$(id -u)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../bin/id -u
  90. # shellcheck disable=SC2086
  91. atf_check -o "inline:$(id -un)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../bin/id -un
  92. # shellcheck disable=SC2086
  93. atf_check -o "inline:$(id -g)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../bin/id -g
  94. # shellcheck disable=SC2086
  95. atf_check -s exit:1 -e "inline:id: cannot find name for group ID $(id -g)\n" -o "inline:$(id -g)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../bin/id -gn
  96. }
  97. atf_test_case badarg
  98. badarg_body() {
  99. atf_check -s exit:1 -e 'inline:Usage: id [-Ggu] [-nr] [user]\n' ../bin/id -a
  100. }
  101. atf_test_case root cleanup
  102. root_body() {
  103. atf_check -o save:root.out ../bin/id root
  104. atf_check grep -q "uid=$(id -u root)($(id -un root)) gid=$(id -g root)($(id -gn root)) groups=" root.out
  105. }
  106. root_cleanup() {
  107. rm -f root.out
  108. }
  109. atf_init_test_cases() {
  110. cd "$(atf_get_srcdir)" || exit 1
  111. . ../test_functions.sh
  112. atf_add_test_case devfull
  113. atf_add_test_case badarg
  114. atf_add_test_case noargs
  115. atf_add_test_case names
  116. atf_add_test_case group
  117. atf_add_test_case user
  118. atf_add_test_case groups
  119. atf_add_test_case noetc
  120. atf_add_test_case nogroup
  121. atf_add_test_case nopasswd
  122. atf_add_test_case root
  123. }