OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2016 The Native Client Authors. All rights reserved. | 2 * Copyright 2016 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
Mark Seaborn
2016/02/24 21:19:19
If I comment out the call to NaClHostDescChdir() i
Sean Klein
2016/02/24 23:40:43
Updated the first test to verify the cwd before ca
Mark Seaborn
2016/02/25 01:18:09
Can you also add a comment to that code in sel_mai
Sean Klein
2016/02/25 01:36:28
Done.
| |
7 /* | 7 /* |
8 * NaCl tests for limited file access | 8 * NaCl tests for limited file access |
9 */ | 9 */ |
10 | 10 |
11 #include <dirent.h> | 11 #include <dirent.h> |
12 #include <errno.h> | 12 #include <errno.h> |
13 #include <fcntl.h> | 13 #include <fcntl.h> |
14 #include <limits.h> | 14 #include <limits.h> |
15 #include <stdlib.h> | 15 #include <stdlib.h> |
16 #include <stdio.h> | 16 #include <stdio.h> |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 } | 120 } |
121 ASSERT_EQ_MSG(closedir(d), 0, "closedir() failed"); | 121 ASSERT_EQ_MSG(closedir(d), 0, "closedir() failed"); |
122 | 122 |
123 ASSERT(temp_file_seen); | 123 ASSERT(temp_file_seen); |
124 ASSERT(temp_symlink_seen); | 124 ASSERT(temp_symlink_seen); |
125 ASSERT(sub_temp_dir_seen); | 125 ASSERT(sub_temp_dir_seen); |
126 ASSERT(parent_directory_seen); | 126 ASSERT(parent_directory_seen); |
127 ASSERT(current_directory_seen); | 127 ASSERT(current_directory_seen); |
128 ASSERT_EQ(count, 5); | 128 ASSERT_EQ(count, 5); |
129 | 129 |
130 // Chdir with relative path name | |
131 ASSERT_EQ_MSG(chdir(g_temp_sub_dir_name), 0, "chdir() failed"); | |
132 ASSERT_NE_MSG(getcwd(dirname, PATH_MAX), NULL, "getcwd() failed"); | |
133 ASSERT_EQ(strcmp(dirname, g_temp_sub_dir_path), 0); | |
134 | |
135 // Chdir with absolute path name | |
130 ASSERT_EQ_MSG(chdir(g_temp_sub_dir_path), 0, "chdir() failed"); | 136 ASSERT_EQ_MSG(chdir(g_temp_sub_dir_path), 0, "chdir() failed"); |
131 ASSERT_NE_MSG(getcwd(dirname, PATH_MAX), NULL, "getcwd() failed"); | 137 ASSERT_NE_MSG(getcwd(dirname, PATH_MAX), NULL, "getcwd() failed"); |
132 ASSERT_EQ(strcmp(dirname, g_temp_sub_dir_path), 0); | 138 ASSERT_EQ(strcmp(dirname, g_temp_sub_dir_path), 0); |
139 | |
133 d = opendir(dirname); | 140 d = opendir(dirname); |
134 count = 0; | 141 count = 0; |
135 | 142 |
136 /* | 143 /* |
137 * We expect to see: | 144 * We expect to see: |
138 * temp_sub_file | 145 * temp_sub_file |
139 * .. | 146 * .. |
140 * . | 147 * . |
141 */ | 148 */ |
142 | 149 |
(...skipping 30 matching lines...) Expand all Loading... | |
173 | 180 |
174 void test_new_directory_access() { | 181 void test_new_directory_access() { |
175 // Create a new directory, removes that directory. | 182 // Create a new directory, removes that directory. |
176 mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR; | 183 mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR; |
177 ASSERT_EQ(mkdir("/test_dir", mode), 0); | 184 ASSERT_EQ(mkdir("/test_dir", mode), 0); |
178 ASSERT_EQ(rmdir("/test_dir"), 0); | 185 ASSERT_EQ(rmdir("/test_dir"), 0); |
179 | 186 |
180 ASSERT_EQ(mkdir("/test_dir/", mode), 0); | 187 ASSERT_EQ(mkdir("/test_dir/", mode), 0); |
181 ASSERT_EQ(rmdir("/test_dir/"), 0); | 188 ASSERT_EQ(rmdir("/test_dir/"), 0); |
182 | 189 |
183 // Cannot make directory using relative path. | 190 // Test that relative paths can also be used. |
184 ASSERT_EQ(mkdir("test_dir/", mode), -1); | 191 ASSERT_EQ(mkdir("test_dir", mode), 0); |
185 ASSERT_EQ(errno, EACCES); | 192 ASSERT_EQ(rmdir("test_dir"), 0); |
186 | 193 |
187 char file_name[PATH_MAX]; | 194 char file_name[PATH_MAX]; |
188 snprintf(file_name, PATH_MAX, "%s/test_dir", g_temp_sub_dir_path); | 195 snprintf(file_name, PATH_MAX, "%s/test_dir", g_temp_sub_dir_path); |
189 ASSERT_EQ(mkdir(file_name, mode), 0); | 196 ASSERT_EQ(mkdir(file_name, mode), 0); |
190 ASSERT_EQ(rmdir(file_name), 0); | 197 ASSERT_EQ(rmdir(file_name), 0); |
191 | 198 |
192 ASSERT_NE(mkdir("/this_dir_does_not_exist/sub_dir", mode), 0); | 199 ASSERT_NE(mkdir("/this_dir_does_not_exist/sub_dir", mode), 0); |
193 passed("test_new_directory_access", "all"); | 200 passed("test_new_directory_access", "all"); |
194 } | 201 } |
195 | 202 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
291 // information leak could also lead to discovering directories and files | 298 // information leak could also lead to discovering directories and files |
292 // outside the mount point. | 299 // outside the mount point. |
293 char path[PATH_MAX]; | 300 char path[PATH_MAX]; |
294 struct stat buf; | 301 struct stat buf; |
295 | 302 |
296 // We should be able to access the root directory. | 303 // We should be able to access the root directory. |
297 ASSERT_EQ(stat("/", &buf), 0); | 304 ASSERT_EQ(stat("/", &buf), 0); |
298 ASSERT_EQ(stat("//", &buf), 0); | 305 ASSERT_EQ(stat("//", &buf), 0); |
299 ASSERT_EQ(stat("/./.", &buf), 0); | 306 ASSERT_EQ(stat("/./.", &buf), 0); |
300 ASSERT_EQ(stat("/./////.", &buf), 0); | 307 ASSERT_EQ(stat("/./////.", &buf), 0); |
301 | 308 ASSERT_EQ(stat(".", &buf), 0); |
302 // We should not be able to access relative paths. | |
303 ASSERT_EQ(stat(".", &buf), -1); | |
304 ASSERT_EQ(errno, EACCES); | |
305 | 309 |
306 // We should not be able to access paths containing "..". | 310 // We should not be able to access paths containing "..". |
307 snprintf(path, PATH_MAX, "%s/..", g_temp_sub_dir_path); | 311 snprintf(path, PATH_MAX, "%s/..", g_temp_sub_dir_path); |
308 ASSERT_EQ(stat(path, &buf), -1); | 312 ASSERT_EQ(stat(path, &buf), -1); |
309 ASSERT_EQ(errno, EACCES); | 313 ASSERT_EQ(errno, EACCES); |
310 | 314 |
311 // We should not be able to access the parent of the root directory. | 315 // We should not be able to access the parent of the root directory. |
312 ASSERT_EQ(stat("/..", &buf), -1); | 316 ASSERT_EQ(stat("/..", &buf), -1); |
313 ASSERT_EQ(errno, EACCES); | 317 ASSERT_EQ(errno, EACCES); |
314 | 318 |
315 // We should not be able to identify our mount point this way. | 319 // We should not be able to identify our mount point this way. |
316 snprintf(path, PATH_MAX, "/../%s", g_temp_dir_name); | 320 snprintf(path, PATH_MAX, "/../%s", g_temp_dir_name); |
317 ASSERT_EQ(stat(path, &buf), -1); | 321 ASSERT_EQ(stat(path, &buf), -1); |
318 ASSERT_EQ(errno, EACCES); | 322 ASSERT_EQ(errno, EACCES); |
319 snprintf(path, PATH_MAX, "//../%s", g_temp_dir_name); | 323 snprintf(path, PATH_MAX, "//../%s", g_temp_dir_name); |
320 ASSERT_EQ(stat(path, &buf), -1); | 324 ASSERT_EQ(stat(path, &buf), -1); |
321 ASSERT_EQ(errno, EACCES); | 325 ASSERT_EQ(errno, EACCES); |
322 snprintf(path, PATH_MAX, "/.//..//%s", g_temp_dir_name); | 326 snprintf(path, PATH_MAX, "/.//..//%s", g_temp_dir_name); |
323 ASSERT_EQ(stat(path, &buf), -1); | 327 ASSERT_EQ(stat(path, &buf), -1); |
324 ASSERT_EQ(errno, EACCES); | 328 ASSERT_EQ(errno, EACCES); |
325 | 329 |
326 passed("test_information_leak", "all"); | 330 passed("test_information_leak", "all"); |
327 } | 331 } |
328 | 332 |
329 void test_valid_file_access() { | 333 void test_valid_file_access() { |
330 // Show that reads and writes to valid files work. | 334 // Show that reads and writes to valid files work. |
331 char file_name[PATH_MAX]; | 335 char file_name[PATH_MAX]; |
336 bool new_file = false; | |
332 | 337 |
338 // Absolute path | |
333 snprintf(file_name, PATH_MAX, "%s", g_temp_file_path); | 339 snprintf(file_name, PATH_MAX, "%s", g_temp_file_path); |
334 do_test_write_read_file(file_name, false); | 340 do_test_write_read_file(file_name, new_file); |
335 | 341 |
342 // Relative path | |
343 snprintf(file_name, PATH_MAX, "%s", g_temp_file_name); | |
344 do_test_write_read_file(file_name, new_file); | |
345 | |
346 // Absolute path | |
336 snprintf(file_name, PATH_MAX, "%s/%s", g_temp_sub_dir_path, | 347 snprintf(file_name, PATH_MAX, "%s/%s", g_temp_sub_dir_path, |
337 g_temp_sub_file_name); | 348 g_temp_sub_file_name); |
338 do_test_write_read_file(file_name, false); | 349 do_test_write_read_file(file_name, new_file); |
339 | 350 |
340 snprintf(file_name, PATH_MAX, "%s/%s", g_temp_sub_dir_path, | 351 // Relative path |
352 snprintf(file_name, PATH_MAX, "%s/%s", g_temp_sub_dir_name, | |
341 g_temp_sub_file_name); | 353 g_temp_sub_file_name); |
342 do_test_write_read_file(file_name, false); | 354 do_test_write_read_file(file_name, new_file); |
355 | |
356 ASSERT_EQ_MSG(chdir(g_temp_sub_dir_name), 0, "chdir() failed"); | |
357 | |
358 // Relative path | |
359 snprintf(file_name, PATH_MAX, "%s", g_temp_sub_file_name); | |
360 do_test_write_read_file(file_name, new_file); | |
343 | 361 |
344 passed("test_valid_file_access", "all"); | 362 passed("test_valid_file_access", "all"); |
345 } | 363 } |
346 | 364 |
347 void test_new_file_access() { | 365 void test_new_file_access() { |
348 // Create a new file, show that it is readable / writable. | 366 // Create a new file, show that it is readable / writable. |
349 char file_name[PATH_MAX]; | 367 char file_name[PATH_MAX]; |
350 do_test_write_read_file("/new_temp_file", true); | 368 do_test_write_read_file("/new_temp_file", true); |
351 | 369 |
352 snprintf(file_name, PATH_MAX, "%s/newer_temp_file", g_temp_sub_dir_path); | 370 snprintf(file_name, PATH_MAX, "%s/newer_temp_file", g_temp_sub_dir_path); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
403 g_temp_sub_dir_name, argv[5]); | 421 g_temp_sub_dir_name, argv[5]); |
404 | 422 |
405 snprintf(g_temp_inaccessible_dir_name, PATH_MAX, "%s", argv[6]); | 423 snprintf(g_temp_inaccessible_dir_name, PATH_MAX, "%s", argv[6]); |
406 snprintf(g_temp_inaccessible_file_name, PATH_MAX, "%s", argv[7]); | 424 snprintf(g_temp_inaccessible_file_name, PATH_MAX, "%s", argv[7]); |
407 | 425 |
408 // Run the full test suite. | 426 // Run the full test suite. |
409 testSuite(); | 427 testSuite(); |
410 printf("All tests PASSED\n"); | 428 printf("All tests PASSED\n"); |
411 exit(0); | 429 exit(0); |
412 } | 430 } |
OLD | NEW |