OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/metrics/persistent_memory_allocator.h" | 5 #include "base/metrics/persistent_memory_allocator.h" |
6 | 6 |
7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/memory_mapped_file.h" | 9 #include "base/files/memory_mapped_file.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 EXPECT_EQ(mmlength, meminfo2.total); | 444 EXPECT_EQ(mmlength, meminfo2.total); |
445 EXPECT_EQ(0U, meminfo2.free); | 445 EXPECT_EQ(0U, meminfo2.free); |
446 } | 446 } |
447 | 447 |
448 TEST(FilePersistentMemoryAllocatorTest, AcceptableTest) { | 448 TEST(FilePersistentMemoryAllocatorTest, AcceptableTest) { |
449 ScopedTempDir temp_dir; | 449 ScopedTempDir temp_dir; |
450 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 450 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
451 FilePath file_path_base = temp_dir.path().AppendASCII("persistent_memory_"); | 451 FilePath file_path_base = temp_dir.path().AppendASCII("persistent_memory_"); |
452 | 452 |
453 LocalPersistentMemoryAllocator local(TEST_MEMORY_SIZE, TEST_ID, ""); | 453 LocalPersistentMemoryAllocator local(TEST_MEMORY_SIZE, TEST_ID, ""); |
| 454 local.Allocate(1, 1); |
| 455 local.Allocate(11, 11); |
454 const size_t minsize = local.used(); | 456 const size_t minsize = local.used(); |
455 scoped_ptr<char[]> garbage(new char[minsize]); | 457 scoped_ptr<char[]> garbage(new char[minsize]); |
456 RandBytes(garbage.get(), minsize); | 458 RandBytes(garbage.get(), minsize); |
457 | 459 |
458 scoped_ptr<MemoryMappedFile> mmfile; | 460 scoped_ptr<MemoryMappedFile> mmfile; |
459 char filename[100]; | 461 char filename[100]; |
460 for (size_t filesize = minsize; filesize > 0; --filesize) { | 462 for (size_t filesize = minsize; filesize > 0; --filesize) { |
461 strings::SafeSPrintf(filename, "memory_%d_A", filesize); | 463 strings::SafeSPrintf(filename, "memory_%d_A", filesize); |
462 FilePath file_path = temp_dir.path().AppendASCII(filename); | 464 FilePath file_path = temp_dir.path().AppendASCII(filename); |
463 ASSERT_FALSE(PathExists(file_path)); | 465 ASSERT_FALSE(PathExists(file_path)); |
464 { | 466 { |
465 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); | 467 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); |
466 ASSERT_TRUE(writer.IsValid()); | 468 ASSERT_TRUE(writer.IsValid()); |
467 writer.Write(0, (const char*)local.data(), filesize); | 469 writer.Write(0, (const char*)local.data(), filesize); |
468 } | 470 } |
469 ASSERT_TRUE(PathExists(file_path)); | 471 ASSERT_TRUE(PathExists(file_path)); |
470 | 472 |
471 mmfile.reset(new MemoryMappedFile()); | 473 mmfile.reset(new MemoryMappedFile()); |
472 mmfile->Initialize(file_path); | 474 mmfile->Initialize(file_path); |
473 EXPECT_EQ(filesize, mmfile->length()); | 475 EXPECT_EQ(filesize, mmfile->length()); |
474 if (FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile)) { | 476 if (FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile)) { |
475 // Just need to make sure it doesn't crash. | 477 // Make sure construction doesn't crash. |
476 FilePersistentMemoryAllocator allocator(mmfile.release(), 0, ""); | 478 FilePersistentMemoryAllocator allocator(mmfile.release(), 0, ""); |
477 (void)allocator; // Ensure compiler can't optimize-out above variable. | 479 // Also make sure that iteration doesn't crash. |
| 480 PersistentMemoryAllocator::Iterator iter; |
| 481 allocator.CreateIterator(&iter); |
| 482 for (;;) { |
| 483 Reference ref = allocator.GetNextIterable(&iter, 0); |
| 484 if (!ref) |
| 485 break; |
| 486 const char* data = allocator.GetAsObject<char>(ref, 0); |
| 487 uint32_t type = allocator.GetType(ref); |
| 488 size_t size = allocator.GetAllocSize(ref); |
| 489 // Ensure compiler can't optimize-out above variables. |
| 490 (void)data; |
| 491 (void)type; |
| 492 (void)size; |
| 493 // Ensure that corruption-detected flag gets properly set. |
| 494 EXPECT_EQ(filesize != minsize, allocator.IsCorrupt()); |
| 495 } |
478 } else { | 496 } else { |
479 // For filesize >= minsize, the file must be acceptable. This | 497 // For filesize >= minsize, the file must be acceptable. This |
480 // else clause (file-not-acceptable) should be reached only if | 498 // else clause (file-not-acceptable) should be reached only if |
481 // filesize < minsize. | 499 // filesize < minsize. |
482 EXPECT_LT(filesize, minsize); | 500 EXPECT_LT(filesize, minsize); |
483 } | 501 } |
484 | 502 |
485 #if !DCHECK_IS_ON() // DCHECK builds will die at a NOTREACHED(). | |
486 strings::SafeSPrintf(filename, "memory_%d_B", filesize); | 503 strings::SafeSPrintf(filename, "memory_%d_B", filesize); |
487 file_path = temp_dir.path().AppendASCII(filename); | 504 file_path = temp_dir.path().AppendASCII(filename); |
488 ASSERT_FALSE(PathExists(file_path)); | 505 ASSERT_FALSE(PathExists(file_path)); |
489 { | 506 { |
490 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); | 507 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); |
491 ASSERT_TRUE(writer.IsValid()); | 508 ASSERT_TRUE(writer.IsValid()); |
492 writer.Write(0, (const char*)garbage.get(), filesize); | 509 writer.Write(0, (const char*)garbage.get(), filesize); |
493 } | 510 } |
494 ASSERT_TRUE(PathExists(file_path)); | 511 ASSERT_TRUE(PathExists(file_path)); |
495 | 512 |
496 mmfile.reset(new MemoryMappedFile()); | 513 mmfile.reset(new MemoryMappedFile()); |
497 mmfile->Initialize(file_path); | 514 mmfile->Initialize(file_path); |
498 EXPECT_EQ(filesize, mmfile->length()); | 515 EXPECT_EQ(filesize, mmfile->length()); |
499 if (FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile)) { | 516 if (FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile)) { |
500 // Just need to make sure it doesn't crash. | 517 // Just need to make sure it doesn't crash. |
501 FilePersistentMemoryAllocator allocator(mmfile.release(), 0, "") ; | 518 FilePersistentMemoryAllocator allocator(mmfile.release(), 0, "") ; |
502 EXPECT_TRUE(allocator.IsCorrupt()); // Garbage data so it should be. | 519 EXPECT_TRUE(allocator.IsCorrupt()); // Garbage data so it should be. |
503 } else { | 520 } else { |
504 // For filesize >= minsize, the file must be acceptable. This | 521 // For filesize >= minsize, the file must be acceptable. This |
505 // else clause (file-not-acceptable) should be reached only if | 522 // else clause (file-not-acceptable) should be reached only if |
506 // filesize < minsize. | 523 // filesize < minsize. |
507 EXPECT_GT(minsize, filesize); | 524 EXPECT_GT(minsize, filesize); |
508 } | 525 } |
509 #endif | |
510 } | 526 } |
511 } | 527 } |
512 | 528 |
513 } // namespace base | 529 } // namespace base |
OLD | NEW |