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