Path: blob/main/contrib/atf/atf-c/detail/user_test.c
39507 views
/* Copyright (c) 2007 The NetBSD Foundation, Inc.1* All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */2425#include "atf-c/detail/user.h"2627#include <sys/param.h>28#include <sys/types.h>2930#include <limits.h>31#include <stdio.h>32#include <unistd.h>3334#include <atf-c.h>3536#include "atf-c/detail/test_helpers.h"3738/* ---------------------------------------------------------------------39* Test cases for the free functions.40* --------------------------------------------------------------------- */4142ATF_TC(euid);43ATF_TC_HEAD(euid, tc)44{45atf_tc_set_md_var(tc, "descr", "Tests the atf_user_euid function");46}47ATF_TC_BODY(euid, tc)48{49ATF_REQUIRE_EQ(atf_user_euid(), geteuid());50}5152ATF_TC(is_member_of_group);53ATF_TC_HEAD(is_member_of_group, tc)54{55atf_tc_set_md_var(tc, "descr", "Tests the atf_user_is_member_of_group "56"function");57}58ATF_TC_BODY(is_member_of_group, tc)59{60gid_t gids[NGROUPS_MAX];61gid_t g, maxgid;62int ngids;63const gid_t maxgid_limit = 1 << 16;6465{66int i;6768ngids = getgroups(NGROUPS_MAX, gids);69if (ngids == -1)70atf_tc_fail("Call to getgroups failed");71maxgid = 0;72for (i = 0; i < ngids; i++) {73printf("User group %d is %u\n", i, gids[i]);74if (maxgid < gids[i])75maxgid = gids[i];76}77printf("User belongs to %d groups\n", ngids);78printf("Last GID is %u\n", maxgid);79}8081if (maxgid > maxgid_limit) {82printf("Test truncated from %u groups to %u to keep the run time "83"reasonable enough\n", maxgid, maxgid_limit);84maxgid = maxgid_limit;85}8687for (g = 0; g < maxgid; g++) {88bool found = false;89int i;9091for (i = 0; !found && i < ngids; i++) {92if (gids[i] == g)93found = true;94}9596if (found) {97printf("Checking if user belongs to group %d\n", g);98ATF_REQUIRE(atf_user_is_member_of_group(g));99} else {100printf("Checking if user does not belong to group %d\n", g);101ATF_REQUIRE(!atf_user_is_member_of_group(g));102}103}104}105106ATF_TC(is_root);107ATF_TC_HEAD(is_root, tc)108{109atf_tc_set_md_var(tc, "descr", "Tests the atf_user_is_root function");110}111ATF_TC_BODY(is_root, tc)112{113if (geteuid() == 0)114ATF_REQUIRE(atf_user_is_root());115else116ATF_REQUIRE(!atf_user_is_root());117}118119ATF_TC(is_unprivileged);120ATF_TC_HEAD(is_unprivileged, tc)121{122atf_tc_set_md_var(tc, "descr", "Tests the atf_user_is_unprivileged "123"function");124}125ATF_TC_BODY(is_unprivileged, tc)126{127if (geteuid() != 0)128ATF_REQUIRE(atf_user_is_unprivileged());129else130ATF_REQUIRE(!atf_user_is_unprivileged());131}132133/* ---------------------------------------------------------------------134* Main.135* --------------------------------------------------------------------- */136137ATF_TP_ADD_TCS(tp)138{139ATF_TP_ADD_TC(tp, euid);140ATF_TP_ADD_TC(tp, is_member_of_group);141ATF_TP_ADD_TC(tp, is_root);142ATF_TP_ADD_TC(tp, is_unprivileged);143144return atf_no_error();145}146147148