Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | #include <fcntl.h> #include <stdlib.h> #include <sys/mount.h> #include <sys/param.h> #include <sys/stat.h> #include <System/sys/content_protection.h> #include <TargetConditionals.h> #include <unistd.h> #include <darwintest.h> #define ALLOWED_MKOSTEMP_FLAGS (O_APPEND | O_SHLOCK | O_EXLOCK | O_CLOEXEC) #define FCNTL_GETFL_EXPOSED_OFLAGS (O_APPEND) #define TEMPLATE_BASE "/tmp/mktemp_test." #define TEMPLATE_XS "XXXXXXXX" static const char template[] = TEMPLATE_BASE TEMPLATE_XS; static void test_mkostemp(int oflags); T_DECL(test_mkstemp, "basic mkstemp test") { char path[sizeof(template)]; struct stat path_stat; struct stat fd_stat; memcpy(path, template, sizeof(template)); int fd = mkstemp(path); T_ASSERT_POSIX_SUCCESS(fd, "mkstemp must return a valid fd"); T_ASSERT_TRUE(memcmp(path, TEMPLATE_BASE, strlen(TEMPLATE_BASE)) == 0, "mkstemp must respect the template. template: %s, got: %s", template, path); // stat fd and path, compare those T_ASSERT_POSIX_ZERO(stat(path, &path_stat), "must be able to stat the path"); T_ASSERT_POSIX_ZERO(fstat(fd, &fd_stat), "must be able to fstat the fd"); T_ASSERT_TRUE( (path_stat.st_dev == fd_stat.st_dev) && (path_stat.st_ino == fd_stat.st_ino), "fd does not match the file path"); // verify file attributes T_ASSERT_TRUE(S_ISREG(path_stat.st_mode), "the path must point to a regular file"); T_ASSERT_EQ(0600, (path_stat.st_mode & 0777), "created file must have 0600 permissions"); T_ASSERT_EQ_LLONG(0LL, path_stat.st_size, "created file must be empty"); // unlink and stat again T_ASSERT_POSIX_ZERO(unlink(path), "must be able to unlink the created file"); T_ASSERT_POSIX_ZERO(fstat(fd, &fd_stat), "must be able to stat the fd after unlink"); T_ASSERT_EQ(0, fd_stat.st_nlink, "must not have any hard links to the file after unlink"); // close T_ASSERT_POSIX_ZERO(close(fd), "must be able to close the fd"); } T_DECL(two_mkstemp_calls, "two mkstemp calls return different paths and fds") { char path1[sizeof(template)]; char path2[sizeof(template)]; memcpy(path1, template, sizeof(path1)); memcpy(path2, template, sizeof(path2)); int fd1 = mkostemp(path1, 0); T_ASSERT_POSIX_SUCCESS(fd1, "mkostemp must return a valid fd"); int fd2 = mkostemp(path2, 0); T_ASSERT_POSIX_SUCCESS(fd2, "mkostemp must return a valid fd"); T_ASSERT_NE(fd1, fd2, "two mkostemp calls must return different fds"); T_ASSERT_NE_STR(path1, path2, "two mkostemp calls must return different paths"); T_EXPECT_POSIX_ZERO(unlink(path1), "unlink must succeed for the first file"); T_EXPECT_POSIX_ZERO(unlink(path2), "unlink must succeed for the second file"); T_EXPECT_POSIX_ZERO(close(fd1), "close must succeed for the first fd"); T_EXPECT_POSIX_ZERO(close(fd2), "close must succeed for the second fd"); } T_DECL(test_mktemp, "basic mktemp test") { char path[sizeof(template)]; memcpy(path, template, sizeof(template)); T_ASSERT_EQ_PTR((void *) mktemp(path), (void *) path, "mktemp must return its argument"); T_ASSERT_TRUE(memcmp(path, TEMPLATE_BASE, strlen(TEMPLATE_BASE)) == 0, "mktemp must respect the template. template: %s, got: %s", template, path); } T_DECL(test_mkdtemp, "basic mkdtemp test") { char path[sizeof(template)]; struct stat dstat; memcpy(path, template, sizeof(template)); T_ASSERT_EQ_PTR((void *) mkdtemp(path), (void *) path, "mkdtemp must return its argument"); T_ASSERT_TRUE(memcmp(path, TEMPLATE_BASE, strlen(TEMPLATE_BASE)) == 0, "mkdtemp must respect the template. template: %s, got: %s", template, path); // stat fd and path, compare those T_ASSERT_POSIX_ZERO(stat(path, &dstat), "must be able to stat the path"); // verify file attributes T_ASSERT_TRUE(S_ISDIR(dstat.st_mode), "the path must point to a directory"); T_ASSERT_EQ(0700, (dstat.st_mode & 0777), "created directory must have 0700 permissions"); // cleanup T_ASSERT_POSIX_ZERO(rmdir(path), "must be able to rmdir the created directory"); } T_DECL(mkostemp_no_flags, "mkostemp works with 0 oflags") { test_mkostemp(0); } T_DECL(mkostemp_cloexec, "mkostemp works with O_CLOEXEC") { test_mkostemp(O_CLOEXEC); } T_DECL(mkostemp_append, "mkostemp works with O_APPEND") { test_mkostemp(O_APPEND); } T_DECL(mkostemp_all_flags, "mkostemp works with all allowed flags") { test_mkostemp(ALLOWED_MKOSTEMP_FLAGS); } T_DECL(mkostemp_bad_flags, "mkostemp checks for disallowed flags") { char path[sizeof(template)]; memcpy(path, template, sizeof(path)); T_ASSERT_EQ(-1, mkostemp(path, O_CREAT), "mkostemp must not allow O_CREAT"); T_ASSERT_EQ(-1, mkostemp(path, O_NONBLOCK), "mkostemp must not allow O_NONBLOCK"); } static void test_mkostemp(int oflags) { char path[sizeof(template)]; int fd; T_LOG("testing mkostemp with oflags %#x\n", oflags); memcpy(path, template, sizeof(template)); fd = mkostemp(path, oflags); T_ASSERT_POSIX_SUCCESS(fd, "mkostemp must return a valid fd"); if (oflags & O_CLOEXEC) { T_ASSERT_EQ(FD_CLOEXEC, fcntl(fd, F_GETFD), "O_CLOEXEC must be visible through fcntl GETFD"); } else { T_ASSERT_EQ(0, fcntl(fd, F_GETFD), "must not add fcntl GETFD flags at will"); } T_ASSERT_EQ( (oflags & FCNTL_GETFL_EXPOSED_OFLAGS), (fcntl(fd, F_GETFL) & FCNTL_GETFL_EXPOSED_OFLAGS), "oflags must be visible through fcntl GETFL"); T_EXPECT_POSIX_ZERO(unlink(path), "must be able to unlink the created file"); T_EXPECT_POSIX_ZERO(close(fd), "must be able to close the fd"); } #if TARGET_OS_IPHONE // mkstemp_dprotected_np is marked as __OSX_UNAVAILABLE, so its usage // has to be guarded by target conditionals. Having a testcase that // always does T_SKIP on OS X yields another issue. The compiler starts // demanding to mark the test function definition as noreturn. Just // don't compile the testcase for OS X. T_DECL(test_mkstemp_dprotected_np, "basic mkstemp_dprotected_np test") { char path[sizeof(template)]; int fd; int protection_class; struct statfs sfs; // the test requires /tmp to be HFS T_ASSERT_POSIX_ZERO(statfs("/tmp", &sfs), "must be able to statfs /tmp"); if (strcmp(sfs.f_fstypename, "hfs") != 0) { T_SKIP("Can only test dprotected on a HFS filesystem. Got %s," "skipping the test.", sfs.f_fstypename); } memcpy(path, template, sizeof(template)); // create a file fd = mkstemp_dprotected_np(path, PROTECTION_CLASS_B, 0); T_ASSERT_POSIX_SUCCESS(fd, "mkstemp_dprotected_np must return a valid fd"); T_ASSERT_TRUE(memcmp(path, TEMPLATE_BASE, strlen(TEMPLATE_BASE)) == 0, "mkstemp_dprotected_np must respect the template. template: %s, got: %s", template, path); // verify protection class protection_class = fcntl(fd, F_GETPROTECTIONCLASS); T_WITH_ERRNO; T_ASSERT_EQ(protection_class, PROTECTION_CLASS_B, "must get back the protection class from fcntl"); // cleanup T_ASSERT_POSIX_ZERO(close(fd), "must be able to close the fd"); T_ASSERT_POSIX_ZERO(unlink(path), "must be able to unlink the created file"); } #endif |