Chromium Code Reviews| Index: base/memory/shared_memory_unittest.cc |
| diff --git a/base/memory/shared_memory_unittest.cc b/base/memory/shared_memory_unittest.cc |
| index 89db5112f620855d24b99e19b0d27b106f36ea75..de7497c927b72f6fa7de43d9e1597912afd91e69 100644 |
| --- a/base/memory/shared_memory_unittest.cc |
| +++ b/base/memory/shared_memory_unittest.cc |
| @@ -8,6 +8,8 @@ |
| #endif |
| #include "base/memory/scoped_ptr.h" |
| #include "base/memory/shared_memory.h" |
| +#include "base/rand_util.h" |
| +#include "base/strings/string_number_conversions.h" |
| #include "base/sys_info.h" |
| #include "base/test/multiprocess_test.h" |
| #include "base/threading/platform_thread.h" |
| @@ -21,6 +23,9 @@ |
| #if defined(OS_POSIX) |
| #include <sys/mman.h> |
| +#include <sys/stat.h> |
| +#include <sys/types.h> |
| +#include <unistd.h> |
| #endif |
| static const int kNumThreads = 5; |
| @@ -401,7 +406,60 @@ TEST(SharedMemoryTest, AnonymousExecutable) { |
| EXPECT_EQ(0, mprotect(shared_memory.memory(), shared_memory.requested_size(), |
| PROT_READ | PROT_EXEC)); |
| } |
| -#endif |
| + |
| +// Create a shared memory object, check its permissions. |
| +TEST(SharedMemoryTest, FilePermissionsAnonymous) { |
| + const uint32 kTestSize = 1 << 8; |
| + |
| + SharedMemory shared_memory; |
| + SharedMemoryCreateOptions options; |
| + options.size = kTestSize; |
| + // Set a permissive umask. |
| + 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
|
| + |
| + EXPECT_TRUE(shared_memory.Create(options)); |
| + |
| + int shm_fd = shared_memory.handle().fd; |
| + struct stat shm_stat; |
| + EXPECT_EQ(0, fstat(shm_fd, &shm_stat)); |
| + // Neither the group, nor others should be able to read the shared memory |
| + // file. |
| + EXPECT_FALSE(shm_stat.st_mode & S_IRWXO); |
| + EXPECT_FALSE(shm_stat.st_mode & S_IRWXG); |
| + |
| + // Restore umask. |
| + umask(old_umask); |
| +} |
| + |
| +// Create a shared memory object, check its permissions. |
| +TEST(SharedMemoryTest, FilePermissionsNamed) { |
| + const uint32 kTestSize = 1 << 8; |
| + |
| + SharedMemory shared_memory; |
| + SharedMemoryCreateOptions options; |
| + options.size = kTestSize; |
| + std::string shared_mem_name = |
| + "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.
|
| + options.name = &shared_mem_name; |
| + // Set a permissive umask. |
| + mode_t old_umask = umask(S_IWGRP | S_IWOTH); |
| + |
| + EXPECT_TRUE(shared_memory.Create(options)); |
| + // Clean-up the backing file immediately, we don't need it. |
| + EXPECT_TRUE(shared_memory.Delete(shared_mem_name)); |
| + |
| + int shm_fd = shared_memory.handle().fd; |
| + struct stat shm_stat; |
| + 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
|
| + // Neither the group, nor others should be able to read the shared memory |
| + // file. |
| + EXPECT_FALSE(shm_stat.st_mode & S_IRWXO); |
| + EXPECT_FALSE(shm_stat.st_mode & S_IRWXG); |
| + // Restore umask. |
| + umask(old_umask); |
| +} |
| + |
| +#endif // defined(OS_POSIX) |
| // Map() will return addresses which are aligned to the platform page size, this |
| // varies from platform to platform though. Since we'd like to advertise a |