Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(258)

Side by Side Diff: native_client_sdk/src/tests/nacl_io_test/kernel_proxy_test.cc

Issue 832413004: [NaCl SDK] nacl_io: Change default root filesystem type from passthroughfs to memfs Base URL: https://chromium.googlesource.com/chromium/src.git@cleanup_path
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <errno.h> 5 #include <errno.h>
6 #include <fcntl.h> 6 #include <fcntl.h>
7 #include <pthread.h> 7 #include <pthread.h>
8 #include <stdio.h> 8 #include <stdio.h>
9 #include <sys/stat.h> 9 #include <sys/stat.h>
10 #include <sys/types.h> 10 #include <sys/types.h>
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 } 66 }
67 67
68 void TearDown() { ki_uninit(); } 68 void TearDown() { ki_uninit(); }
69 69
70 protected: 70 protected:
71 KernelProxyTest_KernelProxy kp_; 71 KernelProxyTest_KernelProxy kp_;
72 }; 72 };
73 73
74 } // namespace 74 } // namespace
75 75
76 // Helper function for calling ki_fcntl without having
77 // to construct a va_list.
76 static int ki_fcntl_wrapper(int fd, int request, ...) { 78 static int ki_fcntl_wrapper(int fd, int request, ...) {
77 va_list ap; 79 va_list ap;
78 va_start(ap, request); 80 va_start(ap, request);
79 int rtn = ki_fcntl(fd, request, ap); 81 int rtn = ki_fcntl(fd, request, ap);
80 va_end(ap); 82 va_end(ap);
81 return rtn; 83 return rtn;
82 } 84 }
83 85
86 // Helper function for calling ki_ioctl without having
87 // to construct a va_list.
88 static int ki_ioctl_wrapper(int fd, int request, ...) {
89 va_list ap;
90 va_start(ap, request);
91 int rtn = ki_ioctl(fd, request, ap);
92 va_end(ap);
93 return rtn;
94 }
95
96 static void check_dir_contents(const char* dirname, const char** expected,
binji 2015/01/07 19:14:59 nit: use CamelCase for functions
Sam Clegg 2015/01/08 12:59:07 Done.
97 int count) {
98 int dir = ki_open(dirname, O_RDONLY, 0);
99 ASSERT_GE(dir, 0);
100 while (1) {
101 char buf[512];
102 int rtn = ki_getdents(dir, buf, sizeof(buf));
103 if (rtn <= 0)
104 break;
105 int offset = 0;
106 while (offset < rtn) {
107 struct dirent* entry = (struct dirent*)(buf + offset);
108 printf("entry: %s\n", entry->d_name);
109 offset += entry->d_reclen;
110 if (count == -1)
111 continue;
112 int i;
113 for (i = 0; i < count; i++) {
binji 2015/01/07 19:14:59 this code is untested. Best to use it or remove it
Sam Clegg 2015/01/08 12:59:08 Sorry, I meant to add a getdents tests. Done that
114 if (expected[i] && strcmp(expected[i], entry->d_name) == 0) {
115 expected[i] = NULL;
binji 2015/01/07 19:14:59 This is a bit unexpected, probably should be docum
Sam Clegg 2015/01/08 12:59:07 Done.
116 break;
117 }
118 }
119 if (i == count) {
120 FAIL() << "Unexpeected entry '" << entry->d_name
121 << "' in directory: " << dirname;
122 }
123 }
124 }
125
126 ASSERT_EQ(0, ki_close(dir));
127
128 if (count != -1) {
129 for (int i = 0; i < count; i++) {
130 if (expected[i] != NULL) {
131 FAIL() << "Expected entry '" << expected[i]
132 << "' not found in directory: " << dirname;
133 }
134 }
135 }
136 }
137
84 /** 138 /**
85 * Test for fcntl commands F_SETFD and F_GETFD. This 139 * Test for fcntl commands F_SETFD and F_GETFD. This
86 * is tested here rather than in the mount_node tests 140 * is tested here rather than in the mount_node tests
87 * since the fd flags are not stored in the kernel_handle 141 * since the fd flags are not stored in the kernel_handle
88 * or the filesystem node but directly in the FD mapping. 142 * or the filesystem node but directly in the FD mapping.
89 */ 143 */
90 TEST_F(KernelProxyTest, Fcntl_GETFD) { 144 TEST_F(KernelProxyTest, Fcntl_GETFD) {
91 int fd = ki_open("/test", O_RDWR | O_CREAT, 0777); 145 int fd = ki_open("/test", O_RDWR | O_CREAT, 0777);
92 ASSERT_NE(-1, fd); 146 ASSERT_NE(-1, fd);
93 147
(...skipping 19 matching lines...) Expand all
113 167
114 MemFs* filesystem = (MemFs*)kp_.RootFs(); 168 MemFs* filesystem = (MemFs*)kp_.RootFs();
115 ScopedNode root; 169 ScopedNode root;
116 170
117 ASSERT_EQ(0, filesystem->Open(Path("/"), O_RDONLY, &root)); 171 ASSERT_EQ(0, filesystem->Open(Path("/"), O_RDONLY, &root));
118 ASSERT_EQ(0, root->ChildCount()); 172 ASSERT_EQ(0, root->ChildCount());
119 173
120 for (int file_num = 0; file_num < 4096; file_num++) { 174 for (int file_num = 0; file_num < 4096; file_num++) {
121 sprintf(filename, "/foo%i.tmp", file_num++); 175 sprintf(filename, "/foo%i.tmp", file_num++);
122 int fd = ki_open(filename, O_WRONLY | O_CREAT, 0777); 176 int fd = ki_open(filename, O_WRONLY | O_CREAT, 0777);
123 ASSERT_GT(fd, -1); 177 ASSERT_GE(fd, 0);
124 ASSERT_EQ(1, root->ChildCount()); 178 ASSERT_EQ(1, root->ChildCount());
125 ASSERT_EQ(buffer_size, ki_write(fd, garbage, buffer_size)); 179 ASSERT_EQ(buffer_size, ki_write(fd, garbage, buffer_size));
126 ki_close(fd); 180 ki_close(fd);
127 ASSERT_EQ(0, ki_remove(filename)); 181 ASSERT_EQ(0, ki_remove(filename));
128 } 182 }
129 ASSERT_EQ(0, root->ChildCount()); 183 ASSERT_EQ(0, root->ChildCount());
130 } 184 }
131 185
132 static bool g_handler_called = false; 186 static bool g_handler_called = false;
133 static void sighandler(int) { g_handler_called = true; } 187 static void sighandler(int) { g_handler_called = true; }
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 // Restore existing handler 283 // Restore existing handler
230 oldsig = ki_signal(SIGWINCH, oldsig); 284 oldsig = ki_signal(SIGWINCH, oldsig);
231 285
232 // Verify the our newsig was returned as previous handler 286 // Verify the our newsig was returned as previous handler
233 ASSERT_EQ(oldsig, newsig); 287 ASSERT_EQ(oldsig, newsig);
234 } 288 }
235 289
236 TEST_F(KernelProxyTest, Rename) { 290 TEST_F(KernelProxyTest, Rename) {
237 // Create a dummy file 291 // Create a dummy file
238 int file1 = ki_open("/test1.txt", O_RDWR | O_CREAT, 0777); 292 int file1 = ki_open("/test1.txt", O_RDWR | O_CREAT, 0777);
239 ASSERT_GT(file1, -1); 293 ASSERT_GE(file1, 0);
240 ASSERT_EQ(0, ki_close(file1)); 294 ASSERT_EQ(0, ki_close(file1));
241 295
242 // Test the renaming works 296 // Test the renaming works
243 ASSERT_EQ(0, ki_rename("/test1.txt", "/test2.txt")); 297 ASSERT_EQ(0, ki_rename("/test1.txt", "/test2.txt"));
244 298
245 // Test that renaming across mount points fails 299 // Test that renaming across mount points fails
246 ASSERT_EQ(0, ki_mount("", "/foo", "memfs", 0, "")); 300 ASSERT_EQ(0, ki_mount("", "/foo", "memfs", 0, ""));
247 ASSERT_EQ(-1, ki_rename("/test2.txt", "/foo/test2.txt")); 301 ASSERT_EQ(-1, ki_rename("/test2.txt", "/foo/test2.txt"));
248 ASSERT_EQ(EXDEV, errno); 302 ASSERT_EQ(EXDEV, errno);
249 } 303 }
(...skipping 29 matching lines...) Expand all
279 333
280 memset(text, 0, sizeof(text)); 334 memset(text, 0, sizeof(text));
281 EXPECT_EQ(-1, ki_chdir("foo")); 335 EXPECT_EQ(-1, ki_chdir("foo"));
282 EXPECT_EQ(ENOENT, errno); 336 EXPECT_EQ(ENOENT, errno);
283 EXPECT_EQ(0, ki_chdir("..")); 337 EXPECT_EQ(0, ki_chdir(".."));
284 EXPECT_EQ(0, ki_chdir("/foo")); 338 EXPECT_EQ(0, ki_chdir("/foo"));
285 EXPECT_EQ(text, ki_getcwd(text, sizeof(text))); 339 EXPECT_EQ(text, ki_getcwd(text, sizeof(text)));
286 EXPECT_STREQ("/foo", text); 340 EXPECT_STREQ("/foo", text);
287 } 341 }
288 342
343 TEST_F(KernelProxyTest, Mkdir) {
344 ASSERT_EQ(0, ki_mkdir("/foo", S_IREAD | S_IWRITE));
345 ASSERT_EQ(-1, ki_mkdir("/foo", S_IREAD | S_IWRITE));
346 ASSERT_EQ(EEXIST, errno);
347
348 // Some component of parent directory does not exist
349 ASSERT_EQ(-1, ki_mkdir("/does_not_exist/foo", S_IREAD | S_IWRITE));
binji 2015/01/07 19:14:59 is this implemented yet?
Sam Clegg 2015/01/08 12:59:07 At least the parent must exist yes.
350 ASSERT_EQ(ENOENT, errno);
351
352 int fd = ki_open("/filename", O_CREAT | O_RDWR, 0777);
353 ASSERT_GE(fd, 0);
354
355 // Some component of parent directory is not actually a directory
356 ASSERT_EQ(-1, ki_mkdir("/filename/foo", S_IREAD | S_IWRITE));
357 ASSERT_EQ(ENOTDIR, errno);
358 }
359
360 TEST_F(KernelProxyTest, Rmdir) {
361 ASSERT_EQ(-1, ki_rmdir("/dev/fs"));
362 ASSERT_EQ(EPERM, errno);
363
364 ASSERT_EQ(-1, ki_rmdir("/foo"));
365 ASSERT_EQ(ENOENT, errno);
366 }
367
289 TEST_F(KernelProxyTest, FDPathMapping) { 368 TEST_F(KernelProxyTest, FDPathMapping) {
290 char text[1024]; 369 char text[1024];
291 370
292 int fd1, fd2, fd3, fd4, fd5; 371 int fd1, fd2, fd3, fd4, fd5;
293 372
294 EXPECT_EQ(0, ki_mkdir("/foo", S_IREAD | S_IWRITE)); 373 EXPECT_EQ(0, ki_mkdir("/foo", S_IREAD | S_IWRITE));
295 EXPECT_EQ(0, ki_mkdir("/foo/bar", S_IREAD | S_IWRITE)); 374 EXPECT_EQ(0, ki_mkdir("/foo/bar", S_IREAD | S_IWRITE));
296 EXPECT_EQ(0, ki_mkdir("/example", S_IREAD | S_IWRITE)); 375 EXPECT_EQ(0, ki_mkdir("/example", S_IREAD | S_IWRITE));
297 ki_chdir("/foo"); 376 ki_chdir("/foo");
298 377
299 fd1 = ki_open("/example", O_RDONLY, 0); 378 fd1 = ki_open("/example", O_RDONLY, 0);
300 EXPECT_NE(-1, fd1); 379 ASSERT_GT(fd1, 0);
301 EXPECT_EQ(ki_fchdir(fd1), 0); 380 EXPECT_EQ(ki_fchdir(fd1), 0);
302 EXPECT_EQ(text, ki_getcwd(text, sizeof(text))); 381 EXPECT_EQ(text, ki_getcwd(text, sizeof(text)));
303 EXPECT_STREQ("/example", text); 382 EXPECT_STREQ("/example", text);
304 383
305 EXPECT_EQ(0, ki_chdir("/foo")); 384 EXPECT_EQ(0, ki_chdir("/foo"));
306 fd2 = ki_open("../example", O_RDONLY, 0); 385 fd2 = ki_open("../example", O_RDONLY, 0);
307 EXPECT_NE(-1, fd2); 386 ASSERT_GE(fd2, 0);
308 EXPECT_EQ(0, ki_fchdir(fd2)); 387 EXPECT_EQ(0, ki_fchdir(fd2));
309 EXPECT_EQ(text, ki_getcwd(text, sizeof(text))); 388 EXPECT_EQ(text, ki_getcwd(text, sizeof(text)));
310 EXPECT_STREQ("/example", text); 389 EXPECT_STREQ("/example", text);
311 390
312 EXPECT_EQ(0, ki_chdir("/foo")); 391 EXPECT_EQ(0, ki_chdir("/foo"));
313 fd3 = ki_open("../test", O_CREAT | O_RDWR, 0777); 392 fd3 = ki_open("../test", O_CREAT | O_RDWR, 0777);
314 EXPECT_NE(-1, fd3); 393 ASSERT_GE(fd3, 0);
315 EXPECT_EQ(-1, ki_fchdir(fd3)); 394 EXPECT_EQ(-1, ki_fchdir(fd3));
316 EXPECT_EQ(ENOTDIR, errno); 395 EXPECT_EQ(ENOTDIR, errno);
317 396
318 EXPECT_EQ(0, ki_chdir("/foo")); 397 EXPECT_EQ(0, ki_chdir("/foo"));
319 fd4 = ki_open("bar", O_RDONLY, 0); 398 fd4 = ki_open("bar", O_RDONLY, 0);
399 ASSERT_GE(fd4, 0);
320 EXPECT_EQ(0, ki_fchdir(fd4)); 400 EXPECT_EQ(0, ki_fchdir(fd4));
321 EXPECT_EQ(text, ki_getcwd(text, sizeof(text))); 401 EXPECT_EQ(text, ki_getcwd(text, sizeof(text)));
322 EXPECT_STREQ("/foo/bar", text); 402 EXPECT_STREQ("/foo/bar", text);
323 EXPECT_EQ(0, ki_chdir("/example")); 403 EXPECT_EQ(0, ki_chdir("/example"));
324 EXPECT_EQ(0, ki_fchdir(fd4)); 404 EXPECT_EQ(0, ki_fchdir(fd4));
325 EXPECT_EQ(text, ki_getcwd(text, sizeof(text))); 405 EXPECT_EQ(text, ki_getcwd(text, sizeof(text)));
326 EXPECT_STREQ("/foo/bar", text); 406 EXPECT_STREQ("/foo/bar", text);
327 407
328 EXPECT_EQ(0, ki_chdir("/example")); 408 EXPECT_EQ(0, ki_chdir("/example"));
329 fd5 = ki_dup(fd4); 409 fd5 = ki_dup(fd4);
330 ASSERT_GT(fd5, -1); 410 ASSERT_GE(fd5, 0);
331 ASSERT_NE(fd4, fd5); 411 ASSERT_NE(fd4, fd5);
332 EXPECT_EQ(0, ki_fchdir(fd5)); 412 EXPECT_EQ(0, ki_fchdir(fd5));
333 EXPECT_EQ(text, ki_getcwd(text, sizeof(text))); 413 EXPECT_EQ(text, ki_getcwd(text, sizeof(text)));
334 EXPECT_STREQ("/foo/bar", text); 414 EXPECT_STREQ("/foo/bar", text);
335 415
336 fd5 = 123; 416 fd5 = 123;
337 417
338 EXPECT_EQ(0, ki_chdir("/example")); 418 EXPECT_EQ(0, ki_chdir("/example"));
339 EXPECT_EQ(fd5, ki_dup2(fd4, fd5)); 419 EXPECT_EQ(fd5, ki_dup2(fd4, fd5));
340 EXPECT_EQ(0, ki_fchdir(fd5)); 420 EXPECT_EQ(0, ki_fchdir(fd5));
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
457 EXPECT_EQ(0, ki_close(fd1)); 537 EXPECT_EQ(0, ki_close(fd1));
458 538
459 // Truncate should fail if the file is not writable. 539 // Truncate should fail if the file is not writable.
460 EXPECT_EQ(0, ki_chmod("/trunc", 0444)); 540 EXPECT_EQ(0, ki_chmod("/trunc", 0444));
461 EXPECT_EQ(-1, ki_truncate("/trunc", 0)); 541 EXPECT_EQ(-1, ki_truncate("/trunc", 0));
462 EXPECT_EQ(EACCES, errno); 542 EXPECT_EQ(EACCES, errno);
463 } 543 }
464 544
465 TEST_F(KernelProxyTest, Lseek) { 545 TEST_F(KernelProxyTest, Lseek) {
466 int fd = ki_open("/foo", O_CREAT | O_RDWR, 0777); 546 int fd = ki_open("/foo", O_CREAT | O_RDWR, 0777);
467 ASSERT_GT(fd, -1); 547 ASSERT_GE(fd, 0);
468 ASSERT_EQ(9, ki_write(fd, "Some text", 9)); 548 ASSERT_EQ(9, ki_write(fd, "Some text", 9));
469 549
470 ASSERT_EQ(9, ki_lseek(fd, 0, SEEK_CUR)); 550 ASSERT_EQ(9, ki_lseek(fd, 0, SEEK_CUR));
471 ASSERT_EQ(9, ki_lseek(fd, 0, SEEK_END)); 551 ASSERT_EQ(9, ki_lseek(fd, 0, SEEK_END));
472 ASSERT_EQ(-1, ki_lseek(fd, -1, SEEK_SET)); 552 ASSERT_EQ(-1, ki_lseek(fd, -1, SEEK_SET));
473 ASSERT_EQ(EINVAL, errno); 553 ASSERT_EQ(EINVAL, errno);
474 554
475 // Seek past end of file. 555 // Seek past end of file.
476 ASSERT_EQ(13, ki_lseek(fd, 13, SEEK_SET)); 556 ASSERT_EQ(13, ki_lseek(fd, 13, SEEK_SET));
477 char buffer[4]; 557 char buffer[4];
478 memset(&buffer[0], 0xfe, 4); 558 memset(&buffer[0], 0xfe, 4);
479 ASSERT_EQ(9, ki_lseek(fd, -4, SEEK_END)); 559 ASSERT_EQ(9, ki_lseek(fd, -4, SEEK_END));
480 ASSERT_EQ(9, ki_lseek(fd, 0, SEEK_CUR)); 560 ASSERT_EQ(9, ki_lseek(fd, 0, SEEK_CUR));
481 ASSERT_EQ(4, ki_read(fd, &buffer[0], 4)); 561 ASSERT_EQ(4, ki_read(fd, &buffer[0], 4));
482 ASSERT_EQ(0, memcmp("\0\0\0\0", buffer, 4)); 562 ASSERT_EQ(0, memcmp("\0\0\0\0", buffer, 4));
483 } 563 }
484 564
485 TEST_F(KernelProxyTest, CloseTwice) { 565 TEST_F(KernelProxyTest, CloseTwice) {
486 int fd = ki_open("/foo", O_CREAT | O_RDWR, 0777); 566 int fd = ki_open("/foo", O_CREAT | O_RDWR, 0777);
487 ASSERT_GT(fd, -1); 567 ASSERT_GE(fd, 0);
488 568
489 EXPECT_EQ(9, ki_write(fd, "Some text", 9)); 569 EXPECT_EQ(9, ki_write(fd, "Some text", 9));
490 570
491 int fd2 = ki_dup(fd); 571 int fd2 = ki_dup(fd);
492 ASSERT_GT(fd2, -1); 572 ASSERT_GE(fd2, 0);
493 573
494 EXPECT_EQ(0, ki_close(fd)); 574 EXPECT_EQ(0, ki_close(fd));
495 EXPECT_EQ(0, ki_close(fd2)); 575 EXPECT_EQ(0, ki_close(fd2));
496 } 576 }
497 577
498 TEST_F(KernelProxyTest, Dup) { 578 TEST_F(KernelProxyTest, Dup) {
499 int fd = ki_open("/foo", O_CREAT | O_RDWR, 0777); 579 int fd = ki_open("/foo", O_CREAT | O_RDWR, 0777);
500 ASSERT_GT(fd, -1); 580 ASSERT_GE(fd, 0);
501 581
502 int dup_fd = ki_dup(fd); 582 int dup_fd = ki_dup(fd);
503 ASSERT_NE(-1, dup_fd); 583 ASSERT_NE(-1, dup_fd);
504 584
505 ASSERT_EQ(9, ki_write(fd, "Some text", 9)); 585 ASSERT_EQ(9, ki_write(fd, "Some text", 9));
506 ASSERT_EQ(9, ki_lseek(fd, 0, SEEK_CUR)); 586 ASSERT_EQ(9, ki_lseek(fd, 0, SEEK_CUR));
507 ASSERT_EQ(9, ki_lseek(dup_fd, 0, SEEK_CUR)); 587 ASSERT_EQ(9, ki_lseek(dup_fd, 0, SEEK_CUR));
508 588
509 int dup2_fd = 123; 589 int dup2_fd = 123;
510 ASSERT_EQ(dup2_fd, ki_dup2(fd, dup2_fd)); 590 ASSERT_EQ(dup2_fd, ki_dup2(fd, dup2_fd));
(...skipping 17 matching lines...) Expand all
528 } 608 }
529 609
530 TEST_F(KernelProxyTest, DescriptorAllocationConsistency) { 610 TEST_F(KernelProxyTest, DescriptorAllocationConsistency) {
531 // Check that the descriptor free list returns the expected ones, 611 // Check that the descriptor free list returns the expected ones,
532 // as the order is mandated by POSIX. 612 // as the order is mandated by POSIX.
533 613
534 // Open a file to a get a descriptor to copy for this test. 614 // Open a file to a get a descriptor to copy for this test.
535 // The test makes the assumption at all descriptors 615 // The test makes the assumption at all descriptors
536 // open by default are contiguous starting from zero. 616 // open by default are contiguous starting from zero.
537 int fd = ki_open("/foo", O_CREAT | O_RDWR, 0777); 617 int fd = ki_open("/foo", O_CREAT | O_RDWR, 0777);
538 ASSERT_GT(fd, -1); 618 ASSERT_GE(fd, 0);
539 619
540 // The next descriptor allocated should follow the first. 620 // The next descriptor allocated should follow the first.
541 int dup_fd = ki_dup(fd); 621 int dup_fd = ki_dup(fd);
542 ASSERT_EQ(fd + 1, dup_fd); 622 ASSERT_EQ(fd + 1, dup_fd);
543 623
544 // Allocate a high descriptor number. 624 // Allocate a high descriptor number.
545 ASSERT_EQ(100, ki_dup2(fd, 100)); 625 ASSERT_EQ(100, ki_dup2(fd, 100));
546 626
547 // The next descriptor allocate should still come 2 places 627 // The next descriptor allocate should still come 2 places
548 // after the first. 628 // after the first.
549 int dup_fd2 = ki_dup(fd); 629 int dup_fd2 = ki_dup(fd);
550 ASSERT_EQ(fd + 2, dup_fd2); 630 ASSERT_EQ(fd + 2, dup_fd2);
551 } 631 }
552 632
553 TEST_F(KernelProxyTest, Lstat) { 633 TEST_F(KernelProxyTest, Lstat) {
554 int fd = ki_open("/foo", O_CREAT | O_RDWR, 0777); 634 int fd = ki_open("/foo", O_CREAT | O_RDWR, 0777);
555 ASSERT_GT(fd, -1); 635 ASSERT_GE(fd, 0);
556 ASSERT_EQ(0, ki_mkdir("/bar", S_IREAD | S_IWRITE)); 636 ASSERT_EQ(0, ki_mkdir("/bar", S_IREAD | S_IWRITE));
557 637
558 struct stat buf; 638 struct stat buf;
559 EXPECT_EQ(0, ki_lstat("/foo", &buf)); 639 EXPECT_EQ(0, ki_lstat("/foo", &buf));
560 EXPECT_EQ(0, buf.st_size); 640 EXPECT_EQ(0, buf.st_size);
561 EXPECT_TRUE(S_ISREG(buf.st_mode)); 641 EXPECT_TRUE(S_ISREG(buf.st_mode));
562 642
563 EXPECT_EQ(0, ki_lstat("/bar", &buf)); 643 EXPECT_EQ(0, ki_lstat("/bar", &buf));
564 EXPECT_EQ(0, buf.st_size); 644 EXPECT_EQ(0, buf.st_size);
565 EXPECT_TRUE(S_ISDIR(buf.st_mode)); 645 EXPECT_TRUE(S_ISDIR(buf.st_mode));
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 // is ignored. 691 // is ignored.
612 ASSERT_EQ(0, ki_fchmod(fd, S_IFBLK | 0222)); 692 ASSERT_EQ(0, ki_fchmod(fd, S_IFBLK | 0222));
613 ASSERT_EQ(0, ki_stat("/foo", &buf)); 693 ASSERT_EQ(0, ki_stat("/foo", &buf));
614 EXPECT_TRUE(S_ISREG(buf.st_mode)); 694 EXPECT_TRUE(S_ISREG(buf.st_mode));
615 ASSERT_EQ(0222, buf.st_mode & 0777); 695 ASSERT_EQ(0222, buf.st_mode & 0777);
616 } 696 }
617 697
618 TEST_F(KernelProxyTest, OpenDirectory) { 698 TEST_F(KernelProxyTest, OpenDirectory) {
619 // Opening a directory for read should succeed. 699 // Opening a directory for read should succeed.
620 int fd = ki_open("/", O_RDONLY, 0); 700 int fd = ki_open("/", O_RDONLY, 0);
621 ASSERT_GT(fd, -1); 701 ASSERT_GE(fd, 0);
622 702
623 // Opening a directory for write should fail. 703 // Opening a directory for write should fail.
624 EXPECT_EQ(-1, ki_open("/", O_RDWR, 0)); 704 EXPECT_EQ(-1, ki_open("/", O_RDWR, 0));
625 EXPECT_EQ(errno, EISDIR); 705 EXPECT_EQ(errno, EISDIR);
626 EXPECT_EQ(-1, ki_open("/", O_WRONLY, 0)); 706 EXPECT_EQ(-1, ki_open("/", O_WRONLY, 0));
627 EXPECT_EQ(errno, EISDIR); 707 EXPECT_EQ(errno, EISDIR);
628 } 708 }
629 709
630 TEST_F(KernelProxyTest, OpenWithMode) { 710 TEST_F(KernelProxyTest, OpenWithMode) {
631 int fd = ki_open("/foo", O_CREAT | O_RDWR, 0723); 711 int fd = ki_open("/foo", O_CREAT | O_RDWR, 0723);
632 ASSERT_GT(fd, -1); 712 ASSERT_GE(fd, 0);
633 713
634 struct stat buf; 714 struct stat buf;
635 EXPECT_EQ(0, ki_lstat("/foo", &buf)); 715 EXPECT_EQ(0, ki_lstat("/foo", &buf));
636 EXPECT_EQ(0723, buf.st_mode & 0777); 716 EXPECT_EQ(0723, buf.st_mode & 0777);
637 } 717 }
638 718
639 TEST_F(KernelProxyTest, CreateWronlyWithReadOnlyMode) { 719 TEST_F(KernelProxyTest, CreateWronlyWithReadOnlyMode) {
640 int fd = ki_open("/foo", O_CREAT | O_WRONLY, 0444); 720 int fd = ki_open("/foo", O_CREAT | O_WRONLY, 0444);
641 ASSERT_GT(fd, -1); 721 ASSERT_GE(fd, 0);
642 } 722 }
643 723
644 TEST_F(KernelProxyTest, UseAfterClose) { 724 TEST_F(KernelProxyTest, UseAfterClose) {
645 int fd = ki_open("/dummy", O_CREAT | O_WRONLY, 0777); 725 int fd = ki_open("/dummy", O_CREAT | O_WRONLY, 0777);
646 ASSERT_GT(fd, -1); 726 ASSERT_GE(fd, 0);
647 EXPECT_EQ(5, ki_write(fd, "hello", 5)); 727 EXPECT_EQ(5, ki_write(fd, "hello", 5));
648 EXPECT_EQ(0, ki_close(fd)); 728 EXPECT_EQ(0, ki_close(fd));
649 EXPECT_EQ(-1, ki_write(fd, "hello", 5)); 729 EXPECT_EQ(-1, ki_write(fd, "hello", 5));
650 EXPECT_EQ(EBADF, errno); 730 EXPECT_EQ(EBADF, errno);
651 } 731 }
652 732
653 TEST_F(KernelProxyTest, Utimes) { 733 TEST_F(KernelProxyTest, Utimes) {
654 struct timeval times[2]; 734 struct timeval times[2];
655 times[0].tv_sec = 1000; 735 times[0].tv_sec = 1000;
656 times[0].tv_usec = 2000; 736 times[0].tv_usec = 2000;
657 times[1].tv_sec = 3000; 737 times[1].tv_sec = 3000;
658 times[1].tv_usec = 4000; 738 times[1].tv_usec = 4000;
659 739
660 int fd = ki_open("/dummy", O_CREAT | O_WRONLY, 0222); 740 int fd = ki_open("/dummy", O_CREAT | O_WRONLY, 0222);
661 ASSERT_GT(fd, -1); 741 ASSERT_GE(fd, 0);
662 EXPECT_EQ(0, ki_close(fd)); 742 EXPECT_EQ(0, ki_close(fd));
663 743
664 // utime should work if the file is write-only. 744 // utime should work if the file is write-only.
665 EXPECT_EQ(0, ki_utimes("/dummy", times)); 745 EXPECT_EQ(0, ki_utimes("/dummy", times));
666 746
667 // utime should work on directories (which can never be opened for write) 747 // utime should work on directories (which can never be opened for write)
668 EXPECT_EQ(0, ki_utimes("/", times)); 748 EXPECT_EQ(0, ki_utimes("/", times));
669 749
670 // or if the file is read-only. 750 // or if the file is read-only.
671 EXPECT_EQ(0, ki_chmod("/dummy", 0444)); 751 EXPECT_EQ(0, ki_chmod("/dummy", 0444));
(...skipping 18 matching lines...) Expand all
690 buf.st_mtime > tm.tv_sec || 770 buf.st_mtime > tm.tv_sec ||
691 (buf.st_mtime == tm.tv_sec && buf.st_mtimensec >= tm.tv_usec * 1000)); 771 (buf.st_mtime == tm.tv_sec && buf.st_mtimensec >= tm.tv_usec * 1000));
692 } 772 }
693 773
694 TEST_F(KernelProxyTest, Utime) { 774 TEST_F(KernelProxyTest, Utime) {
695 struct utimbuf times; 775 struct utimbuf times;
696 times.actime = 1000; 776 times.actime = 1000;
697 times.modtime = 2000; 777 times.modtime = 2000;
698 778
699 int fd = ki_open("/dummy", O_CREAT | O_WRONLY, 0222); 779 int fd = ki_open("/dummy", O_CREAT | O_WRONLY, 0222);
700 ASSERT_GT(fd, -1); 780 ASSERT_GE(fd, 0);
701 EXPECT_EQ(0, ki_close(fd)); 781 EXPECT_EQ(0, ki_close(fd));
702 782
703 // utime should work if the file is write-only. 783 // utime should work if the file is write-only.
704 EXPECT_EQ(0, ki_utime("/dummy", &times)); 784 EXPECT_EQ(0, ki_utime("/dummy", &times));
705 785
706 // or if the file is read-only. 786 // or if the file is read-only.
707 EXPECT_EQ(0, ki_chmod("/dummy", 0444)); 787 EXPECT_EQ(0, ki_chmod("/dummy", 0444));
708 EXPECT_EQ(0, ki_utime("/dummy", &times)); 788 EXPECT_EQ(0, ki_utime("/dummy", &times));
709 789
710 // times can be NULL. In that case the access/mod times will be set to the 790 // times can be NULL. In that case the access/mod times will be set to the
(...skipping 14 matching lines...) Expand all
725 EXPECT_TRUE( 805 EXPECT_TRUE(
726 buf.st_mtime > tm.tv_sec || 806 buf.st_mtime > tm.tv_sec ||
727 (buf.st_mtime == tm.tv_sec && buf.st_mtimensec >= tm.tv_usec * 1000)); 807 (buf.st_mtime == tm.tv_sec && buf.st_mtimensec >= tm.tv_usec * 1000));
728 } 808 }
729 809
730 TEST_F(KernelProxyTest, Umask) { 810 TEST_F(KernelProxyTest, Umask) {
731 mode_t oldmask = ki_umask(0222); 811 mode_t oldmask = ki_umask(0222);
732 EXPECT_EQ(0, oldmask); 812 EXPECT_EQ(0, oldmask);
733 813
734 int fd = ki_open("/foo", O_CREAT | O_RDONLY, 0666); 814 int fd = ki_open("/foo", O_CREAT | O_RDONLY, 0666);
735 ASSERT_GT(fd, -1); 815 ASSERT_GE(fd, 0);
736 ki_close(fd); 816 ki_close(fd);
737 817
738 EXPECT_EQ(0, ki_mkdir("/dir", 0777)); 818 EXPECT_EQ(0, ki_mkdir("/dir", 0777));
739 819
740 struct stat buf; 820 struct stat buf;
741 EXPECT_EQ(0, ki_stat("/foo", &buf)); 821 EXPECT_EQ(0, ki_stat("/foo", &buf));
742 EXPECT_EQ(0444, buf.st_mode & 0777); 822 EXPECT_EQ(0444, buf.st_mode & 0777);
743 823
744 EXPECT_EQ(0, ki_stat("/dir", &buf)); 824 EXPECT_EQ(0, ki_stat("/dir", &buf));
745 EXPECT_EQ(0555, buf.st_mode & 0777); 825 EXPECT_EQ(0555, buf.st_mode & 0777);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
797 877
798 void TearDown() { 878 void TearDown() {
799 g_string_map.clear(); 879 g_string_map.clear();
800 ki_uninit(); 880 ki_uninit();
801 } 881 }
802 882
803 protected: 883 protected:
804 KernelProxyMountTest_KernelProxy kp_; 884 KernelProxyMountTest_KernelProxy kp_;
805 }; 885 };
806 886
807 // Helper function for calling ki_ioctl without having
808 // to construct a va_list.
809 int ki_ioctl_wrapper(int fd, int request, ...) {
810 va_list ap;
811 va_start(ap, request);
812 int rtn = ki_ioctl(fd, request, ap);
813 va_end(ap);
814 return rtn;
815 }
816
817 } // namespace 887 } // namespace
818 888
819 TEST_F(KernelProxyMountTest, MountInit) { 889 TEST_F(KernelProxyMountTest, MountInit) {
820 int res1 = ki_mount("/", "/mnt1", "initfs", 0, "false,foo=bar"); 890 ASSERT_EQ(0, ki_mkdir("/mnt1", 0777));
891 int res1 = ki_mount("", "/mnt1", "initfs", 0, "false,foo=bar");
821 892
822 EXPECT_EQ("bar", g_string_map["foo"]); 893 EXPECT_EQ("bar", g_string_map["foo"]);
823 EXPECT_EQ(-1, res1); 894 EXPECT_EQ(-1, res1);
824 EXPECT_EQ(EINVAL, errno); 895 EXPECT_EQ(EINVAL, errno);
825 896
826 int res2 = ki_mount("/", "/mnt2", "initfs", 0, "true,bar=foo,x=y"); 897 ASSERT_EQ(0, ki_mkdir("/mnt2", 0777));
898 int res2 = ki_mount("", "/mnt2", "initfs", 0, "true,bar=foo,x=y");
827 EXPECT_NE(-1, res2); 899 EXPECT_NE(-1, res2);
828 EXPECT_EQ("y", g_string_map["x"]); 900 EXPECT_EQ("y", g_string_map["x"]);
901
902 }
903
904 TEST_F(KernelProxyMountTest, Passthroughfs) {
905 ASSERT_EQ(0, ki_mkdir("/passthrough", 0777));
906 int res1 = ki_mount("", "/passthrough", "passthroughfs", 0, "");
907 EXPECT_NE(-1, res1);
908 check_dir_contents("/passthrough", NULL, -1);
binji 2015/01/07 19:14:59 This is a manual test only?
Sam Clegg 2015/01/08 12:59:07 It verified that the directory can be read, nothin
829 } 909 }
830 910
831 TEST_F(KernelProxyMountTest, MountAndIoctl) { 911 TEST_F(KernelProxyMountTest, MountAndIoctl) {
832 ASSERT_EQ(0, ki_mount("/", "/mnt1", "initfs", 0, "")); 912 ASSERT_EQ(0, ki_mkdir("/mnt1", 0777));
913 ASSERT_EQ(0, ki_mount("", "/mnt1", "initfs", 0, ""));
833 ASSERT_NE(-1, g_fs_dev); 914 ASSERT_NE(-1, g_fs_dev);
834 915
835 char path[100]; 916 char path[100];
836 snprintf(path, 100, "dev/fs/%d", g_fs_dev); 917 snprintf(path, 100, "/dev/fs/%d", g_fs_dev);
837 918
838 int fd = ki_open(path, O_RDONLY, 0); 919 int fd = ki_open(path, O_RDONLY, 0);
839 ASSERT_GT(fd, -1); 920 ASSERT_GE(fd, 0);
840 921
841 EXPECT_EQ(0, ki_ioctl_wrapper(fd, 0xdeadbeef)); 922 EXPECT_EQ(0, ki_ioctl_wrapper(fd, 0xdeadbeef));
842 EXPECT_EQ(true, g_fs_ioctl_called); 923 EXPECT_EQ(true, g_fs_ioctl_called);
843 } 924 }
844 925
845 static void mount_callback(const char* source, 926 static void mount_callback(const char* source,
846 const char* target, 927 const char* target,
847 const char* filesystemtype, 928 const char* filesystemtype,
848 unsigned long mountflags, 929 unsigned long mountflags,
849 const void* data, 930 const void* data,
850 dev_t dev, 931 dev_t dev,
851 void* user_data) { 932 void* user_data) {
852 EXPECT_STREQ("/", source); 933 EXPECT_STREQ("dummy", source);
853 EXPECT_STREQ("/mnt1", target); 934 EXPECT_STREQ("/mnt1", target);
854 EXPECT_STREQ("initfs", filesystemtype); 935 EXPECT_STREQ("initfs", filesystemtype);
855 EXPECT_EQ(0, mountflags); 936 EXPECT_EQ(0, mountflags);
856 EXPECT_STREQ("", (const char*) data); 937 EXPECT_STREQ("", (const char*) data);
857 EXPECT_EQ(g_fs_dev, dev); 938 EXPECT_EQ(g_fs_dev, dev);
858 939
859 bool* callback_called = static_cast<bool*>(user_data); 940 bool* callback_called = static_cast<bool*>(user_data);
860 *callback_called = true; 941 *callback_called = true;
861 } 942 }
862 943
863 TEST_F(KernelProxyMountTest, MountCallback) { 944 TEST_F(KernelProxyMountTest, MountCallback) {
864 bool callback_called = false; 945 bool callback_called = false;
865 kp_.SetMountCallback(&mount_callback, &callback_called); 946 kp_.SetMountCallback(&mount_callback, &callback_called);
866 ASSERT_EQ(0, ki_mount("/", "/mnt1", "initfs", 0, "")); 947 ASSERT_EQ(0, ki_mkdir("/mnt1", 0777));
948 ASSERT_EQ(0, ki_mount("dummy", "/mnt1", "initfs", 0, ""));
867 ASSERT_NE(-1, g_fs_dev); 949 ASSERT_NE(-1, g_fs_dev);
868 EXPECT_EQ(true, callback_called); 950 EXPECT_EQ(true, callback_called);
869 } 951 }
870 952
871 namespace { 953 namespace {
872 954
873 int g_MMapCount = 0; 955 int g_MMapCount = 0;
874 956
875 class KernelProxyMMapTest_Node : public Node { 957 class KernelProxyMMapTest_Node : public Node {
876 public: 958 public:
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
947 1029
948 void TearDown() { ki_uninit(); } 1030 void TearDown() { ki_uninit(); }
949 1031
950 private: 1032 private:
951 KernelProxyMMapTest_KernelProxy kp_; 1033 KernelProxyMMapTest_KernelProxy kp_;
952 }; 1034 };
953 1035
954 } // namespace 1036 } // namespace
955 1037
956 TEST_F(KernelProxyMMapTest, MMap) { 1038 TEST_F(KernelProxyMMapTest, MMap) {
957 ASSERT_EQ(0, ki_umount("/")); 1039 ASSERT_EQ(0, ki_mkdir("/mmap", 0777));
958 ASSERT_EQ(0, ki_mount("", "/", "mmapfs", 0, NULL)); 1040 ASSERT_EQ(0, ki_mount("", "/mmap", "mmapfs", 0, NULL));
959 int fd = ki_open("/file", O_RDWR | O_CREAT, 0777); 1041 int fd = ki_open("/mmap/file", O_RDWR | O_CREAT, 0777);
960 ASSERT_NE(-1, fd); 1042 ASSERT_NE(-1, fd);
961 1043
962 void* addr1 = ki_mmap(NULL, 0x800, PROT_READ, MAP_PRIVATE, fd, 0); 1044 void* addr1 = ki_mmap(NULL, 0x800, PROT_READ, MAP_PRIVATE, fd, 0);
963 ASSERT_EQ(reinterpret_cast<void*>(0x1000), addr1); 1045 ASSERT_EQ(reinterpret_cast<void*>(0x1000), addr1);
964 ASSERT_EQ(1, g_MMapCount); 1046 ASSERT_EQ(1, g_MMapCount);
965 1047
966 void* addr2 = ki_mmap(NULL, 0x800, PROT_READ, MAP_PRIVATE, fd, 0); 1048 void* addr2 = ki_mmap(NULL, 0x800, PROT_READ, MAP_PRIVATE, fd, 0);
967 ASSERT_EQ(reinterpret_cast<void*>(0x2000), addr2); 1049 ASSERT_EQ(reinterpret_cast<void*>(0x2000), addr2);
968 ASSERT_EQ(2, g_MMapCount); 1050 ASSERT_EQ(2, g_MMapCount);
969 1051
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1013 ScopedRef<MockFs> fs_; 1095 ScopedRef<MockFs> fs_;
1014 }; 1096 };
1015 1097
1016 class KernelProxyErrorTest : public ::testing::Test { 1098 class KernelProxyErrorTest : public ::testing::Test {
1017 public: 1099 public:
1018 KernelProxyErrorTest() {} 1100 KernelProxyErrorTest() {}
1019 1101
1020 void SetUp() { 1102 void SetUp() {
1021 ASSERT_EQ(0, ki_push_state_for_testing()); 1103 ASSERT_EQ(0, ki_push_state_for_testing());
1022 ASSERT_EQ(0, ki_init(&kp_)); 1104 ASSERT_EQ(0, ki_init(&kp_));
1023 // Unmount the passthrough FS and mount a testfs. 1105 // Unmount root fs and mount a testfs.
1024 EXPECT_EQ(0, kp_.umount("/")); 1106 EXPECT_EQ(0, kp_.umount("/"));
1025 EXPECT_EQ(0, kp_.mount("", "/", "testfs", 0, NULL)); 1107 EXPECT_EQ(0, kp_.mount("", "/", "testfs", 0, NULL));
1026 } 1108 }
1027 1109
1028 void TearDown() { ki_uninit(); } 1110 void TearDown() { ki_uninit(); }
1029 1111
1030 ScopedRef<MockFs> fs() { return kp_.fs(); } 1112 ScopedRef<MockFs> fs() { return kp_.fs(); }
1031 1113
1032 private: 1114 private:
1033 KernelProxyErrorTest_KernelProxy kp_; 1115 KernelProxyErrorTest_KernelProxy kp_;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1071 1153
1072 int fd = ki_open("/dummy", O_RDONLY, 0); 1154 int fd = ki_open("/dummy", O_RDONLY, 0);
1073 EXPECT_NE(0, fd); 1155 EXPECT_NE(0, fd);
1074 1156
1075 char buf[20]; 1157 char buf[20];
1076 EXPECT_EQ(-1, ki_read(fd, &buf[0], 20)); 1158 EXPECT_EQ(-1, ki_read(fd, &buf[0], 20));
1077 // The Filesystem should be able to return whatever error it wants and have it 1159 // The Filesystem should be able to return whatever error it wants and have it
1078 // propagate through. 1160 // propagate through.
1079 EXPECT_EQ(1234, errno); 1161 EXPECT_EQ(1234, errno);
1080 } 1162 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698