extern "C" {
#include <sys/param.h>
#include <sys/types.h>
#include <limits.h>
#include <unistd.h>
}
#include <iostream>
#include <set>
#include "../atf-c++/macros.hpp"
#include "user.hpp"
ATF_TEST_CASE(euid);
ATF_TEST_CASE_HEAD(euid)
{
set_md_var("descr", "Tests the euid function");
}
ATF_TEST_CASE_BODY(euid)
{
using atf::atf_run::euid;
ATF_REQUIRE_EQ(euid(), ::geteuid());
}
ATF_TEST_CASE(is_member_of_group);
ATF_TEST_CASE_HEAD(is_member_of_group)
{
set_md_var("descr", "Tests the is_member_of_group function");
}
ATF_TEST_CASE_BODY(is_member_of_group)
{
using atf::atf_run::is_member_of_group;
std::set< gid_t > groups;
gid_t maxgid = 0;
{
gid_t gids[NGROUPS_MAX];
int ngids = ::getgroups(NGROUPS_MAX, gids);
if (ngids == -1)
ATF_FAIL("Call to ::getgroups failed");
for (int i = 0; i < ngids; i++) {
groups.insert(gids[i]);
if (gids[i] > maxgid)
maxgid = gids[i];
}
std::cout << "User belongs to " << ngids << " groups\n";
std::cout << "Last GID is " << maxgid << "\n";
}
for (gid_t g = 0; g <= maxgid; g++) {
if (groups.find(g) == groups.end()) {
std::cout << "Checking if user does not belong to group "
<< g << "\n";
ATF_REQUIRE(!is_member_of_group(g));
} else {
std::cout << "Checking if user belongs to group " << g << "\n";
ATF_REQUIRE(is_member_of_group(g));
}
}
}
ATF_TEST_CASE(is_root);
ATF_TEST_CASE_HEAD(is_root)
{
set_md_var("descr", "Tests the is_root function");
}
ATF_TEST_CASE_BODY(is_root)
{
using atf::atf_run::is_root;
if (::geteuid() == 0) {
ATF_REQUIRE(is_root());
} else {
ATF_REQUIRE(!is_root());
}
}
ATF_TEST_CASE(is_unprivileged);
ATF_TEST_CASE_HEAD(is_unprivileged)
{
set_md_var("descr", "Tests the is_unprivileged function");
}
ATF_TEST_CASE_BODY(is_unprivileged)
{
using atf::atf_run::is_unprivileged;
if (::geteuid() != 0) {
ATF_REQUIRE(is_unprivileged());
} else {
ATF_REQUIRE(!is_unprivileged());
}
}
ATF_INIT_TEST_CASES(tcs)
{
ATF_ADD_TEST_CASE(tcs, euid);
ATF_ADD_TEST_CASE(tcs, is_member_of_group);
ATF_ADD_TEST_CASE(tcs, is_root);
ATF_ADD_TEST_CASE(tcs, is_unprivileged);
}