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

Side by Side Diff: base/metrics/persistent_memory_allocator_unittest.cc

Issue 1654053002: New test and off-by-one fix for data persisted to disk. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 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/metrics/persistent_memory_allocator.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 427 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 EXPECT_EQ(789U, file.GetType(r789)); 438 EXPECT_EQ(789U, file.GetType(r789));
439 439
440 PersistentMemoryAllocator::MemoryInfo meminfo2; 440 PersistentMemoryAllocator::MemoryInfo meminfo2;
441 file.GetMemoryInfo(&meminfo2); 441 file.GetMemoryInfo(&meminfo2);
442 EXPECT_GE(meminfo1.total, meminfo2.total); 442 EXPECT_GE(meminfo1.total, meminfo2.total);
443 EXPECT_GE(meminfo1.free, meminfo2.free); 443 EXPECT_GE(meminfo1.free, meminfo2.free);
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 #if !DCHECK_IS_ON() // DCHECK builds die at a various debug checks.
Alexei Svitkine (slow) 2016/02/08 21:43:26 Aren't most of our tests run with DCHECKs? So this
bcwhite 2016/02/10 16:28:58 I think all the bot-tests run off a release build.
Alexei Svitkine (slow) 2016/02/10 16:35:34 I believe the bots run Release with DCHECK on. I
bcwhite 2016/02/10 16:54:09 Complicated. Result to the caller is always "good
448 TEST(FilePersistentMemoryAllocatorTest, AcceptableTest) { 449 TEST(FilePersistentMemoryAllocatorTest, AcceptableTest) {
449 ScopedTempDir temp_dir; 450 ScopedTempDir temp_dir;
450 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 451 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
451 FilePath file_path_base = temp_dir.path().AppendASCII("persistent_memory_"); 452 FilePath file_path_base = temp_dir.path().AppendASCII("persistent_memory_");
452 453
453 LocalPersistentMemoryAllocator local(TEST_MEMORY_SIZE, TEST_ID, ""); 454 LocalPersistentMemoryAllocator local(TEST_MEMORY_SIZE, TEST_ID, "");
455 local.Allocate(1, 1);
456 local.Allocate(11, 11);
454 const size_t minsize = local.used(); 457 const size_t minsize = local.used();
455 scoped_ptr<char[]> garbage(new char[minsize]); 458 scoped_ptr<char[]> garbage(new char[minsize]);
456 RandBytes(garbage.get(), minsize); 459 RandBytes(garbage.get(), minsize);
457 460
458 scoped_ptr<MemoryMappedFile> mmfile; 461 scoped_ptr<MemoryMappedFile> mmfile;
459 char filename[100]; 462 char filename[100];
460 for (size_t filesize = minsize; filesize > 0; --filesize) { 463 for (size_t filesize = minsize; filesize > 0; --filesize) {
461 strings::SafeSPrintf(filename, "memory_%d_A", filesize); 464 strings::SafeSPrintf(filename, "memory_%d_A", filesize);
462 FilePath file_path = temp_dir.path().AppendASCII(filename); 465 FilePath file_path = temp_dir.path().AppendASCII(filename);
463 ASSERT_FALSE(PathExists(file_path)); 466 ASSERT_FALSE(PathExists(file_path));
464 { 467 {
465 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); 468 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE);
466 ASSERT_TRUE(writer.IsValid()); 469 ASSERT_TRUE(writer.IsValid());
467 writer.Write(0, (const char*)local.data(), filesize); 470 writer.Write(0, (const char*)local.data(), filesize);
468 } 471 }
469 ASSERT_TRUE(PathExists(file_path)); 472 ASSERT_TRUE(PathExists(file_path));
470 473
471 mmfile.reset(new MemoryMappedFile()); 474 mmfile.reset(new MemoryMappedFile());
472 mmfile->Initialize(file_path); 475 mmfile->Initialize(file_path);
473 EXPECT_EQ(filesize, mmfile->length()); 476 EXPECT_EQ(filesize, mmfile->length());
474 if (FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile)) { 477 if (FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile)) {
475 // Just need to make sure it doesn't crash. 478 // Make sure construction doesn't crash.
476 FilePersistentMemoryAllocator allocator(mmfile.release(), 0, ""); 479 FilePersistentMemoryAllocator allocator(mmfile.release(), 0, "");
477 (void)allocator; // Ensure compiler can't optimize-out above variable. 480 // Also make sure that iteration doesn't crash.
481 PersistentMemoryAllocator::Iterator iter;
482 allocator.CreateIterator(&iter);
483 for (;;) {
484 Reference ref = allocator.GetNextIterable(&iter, 0);
485 if (!ref)
486 break;
487 const char* data = allocator.GetAsObject<char>(ref, 0);
488 uint32_t type = allocator.GetType(ref);
489 size_t size = allocator.GetAllocSize(ref);
490 // Ensure compiler can't optimize-out above variables.
491 (void)data;
492 (void)type;
493 (void)size;
494 // Ensure that corruption-detected flag gets properly set.
495 EXPECT_EQ(filesize != minsize, allocator.IsCorrupt());
496 }
478 } else { 497 } else {
479 // For filesize >= minsize, the file must be acceptable. This 498 // For filesize >= minsize, the file must be acceptable. This
480 // else clause (file-not-acceptable) should be reached only if 499 // else clause (file-not-acceptable) should be reached only if
481 // filesize < minsize. 500 // filesize < minsize.
482 EXPECT_LT(filesize, minsize); 501 EXPECT_LT(filesize, minsize);
483 } 502 }
484 503
485 #if !DCHECK_IS_ON() // DCHECK builds will die at a NOTREACHED().
486 strings::SafeSPrintf(filename, "memory_%d_B", filesize); 504 strings::SafeSPrintf(filename, "memory_%d_B", filesize);
487 file_path = temp_dir.path().AppendASCII(filename); 505 file_path = temp_dir.path().AppendASCII(filename);
488 ASSERT_FALSE(PathExists(file_path)); 506 ASSERT_FALSE(PathExists(file_path));
489 { 507 {
490 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); 508 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE);
491 ASSERT_TRUE(writer.IsValid()); 509 ASSERT_TRUE(writer.IsValid());
492 writer.Write(0, (const char*)garbage.get(), filesize); 510 writer.Write(0, (const char*)garbage.get(), filesize);
493 } 511 }
494 ASSERT_TRUE(PathExists(file_path)); 512 ASSERT_TRUE(PathExists(file_path));
495 513
496 mmfile.reset(new MemoryMappedFile()); 514 mmfile.reset(new MemoryMappedFile());
497 mmfile->Initialize(file_path); 515 mmfile->Initialize(file_path);
498 EXPECT_EQ(filesize, mmfile->length()); 516 EXPECT_EQ(filesize, mmfile->length());
499 if (FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile)) { 517 if (FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile)) {
500 // Just need to make sure it doesn't crash. 518 // Just need to make sure it doesn't crash.
501 FilePersistentMemoryAllocator allocator(mmfile.release(), 0, "") ; 519 FilePersistentMemoryAllocator allocator(mmfile.release(), 0, "") ;
502 EXPECT_TRUE(allocator.IsCorrupt()); // Garbage data so it should be. 520 EXPECT_TRUE(allocator.IsCorrupt()); // Garbage data so it should be.
503 } else { 521 } else {
504 // For filesize >= minsize, the file must be acceptable. This 522 // For filesize >= minsize, the file must be acceptable. This
505 // else clause (file-not-acceptable) should be reached only if 523 // else clause (file-not-acceptable) should be reached only if
506 // filesize < minsize. 524 // filesize < minsize.
507 EXPECT_GT(minsize, filesize); 525 EXPECT_GT(minsize, filesize);
508 } 526 }
509 #endif
510 } 527 }
511 } 528 }
529 #endif // !DCHECK_IS_ON()
512 530
513 } // namespace base 531 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/persistent_memory_allocator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698