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

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

Issue 1549003002: Switch to standard integer types in base/memory/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 12 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
« no previous file with comments | « base/memory/shared_memory_posix.cc ('k') | base/memory/shared_memory_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <stddef.h>
6 #include <stdint.h>
7
5 #include "base/atomicops.h" 8 #include "base/atomicops.h"
6 #include "base/basictypes.h" 9 #include "base/macros.h"
7 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
8 #include "base/memory/shared_memory.h" 11 #include "base/memory/shared_memory.h"
9 #include "base/memory/shared_memory_handle.h" 12 #include "base/memory/shared_memory_handle.h"
10 #include "base/process/kill.h" 13 #include "base/process/kill.h"
11 #include "base/rand_util.h" 14 #include "base/rand_util.h"
12 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
13 #include "base/sys_info.h" 16 #include "base/sys_info.h"
14 #include "base/test/multiprocess_test.h" 17 #include "base/test/multiprocess_test.h"
15 #include "base/threading/platform_thread.h" 18 #include "base/threading/platform_thread.h"
16 #include "base/time/time.h" 19 #include "base/time/time.h"
20 #include "build/build_config.h"
17 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
18 #include "testing/multiprocess_func_list.h" 22 #include "testing/multiprocess_func_list.h"
19 23
20 #if defined(OS_POSIX) 24 #if defined(OS_POSIX)
21 #include <errno.h> 25 #include <errno.h>
22 #include <fcntl.h> 26 #include <fcntl.h>
23 #include <sys/mman.h> 27 #include <sys/mman.h>
24 #include <sys/stat.h> 28 #include <sys/stat.h>
25 #include <sys/types.h> 29 #include <sys/types.h>
26 #include <unistd.h> 30 #include <unistd.h>
27 #endif 31 #endif
28 32
29 #if defined(OS_WIN) 33 #if defined(OS_WIN)
30 #include "base/win/scoped_handle.h" 34 #include "base/win/scoped_handle.h"
31 #endif 35 #endif
32 36
33 namespace base { 37 namespace base {
34 38
35 namespace { 39 namespace {
36 40
37 #if !defined(OS_MACOSX) 41 #if !defined(OS_MACOSX)
38 // 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
39 // 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.
40 // 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.
41 class MultipleThreadMain : public PlatformThread::Delegate { 45 class MultipleThreadMain : public PlatformThread::Delegate {
42 public: 46 public:
43 explicit MultipleThreadMain(int16 id) : id_(id) {} 47 explicit MultipleThreadMain(int16_t id) : id_(id) {}
44 ~MultipleThreadMain() override {} 48 ~MultipleThreadMain() override {}
45 49
46 static void CleanUp() { 50 static void CleanUp() {
47 SharedMemory memory; 51 SharedMemory memory;
48 memory.Delete(s_test_name_); 52 memory.Delete(s_test_name_);
49 } 53 }
50 54
51 // PlatformThread::Delegate interface. 55 // PlatformThread::Delegate interface.
52 void ThreadMain() override { 56 void ThreadMain() override {
53 const uint32 kDataSize = 1024; 57 const uint32_t kDataSize = 1024;
54 SharedMemory memory; 58 SharedMemory memory;
55 bool rv = memory.CreateNamedDeprecated(s_test_name_, true, kDataSize); 59 bool rv = memory.CreateNamedDeprecated(s_test_name_, true, kDataSize);
56 EXPECT_TRUE(rv); 60 EXPECT_TRUE(rv);
57 rv = memory.Map(kDataSize); 61 rv = memory.Map(kDataSize);
58 EXPECT_TRUE(rv); 62 EXPECT_TRUE(rv);
59 int* ptr = static_cast<int*>(memory.memory()) + id_; 63 int* ptr = static_cast<int*>(memory.memory()) + id_;
60 EXPECT_EQ(0, *ptr); 64 EXPECT_EQ(0, *ptr);
61 65
62 for (int idx = 0; idx < 100; idx++) { 66 for (int idx = 0; idx < 100; idx++) {
63 *ptr = idx; 67 *ptr = idx;
64 PlatformThread::Sleep(TimeDelta::FromMilliseconds(1)); 68 PlatformThread::Sleep(TimeDelta::FromMilliseconds(1));
65 EXPECT_EQ(*ptr, idx); 69 EXPECT_EQ(*ptr, idx);
66 } 70 }
67 // Reset back to 0 for the next test that uses the same name. 71 // Reset back to 0 for the next test that uses the same name.
68 *ptr = 0; 72 *ptr = 0;
69 73
70 memory.Close(); 74 memory.Close();
71 } 75 }
72 76
73 private: 77 private:
74 int16 id_; 78 int16_t id_;
75 79
76 static const char s_test_name_[]; 80 static const char s_test_name_[];
77 81
78 DISALLOW_COPY_AND_ASSIGN(MultipleThreadMain); 82 DISALLOW_COPY_AND_ASSIGN(MultipleThreadMain);
79 }; 83 };
80 84
81 const char MultipleThreadMain::s_test_name_[] = 85 const char MultipleThreadMain::s_test_name_[] =
82 "SharedMemoryOpenThreadTest"; 86 "SharedMemoryOpenThreadTest";
83 #endif // !defined(OS_MACOSX) 87 #endif // !defined(OS_MACOSX)
84 88
85 } // namespace 89 } // namespace
86 90
87 // Android/Mac doesn't support SharedMemory::Open/Delete/ 91 // Android/Mac doesn't support SharedMemory::Open/Delete/
88 // CreateNamedDeprecated(openExisting=true) 92 // CreateNamedDeprecated(openExisting=true)
89 #if !defined(OS_ANDROID) && !defined(OS_MACOSX) 93 #if !defined(OS_ANDROID) && !defined(OS_MACOSX)
90 TEST(SharedMemoryTest, OpenClose) { 94 TEST(SharedMemoryTest, OpenClose) {
91 const uint32 kDataSize = 1024; 95 const uint32_t kDataSize = 1024;
92 std::string test_name = "SharedMemoryOpenCloseTest"; 96 std::string test_name = "SharedMemoryOpenCloseTest";
93 97
94 // Open two handles to a memory segment, confirm that they are mapped 98 // Open two handles to a memory segment, confirm that they are mapped
95 // separately yet point to the same space. 99 // separately yet point to the same space.
96 SharedMemory memory1; 100 SharedMemory memory1;
97 bool rv = memory1.Delete(test_name); 101 bool rv = memory1.Delete(test_name);
98 EXPECT_TRUE(rv); 102 EXPECT_TRUE(rv);
99 rv = memory1.Delete(test_name); 103 rv = memory1.Delete(test_name);
100 EXPECT_TRUE(rv); 104 EXPECT_TRUE(rv);
101 rv = memory1.Open(test_name, false); 105 rv = memory1.Open(test_name, false);
(...skipping 27 matching lines...) Expand all
129 // Close the second memory segment. 133 // Close the second memory segment.
130 memory2.Close(); 134 memory2.Close();
131 135
132 rv = memory1.Delete(test_name); 136 rv = memory1.Delete(test_name);
133 EXPECT_TRUE(rv); 137 EXPECT_TRUE(rv);
134 rv = memory2.Delete(test_name); 138 rv = memory2.Delete(test_name);
135 EXPECT_TRUE(rv); 139 EXPECT_TRUE(rv);
136 } 140 }
137 141
138 TEST(SharedMemoryTest, OpenExclusive) { 142 TEST(SharedMemoryTest, OpenExclusive) {
139 const uint32 kDataSize = 1024; 143 const uint32_t kDataSize = 1024;
140 const uint32 kDataSize2 = 2048; 144 const uint32_t kDataSize2 = 2048;
141 std::ostringstream test_name_stream; 145 std::ostringstream test_name_stream;
142 test_name_stream << "SharedMemoryOpenExclusiveTest." 146 test_name_stream << "SharedMemoryOpenExclusiveTest."
143 << Time::Now().ToDoubleT(); 147 << Time::Now().ToDoubleT();
144 std::string test_name = test_name_stream.str(); 148 std::string test_name = test_name_stream.str();
145 149
146 // Open two handles to a memory segment and check that 150 // Open two handles to a memory segment and check that
147 // open_existing_deprecated works as expected. 151 // open_existing_deprecated works as expected.
148 SharedMemory memory1; 152 SharedMemory memory1;
149 bool rv = memory1.CreateNamedDeprecated(test_name, false, kDataSize); 153 bool rv = memory1.CreateNamedDeprecated(test_name, false, kDataSize);
150 EXPECT_TRUE(rv); 154 EXPECT_TRUE(rv);
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 int threadcounts[] = { 1, kNumThreads }; 246 int threadcounts[] = { 1, kNumThreads };
243 for (size_t i = 0; i < arraysize(threadcounts); i++) { 247 for (size_t i = 0; i < arraysize(threadcounts); i++) {
244 int numthreads = threadcounts[i]; 248 int numthreads = threadcounts[i];
245 scoped_ptr<PlatformThreadHandle[]> thread_handles; 249 scoped_ptr<PlatformThreadHandle[]> thread_handles;
246 scoped_ptr<MultipleThreadMain*[]> thread_delegates; 250 scoped_ptr<MultipleThreadMain*[]> thread_delegates;
247 251
248 thread_handles.reset(new PlatformThreadHandle[numthreads]); 252 thread_handles.reset(new PlatformThreadHandle[numthreads]);
249 thread_delegates.reset(new MultipleThreadMain*[numthreads]); 253 thread_delegates.reset(new MultipleThreadMain*[numthreads]);
250 254
251 // Spawn the threads. 255 // Spawn the threads.
252 for (int16 index = 0; index < numthreads; index++) { 256 for (int16_t index = 0; index < numthreads; index++) {
253 PlatformThreadHandle pth; 257 PlatformThreadHandle pth;
254 thread_delegates[index] = new MultipleThreadMain(index); 258 thread_delegates[index] = new MultipleThreadMain(index);
255 EXPECT_TRUE(PlatformThread::Create(0, thread_delegates[index], &pth)); 259 EXPECT_TRUE(PlatformThread::Create(0, thread_delegates[index], &pth));
256 thread_handles[index] = pth; 260 thread_handles[index] = pth;
257 } 261 }
258 262
259 // Wait for the threads to finish. 263 // Wait for the threads to finish.
260 for (int index = 0; index < numthreads; index++) { 264 for (int index = 0; index < numthreads; index++) {
261 PlatformThread::Join(thread_handles[index]); 265 PlatformThread::Join(thread_handles[index]);
262 delete thread_delegates[index]; 266 delete thread_delegates[index];
263 } 267 }
264 } 268 }
265 MultipleThreadMain::CleanUp(); 269 MultipleThreadMain::CleanUp();
266 } 270 }
267 #endif 271 #endif
268 272
269 // Allocate private (unique) shared memory with an empty string for a 273 // Allocate private (unique) shared memory with an empty string for a
270 // name. Make sure several of them don't point to the same thing as 274 // name. Make sure several of them don't point to the same thing as
271 // we might expect if the names are equal. 275 // we might expect if the names are equal.
272 TEST(SharedMemoryTest, AnonymousPrivate) { 276 TEST(SharedMemoryTest, AnonymousPrivate) {
273 int i, j; 277 int i, j;
274 int count = 4; 278 int count = 4;
275 bool rv; 279 bool rv;
276 const uint32 kDataSize = 8192; 280 const uint32_t kDataSize = 8192;
277 281
278 scoped_ptr<SharedMemory[]> memories(new SharedMemory[count]); 282 scoped_ptr<SharedMemory[]> memories(new SharedMemory[count]);
279 scoped_ptr<int*[]> pointers(new int*[count]); 283 scoped_ptr<int*[]> pointers(new int*[count]);
280 ASSERT_TRUE(memories.get()); 284 ASSERT_TRUE(memories.get());
281 ASSERT_TRUE(pointers.get()); 285 ASSERT_TRUE(pointers.get());
282 286
283 for (i = 0; i < count; i++) { 287 for (i = 0; i < count; i++) {
284 rv = memories[i].CreateAndMapAnonymous(kDataSize); 288 rv = memories[i].CreateAndMapAnonymous(kDataSize);
285 EXPECT_TRUE(rv); 289 EXPECT_TRUE(rv);
286 int* ptr = static_cast<int*>(memories[i].memory()); 290 int* ptr = static_cast<int*>(memories[i].memory());
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 ASSERT_TRUE(shmem.ShareToProcess(GetCurrentProcessHandle(), &shared_handle)); 422 ASSERT_TRUE(shmem.ShareToProcess(GetCurrentProcessHandle(), &shared_handle));
419 SharedMemory readonly(shared_handle, /*readonly=*/true); 423 SharedMemory readonly(shared_handle, /*readonly=*/true);
420 424
421 ASSERT_TRUE(readonly.Map(contents.size())); 425 ASSERT_TRUE(readonly.Map(contents.size()));
422 EXPECT_EQ(contents, 426 EXPECT_EQ(contents,
423 StringPiece(static_cast<const char*>(readonly.memory()), 427 StringPiece(static_cast<const char*>(readonly.memory()),
424 contents.size())); 428 contents.size()));
425 } 429 }
426 430
427 TEST(SharedMemoryTest, MapAt) { 431 TEST(SharedMemoryTest, MapAt) {
428 ASSERT_TRUE(SysInfo::VMAllocationGranularity() >= sizeof(uint32)); 432 ASSERT_TRUE(SysInfo::VMAllocationGranularity() >= sizeof(uint32_t));
429 const size_t kCount = SysInfo::VMAllocationGranularity(); 433 const size_t kCount = SysInfo::VMAllocationGranularity();
430 const size_t kDataSize = kCount * sizeof(uint32); 434 const size_t kDataSize = kCount * sizeof(uint32_t);
431 435
432 SharedMemory memory; 436 SharedMemory memory;
433 ASSERT_TRUE(memory.CreateAndMapAnonymous(kDataSize)); 437 ASSERT_TRUE(memory.CreateAndMapAnonymous(kDataSize));
434 uint32* ptr = static_cast<uint32*>(memory.memory()); 438 uint32_t* ptr = static_cast<uint32_t*>(memory.memory());
435 ASSERT_NE(ptr, static_cast<void*>(NULL)); 439 ASSERT_NE(ptr, static_cast<void*>(NULL));
436 440
437 for (size_t i = 0; i < kCount; ++i) { 441 for (size_t i = 0; i < kCount; ++i) {
438 ptr[i] = i; 442 ptr[i] = i;
439 } 443 }
440 444
441 memory.Unmap(); 445 memory.Unmap();
442 446
443 off_t offset = SysInfo::VMAllocationGranularity(); 447 off_t offset = SysInfo::VMAllocationGranularity();
444 ASSERT_TRUE(memory.MapAt(offset, kDataSize - offset)); 448 ASSERT_TRUE(memory.MapAt(offset, kDataSize - offset));
445 offset /= sizeof(uint32); 449 offset /= sizeof(uint32_t);
446 ptr = static_cast<uint32*>(memory.memory()); 450 ptr = static_cast<uint32_t*>(memory.memory());
447 ASSERT_NE(ptr, static_cast<void*>(NULL)); 451 ASSERT_NE(ptr, static_cast<void*>(NULL));
448 for (size_t i = offset; i < kCount; ++i) { 452 for (size_t i = offset; i < kCount; ++i) {
449 EXPECT_EQ(ptr[i - offset], i); 453 EXPECT_EQ(ptr[i - offset], i);
450 } 454 }
451 } 455 }
452 456
453 TEST(SharedMemoryTest, MapTwice) { 457 TEST(SharedMemoryTest, MapTwice) {
454 const uint32 kDataSize = 1024; 458 const uint32_t kDataSize = 1024;
455 SharedMemory memory; 459 SharedMemory memory;
456 bool rv = memory.CreateAndMapAnonymous(kDataSize); 460 bool rv = memory.CreateAndMapAnonymous(kDataSize);
457 EXPECT_TRUE(rv); 461 EXPECT_TRUE(rv);
458 462
459 void* old_address = memory.memory(); 463 void* old_address = memory.memory();
460 464
461 rv = memory.Map(kDataSize); 465 rv = memory.Map(kDataSize);
462 EXPECT_FALSE(rv); 466 EXPECT_FALSE(rv);
463 EXPECT_EQ(old_address, memory.memory()); 467 EXPECT_EQ(old_address, memory.memory());
464 } 468 }
465 469
466 #if defined(OS_POSIX) 470 #if defined(OS_POSIX)
467 // This test is not applicable for iOS (crbug.com/399384). 471 // This test is not applicable for iOS (crbug.com/399384).
468 #if !defined(OS_IOS) 472 #if !defined(OS_IOS)
469 // Create a shared memory object, mmap it, and mprotect it to PROT_EXEC. 473 // Create a shared memory object, mmap it, and mprotect it to PROT_EXEC.
470 TEST(SharedMemoryTest, AnonymousExecutable) { 474 TEST(SharedMemoryTest, AnonymousExecutable) {
471 const uint32 kTestSize = 1 << 16; 475 const uint32_t kTestSize = 1 << 16;
472 476
473 SharedMemory shared_memory; 477 SharedMemory shared_memory;
474 SharedMemoryCreateOptions options; 478 SharedMemoryCreateOptions options;
475 options.size = kTestSize; 479 options.size = kTestSize;
476 options.executable = true; 480 options.executable = true;
477 #if defined(OS_MACOSX) && !defined(OS_IOS) 481 #if defined(OS_MACOSX) && !defined(OS_IOS)
478 // The Mach functionality is tested in shared_memory_mac_unittest.cc. 482 // The Mach functionality is tested in shared_memory_mac_unittest.cc.
479 options.type = SharedMemoryHandle::POSIX; 483 options.type = SharedMemoryHandle::POSIX;
480 #endif 484 #endif
481 485
(...skipping 17 matching lines...) Expand all
499 old_umask_ = umask(target_mask); 503 old_umask_ = umask(target_mask);
500 } 504 }
501 ~ScopedUmaskSetter() { umask(old_umask_); } 505 ~ScopedUmaskSetter() { umask(old_umask_); }
502 private: 506 private:
503 mode_t old_umask_; 507 mode_t old_umask_;
504 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedUmaskSetter); 508 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedUmaskSetter);
505 }; 509 };
506 510
507 // Create a shared memory object, check its permissions. 511 // Create a shared memory object, check its permissions.
508 TEST(SharedMemoryTest, FilePermissionsAnonymous) { 512 TEST(SharedMemoryTest, FilePermissionsAnonymous) {
509 const uint32 kTestSize = 1 << 8; 513 const uint32_t kTestSize = 1 << 8;
510 514
511 SharedMemory shared_memory; 515 SharedMemory shared_memory;
512 SharedMemoryCreateOptions options; 516 SharedMemoryCreateOptions options;
513 options.size = kTestSize; 517 options.size = kTestSize;
514 #if defined(OS_MACOSX) && !defined(OS_IOS) 518 #if defined(OS_MACOSX) && !defined(OS_IOS)
515 // The Mach functionality is tested in shared_memory_mac_unittest.cc. 519 // The Mach functionality is tested in shared_memory_mac_unittest.cc.
516 options.type = SharedMemoryHandle::POSIX; 520 options.type = SharedMemoryHandle::POSIX;
517 #endif 521 #endif
518 // Set a file mode creation mask that gives all permissions. 522 // Set a file mode creation mask that gives all permissions.
519 ScopedUmaskSetter permissive_mask(S_IWGRP | S_IWOTH); 523 ScopedUmaskSetter permissive_mask(S_IWGRP | S_IWOTH);
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
647 errors++; 651 errors++;
648 int* ptr = static_cast<int*>(memory.memory()); 652 int* ptr = static_cast<int*>(memory.memory());
649 653
650 // This runs concurrently in multiple processes. Writes need to be atomic. 654 // This runs concurrently in multiple processes. Writes need to be atomic.
651 subtle::Barrier_AtomicIncrement(ptr, 1); 655 subtle::Barrier_AtomicIncrement(ptr, 1);
652 memory.Close(); 656 memory.Close();
653 return errors; 657 return errors;
654 } 658 }
655 659
656 static const char s_test_name_[]; 660 static const char s_test_name_[];
657 static const uint32 s_data_size_; 661 static const uint32_t s_data_size_;
658 }; 662 };
659 663
660 const char SharedMemoryProcessTest::s_test_name_[] = "MPMem"; 664 const char SharedMemoryProcessTest::s_test_name_[] = "MPMem";
661 const uint32 SharedMemoryProcessTest::s_data_size_ = 1024; 665 const uint32_t SharedMemoryProcessTest::s_data_size_ = 1024;
662 666
663 TEST_F(SharedMemoryProcessTest, SharedMemoryAcrossProcesses) { 667 TEST_F(SharedMemoryProcessTest, SharedMemoryAcrossProcesses) {
664 const int kNumTasks = 5; 668 const int kNumTasks = 5;
665 669
666 SharedMemoryProcessTest::CleanUp(); 670 SharedMemoryProcessTest::CleanUp();
667 671
668 // Create a shared memory region. Set the first word to 0. 672 // Create a shared memory region. Set the first word to 0.
669 SharedMemory memory; 673 SharedMemory memory;
670 bool rv = memory.CreateNamedDeprecated(s_test_name_, true, s_data_size_); 674 bool rv = memory.CreateNamedDeprecated(s_test_name_, true, s_data_size_);
671 ASSERT_TRUE(rv); 675 ASSERT_TRUE(rv);
(...skipping 23 matching lines...) Expand all
695 memory.Close(); 699 memory.Close();
696 SharedMemoryProcessTest::CleanUp(); 700 SharedMemoryProcessTest::CleanUp();
697 } 701 }
698 702
699 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { 703 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) {
700 return SharedMemoryProcessTest::TaskTestMain(); 704 return SharedMemoryProcessTest::TaskTestMain();
701 } 705 }
702 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX) 706 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX)
703 707
704 } // namespace base 708 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/shared_memory_posix.cc ('k') | base/memory/shared_memory_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698