blob: f4490cadde2bea30967b56ad9bbe8fc106b0a483 [file] [log] [blame]
Mike Frysinger0b5cffa2017-08-15 18:06:18 -04001// system_unittest.cpp
2// Copyright (C) 2017 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// Test system.[ch] module code using gtest.
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <unistd.h>
22
23#include <gtest/gtest.h>
24
25#include "system.h"
26
27namespace {
28
29// A random path that really really should not exist on the host.
30const char kNoSuchDir[] = "/.x/..x/...x/path/should/not/exist/";
31
32// Return a temp filename in the cwd that this test can manipulate.
33// It will not exist when it returns, and the user has to free the memory.
34char *get_temp_path() {
35 char *path = strdup("minijail.tests.XXXXXX");
36 if (!path)
37 return nullptr;
38
39 // Just create the temp path.
40 int fd = mkstemp(path);
41 if (fd < 0)
42 return nullptr;
43 close(fd);
44 unlink(path);
45
46 return path;
47}
48
49} // namespace
50
51// Sanity check for the cap range.
52TEST(get_last_valid_cap, basic) {
53 unsigned int cap = get_last_valid_cap();
54
55 // We pick 35 as it's been that since at least v3.0.
56 // If this test is run on older kernels, it might fail.
57 EXPECT_GE(cap, 35u);
58
59 // Pick a really large number that we probably won't hit for a long time.
60 // It helps that caps are bitfields.
61 EXPECT_LT(cap, 128u);
62}
63
64// Might be useful to figure out the return value, but for now,
65// just make sure it doesn't crash?
66TEST(cap_ambient_supported, smoke) {
67 cap_ambient_supported();
68}
69
70// Invalid indexes should return errors, not crash.
71TEST(setup_pipe_end, bad_index) {
72 EXPECT_LT(setup_pipe_end(nullptr, 2), 0);
73 EXPECT_LT(setup_pipe_end(nullptr, 3), 0);
74 EXPECT_LT(setup_pipe_end(nullptr, 4), 0);
75}
76
77// Verify getting the first fd works.
78TEST(setup_pipe_end, index0) {
79 int fds[2];
80 EXPECT_EQ(0, pipe(fds));
81 // This should close fds[1] and return fds[0].
82 EXPECT_EQ(fds[0], setup_pipe_end(fds, 0));
83 // Use close() to verify open/close state.
84 EXPECT_EQ(-1, close(fds[1]));
85 EXPECT_EQ(0, close(fds[0]));
86}
87
88// Verify getting the second fd works.
89TEST(setup_pipe_end, index1) {
90 int fds[2];
91 EXPECT_EQ(0, pipe(fds));
92 // This should close fds[0] and return fds[1].
93 EXPECT_EQ(fds[1], setup_pipe_end(fds, 1));
94 // Use close() to verify open/close state.
95 EXPECT_EQ(-1, close(fds[0]));
96 EXPECT_EQ(0, close(fds[1]));
97}
98
99// Invalid indexes should return errors, not crash.
100TEST(setup_and_dupe_pipe_end, bad_index) {
101 EXPECT_LT(setup_and_dupe_pipe_end(nullptr, 2, -1), 0);
102 EXPECT_LT(setup_and_dupe_pipe_end(nullptr, 3, -1), 0);
103 EXPECT_LT(setup_and_dupe_pipe_end(nullptr, 4, -1), 0);
104}
105
106// An invalid path should return an error.
107TEST(write_pid_to_path, bad_path) {
108 EXPECT_NE(0, write_pid_to_path(0, kNoSuchDir));
109}
110
111// Make sure we can write a pid to the file.
112TEST(write_pid_to_path, basic) {
113 char *path = get_temp_path();
114 ASSERT_NE(nullptr, path);
115
116 EXPECT_EQ(0, write_pid_to_path(1234, path));
117 FILE *fp = fopen(path, "re");
118 unlink(path);
119 EXPECT_NE(nullptr, fp);
120 char data[5];
121 EXPECT_EQ(5u, fread(data, 1, sizeof(data), fp));
122 fclose(fp);
123 EXPECT_EQ(0, strcmp(data, "1234\n"));
124}