| 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/shared_memory.h" | 10 #include "base/shared_memory.h" |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 << Time::Now().ToDoubleT(); | 190 << Time::Now().ToDoubleT(); |
| 191 std::string test_name = test_name_stream.str(); | 191 std::string test_name = test_name_stream.str(); |
| 192 | 192 |
| 193 // Open two handles to a memory segment and check that open_existing works | 193 // Open two handles to a memory segment and check that open_existing works |
| 194 // as expected. | 194 // as expected. |
| 195 SharedMemory memory1; | 195 SharedMemory memory1; |
| 196 bool rv = memory1.CreateNamed(test_name, false, kDataSize); | 196 bool rv = memory1.CreateNamed(test_name, false, kDataSize); |
| 197 EXPECT_TRUE(rv); | 197 EXPECT_TRUE(rv); |
| 198 | 198 |
| 199 // Memory1 knows it's size because it created it. | 199 // Memory1 knows it's size because it created it. |
| 200 EXPECT_EQ(memory1.created_size(), kDataSize); | 200 EXPECT_EQ(memory1.requested_size(), kDataSize); |
| 201 | 201 |
| 202 rv = memory1.Map(kDataSize); | 202 rv = memory1.Map(kDataSize); |
| 203 EXPECT_TRUE(rv); | 203 EXPECT_TRUE(rv); |
| 204 | 204 |
| 205 // The mapped memory1 must be at least the size we asked for. |
| 206 EXPECT_GE(memory1.mapped_size(), kDataSize); |
| 207 |
| 208 // The mapped memory1 shouldn't exceed rounding for allocation granularity. |
| 209 EXPECT_LT(memory1.mapped_size(), |
| 210 kDataSize + base::SysInfo::VMAllocationGranularity()); |
| 211 |
| 205 memset(memory1.memory(), 'G', kDataSize); | 212 memset(memory1.memory(), 'G', kDataSize); |
| 206 | 213 |
| 207 SharedMemory memory2; | 214 SharedMemory memory2; |
| 208 // Should not be able to create if openExisting is false. | 215 // Should not be able to create if openExisting is false. |
| 209 rv = memory2.CreateNamed(test_name, false, kDataSize2); | 216 rv = memory2.CreateNamed(test_name, false, kDataSize2); |
| 210 EXPECT_FALSE(rv); | 217 EXPECT_FALSE(rv); |
| 211 | 218 |
| 212 // Should be able to create with openExisting true. | 219 // Should be able to create with openExisting true. |
| 213 rv = memory2.CreateNamed(test_name, true, kDataSize2); | 220 rv = memory2.CreateNamed(test_name, true, kDataSize2); |
| 214 EXPECT_TRUE(rv); | 221 EXPECT_TRUE(rv); |
| 215 | 222 |
| 216 // Memory2 shouldn't know the size because we didn't create it. | 223 // Memory2 shouldn't know the size because we didn't create it. |
| 217 EXPECT_EQ(memory2.created_size(), 0U); | 224 EXPECT_EQ(memory2.requested_size(), 0U); |
| 218 | 225 |
| 219 // We should be able to map the original size. | 226 // We should be able to map the original size. |
| 220 rv = memory2.Map(kDataSize); | 227 rv = memory2.Map(kDataSize); |
| 221 EXPECT_TRUE(rv); | 228 EXPECT_TRUE(rv); |
| 222 | 229 |
| 230 // The mapped memory2 must be at least the size of the original. |
| 231 EXPECT_GE(memory2.mapped_size(), kDataSize); |
| 232 |
| 233 // The mapped memory2 shouldn't exceed rounding for allocation granularity. |
| 234 EXPECT_LT(memory2.mapped_size(), |
| 235 kDataSize2 + base::SysInfo::VMAllocationGranularity()); |
| 236 |
| 223 // Verify that opening memory2 didn't truncate or delete memory 1. | 237 // Verify that opening memory2 didn't truncate or delete memory 1. |
| 224 char *start_ptr = static_cast<char *>(memory2.memory()); | 238 char *start_ptr = static_cast<char *>(memory2.memory()); |
| 225 char *end_ptr = start_ptr + kDataSize; | 239 char *end_ptr = start_ptr + kDataSize; |
| 226 for (char* ptr = start_ptr; ptr < end_ptr; ptr++) { | 240 for (char* ptr = start_ptr; ptr < end_ptr; ptr++) { |
| 227 EXPECT_EQ(*ptr, 'G'); | 241 EXPECT_EQ(*ptr, 'G'); |
| 228 } | 242 } |
| 229 | 243 |
| 230 memory1.Close(); | 244 memory1.Close(); |
| 231 memory2.Close(); | 245 memory2.Close(); |
| 232 | 246 |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 // Create a shared memory object, mmap it, and mprotect it to PROT_EXEC. | 389 // Create a shared memory object, mmap it, and mprotect it to PROT_EXEC. |
| 376 TEST(SharedMemoryTest, AnonymousExecutable) { | 390 TEST(SharedMemoryTest, AnonymousExecutable) { |
| 377 const uint32 kTestSize = 1 << 16; | 391 const uint32 kTestSize = 1 << 16; |
| 378 | 392 |
| 379 SharedMemory shared_memory; | 393 SharedMemory shared_memory; |
| 380 SharedMemoryCreateOptions options; | 394 SharedMemoryCreateOptions options; |
| 381 options.size = kTestSize; | 395 options.size = kTestSize; |
| 382 options.executable = true; | 396 options.executable = true; |
| 383 | 397 |
| 384 EXPECT_TRUE(shared_memory.Create(options)); | 398 EXPECT_TRUE(shared_memory.Create(options)); |
| 385 EXPECT_TRUE(shared_memory.Map(shared_memory.created_size())); | 399 EXPECT_TRUE(shared_memory.Map(shared_memory.requested_size())); |
| 386 | 400 |
| 387 EXPECT_EQ(0, mprotect(shared_memory.memory(), shared_memory.created_size(), | 401 EXPECT_EQ(0, mprotect(shared_memory.memory(), shared_memory.requested_size(), |
| 388 PROT_READ | PROT_EXEC)); | 402 PROT_READ | PROT_EXEC)); |
| 389 } | 403 } |
| 390 #endif | 404 #endif |
| 391 | 405 |
| 392 // Map() will return addresses which are aligned to the platform page size, this | 406 // Map() will return addresses which are aligned to the platform page size, this |
| 393 // varies from platform to platform though. Since we'd like to advertise a | 407 // varies from platform to platform though. Since we'd like to advertise a |
| 394 // minimum alignment that callers can count on, test for it here. | 408 // minimum alignment that callers can count on, test for it here. |
| 395 TEST(SharedMemoryTest, MapMinimumAlignment) { | 409 TEST(SharedMemoryTest, MapMinimumAlignment) { |
| 396 static const int kDataSize = 8192; | 410 static const int kDataSize = 8192; |
| 397 | 411 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 SharedMemoryProcessTest::CleanUp(); | 483 SharedMemoryProcessTest::CleanUp(); |
| 470 } | 484 } |
| 471 | 485 |
| 472 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { | 486 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { |
| 473 return SharedMemoryProcessTest::TaskTestMain(); | 487 return SharedMemoryProcessTest::TaskTestMain(); |
| 474 } | 488 } |
| 475 | 489 |
| 476 #endif // !OS_IOS | 490 #endif // !OS_IOS |
| 477 | 491 |
| 478 } // namespace base | 492 } // namespace base |
| OLD | NEW |