OLD | NEW |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "minidump/minidump_memory_writer.h" | 15 #include "minidump/minidump_memory_writer.h" |
16 | 16 |
17 #include <dbghelp.h> | 17 #include <dbghelp.h> |
18 #include <stdint.h> | 18 #include <stdint.h> |
19 | 19 |
20 #include "base/basictypes.h" | 20 #include "base/basictypes.h" |
| 21 #include "base/strings/stringprintf.h" |
21 #include "gtest/gtest.h" | 22 #include "gtest/gtest.h" |
22 #include "minidump/minidump_extensions.h" | 23 #include "minidump/minidump_extensions.h" |
23 #include "minidump/minidump_file_writer.h" | 24 #include "minidump/minidump_file_writer.h" |
24 #include "minidump/minidump_stream_writer.h" | 25 #include "minidump/minidump_stream_writer.h" |
25 #include "minidump/test/minidump_file_writer_test_util.h" | 26 #include "minidump/test/minidump_file_writer_test_util.h" |
26 #include "minidump/test/minidump_memory_writer_test_util.h" | 27 #include "minidump/test/minidump_memory_writer_test_util.h" |
27 #include "minidump/test/minidump_writable_test_util.h" | 28 #include "minidump/test/minidump_writable_test_util.h" |
| 29 #include "snapshot/test/test_memory_snapshot.h" |
28 #include "util/file/string_file_writer.h" | 30 #include "util/file/string_file_writer.h" |
| 31 #include "util/stdlib/pointer_container.h" |
29 | 32 |
30 namespace crashpad { | 33 namespace crashpad { |
31 namespace test { | 34 namespace test { |
32 namespace { | 35 namespace { |
33 | 36 |
34 const MinidumpStreamType kBogusStreamType = | 37 const MinidumpStreamType kBogusStreamType = |
35 static_cast<MinidumpStreamType>(1234); | 38 static_cast<MinidumpStreamType>(1234); |
36 | 39 |
37 // expected_streams is the expected number of streams in the file. The memory | 40 // expected_streams is the expected number of streams in the file. The memory |
38 // list must be the last stream. If there is another stream, it must come first, | 41 // list must be the last stream. If there is another stream, it must come first, |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 DISALLOW_COPY_AND_ASSIGN(TestMemoryStream); | 231 DISALLOW_COPY_AND_ASSIGN(TestMemoryStream); |
229 }; | 232 }; |
230 | 233 |
231 TEST(MinidumpMemoryWriter, ExtraMemory) { | 234 TEST(MinidumpMemoryWriter, ExtraMemory) { |
232 // This tests MinidumpMemoryListWriter::AddExtraMemory(). That method adds | 235 // This tests MinidumpMemoryListWriter::AddExtraMemory(). That method adds |
233 // a MinidumpMemoryWriter to the MinidumpMemoryListWriter without making the | 236 // a MinidumpMemoryWriter to the MinidumpMemoryListWriter without making the |
234 // memory writer a child of the memory list writer. | 237 // memory writer a child of the memory list writer. |
235 MinidumpFileWriter minidump_file_writer; | 238 MinidumpFileWriter minidump_file_writer; |
236 | 239 |
237 const uint64_t kBaseAddress0 = 0x1000; | 240 const uint64_t kBaseAddress0 = 0x1000; |
238 const uint64_t kSize0 = 0x0400; | 241 const size_t kSize0 = 0x0400; |
239 const uint8_t kValue0 = '1'; | 242 const uint8_t kValue0 = '1'; |
240 auto test_memory_stream = | 243 auto test_memory_stream = |
241 make_scoped_ptr(new TestMemoryStream(kBaseAddress0, kSize0, kValue0)); | 244 make_scoped_ptr(new TestMemoryStream(kBaseAddress0, kSize0, kValue0)); |
242 | 245 |
243 auto memory_list_writer = make_scoped_ptr(new MinidumpMemoryListWriter()); | 246 auto memory_list_writer = make_scoped_ptr(new MinidumpMemoryListWriter()); |
244 memory_list_writer->AddExtraMemory(test_memory_stream->memory()); | 247 memory_list_writer->AddExtraMemory(test_memory_stream->memory()); |
245 | 248 |
246 minidump_file_writer.AddStream(test_memory_stream.Pass()); | 249 minidump_file_writer.AddStream(test_memory_stream.Pass()); |
247 | 250 |
248 const uint64_t kBaseAddress1 = 0x2000; | 251 const uint64_t kBaseAddress1 = 0x2000; |
249 const uint64_t kSize1 = 0x0400; | 252 const size_t kSize1 = 0x0400; |
250 const uint8_t kValue1 = 'm'; | 253 const uint8_t kValue1 = 'm'; |
251 | 254 |
252 auto memory_writer = make_scoped_ptr( | 255 auto memory_writer = make_scoped_ptr( |
253 new TestMinidumpMemoryWriter(kBaseAddress1, kSize1, kValue1)); | 256 new TestMinidumpMemoryWriter(kBaseAddress1, kSize1, kValue1)); |
254 memory_list_writer->AddMemory(memory_writer.Pass()); | 257 memory_list_writer->AddMemory(memory_writer.Pass()); |
255 | 258 |
256 minidump_file_writer.AddStream(memory_list_writer.Pass()); | 259 minidump_file_writer.AddStream(memory_list_writer.Pass()); |
257 | 260 |
258 StringFileWriter file_writer; | 261 StringFileWriter file_writer; |
259 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer)); | 262 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer)); |
(...skipping 30 matching lines...) Expand all Loading... |
290 expected.Memory.Rva = memory_list->MemoryRanges[0].Memory.Rva + | 293 expected.Memory.Rva = memory_list->MemoryRanges[0].Memory.Rva + |
291 memory_list->MemoryRanges[0].Memory.DataSize; | 294 memory_list->MemoryRanges[0].Memory.DataSize; |
292 ExpectMinidumpMemoryDescriptorAndContents(&expected, | 295 ExpectMinidumpMemoryDescriptorAndContents(&expected, |
293 &memory_list->MemoryRanges[1], | 296 &memory_list->MemoryRanges[1], |
294 file_writer.string(), | 297 file_writer.string(), |
295 kValue1, | 298 kValue1, |
296 true); | 299 true); |
297 } | 300 } |
298 } | 301 } |
299 | 302 |
| 303 TEST(MinidumpMemoryWriter, AddFromSnapshot) { |
| 304 MINIDUMP_MEMORY_DESCRIPTOR expect_memory_descriptors[3] = {}; |
| 305 uint8_t values[arraysize(expect_memory_descriptors)] = {}; |
| 306 |
| 307 expect_memory_descriptors[0].StartOfMemoryRange = 0; |
| 308 expect_memory_descriptors[0].Memory.DataSize = 0x1000; |
| 309 values[0] = 0x01; |
| 310 |
| 311 expect_memory_descriptors[1].StartOfMemoryRange = 0x1000; |
| 312 expect_memory_descriptors[1].Memory.DataSize = 0x2000; |
| 313 values[1] = 0xf4; |
| 314 |
| 315 expect_memory_descriptors[2].StartOfMemoryRange = 0x7654321000000000; |
| 316 expect_memory_descriptors[2].Memory.DataSize = 0x800; |
| 317 values[2] = 0xa9; |
| 318 |
| 319 PointerVector<TestMemorySnapshot> memory_snapshots_owner; |
| 320 std::vector<const MemorySnapshot*> memory_snapshots; |
| 321 for (size_t index = 0; |
| 322 index < arraysize(expect_memory_descriptors); |
| 323 ++index) { |
| 324 TestMemorySnapshot* memory_snapshot = new TestMemorySnapshot(); |
| 325 memory_snapshots_owner.push_back(memory_snapshot); |
| 326 memory_snapshot->SetAddress( |
| 327 expect_memory_descriptors[index].StartOfMemoryRange); |
| 328 memory_snapshot->SetSize(expect_memory_descriptors[index].Memory.DataSize); |
| 329 memory_snapshot->SetValue(values[index]); |
| 330 memory_snapshots.push_back(memory_snapshot); |
| 331 } |
| 332 |
| 333 auto memory_list_writer = make_scoped_ptr(new MinidumpMemoryListWriter()); |
| 334 memory_list_writer->AddFromSnapshot(memory_snapshots); |
| 335 |
| 336 MinidumpFileWriter minidump_file_writer; |
| 337 minidump_file_writer.AddStream(memory_list_writer.Pass()); |
| 338 |
| 339 StringFileWriter file_writer; |
| 340 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer)); |
| 341 |
| 342 const MINIDUMP_MEMORY_LIST* memory_list; |
| 343 ASSERT_NO_FATAL_FAILURE( |
| 344 GetMemoryListStream(file_writer.string(), &memory_list, 1)); |
| 345 |
| 346 ASSERT_EQ(3u, memory_list->NumberOfMemoryRanges); |
| 347 |
| 348 for (size_t index = 0; index < memory_list->NumberOfMemoryRanges; ++index) { |
| 349 SCOPED_TRACE(base::StringPrintf("index %zu", index)); |
| 350 ExpectMinidumpMemoryDescriptorAndContents( |
| 351 &expect_memory_descriptors[index], |
| 352 &memory_list->MemoryRanges[index], |
| 353 file_writer.string(), |
| 354 values[index], |
| 355 index == memory_list->NumberOfMemoryRanges - 1); |
| 356 } |
| 357 } |
| 358 |
300 } // namespace | 359 } // namespace |
301 } // namespace test | 360 } // namespace test |
302 } // namespace crashpad | 361 } // namespace crashpad |
OLD | NEW |