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

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

Issue 565763002: Plumbing though mode parameter to open, since fusefs can make use of it. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixes Created 6 years, 3 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 10
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 return rtn; 78 return rtn;
79 } 79 }
80 80
81 /** 81 /**
82 * Test for fcntl commands F_SETFD and F_GETFD. This 82 * Test for fcntl commands F_SETFD and F_GETFD. This
83 * is tested here rather than in the mount_node tests 83 * is tested here rather than in the mount_node tests
84 * since the fd flags are not stored in the kernel_handle 84 * since the fd flags are not stored in the kernel_handle
85 * or the filesystem node but directly in the FD mapping. 85 * or the filesystem node but directly in the FD mapping.
86 */ 86 */
87 TEST_F(KernelProxyTest, Fcntl_GETFD) { 87 TEST_F(KernelProxyTest, Fcntl_GETFD) {
88 int fd = ki_open("/test", O_RDWR | O_CREAT); 88 int fd = ki_open("/test", O_RDWR | O_CREAT, 0777);
89 ASSERT_NE(-1, fd); 89 ASSERT_NE(-1, fd);
90 90
91 // FD flags should start as zero. 91 // FD flags should start as zero.
92 ASSERT_EQ(0, ki_fcntl_wrapper(fd, F_GETFD)); 92 ASSERT_EQ(0, ki_fcntl_wrapper(fd, F_GETFD));
93 93
94 // Check that setting FD_CLOEXEC works 94 // Check that setting FD_CLOEXEC works
95 int flags = FD_CLOEXEC; 95 int flags = FD_CLOEXEC;
96 ASSERT_EQ(0, ki_fcntl_wrapper(fd, F_SETFD, flags)) 96 ASSERT_EQ(0, ki_fcntl_wrapper(fd, F_SETFD, flags))
97 << "fcntl failed with: " << strerror(errno); 97 << "fcntl failed with: " << strerror(errno);
98 ASSERT_EQ(FD_CLOEXEC, ki_fcntl_wrapper(fd, F_GETFD)); 98 ASSERT_EQ(FD_CLOEXEC, ki_fcntl_wrapper(fd, F_GETFD));
(...skipping 10 matching lines...) Expand all
109 int garbage[buffer_size]; 109 int garbage[buffer_size];
110 110
111 MemFs* filesystem = (MemFs*)kp_.RootFs(); 111 MemFs* filesystem = (MemFs*)kp_.RootFs();
112 ScopedNode root; 112 ScopedNode root;
113 113
114 ASSERT_EQ(0, filesystem->Open(Path("/"), O_RDONLY, &root)); 114 ASSERT_EQ(0, filesystem->Open(Path("/"), O_RDONLY, &root));
115 ASSERT_EQ(0, root->ChildCount()); 115 ASSERT_EQ(0, root->ChildCount());
116 116
117 for (int file_num = 0; file_num < 4096; file_num++) { 117 for (int file_num = 0; file_num < 4096; file_num++) {
118 sprintf(filename, "/foo%i.tmp", file_num++); 118 sprintf(filename, "/foo%i.tmp", file_num++);
119 int fd = ki_open(filename, O_WRONLY | O_CREAT); 119 int fd = ki_open(filename, O_WRONLY | O_CREAT, 0777);
120 ASSERT_GT(fd, -1); 120 ASSERT_GT(fd, -1);
121 ASSERT_EQ(1, root->ChildCount()); 121 ASSERT_EQ(1, root->ChildCount());
122 ASSERT_EQ(buffer_size, ki_write(fd, garbage, buffer_size)); 122 ASSERT_EQ(buffer_size, ki_write(fd, garbage, buffer_size));
123 ki_close(fd); 123 ki_close(fd);
124 ASSERT_EQ(0, ki_remove(filename)); 124 ASSERT_EQ(0, ki_remove(filename));
125 } 125 }
126 ASSERT_EQ(0, root->ChildCount()); 126 ASSERT_EQ(0, root->ChildCount());
127 } 127 }
128 128
129 static bool g_handler_called = false; 129 static bool g_handler_called = false;
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 225
226 // Restore existing handler 226 // Restore existing handler
227 oldsig = ki_signal(SIGWINCH, oldsig); 227 oldsig = ki_signal(SIGWINCH, oldsig);
228 228
229 // Verify the our newsig was returned as previous handler 229 // Verify the our newsig was returned as previous handler
230 ASSERT_EQ(oldsig, newsig); 230 ASSERT_EQ(oldsig, newsig);
231 } 231 }
232 232
233 TEST_F(KernelProxyTest, Rename) { 233 TEST_F(KernelProxyTest, Rename) {
234 // Create a dummy file 234 // Create a dummy file
235 int file1 = ki_open("/test1.txt", O_RDWR | O_CREAT); 235 int file1 = ki_open("/test1.txt", O_RDWR | O_CREAT, 0777);
236 ASSERT_GT(file1, -1); 236 ASSERT_GT(file1, -1);
237 ASSERT_EQ(0, ki_close(file1)); 237 ASSERT_EQ(0, ki_close(file1));
238 238
239 // Test the renaming works 239 // Test the renaming works
240 ASSERT_EQ(0, ki_rename("/test1.txt", "/test2.txt")); 240 ASSERT_EQ(0, ki_rename("/test1.txt", "/test2.txt"));
241 241
242 // Test that renaming across mount points fails 242 // Test that renaming across mount points fails
243 ASSERT_EQ(0, ki_mount("", "/foo", "memfs", 0, "")); 243 ASSERT_EQ(0, ki_mount("", "/foo", "memfs", 0, ""));
244 ASSERT_EQ(-1, ki_rename("/test2.txt", "/foo/test2.txt")); 244 ASSERT_EQ(-1, ki_rename("/test2.txt", "/foo/test2.txt"));
245 ASSERT_EQ(EXDEV, errno); 245 ASSERT_EQ(EXDEV, errno);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 TEST_F(KernelProxyTest, FDPathMapping) { 286 TEST_F(KernelProxyTest, FDPathMapping) {
287 char text[1024]; 287 char text[1024];
288 288
289 int fd1, fd2, fd3, fd4, fd5; 289 int fd1, fd2, fd3, fd4, fd5;
290 290
291 EXPECT_EQ(0, ki_mkdir("/foo", S_IREAD | S_IWRITE)); 291 EXPECT_EQ(0, ki_mkdir("/foo", S_IREAD | S_IWRITE));
292 EXPECT_EQ(0, ki_mkdir("/foo/bar", S_IREAD | S_IWRITE)); 292 EXPECT_EQ(0, ki_mkdir("/foo/bar", S_IREAD | S_IWRITE));
293 EXPECT_EQ(0, ki_mkdir("/example", S_IREAD | S_IWRITE)); 293 EXPECT_EQ(0, ki_mkdir("/example", S_IREAD | S_IWRITE));
294 ki_chdir("/foo"); 294 ki_chdir("/foo");
295 295
296 fd1 = ki_open("/example", O_RDONLY); 296 fd1 = ki_open("/example", O_RDONLY, 0);
297 EXPECT_NE(-1, fd1); 297 EXPECT_NE(-1, fd1);
298 EXPECT_EQ(ki_fchdir(fd1), 0); 298 EXPECT_EQ(ki_fchdir(fd1), 0);
299 EXPECT_EQ(text, ki_getcwd(text, sizeof(text))); 299 EXPECT_EQ(text, ki_getcwd(text, sizeof(text)));
300 EXPECT_STREQ("/example", text); 300 EXPECT_STREQ("/example", text);
301 301
302 EXPECT_EQ(0, ki_chdir("/foo")); 302 EXPECT_EQ(0, ki_chdir("/foo"));
303 fd2 = ki_open("../example", O_RDONLY); 303 fd2 = ki_open("../example", O_RDONLY, 0);
304 EXPECT_NE(-1, fd2); 304 EXPECT_NE(-1, fd2);
305 EXPECT_EQ(0, ki_fchdir(fd2)); 305 EXPECT_EQ(0, ki_fchdir(fd2));
306 EXPECT_EQ(text, ki_getcwd(text, sizeof(text))); 306 EXPECT_EQ(text, ki_getcwd(text, sizeof(text)));
307 EXPECT_STREQ("/example", text); 307 EXPECT_STREQ("/example", text);
308 308
309 EXPECT_EQ(0, ki_chdir("/foo")); 309 EXPECT_EQ(0, ki_chdir("/foo"));
310 fd3 = ki_open("../test", O_CREAT | O_RDWR); 310 fd3 = ki_open("../test", O_CREAT | O_RDWR, 0777);
311 EXPECT_NE(-1, fd3); 311 EXPECT_NE(-1, fd3);
312 EXPECT_EQ(-1, ki_fchdir(fd3)); 312 EXPECT_EQ(-1, ki_fchdir(fd3));
313 EXPECT_EQ(ENOTDIR, errno); 313 EXPECT_EQ(ENOTDIR, errno);
314 314
315 EXPECT_EQ(0, ki_chdir("/foo")); 315 EXPECT_EQ(0, ki_chdir("/foo"));
316 fd4 = ki_open("bar", O_RDONLY); 316 fd4 = ki_open("bar", O_RDONLY, 0);
317 EXPECT_EQ(0, ki_fchdir(fd4)); 317 EXPECT_EQ(0, ki_fchdir(fd4));
318 EXPECT_EQ(text, ki_getcwd(text, sizeof(text))); 318 EXPECT_EQ(text, ki_getcwd(text, sizeof(text)));
319 EXPECT_STREQ("/foo/bar", text); 319 EXPECT_STREQ("/foo/bar", text);
320 EXPECT_EQ(0, ki_chdir("/example")); 320 EXPECT_EQ(0, ki_chdir("/example"));
321 EXPECT_EQ(0, ki_fchdir(fd4)); 321 EXPECT_EQ(0, ki_fchdir(fd4));
322 EXPECT_EQ(text, ki_getcwd(text, sizeof(text))); 322 EXPECT_EQ(text, ki_getcwd(text, sizeof(text)));
323 EXPECT_STREQ("/foo/bar", text); 323 EXPECT_STREQ("/foo/bar", text);
324 324
325 EXPECT_EQ(0, ki_chdir("/example")); 325 EXPECT_EQ(0, ki_chdir("/example"));
326 fd5 = ki_dup(fd4); 326 fd5 = ki_dup(fd4);
(...skipping 26 matching lines...) Expand all
353 EXPECT_EQ(-1, ki_mkdir("/foo", S_IREAD | S_IWRITE)); 353 EXPECT_EQ(-1, ki_mkdir("/foo", S_IREAD | S_IWRITE));
354 EXPECT_EQ(EEXIST, errno); 354 EXPECT_EQ(EEXIST, errno);
355 355
356 // Delete "/foo" 356 // Delete "/foo"
357 EXPECT_EQ(0, ki_rmdir("/foo")); 357 EXPECT_EQ(0, ki_rmdir("/foo"));
358 358
359 // Recreate "/foo" 359 // Recreate "/foo"
360 EXPECT_EQ(0, ki_mkdir("/foo", S_IREAD | S_IWRITE)); 360 EXPECT_EQ(0, ki_mkdir("/foo", S_IREAD | S_IWRITE));
361 361
362 // Fail to open "/foo/bar" 362 // Fail to open "/foo/bar"
363 EXPECT_EQ(-1, ki_open("/foo/bar", O_RDONLY)); 363 EXPECT_EQ(-1, ki_open("/foo/bar", O_RDONLY, 0));
364 EXPECT_EQ(ENOENT, errno); 364 EXPECT_EQ(ENOENT, errno);
365 365
366 // Create bar "/foo/bar" 366 // Create bar "/foo/bar"
367 fd1 = ki_open("/foo/bar", O_RDWR | O_CREAT); 367 fd1 = ki_open("/foo/bar", O_RDWR | O_CREAT, 0777);
368 ASSERT_NE(-1, fd1); 368 ASSERT_NE(-1, fd1);
369 369
370 // Open (optionally create) bar "/foo/bar" 370 // Open (optionally create) bar "/foo/bar"
371 fd2 = ki_open("/foo/bar", O_RDWR | O_CREAT); 371 fd2 = ki_open("/foo/bar", O_RDWR | O_CREAT, 0777);
372 ASSERT_NE(-1, fd2); 372 ASSERT_NE(-1, fd2);
373 373
374 // Fail to exclusively create bar "/foo/bar" 374 // Fail to exclusively create bar "/foo/bar"
375 EXPECT_EQ(-1, ki_open("/foo/bar", O_RDONLY | O_CREAT | O_EXCL)); 375 EXPECT_EQ(-1, ki_open("/foo/bar", O_RDONLY | O_CREAT | O_EXCL, 0777));
376 EXPECT_EQ(EEXIST, errno); 376 EXPECT_EQ(EEXIST, errno);
377 377
378 // Write hello and world to same node with different descriptors 378 // Write hello and world to same node with different descriptors
379 // so that we overwrite each other 379 // so that we overwrite each other
380 EXPECT_EQ(5, ki_write(fd2, "WORLD", 5)); 380 EXPECT_EQ(5, ki_write(fd2, "WORLD", 5));
381 EXPECT_EQ(5, ki_write(fd1, "HELLO", 5)); 381 EXPECT_EQ(5, ki_write(fd1, "HELLO", 5));
382 382
383 fd3 = ki_open("/foo/bar", O_RDONLY); 383 fd3 = ki_open("/foo/bar", O_RDONLY, 0);
384 ASSERT_NE(-1, fd3); 384 ASSERT_NE(-1, fd3);
385 385
386 len = ki_read(fd3, text, sizeof(text)); 386 len = ki_read(fd3, text, sizeof(text));
387 ASSERT_EQ(5, len); 387 ASSERT_EQ(5, len);
388 text[len] = 0; 388 text[len] = 0;
389 EXPECT_STREQ("HELLO", text); 389 EXPECT_STREQ("HELLO", text);
390 EXPECT_EQ(0, ki_close(fd1)); 390 EXPECT_EQ(0, ki_close(fd1));
391 EXPECT_EQ(0, ki_close(fd2)); 391 EXPECT_EQ(0, ki_close(fd2));
392 392
393 fd1 = ki_open("/foo/bar", O_WRONLY | O_APPEND); 393 fd1 = ki_open("/foo/bar", O_WRONLY | O_APPEND, 0);
394 ASSERT_NE(-1, fd1); 394 ASSERT_NE(-1, fd1);
395 EXPECT_EQ(5, ki_write(fd1, "WORLD", 5)); 395 EXPECT_EQ(5, ki_write(fd1, "WORLD", 5));
396 396
397 len = ki_read(fd3, text, sizeof(text)); 397 len = ki_read(fd3, text, sizeof(text));
398 ASSERT_EQ(5, len); 398 ASSERT_EQ(5, len);
399 text[len] = 0; 399 text[len] = 0;
400 EXPECT_STREQ("WORLD", text); 400 EXPECT_STREQ("WORLD", text);
401 401
402 fd2 = ki_open("/foo/bar", O_RDONLY); 402 fd2 = ki_open("/foo/bar", O_RDONLY, 0);
403 ASSERT_NE(-1, fd2); 403 ASSERT_NE(-1, fd2);
404 len = ki_read(fd2, text, sizeof(text)); 404 len = ki_read(fd2, text, sizeof(text));
405 if (len > 0) 405 if (len > 0)
406 text[len] = 0; 406 text[len] = 0;
407 EXPECT_EQ(10, len); 407 EXPECT_EQ(10, len);
408 EXPECT_STREQ("HELLOWORLD", text); 408 EXPECT_STREQ("HELLOWORLD", text);
409 } 409 }
410 410
411 TEST_F(KernelProxyTest, MemMountFTruncate) { 411 TEST_F(KernelProxyTest, MemMountFTruncate) {
412 char text[1024]; 412 char text[1024];
413 int fd1, fd2; 413 int fd1, fd2;
414 414
415 // Open a file write only, write some text, then test that using a 415 // Open a file write only, write some text, then test that using a
416 // separate file descriptor pointing to it that it is correctly 416 // separate file descriptor pointing to it that it is correctly
417 // truncated at a specified number of bytes (2). 417 // truncated at a specified number of bytes (2).
418 fd1 = ki_open("/trunc", O_WRONLY | O_CREAT); 418 fd1 = ki_open("/trunc", O_WRONLY | O_CREAT, 0777);
419 ASSERT_NE(-1, fd1); 419 ASSERT_NE(-1, fd1);
420 fd2 = ki_open("/trunc", O_RDONLY); 420 fd2 = ki_open("/trunc", O_RDONLY, 0);
421 ASSERT_NE(-1, fd2); 421 ASSERT_NE(-1, fd2);
422 EXPECT_EQ(5, ki_write(fd1, "HELLO", 5)); 422 EXPECT_EQ(5, ki_write(fd1, "HELLO", 5));
423 EXPECT_EQ(0, ki_ftruncate(fd1, 2)); 423 EXPECT_EQ(0, ki_ftruncate(fd1, 2));
424 // Verify the remaining file (using fd2, opened pre-truncation) is 424 // Verify the remaining file (using fd2, opened pre-truncation) is
425 // only 2 bytes in length. 425 // only 2 bytes in length.
426 EXPECT_EQ(2, ki_read(fd2, text, sizeof(text))); 426 EXPECT_EQ(2, ki_read(fd2, text, sizeof(text)));
427 EXPECT_EQ(0, ki_close(fd1)); 427 EXPECT_EQ(0, ki_close(fd1));
428 EXPECT_EQ(0, ki_close(fd2)); 428 EXPECT_EQ(0, ki_close(fd2));
429 } 429 }
430 430
431 TEST_F(KernelProxyTest, MemMountTruncate) { 431 TEST_F(KernelProxyTest, MemMountTruncate) {
432 char text[1024]; 432 char text[1024];
433 int fd1; 433 int fd1;
434 434
435 // Open a file write only, write some text, then test that by 435 // Open a file write only, write some text, then test that by
436 // referring to it by its path and truncating it we correctly truncate 436 // referring to it by its path and truncating it we correctly truncate
437 // it at a specified number of bytes (2). 437 // it at a specified number of bytes (2).
438 fd1 = ki_open("/trunc", O_WRONLY | O_CREAT); 438 fd1 = ki_open("/trunc", O_WRONLY | O_CREAT, 0777);
439 ASSERT_NE(-1, fd1); 439 ASSERT_NE(-1, fd1);
440 EXPECT_EQ(5, ki_write(fd1, "HELLO", 5)); 440 EXPECT_EQ(5, ki_write(fd1, "HELLO", 5));
441 EXPECT_EQ(0, ki_close(fd1)); 441 EXPECT_EQ(0, ki_close(fd1));
442 EXPECT_EQ(0, ki_truncate("/trunc", 2)); 442 EXPECT_EQ(0, ki_truncate("/trunc", 2));
443 // Verify the text is only 2 bytes long with new file descriptor. 443 // Verify the text is only 2 bytes long with new file descriptor.
444 fd1 = ki_open("/trunc", O_RDONLY); 444 fd1 = ki_open("/trunc", O_RDONLY, 0);
445 ASSERT_NE(-1, fd1); 445 ASSERT_NE(-1, fd1);
446 EXPECT_EQ(2, ki_read(fd1, text, sizeof(text))); 446 EXPECT_EQ(2, ki_read(fd1, text, sizeof(text)));
447 EXPECT_EQ(0, ki_close(fd1)); 447 EXPECT_EQ(0, ki_close(fd1));
448 } 448 }
449 449
450 TEST_F(KernelProxyTest, MemMountLseek) { 450 TEST_F(KernelProxyTest, MemMountLseek) {
451 int fd = ki_open("/foo", O_CREAT | O_RDWR); 451 int fd = ki_open("/foo", O_CREAT | O_RDWR, 0777);
452 ASSERT_GT(fd, -1); 452 ASSERT_GT(fd, -1);
453 ASSERT_EQ(9, ki_write(fd, "Some text", 9)); 453 ASSERT_EQ(9, ki_write(fd, "Some text", 9));
454 454
455 ASSERT_EQ(9, ki_lseek(fd, 0, SEEK_CUR)); 455 ASSERT_EQ(9, ki_lseek(fd, 0, SEEK_CUR));
456 ASSERT_EQ(9, ki_lseek(fd, 0, SEEK_END)); 456 ASSERT_EQ(9, ki_lseek(fd, 0, SEEK_END));
457 ASSERT_EQ(-1, ki_lseek(fd, -1, SEEK_SET)); 457 ASSERT_EQ(-1, ki_lseek(fd, -1, SEEK_SET));
458 ASSERT_EQ(EINVAL, errno); 458 ASSERT_EQ(EINVAL, errno);
459 459
460 // Seek past end of file. 460 // Seek past end of file.
461 ASSERT_EQ(13, ki_lseek(fd, 13, SEEK_SET)); 461 ASSERT_EQ(13, ki_lseek(fd, 13, SEEK_SET));
462 char buffer[4]; 462 char buffer[4];
463 memset(&buffer[0], 0xfe, 4); 463 memset(&buffer[0], 0xfe, 4);
464 ASSERT_EQ(9, ki_lseek(fd, -4, SEEK_END)); 464 ASSERT_EQ(9, ki_lseek(fd, -4, SEEK_END));
465 ASSERT_EQ(9, ki_lseek(fd, 0, SEEK_CUR)); 465 ASSERT_EQ(9, ki_lseek(fd, 0, SEEK_CUR));
466 ASSERT_EQ(4, ki_read(fd, &buffer[0], 4)); 466 ASSERT_EQ(4, ki_read(fd, &buffer[0], 4));
467 ASSERT_EQ(0, memcmp("\0\0\0\0", buffer, 4)); 467 ASSERT_EQ(0, memcmp("\0\0\0\0", buffer, 4));
468 } 468 }
469 469
470 TEST_F(KernelProxyTest, CloseTwice) { 470 TEST_F(KernelProxyTest, CloseTwice) {
471 int fd = ki_open("/foo", O_CREAT | O_RDWR); 471 int fd = ki_open("/foo", O_CREAT | O_RDWR, 0777);
472 ASSERT_GT(fd, -1); 472 ASSERT_GT(fd, -1);
473 473
474 EXPECT_EQ(9, ki_write(fd, "Some text", 9)); 474 EXPECT_EQ(9, ki_write(fd, "Some text", 9));
475 475
476 int fd2 = ki_dup(fd); 476 int fd2 = ki_dup(fd);
477 ASSERT_GT(fd2, -1); 477 ASSERT_GT(fd2, -1);
478 478
479 EXPECT_EQ(0, ki_close(fd)); 479 EXPECT_EQ(0, ki_close(fd));
480 EXPECT_EQ(0, ki_close(fd2)); 480 EXPECT_EQ(0, ki_close(fd2));
481 } 481 }
482 482
483 TEST_F(KernelProxyTest, MemMountDup) { 483 TEST_F(KernelProxyTest, MemMountDup) {
484 int fd = ki_open("/foo", O_CREAT | O_RDWR); 484 int fd = ki_open("/foo", O_CREAT | O_RDWR, 0777);
485 ASSERT_GT(fd, -1); 485 ASSERT_GT(fd, -1);
486 486
487 int dup_fd = ki_dup(fd); 487 int dup_fd = ki_dup(fd);
488 ASSERT_NE(-1, dup_fd); 488 ASSERT_NE(-1, dup_fd);
489 489
490 ASSERT_EQ(9, ki_write(fd, "Some text", 9)); 490 ASSERT_EQ(9, ki_write(fd, "Some text", 9));
491 ASSERT_EQ(9, ki_lseek(fd, 0, SEEK_CUR)); 491 ASSERT_EQ(9, ki_lseek(fd, 0, SEEK_CUR));
492 ASSERT_EQ(9, ki_lseek(dup_fd, 0, SEEK_CUR)); 492 ASSERT_EQ(9, ki_lseek(dup_fd, 0, SEEK_CUR));
493 493
494 int dup2_fd = 123; 494 int dup2_fd = 123;
495 ASSERT_EQ(dup2_fd, ki_dup2(fd, dup2_fd)); 495 ASSERT_EQ(dup2_fd, ki_dup2(fd, dup2_fd));
496 ASSERT_EQ(9, ki_lseek(dup2_fd, 0, SEEK_CUR)); 496 ASSERT_EQ(9, ki_lseek(dup2_fd, 0, SEEK_CUR));
497 497
498 int new_fd = ki_open("/bar", O_CREAT | O_RDWR); 498 int new_fd = ki_open("/bar", O_CREAT | O_RDWR, 0777);
499 499
500 ASSERT_EQ(fd, ki_dup2(new_fd, fd)); 500 ASSERT_EQ(fd, ki_dup2(new_fd, fd));
501 // fd, new_fd -> "/bar" 501 // fd, new_fd -> "/bar"
502 // dup_fd, dup2_fd -> "/foo" 502 // dup_fd, dup2_fd -> "/foo"
503 503
504 // We should still be able to write to dup_fd (i.e. it should not be closed). 504 // We should still be able to write to dup_fd (i.e. it should not be closed).
505 ASSERT_EQ(4, ki_write(dup_fd, "more", 4)); 505 ASSERT_EQ(4, ki_write(dup_fd, "more", 4));
506 506
507 ASSERT_EQ(0, ki_close(dup2_fd)); 507 ASSERT_EQ(0, ki_close(dup2_fd));
508 // fd, new_fd -> "/bar" 508 // fd, new_fd -> "/bar"
509 // dup_fd -> "/foo" 509 // dup_fd -> "/foo"
510 510
511 ASSERT_EQ(dup_fd, ki_dup2(fd, dup_fd)); 511 ASSERT_EQ(dup_fd, ki_dup2(fd, dup_fd));
512 // fd, new_fd, dup_fd -> "/bar" 512 // fd, new_fd, dup_fd -> "/bar"
513 } 513 }
514 514
515 TEST_F(KernelProxyTest, Lstat) { 515 TEST_F(KernelProxyTest, Lstat) {
516 int fd = ki_open("/foo", O_CREAT | O_RDWR); 516 int fd = ki_open("/foo", O_CREAT | O_RDWR, 0777);
517 ASSERT_GT(fd, -1); 517 ASSERT_GT(fd, -1);
518 ASSERT_EQ(0, ki_mkdir("/bar", S_IREAD | S_IWRITE)); 518 ASSERT_EQ(0, ki_mkdir("/bar", S_IREAD | S_IWRITE));
519 519
520 struct stat buf; 520 struct stat buf;
521 EXPECT_EQ(0, ki_lstat("/foo", &buf)); 521 EXPECT_EQ(0, ki_lstat("/foo", &buf));
522 EXPECT_EQ(0, buf.st_size); 522 EXPECT_EQ(0, buf.st_size);
523 EXPECT_TRUE(S_ISREG(buf.st_mode)); 523 EXPECT_TRUE(S_ISREG(buf.st_mode));
524 524
525 EXPECT_EQ(0, ki_lstat("/bar", &buf)); 525 EXPECT_EQ(0, ki_lstat("/bar", &buf));
526 EXPECT_EQ(0, buf.st_size); 526 EXPECT_EQ(0, buf.st_size);
527 EXPECT_TRUE(S_ISDIR(buf.st_mode)); 527 EXPECT_TRUE(S_ISDIR(buf.st_mode));
528 528
529 EXPECT_EQ(-1, ki_lstat("/no-such-file", &buf)); 529 EXPECT_EQ(-1, ki_lstat("/no-such-file", &buf));
530 EXPECT_EQ(ENOENT, errno); 530 EXPECT_EQ(ENOENT, errno);
531 } 531 }
532 532
533 TEST_F(KernelProxyTest, OpenWithMode) {
534 int fd = ki_open("/foo", O_CREAT | O_RDWR, 0723);
535 ASSERT_GT(fd, -1);
536
537 struct stat buf;
538 EXPECT_EQ(0, ki_lstat("/foo", &buf));
539 EXPECT_EQ(0723, buf.st_mode & ~S_IFMT);
540 }
541
533 TEST_F(KernelProxyTest, UseAfterClose) { 542 TEST_F(KernelProxyTest, UseAfterClose) {
534 int fd = ki_open("/dummy", O_CREAT | O_WRONLY); 543 int fd = ki_open("/dummy", O_CREAT | O_WRONLY, 0777);
535 ASSERT_GT(fd, -1); 544 ASSERT_GT(fd, -1);
536 EXPECT_EQ(5, ki_write(fd, "hello", 5)); 545 EXPECT_EQ(5, ki_write(fd, "hello", 5));
537 EXPECT_EQ(0, ki_close(fd)); 546 EXPECT_EQ(0, ki_close(fd));
538 EXPECT_EQ(-1, ki_write(fd, "hello", 5)); 547 EXPECT_EQ(-1, ki_write(fd, "hello", 5));
539 EXPECT_EQ(EBADF, errno); 548 EXPECT_EQ(EBADF, errno);
540 } 549 }
541 550
542 namespace { 551 namespace {
543 552
544 StringMap_t g_string_map; 553 StringMap_t g_string_map;
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
620 EXPECT_EQ("y", g_string_map["x"]); 629 EXPECT_EQ("y", g_string_map["x"]);
621 } 630 }
622 631
623 TEST_F(KernelProxyMountTest, MountAndIoctl) { 632 TEST_F(KernelProxyMountTest, MountAndIoctl) {
624 ASSERT_EQ(0, ki_mount("/", "/mnt1", "initfs", 0, "")); 633 ASSERT_EQ(0, ki_mount("/", "/mnt1", "initfs", 0, ""));
625 ASSERT_NE(-1, g_fs_dev); 634 ASSERT_NE(-1, g_fs_dev);
626 635
627 char path[100]; 636 char path[100];
628 snprintf(path, 100, "dev/fs/%d", g_fs_dev); 637 snprintf(path, 100, "dev/fs/%d", g_fs_dev);
629 638
630 int fd = ki_open(path, O_RDONLY); 639 int fd = ki_open(path, O_RDONLY, 0);
631 ASSERT_GT(fd, -1); 640 ASSERT_GT(fd, -1);
632 641
633 EXPECT_EQ(0, ki_ioctl_wrapper(fd, 0xdeadbeef)); 642 EXPECT_EQ(0, ki_ioctl_wrapper(fd, 0xdeadbeef));
634 EXPECT_EQ(true, g_fs_ioctl_called); 643 EXPECT_EQ(true, g_fs_ioctl_called);
635 } 644 }
636 645
637 static void mount_callback(const char* source, 646 static void mount_callback(const char* source,
638 const char* target, 647 const char* target,
639 const char* filesystemtype, 648 const char* filesystemtype,
640 unsigned long mountflags, 649 unsigned long mountflags,
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
694 703
695 return 0; 704 return 0;
696 } 705 }
697 706
698 private: 707 private:
699 int node_mmap_count_; 708 int node_mmap_count_;
700 }; 709 };
701 710
702 class KernelProxyMMapTest_Filesystem : public Filesystem { 711 class KernelProxyMMapTest_Filesystem : public Filesystem {
703 public: 712 public:
704 virtual Error Open(const Path& path, int mode, ScopedNode* out_node) { 713 virtual Error OpenWithMode(const Path& path, int open_flags,
714 mode_t mode, ScopedNode* out_node) {
705 out_node->reset(new KernelProxyMMapTest_Node(this)); 715 out_node->reset(new KernelProxyMMapTest_Node(this));
706 return 0; 716 return 0;
707 } 717 }
708 718
709 virtual Error OpenResource(const Path& path, ScopedNode* out_node) { 719 virtual Error OpenResource(const Path& path, ScopedNode* out_node) {
710 out_node->reset(NULL); 720 out_node->reset(NULL);
711 return ENOSYS; 721 return ENOSYS;
712 } 722 }
713 virtual Error Unlink(const Path& path) { return ENOSYS; } 723 virtual Error Unlink(const Path& path) { return ENOSYS; }
714 virtual Error Mkdir(const Path& path, int permissions) { return ENOSYS; } 724 virtual Error Mkdir(const Path& path, int permissions) { return ENOSYS; }
(...skipping 25 matching lines...) Expand all
740 750
741 private: 751 private:
742 KernelProxyMMapTest_KernelProxy kp_; 752 KernelProxyMMapTest_KernelProxy kp_;
743 }; 753 };
744 754
745 } // namespace 755 } // namespace
746 756
747 TEST_F(KernelProxyMMapTest, MMap) { 757 TEST_F(KernelProxyMMapTest, MMap) {
748 ASSERT_EQ(0, ki_umount("/")); 758 ASSERT_EQ(0, ki_umount("/"));
749 ASSERT_EQ(0, ki_mount("", "/", "mmapfs", 0, NULL)); 759 ASSERT_EQ(0, ki_mount("", "/", "mmapfs", 0, NULL));
750 int fd = ki_open("/file", O_RDWR | O_CREAT); 760 int fd = ki_open("/file", O_RDWR | O_CREAT, 0777);
751 ASSERT_NE(-1, fd); 761 ASSERT_NE(-1, fd);
752 762
753 void* addr1 = ki_mmap(NULL, 0x800, PROT_READ, MAP_PRIVATE, fd, 0); 763 void* addr1 = ki_mmap(NULL, 0x800, PROT_READ, MAP_PRIVATE, fd, 0);
754 ASSERT_EQ(reinterpret_cast<void*>(0x1000), addr1); 764 ASSERT_EQ(reinterpret_cast<void*>(0x1000), addr1);
755 ASSERT_EQ(1, g_MMapCount); 765 ASSERT_EQ(1, g_MMapCount);
756 766
757 void* addr2 = ki_mmap(NULL, 0x800, PROT_READ, MAP_PRIVATE, fd, 0); 767 void* addr2 = ki_mmap(NULL, 0x800, PROT_READ, MAP_PRIVATE, fd, 0);
758 ASSERT_EQ(reinterpret_cast<void*>(0x2000), addr2); 768 ASSERT_EQ(reinterpret_cast<void*>(0x2000), addr2);
759 ASSERT_EQ(2, g_MMapCount); 769 ASSERT_EQ(2, g_MMapCount);
760 770
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
822 832
823 private: 833 private:
824 KernelProxyErrorTest_KernelProxy kp_; 834 KernelProxyErrorTest_KernelProxy kp_;
825 }; 835 };
826 836
827 } // namespace 837 } // namespace
828 838
829 TEST_F(KernelProxyErrorTest, WriteError) { 839 TEST_F(KernelProxyErrorTest, WriteError) {
830 ScopedRef<MockFs> mock_fs(fs()); 840 ScopedRef<MockFs> mock_fs(fs());
831 ScopedRef<MockNode> mock_node(new MockNode(&*mock_fs)); 841 ScopedRef<MockNode> mock_node(new MockNode(&*mock_fs));
832 EXPECT_CALL(*mock_fs, Open(_, _, _)) 842 EXPECT_CALL(*mock_fs, OpenWithMode(_, _, _, _))
833 .WillOnce(DoAll(SetArgPointee<2>(mock_node), Return(0))); 843 .WillOnce(DoAll(SetArgPointee<3>(mock_node), Return(0)));
834 844
835 EXPECT_CALL(*mock_node, Write(_, _, _, _)) 845 EXPECT_CALL(*mock_node, Write(_, _, _, _))
836 .WillOnce(DoAll(SetArgPointee<3>(0), // Wrote 0 bytes. 846 .WillOnce(DoAll(SetArgPointee<3>(0), // Wrote 0 bytes.
837 Return(1234))); // Returned error 1234. 847 Return(1234))); // Returned error 1234.
838 848
839 EXPECT_CALL(*mock_node, Destroy()).Times(1); 849 EXPECT_CALL(*mock_node, Destroy()).Times(1);
840 850
841 int fd = ki_open("/dummy", O_WRONLY); 851 int fd = ki_open("/dummy", O_WRONLY, 0);
842 EXPECT_NE(0, fd); 852 EXPECT_NE(0, fd);
843 853
844 char buf[20]; 854 char buf[20];
845 EXPECT_EQ(-1, ki_write(fd, &buf[0], 20)); 855 EXPECT_EQ(-1, ki_write(fd, &buf[0], 20));
846 // The Filesystem should be able to return whatever error it wants and have it 856 // The Filesystem should be able to return whatever error it wants and have it
847 // propagate through. 857 // propagate through.
848 EXPECT_EQ(1234, errno); 858 EXPECT_EQ(1234, errno);
849 } 859 }
850 860
851 TEST_F(KernelProxyErrorTest, ReadError) { 861 TEST_F(KernelProxyErrorTest, ReadError) {
852 ScopedRef<MockFs> mock_fs(fs()); 862 ScopedRef<MockFs> mock_fs(fs());
853 ScopedRef<MockNode> mock_node(new MockNode(&*mock_fs)); 863 ScopedRef<MockNode> mock_node(new MockNode(&*mock_fs));
854 EXPECT_CALL(*mock_fs, Open(_, _, _)) 864 EXPECT_CALL(*mock_fs, OpenWithMode(_, _, _, _))
855 .WillOnce(DoAll(SetArgPointee<2>(mock_node), Return(0))); 865 .WillOnce(DoAll(SetArgPointee<3>(mock_node), Return(0)));
856 866
857 EXPECT_CALL(*mock_node, Read(_, _, _, _)) 867 EXPECT_CALL(*mock_node, Read(_, _, _, _))
858 .WillOnce(DoAll(SetArgPointee<3>(0), // Read 0 bytes. 868 .WillOnce(DoAll(SetArgPointee<3>(0), // Read 0 bytes.
859 Return(1234))); // Returned error 1234. 869 Return(1234))); // Returned error 1234.
860 870
861 EXPECT_CALL(*mock_node, Destroy()).Times(1); 871 EXPECT_CALL(*mock_node, Destroy()).Times(1);
862 872
863 int fd = ki_open("/dummy", O_RDONLY); 873 int fd = ki_open("/dummy", O_RDONLY, 0);
864 EXPECT_NE(0, fd); 874 EXPECT_NE(0, fd);
865 875
866 char buf[20]; 876 char buf[20];
867 EXPECT_EQ(-1, ki_read(fd, &buf[0], 20)); 877 EXPECT_EQ(-1, ki_read(fd, &buf[0], 20));
868 // The Filesystem should be able to return whatever error it wants and have it 878 // The Filesystem should be able to return whatever error it wants and have it
869 // propagate through. 879 // propagate through.
870 EXPECT_EQ(1234, errno); 880 EXPECT_EQ(1234, errno);
871 } 881 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698