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