Add tests for id(1), groups(1) and whoami(1). I'm basically doing this to

check for the command's syntax, but while doing so, I'm adding tests for
all (or almost) possible executions of these commands.

I'm not too happy with the "libfake" directory because there must be some
simpler way to do this, but it will do the trick for now hopefully.
This commit is contained in:
jmmv 2007-11-16 18:06:46 +00:00
parent 777dd06a99
commit 42112a274c
7 changed files with 666 additions and 0 deletions

5
tests/util/id/Atffile Normal file
View File

@ -0,0 +1,5 @@
Content-Type: application/X-atf-atffile; version="1"
prop: test-suite = "NetBSD"
tp-glob: t_*

12
tests/util/id/Makefile Normal file
View File

@ -0,0 +1,12 @@
# $NetBSD: Makefile,v 1.1 2007/11/16 18:06:46 jmmv Exp $
.include <bsd.own.mk>
SUBDIR= libfake
TESTS_SH= t_groups
TESTS_SH+= t_id
TESTS_SH+= t_whoami
.include <bsd.test.mk>
.include <bsd.subdir.mk>

View File

@ -0,0 +1,22 @@
# $NetBSD: Makefile,v 1.1 2007/11/16 18:06:47 jmmv Exp $
.include <bsd.own.mk>
# XXX This installs too much "garbage" in the destination directory, such as
# the libfake_pic.a and libfake.so files. We only need a shared object that
# we can later LD_PRELOAD, but I don't know how to do this from our current
# bsd.lib.mk (or, for that matter, any other .mk file).
LIB= fake
SRCS= pwgr.c
MKLINKLIB= no
MKSTATICLIB= no
MKPIC= yes
MKPROFILE= no
SHLIB_MAJOR= 0
SHLIB_MINOR= 0
LIBDIR= /usr/tests/util/id
.include <bsd.lib.mk>
.include <bsd.subdir.mk>

View File

@ -0,0 +1,193 @@
/* $NetBSD: pwgr.c,v 1.1 2007/11/16 18:06:47 jmmv Exp $ */
/*
* Copyright (c) 2007 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This file implements replacements for all user/group-related functions
* called by id(1). It provides fake but deterministic user and group
* information. The details are as such:
* User root, uid 0, primary group 0 (wheel).
* User test, uid 100, primary group 100 (users), secondary group 0 (wheel).
*/
#include <sys/types.h>
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
char Login[16];
struct group GrEntry;
struct passwd PwEntry;
gid_t
getgid(void)
{
return 100;
}
gid_t
getegid(void)
{
if (getenv("LIBFAKE_EGID_ROOT") != NULL)
return 0;
else
return 100;
}
uid_t
getuid(void)
{
return 100;
}
uid_t
geteuid(void)
{
if (getenv("LIBFAKE_EUID_ROOT") != NULL)
return 0;
else
return 100;
}
char *
getlogin(void)
{
strcpy(Login, "test");
return Login;
}
struct group *
getgrgid(gid_t gid)
{
struct group *g = &GrEntry;
memset(g, 0, sizeof(*g));
if (gid == 0) {
g->gr_name = "wheel";
g->gr_gid = 0;
} else if (gid == 100) {
g->gr_name = "users";
g->gr_gid = 100;
} else
g = NULL;
return g;
}
int
getgrouplist(const char *name, int basegid, int *groups, int *ngroups)
{
int cnt, ret;
if (name == "root") {
if (*ngroups >= 1) {
groups[0] = basegid;
cnt = 1;
}
ret = (*ngroups >= cnt) ? 0 : -1;
*ngroups = cnt;
} else if (name == "test") {
if (*ngroups >= 1) {
groups[0] = basegid;
cnt = 1;
}
if (*ngroups >= 2) {
groups[1] = 0;
cnt = 2;
}
ret = (*ngroups >= cnt) ? 0 : -1;
*ngroups = cnt;
} else
ret = -1;
return ret;
}
int
getgroups(int gidsetlen, gid_t *gidset)
{
if (gidsetlen < 2) {
errno = EINVAL;
return -1;
}
gidset[0] = 100;
gidset[1] = 0;
return 2;
}
struct passwd *
getpwnam(const char *login)
{
struct passwd *p = &PwEntry;
memset(p, 0, sizeof(*p));
if (strcmp(login, "root") == 0) {
p->pw_name = "root";
p->pw_uid = 0;
p->pw_gid = 0;
} else if (strcmp(login, "test") == 0) {
p->pw_name = "test";
p->pw_uid = 100;
p->pw_gid = 100;
} else
p = NULL;
return p;
}
struct passwd *
getpwuid(uid_t uid)
{
struct passwd *p = &PwEntry;
memset(p, 0, sizeof(*p));
if (uid == 0) {
p->pw_name = "root";
p->pw_uid = 0;
p->pw_gid = 0;
} else if (uid == 100) {
p->pw_name = "test";
p->pw_uid = 100;
p->pw_gid = 100;
} else
p = NULL;
return p;
}

69
tests/util/id/t_groups.sh Normal file
View File

@ -0,0 +1,69 @@
# $NetBSD: t_groups.sh,v 1.1 2007/11/16 18:06:46 jmmv Exp $
#
# Copyright (c) 2007 The NetBSD Foundation, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
# must display the following acknowledgement:
# This product includes software developed by the NetBSD
# Foundation, Inc. and its contributors.
# 4. Neither the name of The NetBSD Foundation nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
run_groups() {
LD_PRELOAD=$(atf_get_srcdir)/libfake.so.0 groups "${@}"
}
atf_test_case correct
correct_head() {
atf_set "descr" "Checks that correct queries work"
}
correct_body() {
echo "users wheel" >expout
atf_check "run_groups" 0 expout null
atf_check "run_groups 100" 0 expout null
atf_check "run_groups test" 0 expout null
echo "wheel" >expout
atf_check "run_groups 0" 0 expout null
atf_check "run_groups root" 0 expout null
}
atf_test_case syntax
syntax_head() {
atf_set "descr" "Checks the command's syntax"
}
syntax_body() {
# Give an invalid flag but which is allowed by id (with which
# groups shares code) when using the -Gn options.
atf_check "run_groups -r" 1 null stderr
atf_check "grep '^usage:' stderr" 0 ignore null
}
atf_init_test_cases()
{
atf_add_test_case correct
atf_add_test_case syntax
}

292
tests/util/id/t_id.sh Normal file
View File

@ -0,0 +1,292 @@
# $NetBSD: t_id.sh,v 1.1 2007/11/16 18:06:46 jmmv Exp $
#
# Copyright (c) 2007 The NetBSD Foundation, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
# must display the following acknowledgement:
# This product includes software developed by the NetBSD
# Foundation, Inc. and its contributors.
# 4. Neither the name of The NetBSD Foundation nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
run_id() {
LD_PRELOAD=$(atf_get_srcdir)/libfake.so.0 id "${@}"
}
atf_test_case default
default_head() {
atf_set "descr" "Checks that the output without options is correct"
}
default_body() {
echo "uid=100(test) gid=100(users) groups=100(users),0(wheel)" >expout
atf_check "run_id" 0 expout null
atf_check "run_id 100" 0 expout null
atf_check "run_id test" 0 expout null
echo "uid=0(root) gid=0(wheel) groups=0(wheel)" >expout
atf_check "run_id 0" 0 expout null
atf_check "run_id root" 0 expout null
export LIBFAKE_EGID_ROOT=1 LIBFAKE_EUID_ROOT=1
echo "uid=100(test) gid=100(users) euid=0(root) egid=0(wheel) groups=100(users),0(wheel)" >expout
atf_check "run_id" 0 expout null
unset LIBFAKE_EGID_ROOT LIBFAKE_EUID_ROOT
echo 'id: nonexistent: No such user' >experr
atf_check "run_id nonexistent" 1 null experr
atf_check "run_id root nonexistent" 1 null stderr
atf_check "grep ^usage: stderr" 0 ignore null
}
atf_test_case primaries
primaries_head() {
atf_set "descr" "Checks that giving multiple primaries" \
"simultaneously provides an error"
}
primaries_body() {
for p1 in -G -g -p -u; do
for p2 in -G -g -p -u; do
if [ ${p1} != ${p2} ]; then
atf_check "run_id ${p1} ${p2}" 1 null stderr
atf_check "grep ^usage: stderr" 0 ignore null
fi
done
done
}
atf_test_case Gflag
Gflag_head() {
atf_set "descr" "Checks that the -G primary flag works"
}
Gflag_body() {
echo "100 0" >expout
atf_check "run_id -G" 0 expout null
atf_check "run_id -G 100" 0 expout null
atf_check "run_id -G test" 0 expout null
echo "users wheel" >expout
atf_check "run_id -G -n" 0 expout null
atf_check "run_id -G -n 100" 0 expout null
atf_check "run_id -G -n test" 0 expout null
echo "0" >expout
atf_check "run_id -G 0" 0 expout null
atf_check "run_id -G root" 0 expout null
echo "wheel" >expout
atf_check "run_id -G -n 0" 0 expout null
atf_check "run_id -G -n root" 0 expout null
echo 'id: nonexistent: No such user' >experr
atf_check "run_id -G nonexistent" 1 null experr
atf_check "run_id -G root nonexistent" 1 null stderr
atf_check "grep ^usage: stderr" 0 ignore null
}
atf_test_case gflag
gflag_head() {
atf_set "descr" "Checks that the -g primary flag works"
}
gflag_body() {
echo "100" >expout
atf_check "run_id -g" 0 expout null
atf_check "run_id -g 100" 0 expout null
atf_check "run_id -g test" 0 expout null
echo "users" >expout
atf_check "run_id -g -n" 0 expout null
atf_check "run_id -g -n 100" 0 expout null
atf_check "run_id -g -n test" 0 expout null
echo "0" >expout
atf_check "run_id -g 0" 0 expout null
atf_check "run_id -g root" 0 expout null
echo "wheel" >expout
atf_check "run_id -g -n 0" 0 expout null
atf_check "run_id -g -n root" 0 expout null
echo "100" >expout
atf_check "run_id -g -r" 0 expout null
echo "users" >expout
atf_check "run_id -g -r -n" 0 expout null
echo "100" >expout
atf_check "run_id -g -r 100" 0 expout null
atf_check "run_id -g -r test" 0 expout null
echo "users" >expout
atf_check "run_id -g -r -n 100" 0 expout null
atf_check "run_id -g -r -n test" 0 expout null
export LIBFAKE_EGID_ROOT=1 LIBFAKE_EUID_ROOT=1
echo "0" >expout
atf_check "run_id -g" 0 expout null
echo "wheel" >expout
atf_check "run_id -g -n" 0 expout null
echo "100" >expout
atf_check "run_id -g -r" 0 expout null
echo "users" >expout
atf_check "run_id -g -r -n" 0 expout null
echo "100" >expout
atf_check "run_id -g -r 100" 0 expout null
atf_check "run_id -g -r test" 0 expout null
echo "users" >expout
atf_check "run_id -g -r -n 100" 0 expout null
atf_check "run_id -g -r -n test" 0 expout null
unset LIBFAKE_EGID_ROOT LIBFAKE_EUID_ROOT
echo 'id: nonexistent: No such user' >experr
atf_check "run_id -g nonexistent" 1 null experr
atf_check "run_id -g root nonexistent" 1 null stderr
atf_check "grep ^usage: stderr" 0 ignore null
}
atf_test_case pflag
pflag_head() {
atf_set "descr" "Checks that the -p primary flag works"
}
pflag_body() {
cat >expout <<EOF
uid test
groups users wheel
EOF
atf_check "run_id -p" 0 expout null
atf_check "run_id -p 100" 0 expout null
atf_check "run_id -p test" 0 expout null
cat >expout <<EOF
uid root
groups wheel
EOF
atf_check "run_id -p 0" 0 expout null
atf_check "run_id -p root" 0 expout null
export LIBFAKE_EGID_ROOT=1 LIBFAKE_EUID_ROOT=1
cat >expout <<EOF
uid test
euid root
rgid users
groups users wheel
EOF
atf_check "run_id -p" 0 expout null
unset LIBFAKE_EGID_ROOT LIBFAKE_EUID_ROOT
echo 'id: nonexistent: No such user' >experr
atf_check "run_id -p nonexistent" 1 null experr
atf_check "run_id -p root nonexistent" 1 null stderr
atf_check "grep ^usage: stderr" 0 ignore null
}
atf_test_case uflag
uflag_head() {
atf_set "descr" "Checks that the -u primary flag works"
}
uflag_body() {
echo "100" >expout
atf_check "run_id -u" 0 expout null
atf_check "run_id -u 100" 0 expout null
atf_check "run_id -u test" 0 expout null
echo "test" >expout
atf_check "run_id -u -n" 0 expout null
atf_check "run_id -u -n 100" 0 expout null
atf_check "run_id -u -n test" 0 expout null
echo "0" >expout
atf_check "run_id -u 0" 0 expout null
atf_check "run_id -u root" 0 expout null
echo "root" >expout
atf_check "run_id -u -n 0" 0 expout null
atf_check "run_id -u -n root" 0 expout null
echo "100" >expout
atf_check "run_id -u -r" 0 expout null
echo "test" >expout
atf_check "run_id -u -r -n" 0 expout null
echo "100" >expout
atf_check "run_id -u -r 100" 0 expout null
atf_check "run_id -u -r test" 0 expout null
echo "test" >expout
atf_check "run_id -u -r -n 100" 0 expout null
atf_check "run_id -u -r -n test" 0 expout null
export LIBFAKE_EGID_ROOT=1 LIBFAKE_EUID_ROOT=1
echo "0" >expout
atf_check "run_id -u" 0 expout null
echo "root" >expout
atf_check "run_id -u -n" 0 expout null
echo "100" >expout
atf_check "run_id -u -r" 0 expout null
echo "test" >expout
atf_check "run_id -u -r -n" 0 expout null
echo "100" >expout
atf_check "run_id -u -r 100" 0 expout null
atf_check "run_id -u -r test" 0 expout null
echo "test" >expout
atf_check "run_id -u -r -n 100" 0 expout null
atf_check "run_id -u -r -n test" 0 expout null
unset LIBFAKE_EGID_ROOT LIBFAKE_EUID_ROOT
echo 'id: nonexistent: No such user' >experr
atf_check "run_id -u nonexistent" 1 null experr
atf_check "run_id -u root nonexistent" 1 null stderr
atf_check "grep ^usage: stderr" 0 ignore null
}
atf_init_test_cases()
{
atf_add_test_case default
atf_add_test_case primaries
atf_add_test_case Gflag
atf_add_test_case gflag
atf_add_test_case pflag
atf_add_test_case uflag
}

73
tests/util/id/t_whoami.sh Normal file
View File

@ -0,0 +1,73 @@
# $NetBSD: t_whoami.sh,v 1.1 2007/11/16 18:06:46 jmmv Exp $
#
# Copyright (c) 2007 The NetBSD Foundation, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
# must display the following acknowledgement:
# This product includes software developed by the NetBSD
# Foundation, Inc. and its contributors.
# 4. Neither the name of The NetBSD Foundation nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
run_whoami() {
LD_PRELOAD=$(atf_get_srcdir)/libfake.so.0 whoami "${@}"
}
atf_test_case correct
correct_head() {
atf_set "descr" "Checks that correct queries work"
}
correct_body() {
echo "Checking with EUID=100"
echo "test" >expout
atf_check "run_whoami" 0 expout null
echo "Checking with EUID=0"
export LIBFAKE_EUID_ROOT=1
echo "root" >expout
atf_check "run_whoami" 0 expout null
}
atf_test_case syntax
syntax_head() {
atf_set "descr" "Checks the command's syntax"
}
syntax_body() {
# Give a user to the command.
echo 'usage: whoami' >experr
atf_check "run_whoami root" 1 null experr
# Give an invalid flag but which is allowed by id (with which
# whoami shares code) when using the -un options.
echo 'usage: whoami' >experr
atf_check "run_whoami -r" 1 null experr
}
atf_init_test_cases()
{
atf_add_test_case correct
atf_add_test_case syntax
}