OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/metrics/persistent_memory_allocator.h" |
| 6 |
| 7 #include "base/files/file.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/files/memory_mapped_file.h" |
| 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/metrics/histogram.h" |
| 13 #include "base/rand_util.h" |
| 14 #include "base/strings/safe_sprintf.h" |
| 15 #include "base/threading/simple_thread.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 const uint32_t TEST_MEMORY_SIZE = 1 << 20; // 1 MiB |
| 21 const uint32_t TEST_MEMORY_PAGE = 64 << 10; // 64 KiB |
| 22 const uint32_t TEST_ID = 12345; |
| 23 const char TEST_NAME[] = "TestAllocator"; |
| 24 |
| 25 } // namespace |
| 26 |
| 27 namespace base { |
| 28 |
| 29 typedef PersistentMemoryAllocator::Reference Reference; |
| 30 |
| 31 class PersistentMemoryAllocatorTest : public testing::Test { |
| 32 public: |
| 33 // This can't be statically initialized because it's value isn't defined |
| 34 // in the PersistentMemoryAllocator header file. Instead, it's simply set |
| 35 // in the constructor. |
| 36 uint32_t kAllocAlignment; |
| 37 |
| 38 struct TestObject1 { |
| 39 int onething; |
| 40 char oranother; |
| 41 }; |
| 42 |
| 43 struct TestObject2 { |
| 44 int thiis; |
| 45 long that; |
| 46 float andthe; |
| 47 char other; |
| 48 double thing; |
| 49 }; |
| 50 |
| 51 PersistentMemoryAllocatorTest() { |
| 52 kAllocAlignment = PersistentMemoryAllocator::kAllocAlignment; |
| 53 mem_segment_.reset(new char[TEST_MEMORY_SIZE]); |
| 54 } |
| 55 |
| 56 void SetUp() override { |
| 57 allocator_.reset(); |
| 58 ::memset(mem_segment_.get(), 0, TEST_MEMORY_SIZE); |
| 59 allocator_.reset(new PersistentMemoryAllocator( |
| 60 mem_segment_.get(), TEST_MEMORY_SIZE, TEST_MEMORY_PAGE, |
| 61 TEST_ID, TEST_NAME, false)); |
| 62 allocator_->CreateTrackingHistograms(allocator_->Name()); |
| 63 } |
| 64 |
| 65 void TearDown() override { |
| 66 allocator_.reset(); |
| 67 } |
| 68 |
| 69 unsigned CountIterables() { |
| 70 PersistentMemoryAllocator::Iterator iter; |
| 71 uint32_t type; |
| 72 unsigned count = 0; |
| 73 for (allocator_->CreateIterator(&iter); |
| 74 allocator_->GetNextIterable(&iter, &type) != 0;) { |
| 75 count++; |
| 76 } |
| 77 return count; |
| 78 } |
| 79 |
| 80 protected: |
| 81 scoped_ptr<char[]> mem_segment_; |
| 82 scoped_ptr<PersistentMemoryAllocator> allocator_; |
| 83 }; |
| 84 |
| 85 TEST_F(PersistentMemoryAllocatorTest, AllocateAndIterate) { |
| 86 std::string base_name(TEST_NAME); |
| 87 EXPECT_EQ(TEST_ID, allocator_->Id()); |
| 88 EXPECT_TRUE(allocator_->used_histogram_); |
| 89 EXPECT_EQ(base_name + ".UsedKiB", |
| 90 allocator_->used_histogram_->histogram_name()); |
| 91 EXPECT_TRUE(allocator_->allocs_histogram_); |
| 92 EXPECT_EQ(base_name + ".Allocs", |
| 93 allocator_->allocs_histogram_->histogram_name()); |
| 94 |
| 95 // Get base memory info for later comparison. |
| 96 PersistentMemoryAllocator::MemoryInfo meminfo0; |
| 97 allocator_->GetMemoryInfo(&meminfo0); |
| 98 EXPECT_EQ(TEST_MEMORY_SIZE, meminfo0.total); |
| 99 EXPECT_GT(meminfo0.total, meminfo0.free); |
| 100 |
| 101 // Validate allocation of test object and make sure it can be referenced |
| 102 // and all metadata looks correct. |
| 103 Reference block1 = allocator_->Allocate(sizeof(TestObject1), 1); |
| 104 EXPECT_NE(0U, block1); |
| 105 EXPECT_NE(nullptr, allocator_->GetAsObject<TestObject1>(block1, 1)); |
| 106 EXPECT_EQ(nullptr, allocator_->GetAsObject<TestObject2>(block1, 1)); |
| 107 EXPECT_LE(sizeof(TestObject1), allocator_->GetAllocSize(block1)); |
| 108 EXPECT_GT(sizeof(TestObject1) + kAllocAlignment, |
| 109 allocator_->GetAllocSize(block1)); |
| 110 PersistentMemoryAllocator::MemoryInfo meminfo1; |
| 111 allocator_->GetMemoryInfo(&meminfo1); |
| 112 EXPECT_EQ(meminfo0.total, meminfo1.total); |
| 113 EXPECT_GT(meminfo0.free, meminfo1.free); |
| 114 |
| 115 // Ensure that the test-object can be made iterable. |
| 116 PersistentMemoryAllocator::Iterator iter; |
| 117 uint32_t type; |
| 118 allocator_->CreateIterator(&iter); |
| 119 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); |
| 120 allocator_->MakeIterable(block1); |
| 121 EXPECT_EQ(block1, allocator_->GetNextIterable(&iter, &type)); |
| 122 EXPECT_EQ(1U, type); |
| 123 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); |
| 124 |
| 125 // Create second test-object and ensure everything is good and it cannot |
| 126 // be confused with test-object of another type. |
| 127 Reference block2 = allocator_->Allocate(sizeof(TestObject2), 2); |
| 128 EXPECT_NE(0U, block2); |
| 129 EXPECT_NE(nullptr, allocator_->GetAsObject<TestObject2>(block2, 2)); |
| 130 EXPECT_EQ(nullptr, allocator_->GetAsObject<TestObject2>(block2, 1)); |
| 131 EXPECT_LE(sizeof(TestObject2), allocator_->GetAllocSize(block2)); |
| 132 EXPECT_GT(sizeof(TestObject2) + kAllocAlignment, |
| 133 allocator_->GetAllocSize(block2)); |
| 134 PersistentMemoryAllocator::MemoryInfo meminfo2; |
| 135 allocator_->GetMemoryInfo(&meminfo2); |
| 136 EXPECT_EQ(meminfo1.total, meminfo2.total); |
| 137 EXPECT_GT(meminfo1.free, meminfo2.free); |
| 138 |
| 139 // Ensure that second test-object can also be made iterable. |
| 140 allocator_->MakeIterable(block2); |
| 141 EXPECT_EQ(block2, allocator_->GetNextIterable(&iter, &type)); |
| 142 EXPECT_EQ(2U, type); |
| 143 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); |
| 144 |
| 145 // Check that iteration can begin after an arbitrary location. |
| 146 allocator_->CreateIterator(&iter, block1); |
| 147 EXPECT_EQ(block2, allocator_->GetNextIterable(&iter, &type)); |
| 148 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); |
| 149 |
| 150 // Ensure nothing has gone noticably wrong. |
| 151 EXPECT_FALSE(allocator_->IsFull()); |
| 152 EXPECT_FALSE(allocator_->IsCorrupt()); |
| 153 |
| 154 // Check the internal histogram record of used memory. |
| 155 allocator_->UpdateTrackingHistograms(); |
| 156 scoped_ptr<HistogramSamples> used_samples( |
| 157 allocator_->used_histogram_->SnapshotSamples()); |
| 158 EXPECT_TRUE(used_samples); |
| 159 EXPECT_EQ(1, used_samples->TotalCount()); |
| 160 |
| 161 // Check the internal histogram record of allocation requests. |
| 162 scoped_ptr<HistogramSamples> allocs_samples( |
| 163 allocator_->allocs_histogram_->SnapshotSamples()); |
| 164 EXPECT_TRUE(allocs_samples); |
| 165 EXPECT_EQ(2, allocs_samples->TotalCount()); |
| 166 EXPECT_EQ(0, allocs_samples->GetCount(0)); |
| 167 EXPECT_EQ(1, allocs_samples->GetCount(sizeof(TestObject1))); |
| 168 EXPECT_EQ(1, allocs_samples->GetCount(sizeof(TestObject2))); |
| 169 #if !DCHECK_IS_ON() // DCHECK builds will die at a NOTREACHED(). |
| 170 EXPECT_EQ(0U, allocator_->Allocate(TEST_MEMORY_SIZE + 1, 0)); |
| 171 allocs_samples = allocator_->allocs_histogram_->SnapshotSamples(); |
| 172 EXPECT_EQ(3, allocs_samples->TotalCount()); |
| 173 EXPECT_EQ(1, allocs_samples->GetCount(0)); |
| 174 #endif |
| 175 |
| 176 // Check that an objcet's type can be changed. |
| 177 EXPECT_EQ(2U, allocator_->GetType(block2)); |
| 178 allocator_->SetType(block2, 3); |
| 179 EXPECT_EQ(3U, allocator_->GetType(block2)); |
| 180 allocator_->SetType(block2, 2); |
| 181 EXPECT_EQ(2U, allocator_->GetType(block2)); |
| 182 |
| 183 // Create second allocator (read/write) using the same memory segment. |
| 184 scoped_ptr<PersistentMemoryAllocator> allocator2( |
| 185 new PersistentMemoryAllocator( |
| 186 mem_segment_.get(), TEST_MEMORY_SIZE, TEST_MEMORY_PAGE, 0, "", |
| 187 false)); |
| 188 EXPECT_EQ(TEST_ID, allocator2->Id()); |
| 189 EXPECT_FALSE(allocator2->used_histogram_); |
| 190 EXPECT_FALSE(allocator2->allocs_histogram_); |
| 191 EXPECT_NE(allocator2->allocs_histogram_, allocator_->allocs_histogram_); |
| 192 |
| 193 // Ensure that iteration and access through second allocator works. |
| 194 allocator2->CreateIterator(&iter); |
| 195 EXPECT_EQ(block1, allocator2->GetNextIterable(&iter, &type)); |
| 196 EXPECT_EQ(block2, allocator2->GetNextIterable(&iter, &type)); |
| 197 EXPECT_EQ(0U, allocator2->GetNextIterable(&iter, &type)); |
| 198 EXPECT_NE(nullptr, allocator2->GetAsObject<TestObject1>(block1, 1)); |
| 199 EXPECT_NE(nullptr, allocator2->GetAsObject<TestObject2>(block2, 2)); |
| 200 |
| 201 // Create a third allocator (read-only) using the same memory segment. |
| 202 scoped_ptr<const PersistentMemoryAllocator> allocator3( |
| 203 new PersistentMemoryAllocator( |
| 204 mem_segment_.get(), TEST_MEMORY_SIZE, TEST_MEMORY_PAGE, 0, "", true)); |
| 205 EXPECT_EQ(TEST_ID, allocator3->Id()); |
| 206 EXPECT_FALSE(allocator3->used_histogram_); |
| 207 EXPECT_FALSE(allocator3->allocs_histogram_); |
| 208 |
| 209 // Ensure that iteration and access through third allocator works. |
| 210 allocator3->CreateIterator(&iter); |
| 211 EXPECT_EQ(block1, allocator3->GetNextIterable(&iter, &type)); |
| 212 EXPECT_EQ(block2, allocator3->GetNextIterable(&iter, &type)); |
| 213 EXPECT_EQ(0U, allocator3->GetNextIterable(&iter, &type)); |
| 214 EXPECT_NE(nullptr, allocator3->GetAsObject<TestObject1>(block1, 1)); |
| 215 EXPECT_NE(nullptr, allocator3->GetAsObject<TestObject2>(block2, 2)); |
| 216 } |
| 217 |
| 218 TEST_F(PersistentMemoryAllocatorTest, PageTest) { |
| 219 // This allocation will go into the first memory page. |
| 220 Reference block1 = allocator_->Allocate(TEST_MEMORY_PAGE / 2, 1); |
| 221 EXPECT_LT(0U, block1); |
| 222 EXPECT_GT(TEST_MEMORY_PAGE, block1); |
| 223 |
| 224 // This allocation won't fit in same page as previous block. |
| 225 Reference block2 = |
| 226 allocator_->Allocate(TEST_MEMORY_PAGE - 2 * kAllocAlignment, 2); |
| 227 EXPECT_EQ(TEST_MEMORY_PAGE, block2); |
| 228 |
| 229 // This allocation will also require a new page. |
| 230 Reference block3 = allocator_->Allocate(2 * kAllocAlignment + 99, 3); |
| 231 EXPECT_EQ(2U * TEST_MEMORY_PAGE, block3); |
| 232 } |
| 233 |
| 234 // A simple thread that takes an allocator and repeatedly allocates random- |
| 235 // sized chunks from it until no more can be done. |
| 236 class AllocatorThread : public SimpleThread { |
| 237 public: |
| 238 AllocatorThread(const std::string& name, |
| 239 void* base, |
| 240 uint32_t size, |
| 241 uint32_t page_size) |
| 242 : SimpleThread(name, Options()), |
| 243 count_(0), |
| 244 iterable_(0), |
| 245 allocator_(base, size, page_size, 0, std::string(), false) {} |
| 246 |
| 247 void Run() override { |
| 248 for (;;) { |
| 249 uint32_t size = RandInt(1, 99); |
| 250 uint32_t type = RandInt(100, 999); |
| 251 Reference block = allocator_.Allocate(size, type); |
| 252 if (!block) |
| 253 break; |
| 254 |
| 255 count_++; |
| 256 if (RandInt(0, 1)) { |
| 257 allocator_.MakeIterable(block); |
| 258 iterable_++; |
| 259 } |
| 260 } |
| 261 } |
| 262 |
| 263 unsigned iterable() { return iterable_; } |
| 264 unsigned count() { return count_; } |
| 265 |
| 266 private: |
| 267 unsigned count_; |
| 268 unsigned iterable_; |
| 269 PersistentMemoryAllocator allocator_; |
| 270 }; |
| 271 |
| 272 // Test parallel allocation/iteration and ensure consistency across all |
| 273 // instances. |
| 274 TEST_F(PersistentMemoryAllocatorTest, ParallelismTest) { |
| 275 void* memory = mem_segment_.get(); |
| 276 AllocatorThread t1("t1", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 277 AllocatorThread t2("t2", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 278 AllocatorThread t3("t3", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 279 AllocatorThread t4("t4", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 280 AllocatorThread t5("t5", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 281 |
| 282 t1.Start(); |
| 283 t2.Start(); |
| 284 t3.Start(); |
| 285 t4.Start(); |
| 286 t5.Start(); |
| 287 |
| 288 unsigned last_count = 0; |
| 289 do { |
| 290 unsigned count = CountIterables(); |
| 291 EXPECT_LE(last_count, count); |
| 292 } while (!allocator_->IsCorrupt() && !allocator_->IsFull()); |
| 293 |
| 294 t1.Join(); |
| 295 t2.Join(); |
| 296 t3.Join(); |
| 297 t4.Join(); |
| 298 t5.Join(); |
| 299 |
| 300 EXPECT_FALSE(allocator_->IsCorrupt()); |
| 301 EXPECT_TRUE(allocator_->IsFull()); |
| 302 EXPECT_EQ(CountIterables(), |
| 303 t1.iterable() + t2.iterable() + t3.iterable() + t4.iterable() + |
| 304 t5.iterable()); |
| 305 } |
| 306 |
| 307 // This test doesn't verify anything other than it doesn't crash. Its goal |
| 308 // is to find coding errors that aren't otherwise tested for, much like a |
| 309 // "fuzzer" would. |
| 310 TEST_F(PersistentMemoryAllocatorTest, CorruptionTest) { |
| 311 char* memory = mem_segment_.get(); |
| 312 AllocatorThread t1("t1", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 313 AllocatorThread t2("t2", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 314 AllocatorThread t3("t3", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 315 AllocatorThread t4("t4", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 316 AllocatorThread t5("t5", memory, TEST_MEMORY_SIZE, TEST_MEMORY_PAGE); |
| 317 |
| 318 t1.Start(); |
| 319 t2.Start(); |
| 320 t3.Start(); |
| 321 t4.Start(); |
| 322 t5.Start(); |
| 323 |
| 324 do { |
| 325 size_t offset = RandInt(0, TEST_MEMORY_SIZE - 1); |
| 326 char value = RandInt(0, 255); |
| 327 memory[offset] = value; |
| 328 } while (!allocator_->IsCorrupt() && !allocator_->IsFull()); |
| 329 |
| 330 t1.Join(); |
| 331 t2.Join(); |
| 332 t3.Join(); |
| 333 t4.Join(); |
| 334 t5.Join(); |
| 335 |
| 336 CountIterables(); |
| 337 } |
| 338 |
| 339 // Attempt to cause crashes or loops by expressly creating dangerous conditions. |
| 340 TEST_F(PersistentMemoryAllocatorTest, MaliciousTest) { |
| 341 Reference block1 = allocator_->Allocate(sizeof(TestObject1), 1); |
| 342 Reference block2 = allocator_->Allocate(sizeof(TestObject1), 2); |
| 343 Reference block3 = allocator_->Allocate(sizeof(TestObject1), 3); |
| 344 Reference block4 = allocator_->Allocate(sizeof(TestObject1), 3); |
| 345 Reference block5 = allocator_->Allocate(sizeof(TestObject1), 3); |
| 346 allocator_->MakeIterable(block1); |
| 347 allocator_->MakeIterable(block2); |
| 348 allocator_->MakeIterable(block3); |
| 349 allocator_->MakeIterable(block4); |
| 350 allocator_->MakeIterable(block5); |
| 351 EXPECT_EQ(5U, CountIterables()); |
| 352 EXPECT_FALSE(allocator_->IsCorrupt()); |
| 353 |
| 354 // Create loop in iterable list and ensure it doesn't hang. The return value |
| 355 // from CountIterables() in these cases is unpredictable. If there is a |
| 356 // failure, the call will hang and the test killed for taking too long. |
| 357 uint32_t* header4 = (uint32_t*)(mem_segment_.get() + block4); |
| 358 EXPECT_EQ(block5, header4[3]); |
| 359 header4[3] = block4; |
| 360 CountIterables(); // loop: 1-2-3-4-4 |
| 361 EXPECT_TRUE(allocator_->IsCorrupt()); |
| 362 |
| 363 // Test where loop goes back to previous block. |
| 364 header4[3] = block3; |
| 365 CountIterables(); // loop: 1-2-3-4-3 |
| 366 |
| 367 // Test where loop goes back to the beginning. |
| 368 header4[3] = block1; |
| 369 CountIterables(); // loop: 1-2-3-4-1 |
| 370 } |
| 371 |
| 372 |
| 373 //----- LocalPersistentMemoryAllocator ----------------------------------------- |
| 374 |
| 375 TEST(LocalPersistentMemoryAllocatorTest, CreationTest) { |
| 376 LocalPersistentMemoryAllocator allocator(TEST_MEMORY_SIZE, 42, ""); |
| 377 EXPECT_EQ(42U, allocator.Id()); |
| 378 EXPECT_NE(0U, allocator.Allocate(24, 1)); |
| 379 EXPECT_FALSE(allocator.IsFull()); |
| 380 EXPECT_FALSE(allocator.IsCorrupt()); |
| 381 } |
| 382 |
| 383 |
| 384 //----- FilePersistentMemoryAllocator ------------------------------------------ |
| 385 |
| 386 TEST(FilePersistentMemoryAllocatorTest, CreationTest) { |
| 387 ScopedTempDir temp_dir; |
| 388 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 389 FilePath file_path = temp_dir.path().AppendASCII("persistent_memory"); |
| 390 |
| 391 PersistentMemoryAllocator::MemoryInfo meminfo1; |
| 392 Reference r123, r456, r789; |
| 393 { |
| 394 LocalPersistentMemoryAllocator local(TEST_MEMORY_SIZE, TEST_ID, ""); |
| 395 EXPECT_FALSE(local.IsReadonly()); |
| 396 r123 = local.Allocate(123, 123); |
| 397 r456 = local.Allocate(456, 456); |
| 398 r789 = local.Allocate(789, 789); |
| 399 local.MakeIterable(r123); |
| 400 local.SetType(r456, 654); |
| 401 local.MakeIterable(r789); |
| 402 local.GetMemoryInfo(&meminfo1); |
| 403 EXPECT_FALSE(local.IsFull()); |
| 404 EXPECT_FALSE(local.IsCorrupt()); |
| 405 |
| 406 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); |
| 407 ASSERT_TRUE(writer.IsValid()); |
| 408 writer.Write(0, (const char*)local.data(), local.used()); |
| 409 } |
| 410 |
| 411 scoped_ptr<MemoryMappedFile> mmfile(new MemoryMappedFile()); |
| 412 mmfile->Initialize(file_path); |
| 413 EXPECT_TRUE(mmfile->IsValid()); |
| 414 const size_t mmlength = mmfile->length(); |
| 415 EXPECT_GE(meminfo1.total, mmlength); |
| 416 |
| 417 FilePersistentMemoryAllocator file(mmfile.release(), 0, ""); |
| 418 EXPECT_TRUE(file.IsReadonly()); |
| 419 EXPECT_EQ(TEST_ID, file.Id()); |
| 420 EXPECT_FALSE(file.IsFull()); |
| 421 EXPECT_FALSE(file.IsCorrupt()); |
| 422 |
| 423 PersistentMemoryAllocator::Iterator iter; |
| 424 uint32_t type; |
| 425 file.CreateIterator(&iter); |
| 426 EXPECT_EQ(r123, file.GetNextIterable(&iter, &type)); |
| 427 EXPECT_EQ(r789, file.GetNextIterable(&iter, &type)); |
| 428 EXPECT_EQ(0U, file.GetNextIterable(&iter, &type)); |
| 429 |
| 430 EXPECT_EQ(123U, file.GetType(r123)); |
| 431 EXPECT_EQ(654U, file.GetType(r456)); |
| 432 EXPECT_EQ(789U, file.GetType(r789)); |
| 433 |
| 434 PersistentMemoryAllocator::MemoryInfo meminfo2; |
| 435 file.GetMemoryInfo(&meminfo2); |
| 436 EXPECT_GE(meminfo1.total, meminfo2.total); |
| 437 EXPECT_GE(meminfo1.free, meminfo2.free); |
| 438 EXPECT_EQ(mmlength, meminfo2.total); |
| 439 EXPECT_EQ(0U, meminfo2.free); |
| 440 } |
| 441 |
| 442 TEST(FilePersistentMemoryAllocatorTest, AcceptableTest) { |
| 443 ScopedTempDir temp_dir; |
| 444 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 445 FilePath file_path_base = temp_dir.path().AppendASCII("persistent_memory_"); |
| 446 |
| 447 LocalPersistentMemoryAllocator local(TEST_MEMORY_SIZE, TEST_ID, ""); |
| 448 const size_t minsize = local.used(); |
| 449 scoped_ptr<char[]> garbage(new char[minsize]); |
| 450 RandBytes(garbage.get(), minsize); |
| 451 |
| 452 scoped_ptr<MemoryMappedFile> mmfile; |
| 453 char filename[100]; |
| 454 for (size_t filesize = minsize; filesize > 0; --filesize) { |
| 455 strings::SafeSPrintf(filename, "memory_%d_A", filesize); |
| 456 FilePath file_path = temp_dir.path().AppendASCII(filename); |
| 457 ASSERT_FALSE(PathExists(file_path)); |
| 458 { |
| 459 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); |
| 460 ASSERT_TRUE(writer.IsValid()); |
| 461 writer.Write(0, (const char*)local.data(), filesize); |
| 462 } |
| 463 ASSERT_TRUE(PathExists(file_path)); |
| 464 |
| 465 mmfile.reset(new MemoryMappedFile()); |
| 466 mmfile->Initialize(file_path); |
| 467 EXPECT_EQ(filesize, mmfile->length()); |
| 468 if (FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile)) { |
| 469 // Just need to make sure it doesn't crash. |
| 470 FilePersistentMemoryAllocator allocator(mmfile.release(), 0, ""); |
| 471 (void)allocator; // Ensure compiler can't optimize-out above variable. |
| 472 } else { |
| 473 // For filesize >= minsize, the file must be acceptable. This |
| 474 // else clause (file-not-acceptable) should be reached only if |
| 475 // filesize < minsize. |
| 476 EXPECT_LT(filesize, minsize); |
| 477 } |
| 478 |
| 479 #if !DCHECK_IS_ON() // DCHECK builds will die at a NOTREACHED(). |
| 480 strings::SafeSPrintf(filename, "memory_%d_B", filesize); |
| 481 file_path = temp_dir.path().AppendASCII(filename); |
| 482 ASSERT_FALSE(PathExists(file_path)); |
| 483 { |
| 484 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); |
| 485 ASSERT_TRUE(writer.IsValid()); |
| 486 writer.Write(0, (const char*)garbage.get(), filesize); |
| 487 } |
| 488 ASSERT_TRUE(PathExists(file_path)); |
| 489 |
| 490 mmfile.reset(new MemoryMappedFile()); |
| 491 mmfile->Initialize(file_path); |
| 492 EXPECT_EQ(filesize, mmfile->length()); |
| 493 if (FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile)) { |
| 494 // Just need to make sure it doesn't crash. |
| 495 FilePersistentMemoryAllocator allocator(mmfile.release(), 0, "") ; |
| 496 EXPECT_TRUE(allocator.IsCorrupt()); // Garbage data so it should be. |
| 497 } else { |
| 498 // For filesize >= minsize, the file must be acceptable. This |
| 499 // else clause (file-not-acceptable) should be reached only if |
| 500 // filesize < minsize. |
| 501 EXPECT_GT(minsize, filesize); |
| 502 } |
| 503 #endif |
| 504 } |
| 505 } |
| 506 |
| 507 } // namespace base |
OLD | NEW |