logo

utils

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

id (5338B)


  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 noargs cleanup
  5. noargs_body() {
  6. atf_check -o save:noargs.out ../cmd/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 ../cmd/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 '../cmd/id >/dev/full'
  26. atf_check -s exit:1 sh -c '../cmd/id -n >/dev/full'
  27. atf_check -s exit:1 sh -c '../cmd/id -u >/dev/full'
  28. atf_check -s exit:1 sh -c '../cmd/id -g >/dev/full'
  29. }
  30. atf_test_case group
  31. group_body() {
  32. atf_check -o "inline:$(id -g)\n" ../cmd/id -g
  33. atf_check -o "inline:$(id -gr)\n" ../cmd/id -gr
  34. atf_check -o "inline:$(id -gn)\n" ../cmd/id -gn
  35. atf_check -o "inline:$(id -gnr)\n" ../cmd/id -gnr
  36. }
  37. atf_test_case user
  38. user_body() {
  39. atf_check -o "inline:$(id -u)\n" ../cmd/id -u
  40. atf_check -o "inline:$(id -ur)\n" ../cmd/id -ur
  41. atf_check -o "inline:$(id -un)\n" ../cmd/id -un
  42. atf_check -o "inline:$(id -unr)\n" ../cmd/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 ../cmd/id -G
  48. atf_check -o not-empty ../cmd/id -Gr
  49. atf_check -o not-empty ../cmd/id -Gn
  50. atf_check -o not-empty ../cmd/id -Gnr
  51. }
  52. # Make sure the correct list is returned for different users
  53. # Previously it would only return the runtime list of the current user
  54. atf_test_case regression_groups
  55. regression_groups() {
  56. atf_check -o "not-inline:$(../cmd/id -G root)" ../cmd/id -G nobody
  57. atf_check -o "not-inline:$(../cmd/id -Gr root)" ../cmd/id -Gr nobody
  58. atf_check -o "not-inline:$(../cmd/id -Gn root)" ../cmd/id -Gn nobody
  59. atf_check -o "not-inline:$(../cmd/id -Gnr root)" ../cmd/id -Gnr nobody
  60. }
  61. atf_test_case noetc
  62. noetc_body() {
  63. bwrap_args="--bind / / --bind /var/empty /etc"
  64. command -v "${BWRAP:-bwrap}" >/dev/null 2>/dev/null || atf_skip "${BWRAP:-bwrap} command not found"
  65. [ -n "${NO_BWRAP}" ] && atf_skip "'NO_BWRAP' set"
  66. set -f
  67. # shellcheck disable=SC2086
  68. atf_check -o "inline:$(id -u)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -u
  69. # shellcheck disable=SC2086
  70. 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} ../cmd/id -un
  71. # shellcheck disable=SC2086
  72. atf_check -o "inline:$(id -g)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -g
  73. # shellcheck disable=SC2086
  74. 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} ../cmd/id -gn
  75. }
  76. atf_test_case nopasswd
  77. nopasswd_body() {
  78. bwrap_args="--bind / / --bind /dev/null /etc/passwd"
  79. command -v "${BWRAP:-bwrap}" >/dev/null 2>/dev/null || atf_skip "${BWRAP:-bwrap} command not found"
  80. [ -n "${NO_BWRAP}" ] && atf_skip "'NO_BWRAP' set"
  81. set -f
  82. # shellcheck disable=SC2086
  83. atf_check -o "inline:$(id -u)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -u
  84. # shellcheck disable=SC2086
  85. 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} ../cmd/id -un
  86. # shellcheck disable=SC2086
  87. atf_check -o "inline:$(id -g)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -g
  88. # shellcheck disable=SC2086
  89. atf_check -o "inline:$(id -gn)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -gn
  90. }
  91. atf_test_case nogroup
  92. nogroup_body() {
  93. bwrap_args="--bind / / --bind /dev/null /etc/group"
  94. command -v "${BWRAP:-bwrap}" >/dev/null 2>/dev/null || atf_skip "${BWRAP:-bwrap} command not found"
  95. [ -n "${NO_BWRAP}" ] && atf_skip "'NO_BWRAP' set"
  96. set -f
  97. # shellcheck disable=SC2086
  98. atf_check -o "inline:$(id -u)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -u
  99. # shellcheck disable=SC2086
  100. atf_check -o "inline:$(id -un)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -un
  101. # shellcheck disable=SC2086
  102. atf_check -o "inline:$(id -g)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -g
  103. # shellcheck disable=SC2086
  104. 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} ../cmd/id -gn
  105. }
  106. atf_test_case badarg
  107. badarg_body() {
  108. atf_check -s exit:1 -e 'inline:Usage: id [-Ggu] [-nr] [user]\n' ../cmd/id -a
  109. }
  110. atf_test_case root cleanup
  111. root_body() {
  112. atf_check -o save:root.out ../cmd/id root
  113. atf_check grep -q "uid=$(id -u root)($(id -un root)) gid=$(id -g root)($(id -gn root)) groups=" root.out
  114. }
  115. root_cleanup() {
  116. rm -f root.out
  117. }
  118. atf_init_test_cases() {
  119. cd "$(atf_get_srcdir)" || exit 1
  120. . ../test_functions.sh
  121. atf_add_test_case devfull
  122. atf_add_test_case badarg
  123. atf_add_test_case noargs
  124. atf_add_test_case names
  125. atf_add_test_case group
  126. atf_add_test_case user
  127. atf_add_test_case groups
  128. atf_add_test_case noetc
  129. atf_add_test_case nogroup
  130. atf_add_test_case nopasswd
  131. atf_add_test_case root
  132. }