Path: blob/main/contrib/capsicum-test/capsicum-test-main.cc
39475 views
#include <sys/types.h>1#ifdef __linux__2#include <sys/vfs.h>3#include <linux/magic.h>4#elif defined(__FreeBSD__)5#include <sys/sysctl.h>6#endif7#include <ctype.h>8#include <errno.h>9#include <libgen.h>10#include <pwd.h>11#include <stdio.h>12#include <stdlib.h>13#include <unistd.h>14#include <iostream>15#include "gtest/gtest.h"16#include "capsicum-test.h"1718// For versions of googletest that lack GTEST_SKIP.19#ifndef GTEST_SKIP20#define GTEST_SKIP GTEST_FAIL21#endif2223std::string tmpdir;2425class SetupEnvironment : public ::testing::Environment26{27public:28SetupEnvironment() : teardown_tmpdir_(false) {}29void SetUp() override {30CheckCapsicumSupport();31if (tmpdir.empty()) {32std::cerr << "Generating temporary directory root: ";33CreateTemporaryRoot();34} else {35std::cerr << "User provided temporary directory root: ";36}37std::cerr << tmpdir << std::endl;38}39void CheckCapsicumSupport() {40#ifdef __FreeBSD__41int rc;42bool trap_enotcap_enabled;43size_t trap_enotcap_enabled_len = sizeof(trap_enotcap_enabled);4445if (feature_present("security_capabilities") == 0) {46GTEST_SKIP() << "Skipping tests because capsicum support is not "47<< "enabled in the kernel.";48}49// If this OID is enabled, it will send SIGTRAP to the process when50// `ENOTCAPABLE` is returned.51const char *oid = "kern.trap_enotcap";52rc = sysctlbyname(oid, &trap_enotcap_enabled, &trap_enotcap_enabled_len,53nullptr, 0);54if (rc != 0) {55GTEST_FAIL() << "sysctlbyname failed: " << strerror(errno);56}57if (trap_enotcap_enabled) {58GTEST_SKIP() << "Debug sysctl, " << oid << ", enabled. "59<< "Skipping tests because its enablement invalidates the "60<< "test results.";61}62#endif /* FreeBSD */63}64void CreateTemporaryRoot() {65char *tmpdir_name = tempnam(nullptr, "cptst");6667ASSERT_NE(tmpdir_name, nullptr);68ASSERT_EQ(mkdir(tmpdir_name, 0700), 0) <<69"Could not create temp directory, " << tmpdir_name << ": " <<70strerror(errno);71tmpdir = std::string(tmpdir_name);72free(tmpdir_name);73teardown_tmpdir_ = true;74}75void TearDown() override {76if (teardown_tmpdir_) {77rmdir(tmpdir.c_str());78}79}80private:81bool teardown_tmpdir_;82};8384std::string capsicum_test_bindir;8586// Adds a directory to $PATH.87static void AddDirectoryToPath(const char *dir) {88char *new_path, *old_path;8990old_path = getenv("PATH");91assert(old_path);9293assert(asprintf(&new_path, "%s:%s", dir, old_path) > 0);94assert(setenv("PATH", new_path, 1) == 0);95}9697int main(int argc, char* argv[]) {98// Set up the test program path, so capsicum-test can find programs, like99// mini-me* when executed from an absolute path.100char *program_name;101102// Copy argv[0], so dirname can do an in-place manipulation of the buffer's103// contents.104program_name = strdup(argv[0]);105assert(program_name);106capsicum_test_bindir = std::string(dirname(program_name));107free(program_name);108109AddDirectoryToPath(capsicum_test_bindir.c_str());110111::testing::InitGoogleTest(&argc, argv);112for (int ii = 1; ii < argc; ii++) {113if (strcmp(argv[ii], "-v") == 0) {114verbose = true;115} else if (strcmp(argv[ii], "-T") == 0) {116ii++;117assert(ii < argc);118tmpdir = argv[ii];119struct stat info;120stat(tmpdir.c_str(), &info);121assert(S_ISDIR(info.st_mode));122} else if (strcmp(argv[ii], "-t") == 0) {123force_mt = true;124} else if (strcmp(argv[ii], "-F") == 0) {125force_nofork = true;126} else if (strcmp(argv[ii], "-u") == 0) {127if (++ii >= argc) {128std::cerr << "-u needs argument" << std::endl;129exit(1);130}131if (isdigit(argv[ii][0])) {132other_uid = atoi(argv[ii]);133} else {134struct passwd *p = getpwnam(argv[ii]);135if (!p) {136std::cerr << "Failed to get entry for " << argv[ii] << ", errno=" << errno << std::endl;137exit(1);138}139other_uid = p->pw_uid;140}141}142}143if (other_uid == 0) {144struct stat info;145if (stat(argv[0], &info) == 0) {146other_uid = info.st_uid;147}148}149150#ifdef __linux__151// Check whether our temporary directory is on a tmpfs volume.152struct statfs fsinfo;153statfs(tmpdir.c_str(), &fsinfo);154tmpdir_on_tmpfs = (fsinfo.f_type == TMPFS_MAGIC);155#endif156157testing::AddGlobalTestEnvironment(new SetupEnvironment());158return RUN_ALL_TESTS();159}160161162