id (5312B)
- #!/usr/bin/env atf-sh
- # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
- # SPDX-License-Identifier: MPL-2.0
- atf_test_case noargs cleanup
- noargs_body() {
- atf_check -o save:noargs.out ../cmd/id
- atf_check grep -q "uid=$(id -u)($(id -un)) gid=$(id -g)($(id -gn)) groups=" noargs.out
- }
- noargs_cleanup() {
- rm -f noargs.out
- }
- atf_test_case names cleanup
- names_body() {
- atf_check -o save:names.out ../cmd/id -n
- atf_check grep -q "uid=$(id -un) gid=$(id -gn) groups=" names.out
- }
- names_cleanup() {
- rm -f names.out
- }
- atf_test_case devfull
- devfull_body() {
- has_glibc && atf_skip "glibc ignoring write errors for puts()"
- [ "$(uname -s)" = "NetBSD" ] && atf_skip "NetBSD ignoring write errors for puts()"
- [ "$(uname -s)" = "FreeBSD" ] && atf_skip "FreeBSD ignoring write errors for puts()"
- atf_check -s exit:1 sh -c '../cmd/id >/dev/full'
- atf_check -s exit:1 sh -c '../cmd/id -n >/dev/full'
- atf_check -s exit:1 sh -c '../cmd/id -u >/dev/full'
- atf_check -s exit:1 sh -c '../cmd/id -g >/dev/full'
- }
- atf_test_case group
- group_body() {
- atf_check -o "inline:$(id -g)\n" ../cmd/id -g
- atf_check -o "inline:$(id -gr)\n" ../cmd/id -gr
- atf_check -o "inline:$(id -gn)\n" ../cmd/id -gn
- atf_check -o "inline:$(id -gnr)\n" ../cmd/id -gnr
- }
- atf_test_case user
- user_body() {
- atf_check -o "inline:$(id -u)\n" ../cmd/id -u
- atf_check -o "inline:$(id -ur)\n" ../cmd/id -ur
- atf_check -o "inline:$(id -un)\n" ../cmd/id -un
- atf_check -o "inline:$(id -unr)\n" ../cmd/id -unr
- }
- atf_test_case groups
- groups_body() {
- # sadly GNU coreutils' id(1) sorts it's grouplist
- atf_check -o not-empty ../cmd/id -G
- atf_check -o not-empty ../cmd/id -Gr
- atf_check -o not-empty ../cmd/id -Gn
- atf_check -o not-empty ../cmd/id -Gnr
- }
- # Make sure the correct list is returned for different users
- # Previously it would only return the runtime list of the current user
- atf_test_case regression_groups
- regression_groups() {
- atf_check -o "not-inline:$(../cmd/id -G root)" ../cmd/id -G nobody
- atf_check -o "not-inline:$(../cmd/id -Gr root)" ../cmd/id -Gr nobody
- atf_check -o "not-inline:$(../cmd/id -Gn root)" ../cmd/id -Gn nobody
- atf_check -o "not-inline:$(../cmd/id -Gnr root)" ../cmd/id -Gnr nobody
- }
- atf_test_case noetc
- noetc_body() {
- bwrap_args="--bind / / --bind /var/empty /etc"
- command -v "${BWRAP:-bwrap}" >/dev/null 2>/dev/null || atf_skip "${BWRAP:-bwrap} command not found"
- [ -n "${NO_BWRAP}" ] && atf_skip "'NO_BWRAP' set"
- set -f
- # shellcheck disable=SC2086
- atf_check -o "inline:$(id -u)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -u
- # shellcheck disable=SC2086
- 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
- # shellcheck disable=SC2086
- atf_check -o "inline:$(id -g)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -g
- # shellcheck disable=SC2086
- 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
- }
- atf_test_case nopasswd
- nopasswd_body() {
- bwrap_args="--bind / / --bind /dev/null /etc/passwd"
- command -v "${BWRAP:-bwrap}" >/dev/null 2>/dev/null || atf_skip "${BWRAP:-bwrap} command not found"
- [ -n "${NO_BWRAP}" ] && atf_skip "'NO_BWRAP' set"
- set -f
- # shellcheck disable=SC2086
- atf_check -o "inline:$(id -u)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -u
- # shellcheck disable=SC2086
- 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
- # shellcheck disable=SC2086
- atf_check -o "inline:$(id -g)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -g
- # shellcheck disable=SC2086
- atf_check -o "inline:$(id -gn)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -gn
- }
- atf_test_case nogroup
- nogroup_body() {
- bwrap_args="--bind / / --bind /dev/null /etc/group"
- command -v "${BWRAP:-bwrap}" >/dev/null 2>/dev/null || atf_skip "${BWRAP:-bwrap} command not found"
- [ -n "${NO_BWRAP}" ] && atf_skip "'NO_BWRAP' set"
- set -f
- # shellcheck disable=SC2086
- atf_check -o "inline:$(id -u)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -u
- # shellcheck disable=SC2086
- atf_check -o "inline:$(id -un)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -un
- # shellcheck disable=SC2086
- atf_check -o "inline:$(id -g)\n" -- "${BWRAP:-bwrap}" ${bwrap_args} ../cmd/id -g
- # shellcheck disable=SC2086
- 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
- }
- atf_test_case badarg
- badarg_body() {
- atf_check -s exit:1 -e 'inline:Usage: id [-Ggu] [-nr] [user]\n' ../cmd/id -a
- }
- atf_test_case root cleanup
- root_body() {
- atf_check -o save:root.out ../cmd/id root
- atf_check grep -q "uid=$(id -u root)($(id -un root)) gid=$(id -g root)($(id -gn root)) groups=" root.out
- }
- root_cleanup() {
- rm -f root.out
- }
- atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
- . ../test_functions.sh
- atf_add_test_case devfull
- atf_add_test_case badarg
- atf_add_test_case noargs
- atf_add_test_case names
- atf_add_test_case group
- atf_add_test_case user
- atf_add_test_case groups
- atf_add_test_case noetc
- atf_add_test_case nogroup
- atf_add_test_case nopasswd
- atf_add_test_case root
- }