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 #if defined(OS_MACOSX) | 6 #if defined(OS_MACOSX) |
7 #include "base/mac/scoped_nsautorelease_pool.h" | 7 #include "base/mac/scoped_nsautorelease_pool.h" |
8 #endif | 8 #endif |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/memory/shared_memory.h" | 10 #include "base/memory/shared_memory.h" |
11 #include "base/rand_util.h" | |
12 #include "base/strings/string_number_conversions.h" | |
11 #include "base/sys_info.h" | 13 #include "base/sys_info.h" |
12 #include "base/test/multiprocess_test.h" | 14 #include "base/test/multiprocess_test.h" |
13 #include "base/threading/platform_thread.h" | 15 #include "base/threading/platform_thread.h" |
14 #include "base/time/time.h" | 16 #include "base/time/time.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "testing/multiprocess_func_list.h" | 18 #include "testing/multiprocess_func_list.h" |
17 | 19 |
18 #if defined(OS_MACOSX) | 20 #if defined(OS_MACOSX) |
19 #include "base/mac/scoped_nsautorelease_pool.h" | 21 #include "base/mac/scoped_nsautorelease_pool.h" |
20 #endif | 22 #endif |
21 | 23 |
22 #if defined(OS_POSIX) | 24 #if defined(OS_POSIX) |
23 #include <sys/mman.h> | 25 #include <sys/mman.h> |
26 #include <sys/stat.h> | |
27 #include <sys/types.h> | |
28 #include <unistd.h> | |
24 #endif | 29 #endif |
25 | 30 |
26 static const int kNumThreads = 5; | 31 static const int kNumThreads = 5; |
27 static const int kNumTasks = 5; | 32 static const int kNumTasks = 5; |
28 | 33 |
29 namespace base { | 34 namespace base { |
30 | 35 |
31 namespace { | 36 namespace { |
32 | 37 |
33 // Each thread will open the shared memory. Each thread will take a different 4 | 38 // Each thread will open the shared memory. Each thread will take a different 4 |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
394 SharedMemoryCreateOptions options; | 399 SharedMemoryCreateOptions options; |
395 options.size = kTestSize; | 400 options.size = kTestSize; |
396 options.executable = true; | 401 options.executable = true; |
397 | 402 |
398 EXPECT_TRUE(shared_memory.Create(options)); | 403 EXPECT_TRUE(shared_memory.Create(options)); |
399 EXPECT_TRUE(shared_memory.Map(shared_memory.requested_size())); | 404 EXPECT_TRUE(shared_memory.Map(shared_memory.requested_size())); |
400 | 405 |
401 EXPECT_EQ(0, mprotect(shared_memory.memory(), shared_memory.requested_size(), | 406 EXPECT_EQ(0, mprotect(shared_memory.memory(), shared_memory.requested_size(), |
402 PROT_READ | PROT_EXEC)); | 407 PROT_READ | PROT_EXEC)); |
403 } | 408 } |
404 #endif | 409 |
410 // Create a shared memory object, check its permissions. | |
411 TEST(SharedMemoryTest, FilePermissionsAnonymous) { | |
412 const uint32 kTestSize = 1 << 8; | |
413 | |
414 SharedMemory shared_memory; | |
415 SharedMemoryCreateOptions options; | |
416 options.size = kTestSize; | |
417 // Set a permissive umask. | |
418 mode_t old_umask = umask(S_IWGRP | S_IWOTH); | |
Mark Mentovai
2013/07/02 14:31:54
Can you restore the old umask with a scoper, in ca
jln (very slow on Chromium)
2013/07/02 18:49:08
I ended up created a class for this, since it's le
| |
419 | |
420 EXPECT_TRUE(shared_memory.Create(options)); | |
421 | |
422 int shm_fd = shared_memory.handle().fd; | |
423 struct stat shm_stat; | |
424 EXPECT_EQ(0, fstat(shm_fd, &shm_stat)); | |
425 // Neither the group, nor others should be able to read the shared memory | |
426 // file. | |
427 EXPECT_FALSE(shm_stat.st_mode & S_IRWXO); | |
428 EXPECT_FALSE(shm_stat.st_mode & S_IRWXG); | |
429 | |
430 // Restore umask. | |
431 umask(old_umask); | |
432 } | |
433 | |
434 // Create a shared memory object, check its permissions. | |
435 TEST(SharedMemoryTest, FilePermissionsNamed) { | |
436 const uint32 kTestSize = 1 << 8; | |
437 | |
438 SharedMemory shared_memory; | |
439 SharedMemoryCreateOptions options; | |
440 options.size = kTestSize; | |
441 std::string shared_mem_name = | |
442 "shared_perm_test-" + Uint64ToString(RandUint64()); | |
Mark Mentovai
2013/07/02 14:31:54
Random numbers lead to flake. 64 bits are probably
jln (very slow on Chromium)
2013/07/02 18:49:08
I think this is the best way to do it. We're creat
Mark Mentovai
2013/07/02 19:52:56
Julien Tinnes wrote:
jln (very slow on Chromium)
2013/07/02 21:20:02
I added the PID.
| |
443 options.name = &shared_mem_name; | |
444 // Set a permissive umask. | |
445 mode_t old_umask = umask(S_IWGRP | S_IWOTH); | |
446 | |
447 EXPECT_TRUE(shared_memory.Create(options)); | |
448 // Clean-up the backing file immediately, we don't need it. | |
449 EXPECT_TRUE(shared_memory.Delete(shared_mem_name)); | |
450 | |
451 int shm_fd = shared_memory.handle().fd; | |
452 struct stat shm_stat; | |
453 EXPECT_EQ(0, fstat(shm_fd, &shm_stat)); | |
Mark Mentovai
2013/07/02 14:31:54
If the backing file is gone, what are you testing?
jln (very slow on Chromium)
2013/07/02 18:49:08
The link is gone, but the inode is still there and
Mark Mentovai
2013/07/02 19:52:56
Julien Tinnes wrote:
jln (very slow on Chromium)
2013/07/02 21:20:02
We're checking that the file was created with the
| |
454 // Neither the group, nor others should be able to read the shared memory | |
455 // file. | |
456 EXPECT_FALSE(shm_stat.st_mode & S_IRWXO); | |
457 EXPECT_FALSE(shm_stat.st_mode & S_IRWXG); | |
458 // Restore umask. | |
459 umask(old_umask); | |
460 } | |
461 | |
462 #endif // defined(OS_POSIX) | |
405 | 463 |
406 // Map() will return addresses which are aligned to the platform page size, this | 464 // Map() will return addresses which are aligned to the platform page size, this |
407 // varies from platform to platform though. Since we'd like to advertise a | 465 // varies from platform to platform though. Since we'd like to advertise a |
408 // minimum alignment that callers can count on, test for it here. | 466 // minimum alignment that callers can count on, test for it here. |
409 TEST(SharedMemoryTest, MapMinimumAlignment) { | 467 TEST(SharedMemoryTest, MapMinimumAlignment) { |
410 static const int kDataSize = 8192; | 468 static const int kDataSize = 8192; |
411 | 469 |
412 SharedMemory shared_memory; | 470 SharedMemory shared_memory; |
413 ASSERT_TRUE(shared_memory.CreateAndMapAnonymous(kDataSize)); | 471 ASSERT_TRUE(shared_memory.CreateAndMapAnonymous(kDataSize)); |
414 EXPECT_EQ(0U, reinterpret_cast<uintptr_t>( | 472 EXPECT_EQ(0U, reinterpret_cast<uintptr_t>( |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
483 SharedMemoryProcessTest::CleanUp(); | 541 SharedMemoryProcessTest::CleanUp(); |
484 } | 542 } |
485 | 543 |
486 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { | 544 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { |
487 return SharedMemoryProcessTest::TaskTestMain(); | 545 return SharedMemoryProcessTest::TaskTestMain(); |
488 } | 546 } |
489 | 547 |
490 #endif // !OS_IOS | 548 #endif // !OS_IOS |
491 | 549 |
492 } // namespace base | 550 } // namespace base |
OLD | NEW |