4 #include <linux/magic.h>
5 #elif defined(__FreeBSD__)
6 #include <sys/sysctl.h>
16 #include "gtest/gtest.h"
17 #include "capsicum-test.h"
19 // For versions of googletest that lack GTEST_SKIP.
21 #define GTEST_SKIP GTEST_FAIL
26 class SetupEnvironment : public ::testing::Environment
29 SetupEnvironment() : teardown_tmpdir_(false) {}
30 void SetUp() override {
31 CheckCapsicumSupport();
33 std::cerr << "Generating temporary directory root: ";
34 CreateTemporaryRoot();
36 std::cerr << "User provided temporary directory root: ";
38 std::cerr << tmpdir << std::endl;
40 void CheckCapsicumSupport() {
43 bool trap_enotcap_enabled;
44 size_t trap_enotcap_enabled_len = sizeof(trap_enotcap_enabled);
46 if (feature_present("security_capabilities") == 0) {
47 GTEST_SKIP() << "Skipping tests because capsicum support is not "
48 << "enabled in the kernel.";
50 // If this OID is enabled, it will send SIGTRAP to the process when
51 // `ENOTCAPABLE` is returned.
52 const char *oid = "kern.trap_enotcap";
53 rc = sysctlbyname(oid, &trap_enotcap_enabled, &trap_enotcap_enabled_len,
56 GTEST_FAIL() << "sysctlbyname failed: " << strerror(errno);
58 if (trap_enotcap_enabled) {
59 GTEST_SKIP() << "Debug sysctl, " << oid << ", enabled. "
60 << "Skipping tests because its enablement invalidates the "
65 void CreateTemporaryRoot() {
66 char *tmpdir_name = tempnam(nullptr, "cptst");
68 ASSERT_NE(tmpdir_name, nullptr);
69 ASSERT_EQ(mkdir(tmpdir_name, 0700), 0) <<
70 "Could not create temp directory, " << tmpdir_name << ": " <<
72 tmpdir = std::string(tmpdir_name);
74 teardown_tmpdir_ = true;
76 void TearDown() override {
77 if (teardown_tmpdir_) {
78 rmdir(tmpdir.c_str());
82 bool teardown_tmpdir_;
85 std::string capsicum_test_bindir;
87 int main(int argc, char* argv[]) {
88 // Set up the test program path, so capsicum-test can find programs, like
89 // mini-me* when executed from an absolute path.
91 char *new_path, *old_path, *program_name;
93 program_name = strdup(argv[0]);
95 capsicum_test_bindir = std::string(dirname(program_name));
98 old_path = getenv("PATH");
101 assert(asprintf(&new_path, "%s:%s", capsicum_test_bindir.c_str(),
103 assert(setenv("PATH", new_path, 1) == 0);
106 ::testing::InitGoogleTest(&argc, argv);
107 for (int ii = 1; ii < argc; ii++) {
108 if (strcmp(argv[ii], "-v") == 0) {
110 } else if (strcmp(argv[ii], "-T") == 0) {
115 stat(tmpdir.c_str(), &info);
116 assert(S_ISDIR(info.st_mode));
117 } else if (strcmp(argv[ii], "-t") == 0) {
119 } else if (strcmp(argv[ii], "-F") == 0) {
121 } else if (strcmp(argv[ii], "-u") == 0) {
123 std::cerr << "-u needs argument" << std::endl;
126 if (isdigit(argv[ii][0])) {
127 other_uid = atoi(argv[ii]);
129 struct passwd *p = getpwnam(argv[ii]);
131 std::cerr << "Failed to get entry for " << argv[ii] << ", errno=" << errno << std::endl;
134 other_uid = p->pw_uid;
138 if (other_uid == 0) {
140 if (stat(argv[0], &info) == 0) {
141 other_uid = info.st_uid;
146 // Check whether our temporary directory is on a tmpfs volume.
147 struct statfs fsinfo;
148 statfs(tmpdir.c_str(), &fsinfo);
149 tmpdir_on_tmpfs = (fsinfo.f_type == TMPFS_MAGIC);
152 testing::AddGlobalTestEnvironment(new SetupEnvironment());
153 int rc = RUN_ALL_TESTS();
154 ShowSkippedTests(std::cerr);