Mike Frysinger | 0b5cffa | 2017-08-15 18:06:18 -0400 | [diff] [blame] | 1 | // 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 | |
| 27 | namespace { |
| 28 | |
| 29 | // A random path that really really should not exist on the host. |
| 30 | const char kNoSuchDir[] = "/.x/..x/...x/path/should/not/exist/"; |
| 31 | |
Mike Frysinger | eaab420 | 2017-08-14 14:57:21 -0400 | [diff] [blame^] | 32 | // A random file that should exist. |
| 33 | const char kValidFile[] = "/etc/passwd"; |
| 34 | |
| 35 | // A random directory that should exist. |
| 36 | const char kValidDir[] = "/"; |
| 37 | |
| 38 | // A random character device that should exist. |
| 39 | const char kValidCharDev[] = "/dev/null"; |
| 40 | |
Mike Frysinger | 0b5cffa | 2017-08-15 18:06:18 -0400 | [diff] [blame] | 41 | // Return a temp filename in the cwd that this test can manipulate. |
| 42 | // It will not exist when it returns, and the user has to free the memory. |
| 43 | char *get_temp_path() { |
| 44 | char *path = strdup("minijail.tests.XXXXXX"); |
| 45 | if (!path) |
| 46 | return nullptr; |
| 47 | |
| 48 | // Just create the temp path. |
| 49 | int fd = mkstemp(path); |
| 50 | if (fd < 0) |
| 51 | return nullptr; |
| 52 | close(fd); |
| 53 | unlink(path); |
| 54 | |
| 55 | return path; |
| 56 | } |
| 57 | |
| 58 | } // namespace |
| 59 | |
| 60 | // Sanity check for the cap range. |
| 61 | TEST(get_last_valid_cap, basic) { |
| 62 | unsigned int cap = get_last_valid_cap(); |
| 63 | |
| 64 | // We pick 35 as it's been that since at least v3.0. |
| 65 | // If this test is run on older kernels, it might fail. |
| 66 | EXPECT_GE(cap, 35u); |
| 67 | |
| 68 | // Pick a really large number that we probably won't hit for a long time. |
| 69 | // It helps that caps are bitfields. |
| 70 | EXPECT_LT(cap, 128u); |
| 71 | } |
| 72 | |
| 73 | // Might be useful to figure out the return value, but for now, |
| 74 | // just make sure it doesn't crash? |
| 75 | TEST(cap_ambient_supported, smoke) { |
| 76 | cap_ambient_supported(); |
| 77 | } |
| 78 | |
| 79 | // Invalid indexes should return errors, not crash. |
| 80 | TEST(setup_pipe_end, bad_index) { |
| 81 | EXPECT_LT(setup_pipe_end(nullptr, 2), 0); |
| 82 | EXPECT_LT(setup_pipe_end(nullptr, 3), 0); |
| 83 | EXPECT_LT(setup_pipe_end(nullptr, 4), 0); |
| 84 | } |
| 85 | |
| 86 | // Verify getting the first fd works. |
| 87 | TEST(setup_pipe_end, index0) { |
| 88 | int fds[2]; |
| 89 | EXPECT_EQ(0, pipe(fds)); |
| 90 | // This should close fds[1] and return fds[0]. |
| 91 | EXPECT_EQ(fds[0], setup_pipe_end(fds, 0)); |
| 92 | // Use close() to verify open/close state. |
| 93 | EXPECT_EQ(-1, close(fds[1])); |
| 94 | EXPECT_EQ(0, close(fds[0])); |
| 95 | } |
| 96 | |
| 97 | // Verify getting the second fd works. |
| 98 | TEST(setup_pipe_end, index1) { |
| 99 | int fds[2]; |
| 100 | EXPECT_EQ(0, pipe(fds)); |
| 101 | // This should close fds[0] and return fds[1]. |
| 102 | EXPECT_EQ(fds[1], setup_pipe_end(fds, 1)); |
| 103 | // Use close() to verify open/close state. |
| 104 | EXPECT_EQ(-1, close(fds[0])); |
| 105 | EXPECT_EQ(0, close(fds[1])); |
| 106 | } |
| 107 | |
| 108 | // Invalid indexes should return errors, not crash. |
| 109 | TEST(setup_and_dupe_pipe_end, bad_index) { |
| 110 | EXPECT_LT(setup_and_dupe_pipe_end(nullptr, 2, -1), 0); |
| 111 | EXPECT_LT(setup_and_dupe_pipe_end(nullptr, 3, -1), 0); |
| 112 | EXPECT_LT(setup_and_dupe_pipe_end(nullptr, 4, -1), 0); |
| 113 | } |
| 114 | |
| 115 | // An invalid path should return an error. |
| 116 | TEST(write_pid_to_path, bad_path) { |
| 117 | EXPECT_NE(0, write_pid_to_path(0, kNoSuchDir)); |
| 118 | } |
| 119 | |
| 120 | // Make sure we can write a pid to the file. |
| 121 | TEST(write_pid_to_path, basic) { |
| 122 | char *path = get_temp_path(); |
| 123 | ASSERT_NE(nullptr, path); |
| 124 | |
| 125 | EXPECT_EQ(0, write_pid_to_path(1234, path)); |
| 126 | FILE *fp = fopen(path, "re"); |
| 127 | unlink(path); |
| 128 | EXPECT_NE(nullptr, fp); |
| 129 | char data[5]; |
| 130 | EXPECT_EQ(5u, fread(data, 1, sizeof(data), fp)); |
| 131 | fclose(fp); |
| 132 | EXPECT_EQ(0, strcmp(data, "1234\n")); |
Mike Frysinger | eaab420 | 2017-08-14 14:57:21 -0400 | [diff] [blame^] | 133 | |
| 134 | free(path); |
| 135 | } |
| 136 | |
| 137 | // If the destination exists, there's nothing to do. |
| 138 | TEST(setup_mount_destination, dest_exists) { |
| 139 | // Pick some paths that should always exist. We pass in invalid pointers |
| 140 | // for other args so we crash if the dest check doesn't short circuit. |
| 141 | EXPECT_EQ(0, setup_mount_destination(nullptr, kValidDir, 0, 0, false)); |
| 142 | EXPECT_EQ(0, setup_mount_destination(nullptr, "/proc", 0, 0, true)); |
| 143 | EXPECT_EQ(0, setup_mount_destination(nullptr, "/dev", 0, 0, false)); |
| 144 | } |
| 145 | |
| 146 | // When given a bind mount where the source is relative, reject it. |
| 147 | TEST(setup_mount_destination, reject_relative_bind) { |
| 148 | // Pick a destination we know doesn't exist. |
| 149 | EXPECT_NE(0, setup_mount_destination("foo", kNoSuchDir, 0, 0, true)); |
| 150 | } |
| 151 | |
| 152 | // A mount of a pseudo filesystem should make the destination dir. |
| 153 | TEST(setup_mount_destination, create_pseudo_fs) { |
| 154 | char *path = get_temp_path(); |
| 155 | ASSERT_NE(nullptr, path); |
| 156 | |
| 157 | // Passing -1 for uid/gid tells chown to make no changes. |
| 158 | EXPECT_EQ(0, setup_mount_destination("none", path, -1, -1, false)); |
| 159 | // We check it's a directory by deleting it as such. |
| 160 | EXPECT_EQ(0, rmdir(path)); |
| 161 | |
| 162 | free(path); |
| 163 | } |
| 164 | |
| 165 | // If the source path does not exist, we should error out. |
| 166 | TEST(setup_mount_destination, missing_source) { |
| 167 | // The missing dest path is so we can exercise the source logic. |
| 168 | EXPECT_NE(0, setup_mount_destination(kNoSuchDir, kNoSuchDir, 0, 0, false)); |
| 169 | EXPECT_NE(0, setup_mount_destination(kNoSuchDir, kNoSuchDir, 0, 0, true)); |
| 170 | } |
| 171 | |
| 172 | // A bind mount of a directory should create the destination dir. |
| 173 | TEST(setup_mount_destination, create_bind_dir) { |
| 174 | char *path = get_temp_path(); |
| 175 | ASSERT_NE(nullptr, path); |
| 176 | |
| 177 | // Passing -1 for uid/gid tells chown to make no changes. |
| 178 | EXPECT_EQ(0, setup_mount_destination(kValidDir, path, -1, -1, true)); |
| 179 | // We check it's a directory by deleting it as such. |
| 180 | EXPECT_EQ(0, rmdir(path)); |
| 181 | |
| 182 | free(path); |
| 183 | } |
| 184 | |
| 185 | // A bind mount of a file should create the destination file. |
| 186 | TEST(setup_mount_destination, create_bind_file) { |
| 187 | char *path = get_temp_path(); |
| 188 | ASSERT_NE(nullptr, path); |
| 189 | |
| 190 | // Passing -1 for uid/gid tells chown to make no changes. |
| 191 | EXPECT_EQ(0, setup_mount_destination(kValidFile, path, -1, -1, true)); |
| 192 | // We check it's a file by deleting it as such. |
| 193 | EXPECT_EQ(0, unlink(path)); |
| 194 | |
| 195 | free(path); |
| 196 | } |
| 197 | |
| 198 | // A mount of a character device should create the destination char. |
| 199 | TEST(setup_mount_destination, create_char_dev) { |
| 200 | char *path = get_temp_path(); |
| 201 | ASSERT_NE(nullptr, path); |
| 202 | |
| 203 | // Passing -1 for uid/gid tells chown to make no changes. |
| 204 | EXPECT_EQ(0, setup_mount_destination(kValidCharDev, path, -1, -1, false)); |
| 205 | // We check it's a directory by deleting it as such. |
| 206 | EXPECT_EQ(0, rmdir(path)); |
| 207 | |
| 208 | free(path); |
Mike Frysinger | 0b5cffa | 2017-08-15 18:06:18 -0400 | [diff] [blame] | 209 | } |