OLD | NEW |
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 "base/atomicops.h" | 5 #include "base/atomicops.h" |
6 #include "base/basictypes.h" | 6 #include "base/basictypes.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/memory/shared_memory.h" | 8 #include "base/memory/shared_memory.h" |
9 #include "base/process/kill.h" | 9 #include "base/process/kill.h" |
10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 // segment read/write. I think the test here is stronger than we actually | 353 // segment read/write. I think the test here is stronger than we actually |
354 // care about, but there's a remote possibility that sending a file over a | 354 // care about, but there's a remote possibility that sending a file over a |
355 // pipe would transform it into read/write. | 355 // pipe would transform it into read/write. |
356 SharedMemoryHandle handle = readonly_shmem.handle(); | 356 SharedMemoryHandle handle = readonly_shmem.handle(); |
357 | 357 |
358 #if defined(OS_ANDROID) | 358 #if defined(OS_ANDROID) |
359 // The "read-only" handle is still writable on Android: | 359 // The "read-only" handle is still writable on Android: |
360 // http://crbug.com/320865 | 360 // http://crbug.com/320865 |
361 (void)handle; | 361 (void)handle; |
362 #elif defined(OS_POSIX) | 362 #elif defined(OS_POSIX) |
363 int handle_fd = SharedMemory::GetFdFromSharedMemoryHandle(handle); | 363 EXPECT_EQ(O_RDONLY, fcntl(handle.fd, F_GETFL) & O_ACCMODE) |
364 EXPECT_EQ(O_RDONLY, fcntl(handle_fd, F_GETFL) & O_ACCMODE) | |
365 << "The descriptor itself should be read-only."; | 364 << "The descriptor itself should be read-only."; |
366 | 365 |
367 errno = 0; | 366 errno = 0; |
368 void* writable = mmap(NULL, contents.size(), PROT_READ | PROT_WRITE, | 367 void* writable = mmap( |
369 MAP_SHARED, handle_fd, 0); | 368 NULL, contents.size(), PROT_READ | PROT_WRITE, MAP_SHARED, handle.fd, 0); |
370 int mmap_errno = errno; | 369 int mmap_errno = errno; |
371 EXPECT_EQ(MAP_FAILED, writable) | 370 EXPECT_EQ(MAP_FAILED, writable) |
372 << "It shouldn't be possible to re-mmap the descriptor writable."; | 371 << "It shouldn't be possible to re-mmap the descriptor writable."; |
373 EXPECT_EQ(EACCES, mmap_errno) << strerror(mmap_errno); | 372 EXPECT_EQ(EACCES, mmap_errno) << strerror(mmap_errno); |
374 if (writable != MAP_FAILED) | 373 if (writable != MAP_FAILED) |
375 EXPECT_EQ(0, munmap(writable, readonly_shmem.mapped_size())); | 374 EXPECT_EQ(0, munmap(writable, readonly_shmem.mapped_size())); |
376 | 375 |
377 #elif defined(OS_WIN) | 376 #elif defined(OS_WIN) |
378 EXPECT_EQ(NULL, MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, 0)) | 377 EXPECT_EQ(NULL, MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, 0)) |
379 << "Shouldn't be able to map memory writable."; | 378 << "Shouldn't be able to map memory writable."; |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 const uint32 kTestSize = 1 << 8; | 512 const uint32 kTestSize = 1 << 8; |
514 | 513 |
515 SharedMemory shared_memory; | 514 SharedMemory shared_memory; |
516 SharedMemoryCreateOptions options; | 515 SharedMemoryCreateOptions options; |
517 options.size = kTestSize; | 516 options.size = kTestSize; |
518 // Set a file mode creation mask that gives all permissions. | 517 // Set a file mode creation mask that gives all permissions. |
519 ScopedUmaskSetter permissive_mask(S_IWGRP | S_IWOTH); | 518 ScopedUmaskSetter permissive_mask(S_IWGRP | S_IWOTH); |
520 | 519 |
521 EXPECT_TRUE(shared_memory.Create(options)); | 520 EXPECT_TRUE(shared_memory.Create(options)); |
522 | 521 |
523 int shm_fd = | 522 int shm_fd = shared_memory.handle().fd; |
524 SharedMemory::GetFdFromSharedMemoryHandle(shared_memory.handle()); | |
525 struct stat shm_stat; | 523 struct stat shm_stat; |
526 EXPECT_EQ(0, fstat(shm_fd, &shm_stat)); | 524 EXPECT_EQ(0, fstat(shm_fd, &shm_stat)); |
527 // Neither the group, nor others should be able to read the shared memory | 525 // Neither the group, nor others should be able to read the shared memory |
528 // file. | 526 // file. |
529 EXPECT_FALSE(shm_stat.st_mode & S_IRWXO); | 527 EXPECT_FALSE(shm_stat.st_mode & S_IRWXO); |
530 EXPECT_FALSE(shm_stat.st_mode & S_IRWXG); | 528 EXPECT_FALSE(shm_stat.st_mode & S_IRWXG); |
531 } | 529 } |
532 | 530 |
533 // Create a shared memory object, check its permissions. | 531 // Create a shared memory object, check its permissions. |
534 TEST(SharedMemoryTest, FilePermissionsNamed) { | 532 TEST(SharedMemoryTest, FilePermissionsNamed) { |
535 const uint32 kTestSize = 1 << 8; | 533 const uint32 kTestSize = 1 << 8; |
536 | 534 |
537 SharedMemory shared_memory; | 535 SharedMemory shared_memory; |
538 SharedMemoryCreateOptions options; | 536 SharedMemoryCreateOptions options; |
539 options.size = kTestSize; | 537 options.size = kTestSize; |
540 std::string shared_mem_name = "shared_perm_test-" + IntToString(getpid()) + | 538 std::string shared_mem_name = "shared_perm_test-" + IntToString(getpid()) + |
541 "-" + Uint64ToString(RandUint64()); | 539 "-" + Uint64ToString(RandUint64()); |
542 options.name_deprecated = &shared_mem_name; | 540 options.name_deprecated = &shared_mem_name; |
543 // Set a file mode creation mask that gives all permissions. | 541 // Set a file mode creation mask that gives all permissions. |
544 ScopedUmaskSetter permissive_mask(S_IWGRP | S_IWOTH); | 542 ScopedUmaskSetter permissive_mask(S_IWGRP | S_IWOTH); |
545 | 543 |
546 EXPECT_TRUE(shared_memory.Create(options)); | 544 EXPECT_TRUE(shared_memory.Create(options)); |
547 // Clean-up the backing file name immediately, we don't need it. | 545 // Clean-up the backing file name immediately, we don't need it. |
548 EXPECT_TRUE(shared_memory.Delete(shared_mem_name)); | 546 EXPECT_TRUE(shared_memory.Delete(shared_mem_name)); |
549 | 547 |
550 int shm_fd = | 548 int shm_fd = shared_memory.handle().fd; |
551 SharedMemory::GetFdFromSharedMemoryHandle(shared_memory.handle()); | |
552 struct stat shm_stat; | 549 struct stat shm_stat; |
553 EXPECT_EQ(0, fstat(shm_fd, &shm_stat)); | 550 EXPECT_EQ(0, fstat(shm_fd, &shm_stat)); |
554 // Neither the group, nor others should have been able to open the shared | 551 // Neither the group, nor others should have been able to open the shared |
555 // memory file while its name existed. | 552 // memory file while its name existed. |
556 EXPECT_FALSE(shm_stat.st_mode & S_IRWXO); | 553 EXPECT_FALSE(shm_stat.st_mode & S_IRWXO); |
557 EXPECT_FALSE(shm_stat.st_mode & S_IRWXG); | 554 EXPECT_FALSE(shm_stat.st_mode & S_IRWXG); |
558 } | 555 } |
559 #endif // !defined(OS_ANDROID) | 556 #endif // !defined(OS_ANDROID) |
560 | 557 |
561 #endif // defined(OS_POSIX) | 558 #endif // defined(OS_POSIX) |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
650 SharedMemoryProcessTest::CleanUp(); | 647 SharedMemoryProcessTest::CleanUp(); |
651 } | 648 } |
652 | 649 |
653 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { | 650 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { |
654 return SharedMemoryProcessTest::TaskTestMain(); | 651 return SharedMemoryProcessTest::TaskTestMain(); |
655 } | 652 } |
656 | 653 |
657 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) | 654 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) |
658 | 655 |
659 } // namespace base | 656 } // namespace base |
OLD | NEW |