Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: base/memory/shared_memory_unittest.cc

Issue 27265002: Implement SharedMemory::NewAnonymousReadOnly(contents). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Try to fix Android and Windows Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
11 #include "base/sys_info.h" 11 #include "base/sys_info.h"
12 #include "base/test/multiprocess_test.h" 12 #include "base/test/multiprocess_test.h"
13 #include "base/threading/platform_thread.h" 13 #include "base/threading/platform_thread.h"
14 #include "base/time/time.h" 14 #include "base/time/time.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/multiprocess_func_list.h" 16 #include "testing/multiprocess_func_list.h"
17 17
18 #if defined(OS_MACOSX) 18 #if defined(OS_MACOSX)
19 #include "base/mac/scoped_nsautorelease_pool.h" 19 #include "base/mac/scoped_nsautorelease_pool.h"
20 #endif 20 #endif
21 21
22 #if defined(OS_POSIX) 22 #if defined(OS_POSIX)
23 #include <errno.h>
24 #include <fcntl.h>
23 #include <sys/mman.h> 25 #include <sys/mman.h>
24 #include <sys/stat.h> 26 #include <sys/stat.h>
25 #include <sys/types.h> 27 #include <sys/types.h>
26 #include <unistd.h> 28 #include <unistd.h>
27 #endif 29 #endif
28 30
31 #if defined(OS_WIN)
32 #include "base/win/scoped_handle.h"
33 #endif
34
29 static const int kNumThreads = 5; 35 static const int kNumThreads = 5;
30 static const int kNumTasks = 5; 36 static const int kNumTasks = 5;
31 37
32 namespace base { 38 namespace base {
33 39
34 namespace { 40 namespace {
35 41
36 // Each thread will open the shared memory. Each thread will take a different 4 42 // Each thread will open the shared memory. Each thread will take a different 4
37 // byte int pointer, and keep changing it, with some small pauses in between. 43 // byte int pointer, and keep changing it, with some small pauses in between.
38 // Verify that each thread's value in the shared memory is always correct. 44 // Verify that each thread's value in the shared memory is always correct.
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 else 360 else
355 EXPECT_EQ(0, pointers[j][0]); 361 EXPECT_EQ(0, pointers[j][0]);
356 } 362 }
357 } 363 }
358 364
359 for (int i = 0; i < count; i++) { 365 for (int i = 0; i < count; i++) {
360 memories[i].Close(); 366 memories[i].Close();
361 } 367 }
362 } 368 }
363 369
370 TEST(SharedMemoryTest, ShareReadOnly) {
371 StringPiece contents = "Hello World";
372
373 SharedMemory writable_shmem;
374 ASSERT_TRUE(writable_shmem.CreateAndMapAnonymous(contents.size()));
375 memcpy(writable_shmem.memory(), contents.data(), contents.size());
376
377 SharedMemoryHandle readonly_handle;
378 ASSERT_TRUE(writable_shmem.ShareReadOnlyToProcess(GetCurrentProcessHandle(),
379 &readonly_handle));
380 SharedMemory readonly_shmem(readonly_handle, /*readonly=*/true);
381
382 ASSERT_TRUE(readonly_shmem.Map(contents.size()));
383 EXPECT_EQ(contents,
384 StringPiece(static_cast<const char*>(readonly_shmem.memory()),
385 contents.size()));
386
387 // We'd like to check that if we send the read-only segment to another
388 // process, then that other process can't reopen it read/write. (Since that
389 // would be a security hole.) Setting up multiple processes is hard in a
390 // unittest, so this test checks that the *current* process can't reopen the
391 // segment read/write. I think the test here is stronger than we actually
392 // care about, but there's a remote possibility that sending a file over a
393 // pipe would transform it into read/write.
394 SharedMemoryHandle handle = readonly_shmem.handle();
395
396 #if defined(OS_POSIX)
397 EXPECT_EQ(O_RDONLY, fcntl(handle.fd, F_GETFL) & O_ACCMODE)
398 << "The descriptor itself should be read-only.";
399
400 errno = 0;
401 void* writable = mmap(NULL,
402 readonly_shmem.mapped_size(),
403 PROT_READ | PROT_WRITE,
404 MAP_SHARED,
405 handle.fd,
406 0);
407 int mmap_errno = errno;
408 EXPECT_EQ(MAP_FAILED, writable)
409 << "It shouldn't be possible to re-mmap the descriptor writable.";
410 EXPECT_EQ(EACCES, mmap_errno);
411 if (writable != MAP_FAILED)
412 EXPECT_EQ(0, munmap(writable, readonly_shmem.mapped_size()));
413
414 #elif defined(OS_WIN)
415 EXPECT_EQ(NULL, MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, 0))
416 << "Shouldn't be able to map memory writable.";
417
418 base::win::ScopedHandle writable_handle;
419 EXPECT_EQ(0,
420 ::DuplicateHandle(GetCurrentProcess(),
421 handle,
422 GetCurrentProcess,
423 writable_handle.Receive(),
424 FILE_MAP_ALL_ACCESS,
425 false,
426 0))
427 << "Shouldn't be able to duplicate the handle into a writable one.";
428 #else
429 #error Unexpected platform; write a test that tries to make 'handle' writable.
430 #endif // defined(OS_POSIX) || defined(OS_WIN)
431 }
432
364 TEST(SharedMemoryTest, MapAt) { 433 TEST(SharedMemoryTest, MapAt) {
365 ASSERT_TRUE(SysInfo::VMAllocationGranularity() >= sizeof(uint32)); 434 ASSERT_TRUE(SysInfo::VMAllocationGranularity() >= sizeof(uint32));
366 const size_t kCount = SysInfo::VMAllocationGranularity(); 435 const size_t kCount = SysInfo::VMAllocationGranularity();
367 const size_t kDataSize = kCount * sizeof(uint32); 436 const size_t kDataSize = kCount * sizeof(uint32);
368 437
369 SharedMemory memory; 438 SharedMemory memory;
370 ASSERT_TRUE(memory.CreateAndMapAnonymous(kDataSize)); 439 ASSERT_TRUE(memory.CreateAndMapAnonymous(kDataSize));
371 ASSERT_TRUE(memory.Map(kDataSize)); 440 ASSERT_TRUE(memory.Map(kDataSize));
372 uint32* ptr = static_cast<uint32*>(memory.memory()); 441 uint32* ptr = static_cast<uint32*>(memory.memory());
373 ASSERT_NE(ptr, static_cast<void*>(NULL)); 442 ASSERT_NE(ptr, static_cast<void*>(NULL));
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 SharedMemoryProcessTest::CleanUp(); 621 SharedMemoryProcessTest::CleanUp();
553 } 622 }
554 623
555 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { 624 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) {
556 return SharedMemoryProcessTest::TaskTestMain(); 625 return SharedMemoryProcessTest::TaskTestMain();
557 } 626 }
558 627
559 #endif // !OS_IOS 628 #endif // !OS_IOS
560 629
561 } // namespace base 630 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698