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/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/memory/shared_memory.h" | 7 #include "base/memory/shared_memory.h" |
8 #include "base/process/kill.h" | 8 #include "base/process/kill.h" |
9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 // segment read/write. I think the test here is stronger than we actually | 430 // segment read/write. I think the test here is stronger than we actually |
431 // care about, but there's a remote possibility that sending a file over a | 431 // care about, but there's a remote possibility that sending a file over a |
432 // pipe would transform it into read/write. | 432 // pipe would transform it into read/write. |
433 SharedMemoryHandle handle = readonly_shmem.handle(); | 433 SharedMemoryHandle handle = readonly_shmem.handle(); |
434 | 434 |
435 #if defined(OS_ANDROID) | 435 #if defined(OS_ANDROID) |
436 // The "read-only" handle is still writable on Android: | 436 // The "read-only" handle is still writable on Android: |
437 // http://crbug.com/320865 | 437 // http://crbug.com/320865 |
438 (void)handle; | 438 (void)handle; |
439 #elif defined(OS_POSIX) | 439 #elif defined(OS_POSIX) |
440 EXPECT_EQ(O_RDONLY, fcntl(handle.fd, F_GETFL) & O_ACCMODE) | 440 int handle_fd = SharedMemory::GetFdFromSharedMemoryHandle(handle); |
| 441 EXPECT_EQ(O_RDONLY, fcntl(handle_fd, F_GETFL) & O_ACCMODE) |
441 << "The descriptor itself should be read-only."; | 442 << "The descriptor itself should be read-only."; |
442 | 443 |
443 errno = 0; | 444 errno = 0; |
444 void* writable = mmap( | 445 void* writable = mmap(NULL, contents.size(), PROT_READ | PROT_WRITE, |
445 NULL, contents.size(), PROT_READ | PROT_WRITE, MAP_SHARED, handle.fd, 0); | 446 MAP_SHARED, handle_fd, 0); |
446 int mmap_errno = errno; | 447 int mmap_errno = errno; |
447 EXPECT_EQ(MAP_FAILED, writable) | 448 EXPECT_EQ(MAP_FAILED, writable) |
448 << "It shouldn't be possible to re-mmap the descriptor writable."; | 449 << "It shouldn't be possible to re-mmap the descriptor writable."; |
449 EXPECT_EQ(EACCES, mmap_errno) << strerror(mmap_errno); | 450 EXPECT_EQ(EACCES, mmap_errno) << strerror(mmap_errno); |
450 if (writable != MAP_FAILED) | 451 if (writable != MAP_FAILED) |
451 EXPECT_EQ(0, munmap(writable, readonly_shmem.mapped_size())); | 452 EXPECT_EQ(0, munmap(writable, readonly_shmem.mapped_size())); |
452 | 453 |
453 #elif defined(OS_WIN) | 454 #elif defined(OS_WIN) |
454 EXPECT_EQ(NULL, MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, 0)) | 455 EXPECT_EQ(NULL, MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, 0)) |
455 << "Shouldn't be able to map memory writable."; | 456 << "Shouldn't be able to map memory writable."; |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 const uint32 kTestSize = 1 << 8; | 590 const uint32 kTestSize = 1 << 8; |
590 | 591 |
591 SharedMemory shared_memory; | 592 SharedMemory shared_memory; |
592 SharedMemoryCreateOptions options; | 593 SharedMemoryCreateOptions options; |
593 options.size = kTestSize; | 594 options.size = kTestSize; |
594 // Set a file mode creation mask that gives all permissions. | 595 // Set a file mode creation mask that gives all permissions. |
595 ScopedUmaskSetter permissive_mask(S_IWGRP | S_IWOTH); | 596 ScopedUmaskSetter permissive_mask(S_IWGRP | S_IWOTH); |
596 | 597 |
597 EXPECT_TRUE(shared_memory.Create(options)); | 598 EXPECT_TRUE(shared_memory.Create(options)); |
598 | 599 |
599 int shm_fd = shared_memory.handle().fd; | 600 int shm_fd = |
| 601 SharedMemory::GetFdFromSharedMemoryHandle(shared_memory.handle()); |
600 struct stat shm_stat; | 602 struct stat shm_stat; |
601 EXPECT_EQ(0, fstat(shm_fd, &shm_stat)); | 603 EXPECT_EQ(0, fstat(shm_fd, &shm_stat)); |
602 // Neither the group, nor others should be able to read the shared memory | 604 // Neither the group, nor others should be able to read the shared memory |
603 // file. | 605 // file. |
604 EXPECT_FALSE(shm_stat.st_mode & S_IRWXO); | 606 EXPECT_FALSE(shm_stat.st_mode & S_IRWXO); |
605 EXPECT_FALSE(shm_stat.st_mode & S_IRWXG); | 607 EXPECT_FALSE(shm_stat.st_mode & S_IRWXG); |
606 } | 608 } |
607 | 609 |
608 // Create a shared memory object, check its permissions. | 610 // Create a shared memory object, check its permissions. |
609 TEST(SharedMemoryTest, FilePermissionsNamed) { | 611 TEST(SharedMemoryTest, FilePermissionsNamed) { |
610 const uint32 kTestSize = 1 << 8; | 612 const uint32 kTestSize = 1 << 8; |
611 | 613 |
612 SharedMemory shared_memory; | 614 SharedMemory shared_memory; |
613 SharedMemoryCreateOptions options; | 615 SharedMemoryCreateOptions options; |
614 options.size = kTestSize; | 616 options.size = kTestSize; |
615 std::string shared_mem_name = "shared_perm_test-" + IntToString(getpid()) + | 617 std::string shared_mem_name = "shared_perm_test-" + IntToString(getpid()) + |
616 "-" + Uint64ToString(RandUint64()); | 618 "-" + Uint64ToString(RandUint64()); |
617 options.name_deprecated = &shared_mem_name; | 619 options.name_deprecated = &shared_mem_name; |
618 // Set a file mode creation mask that gives all permissions. | 620 // Set a file mode creation mask that gives all permissions. |
619 ScopedUmaskSetter permissive_mask(S_IWGRP | S_IWOTH); | 621 ScopedUmaskSetter permissive_mask(S_IWGRP | S_IWOTH); |
620 | 622 |
621 EXPECT_TRUE(shared_memory.Create(options)); | 623 EXPECT_TRUE(shared_memory.Create(options)); |
622 // Clean-up the backing file name immediately, we don't need it. | 624 // Clean-up the backing file name immediately, we don't need it. |
623 EXPECT_TRUE(shared_memory.Delete(shared_mem_name)); | 625 EXPECT_TRUE(shared_memory.Delete(shared_mem_name)); |
624 | 626 |
625 int shm_fd = shared_memory.handle().fd; | 627 int shm_fd = |
| 628 SharedMemory::GetFdFromSharedMemoryHandle(shared_memory.handle()); |
626 struct stat shm_stat; | 629 struct stat shm_stat; |
627 EXPECT_EQ(0, fstat(shm_fd, &shm_stat)); | 630 EXPECT_EQ(0, fstat(shm_fd, &shm_stat)); |
628 // Neither the group, nor others should have been able to open the shared | 631 // Neither the group, nor others should have been able to open the shared |
629 // memory file while its name existed. | 632 // memory file while its name existed. |
630 EXPECT_FALSE(shm_stat.st_mode & S_IRWXO); | 633 EXPECT_FALSE(shm_stat.st_mode & S_IRWXO); |
631 EXPECT_FALSE(shm_stat.st_mode & S_IRWXG); | 634 EXPECT_FALSE(shm_stat.st_mode & S_IRWXG); |
632 } | 635 } |
633 #endif // !defined(OS_ANDROID) | 636 #endif // !defined(OS_ANDROID) |
634 | 637 |
635 #endif // defined(OS_POSIX) | 638 #endif // defined(OS_POSIX) |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
714 SharedMemoryProcessTest::CleanUp(); | 717 SharedMemoryProcessTest::CleanUp(); |
715 } | 718 } |
716 | 719 |
717 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { | 720 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { |
718 return SharedMemoryProcessTest::TaskTestMain(); | 721 return SharedMemoryProcessTest::TaskTestMain(); |
719 } | 722 } |
720 | 723 |
721 #endif // !OS_IOS | 724 #endif // !OS_IOS |
722 | 725 |
723 } // namespace base | 726 } // namespace base |
OLD | NEW |