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

Side by Side Diff: minidump/minidump_file_writer_test.cc

Issue 703223003: Add MinidumpFileWriter::InitializeFromSnapshot() and its tests (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@minidump_crashpad_info_init_from_snapshot
Patch Set: Address review feedback Created 6 years, 1 month 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 | « minidump/minidump_file_writer.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 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_file_writer.h" 15 #include "minidump/minidump_file_writer.h"
16 16
17 #include <dbghelp.h> 17 #include <dbghelp.h>
18 18
19 #include <string> 19 #include <string>
20 20
21 #include "base/basictypes.h" 21 #include "base/basictypes.h"
22 #include "gtest/gtest.h" 22 #include "gtest/gtest.h"
23 #include "minidump/minidump_stream_writer.h" 23 #include "minidump/minidump_stream_writer.h"
24 #include "minidump/minidump_writable.h" 24 #include "minidump/minidump_writable.h"
25 #include "minidump/test/minidump_file_writer_test_util.h" 25 #include "minidump/test/minidump_file_writer_test_util.h"
26 #include "minidump/test/minidump_writable_test_util.h" 26 #include "minidump/test/minidump_writable_test_util.h"
27 #include "snapshot/test/test_cpu_context.h"
28 #include "snapshot/test/test_exception_snapshot.h"
29 #include "snapshot/test/test_module_snapshot.h"
30 #include "snapshot/test/test_process_snapshot.h"
31 #include "snapshot/test/test_system_snapshot.h"
32 #include "snapshot/test/test_thread_snapshot.h"
27 #include "util/file/file_writer.h" 33 #include "util/file/file_writer.h"
28 #include "util/file/string_file_writer.h" 34 #include "util/file/string_file_writer.h"
29 35
30 namespace crashpad { 36 namespace crashpad {
31 namespace test { 37 namespace test {
32 namespace { 38 namespace {
33 39
34 TEST(MinidumpFileWriter, Empty) { 40 TEST(MinidumpFileWriter, Empty) {
35 MinidumpFileWriter minidump_file; 41 MinidumpFileWriter minidump_file;
36 StringFileWriter file_writer; 42 StringFileWriter file_writer;
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 const MINIDUMP_HEADER* header = 231 const MINIDUMP_HEADER* header =
226 MinidumpHeaderAtStart(file_writer.string(), &directory); 232 MinidumpHeaderAtStart(file_writer.string(), &directory);
227 ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 1, 0)); 233 ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 1, 0));
228 ASSERT_TRUE(directory); 234 ASSERT_TRUE(directory);
229 235
230 EXPECT_EQ(kStreamType, directory[0].StreamType); 236 EXPECT_EQ(kStreamType, directory[0].StreamType);
231 EXPECT_EQ(kStreamSize, directory[0].Location.DataSize); 237 EXPECT_EQ(kStreamSize, directory[0].Location.DataSize);
232 EXPECT_EQ(kStreamOffset, directory[0].Location.Rva); 238 EXPECT_EQ(kStreamOffset, directory[0].Location.Rva);
233 } 239 }
234 240
241 TEST(MinidumpFileWriter, InitializeFromSnapshot_Basic) {
242 const uint32_t kSnapshotTime = 0x4976043c;
243 const timeval kSnapshotTimeval = { implicit_cast<time_t>(kSnapshotTime), 0 };
244
245 TestProcessSnapshot process_snapshot;
246 process_snapshot.SetSnapshotTime(kSnapshotTimeval);
247
248 auto system_snapshot = make_scoped_ptr(new TestSystemSnapshot());
249 system_snapshot->SetCPUArchitecture(kCPUArchitectureX86_64);
250 system_snapshot->SetOperatingSystem(SystemSnapshot::kOperatingSystemMacOSX);
251 process_snapshot.SetSystem(system_snapshot.Pass());
252
253 MinidumpFileWriter minidump_file_writer;
254 minidump_file_writer.InitializeFromSnapshot(&process_snapshot);
255
256 StringFileWriter file_writer;
257 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer));
258
259 const MINIDUMP_DIRECTORY* directory;
260 const MINIDUMP_HEADER* header =
261 MinidumpHeaderAtStart(file_writer.string(), &directory);
262 ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 5, kSnapshotTime));
263 ASSERT_TRUE(directory);
264
265 EXPECT_EQ(kMinidumpStreamTypeSystemInfo, directory[0].StreamType);
266 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_SYSTEM_INFO>(
267 file_writer.string(), directory[0].Location));
268
269 EXPECT_EQ(kMinidumpStreamTypeMiscInfo, directory[1].StreamType);
270 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MISC_INFO_4>(
271 file_writer.string(), directory[1].Location));
272
273 EXPECT_EQ(kMinidumpStreamTypeThreadList, directory[2].StreamType);
274 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_THREAD_LIST>(
275 file_writer.string(), directory[2].Location));
276
277 EXPECT_EQ(kMinidumpStreamTypeModuleList, directory[3].StreamType);
278 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MODULE_LIST>(
279 file_writer.string(), directory[3].Location));
280
281 EXPECT_EQ(kMinidumpStreamTypeMemoryList, directory[4].StreamType);
282 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MEMORY_LIST>(
283 file_writer.string(), directory[4].Location));
284 }
285
286 TEST(MinidumpFileWriter, InitializeFromSnapshot_Exception) {
287 // In a 32-bit environment, this will give a “timestamp out of range” warning,
288 // but the test should complete without failure.
289 const uint32_t kSnapshotTime = 0xfd469ab8;
290 const timeval kSnapshotTimeval = { implicit_cast<time_t>(kSnapshotTime), 0 };
291
292 TestProcessSnapshot process_snapshot;
293 process_snapshot.SetSnapshotTime(kSnapshotTimeval);
294
295 auto system_snapshot = make_scoped_ptr(new TestSystemSnapshot());
296 system_snapshot->SetCPUArchitecture(kCPUArchitectureX86_64);
297 system_snapshot->SetOperatingSystem(SystemSnapshot::kOperatingSystemMacOSX);
298 process_snapshot.SetSystem(system_snapshot.Pass());
299
300 auto thread_snapshot = make_scoped_ptr(new TestThreadSnapshot());
301 InitializeCPUContextX86_64(thread_snapshot->MutableContext(), 5);
302 process_snapshot.AddThread(thread_snapshot.Pass());
303
304 auto exception_snapshot = make_scoped_ptr(new TestExceptionSnapshot());
305 InitializeCPUContextX86_64(exception_snapshot->MutableContext(), 11);
306 process_snapshot.SetException(exception_snapshot.Pass());
307
308 // The module does not have anything that needs to be represented in a
309 // MinidumpModuleCrashpadInfo structure, so no such structure is expected to
310 // be present, which will in turn suppress the addition of a
311 // MinidumpCrashpadInfo stream.
312 auto module_snapshot = make_scoped_ptr(new TestModuleSnapshot());
313 process_snapshot.AddModule(module_snapshot.Pass());
314
315 MinidumpFileWriter minidump_file_writer;
316 minidump_file_writer.InitializeFromSnapshot(&process_snapshot);
317
318 StringFileWriter file_writer;
319 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer));
320
321 const MINIDUMP_DIRECTORY* directory;
322 const MINIDUMP_HEADER* header =
323 MinidumpHeaderAtStart(file_writer.string(), &directory);
324 ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 6, kSnapshotTime));
325 ASSERT_TRUE(directory);
326
327 EXPECT_EQ(kMinidumpStreamTypeSystemInfo, directory[0].StreamType);
328 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_SYSTEM_INFO>(
329 file_writer.string(), directory[0].Location));
330
331 EXPECT_EQ(kMinidumpStreamTypeMiscInfo, directory[1].StreamType);
332 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MISC_INFO_4>(
333 file_writer.string(), directory[1].Location));
334
335 EXPECT_EQ(kMinidumpStreamTypeThreadList, directory[2].StreamType);
336 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_THREAD_LIST>(
337 file_writer.string(), directory[2].Location));
338
339 EXPECT_EQ(kMinidumpStreamTypeException, directory[3].StreamType);
340 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_EXCEPTION_STREAM>(
341 file_writer.string(), directory[3].Location));
342
343 EXPECT_EQ(kMinidumpStreamTypeModuleList, directory[4].StreamType);
344 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MODULE_LIST>(
345 file_writer.string(), directory[4].Location));
346
347 EXPECT_EQ(kMinidumpStreamTypeMemoryList, directory[5].StreamType);
348 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MEMORY_LIST>(
349 file_writer.string(), directory[5].Location));
350 }
351
352 TEST(MinidumpFileWriter, InitializeFromSnapshot_CrashpadInfo) {
353 const uint32_t kSnapshotTime = 0x15393bd3;
354 const timeval kSnapshotTimeval = { implicit_cast<time_t>(kSnapshotTime), 0 };
355
356 TestProcessSnapshot process_snapshot;
357 process_snapshot.SetSnapshotTime(kSnapshotTimeval);
358
359 auto system_snapshot = make_scoped_ptr(new TestSystemSnapshot());
360 system_snapshot->SetCPUArchitecture(kCPUArchitectureX86_64);
361 system_snapshot->SetOperatingSystem(SystemSnapshot::kOperatingSystemMacOSX);
362 process_snapshot.SetSystem(system_snapshot.Pass());
363
364 auto thread_snapshot = make_scoped_ptr(new TestThreadSnapshot());
365 InitializeCPUContextX86_64(thread_snapshot->MutableContext(), 5);
366 process_snapshot.AddThread(thread_snapshot.Pass());
367
368 auto exception_snapshot = make_scoped_ptr(new TestExceptionSnapshot());
369 InitializeCPUContextX86_64(exception_snapshot->MutableContext(), 11);
370 process_snapshot.SetException(exception_snapshot.Pass());
371
372 // The module needs an annotation for the MinidumpCrashpadInfo stream to be
373 // considered useful and be included.
374 auto module_snapshot = make_scoped_ptr(new TestModuleSnapshot());
375 std::vector<std::string> annotations_list(1, std::string("annotation"));
376 module_snapshot->SetAnnotationsVector(annotations_list);
377 process_snapshot.AddModule(module_snapshot.Pass());
378
379 MinidumpFileWriter minidump_file_writer;
380 minidump_file_writer.InitializeFromSnapshot(&process_snapshot);
381
382 StringFileWriter file_writer;
383 ASSERT_TRUE(minidump_file_writer.WriteEverything(&file_writer));
384
385 const MINIDUMP_DIRECTORY* directory;
386 const MINIDUMP_HEADER* header =
387 MinidumpHeaderAtStart(file_writer.string(), &directory);
388 ASSERT_NO_FATAL_FAILURE(VerifyMinidumpHeader(header, 7, kSnapshotTime));
389 ASSERT_TRUE(directory);
390
391 EXPECT_EQ(kMinidumpStreamTypeSystemInfo, directory[0].StreamType);
392 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_SYSTEM_INFO>(
393 file_writer.string(), directory[0].Location));
394
395 EXPECT_EQ(kMinidumpStreamTypeMiscInfo, directory[1].StreamType);
396 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MISC_INFO_4>(
397 file_writer.string(), directory[1].Location));
398
399 EXPECT_EQ(kMinidumpStreamTypeThreadList, directory[2].StreamType);
400 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_THREAD_LIST>(
401 file_writer.string(), directory[2].Location));
402
403 EXPECT_EQ(kMinidumpStreamTypeException, directory[3].StreamType);
404 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_EXCEPTION_STREAM>(
405 file_writer.string(), directory[3].Location));
406
407 EXPECT_EQ(kMinidumpStreamTypeModuleList, directory[4].StreamType);
408 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MODULE_LIST>(
409 file_writer.string(), directory[4].Location));
410
411 EXPECT_EQ(kMinidumpStreamTypeCrashpadInfo, directory[5].StreamType);
412 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MinidumpCrashpadInfo>(
413 file_writer.string(), directory[5].Location));
414
415 EXPECT_EQ(kMinidumpStreamTypeMemoryList, directory[6].StreamType);
416 EXPECT_TRUE(MinidumpWritableAtLocationDescriptor<MINIDUMP_MEMORY_LIST>(
417 file_writer.string(), directory[6].Location));
418 }
419
235 TEST(MinidumpFileWriterDeathTest, SameStreamType) { 420 TEST(MinidumpFileWriterDeathTest, SameStreamType) {
236 MinidumpFileWriter minidump_file; 421 MinidumpFileWriter minidump_file;
237 422
238 const size_t kStream0Size = 5; 423 const size_t kStream0Size = 5;
239 const MinidumpStreamType kStream0Type = static_cast<MinidumpStreamType>(0x4d); 424 const MinidumpStreamType kStream0Type = static_cast<MinidumpStreamType>(0x4d);
240 const uint8_t kStream0Value = 0x5a; 425 const uint8_t kStream0Value = 0x5a;
241 auto stream0 = make_scoped_ptr( 426 auto stream0 = make_scoped_ptr(
242 new TestStream(kStream0Type, kStream0Size, kStream0Value)); 427 new TestStream(kStream0Type, kStream0Size, kStream0Value));
243 minidump_file.AddStream(stream0.Pass()); 428 minidump_file.AddStream(stream0.Pass());
244 429
245 // It is an error to add a second stream of the same type. 430 // It is an error to add a second stream of the same type.
246 const size_t kStream1Size = 3; 431 const size_t kStream1Size = 3;
247 const MinidumpStreamType kStream1Type = static_cast<MinidumpStreamType>(0x4d); 432 const MinidumpStreamType kStream1Type = static_cast<MinidumpStreamType>(0x4d);
248 const uint8_t kStream1Value = 0xa5; 433 const uint8_t kStream1Value = 0xa5;
249 auto stream1 = make_scoped_ptr( 434 auto stream1 = make_scoped_ptr(
250 new TestStream(kStream1Type, kStream1Size, kStream1Value)); 435 new TestStream(kStream1Type, kStream1Size, kStream1Value));
251 ASSERT_DEATH(minidump_file.AddStream(stream1.Pass()), "already present"); 436 ASSERT_DEATH(minidump_file.AddStream(stream1.Pass()), "already present");
252 } 437 }
253 438
254 } // namespace 439 } // namespace
255 } // namespace test 440 } // namespace test
256 } // namespace crashpad 441 } // namespace crashpad
OLDNEW
« no previous file with comments | « minidump/minidump_file_writer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698