Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "components/browser_watcher/postmortem_minidump_writer.h" | |
| 6 | |
| 7 #include <windows.h> // NOLINT | |
| 8 #include <dbghelp.h> | |
| 9 | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/files/file_util.h" | |
| 12 #include "base/files/scoped_file.h" | |
| 13 #include "base/files/scoped_temp_dir.h" | |
| 14 #include "components/browser_watcher/stability_report.pb.h" | |
| 15 #include "components/version_info/version_info.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 #include "third_party/crashpad/crashpad/snapshot/minidump/process_snapshot_minid ump.h" | |
| 18 #include "third_party/crashpad/crashpad/util/file/file_reader.h" | |
| 19 #include "third_party/crashpad/crashpad/util/misc/uuid.h" | |
| 20 | |
| 21 namespace browser_watcher { | |
| 22 | |
| 23 using crashpad::UUID; | |
| 24 | |
| 25 class PostmortemMinidumpWriterWinTest : public testing::Test { | |
| 26 public: | |
| 27 void SetUp() override { | |
| 28 testing::Test::SetUp(); | |
| 29 | |
| 30 expected_client_id_ = UUID(UUID::InitializeWithNewTag{}); | |
| 31 expected_report_id_ = UUID(UUID::InitializeWithNewTag{}); | |
| 32 | |
| 33 // Create a stability report. | |
| 34 // TODO(manzagop): flesh out the report once proto is more detailed. | |
| 35 ProcessState* process_state = expected_report_.add_process_states(); | |
| 36 CodeModule* module = process_state->add_modules(); | |
| 37 module->set_base_address(1024); | |
| 38 module->set_code_file("some_code_file.dll"); | |
| 39 | |
| 40 // Write the minidump. | |
| 41 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 42 minidump_path_ = temp_dir_.path().AppendASCII("minidump.dmp"); | |
| 43 base::ScopedFILE file(base::OpenFile(minidump_path_, "wb")); | |
| 44 ASSERT_TRUE(file.get()); | |
| 45 | |
| 46 PostmortemMinidumpWriter writer(file.get()); | |
|
Sigurður Ásgeirsson
2016/08/11 17:44:15
nit: I'd separate the actual writing of the report
manzagop (departed)
2016/08/12 21:23:22
Done.
| |
| 47 ASSERT_TRUE(writer.WriteDump(expected_report_, expected_client_id_, | |
|
Sigurður Ásgeirsson
2016/08/11 17:44:15
it's awkward that this function derives some of th
manzagop (departed)
2016/08/12 21:23:22
Assuming you mean product/version. Done.
| |
| 48 expected_report_id_)); | |
| 49 } | |
| 50 | |
| 51 const base::FilePath& minidump_path() { return minidump_path_; } | |
| 52 const UUID& expected_client_id() { return expected_client_id_; } | |
| 53 const UUID& expected_report_id() { return expected_report_id_; } | |
| 54 const StabilityReport& expected_report() { return expected_report_; } | |
| 55 | |
| 56 private: | |
| 57 base::ScopedTempDir temp_dir_; | |
| 58 base::FilePath minidump_path_; | |
| 59 UUID expected_client_id_; | |
| 60 UUID expected_report_id_; | |
| 61 StabilityReport expected_report_; | |
| 62 }; | |
| 63 | |
| 64 TEST_F(PostmortemMinidumpWriterWinTest, ValidateStabilityReportTest) { | |
| 65 // Read back the minidump. | |
| 66 base::ScopedFILE minidump_file; | |
| 67 minidump_file.reset(base::OpenFile(minidump_path(), "rb")); | |
| 68 ASSERT_TRUE(minidump_file.get()); | |
| 69 | |
| 70 MINIDUMP_HEADER header = {}; | |
| 71 ASSERT_EQ(1, fread(&header, sizeof(header), 1, minidump_file.get())); | |
| 72 ASSERT_EQ(MINIDUMP_SIGNATURE, header.Signature); | |
| 73 ASSERT_EQ(2, header.NumberOfStreams); | |
| 74 RVA directory_rva = header.StreamDirectoryRva; | |
| 75 | |
| 76 MINIDUMP_DIRECTORY directory = {}; | |
| 77 ASSERT_EQ(0, fseek(minidump_file.get(), directory_rva, SEEK_SET)); | |
| 78 ASSERT_EQ(1, fread(&directory, sizeof(directory), 1, minidump_file.get())); | |
| 79 ASSERT_EQ(0x4B6B0002, directory.StreamType); | |
| 80 RVA report_rva = directory.Location.Rva; | |
| 81 ULONG32 report_size_bytes = directory.Location.DataSize; | |
| 82 | |
| 83 std::string recovered_serialized_report; | |
|
Sigurður Ásgeirsson
2016/08/11 17:44:15
if there's a way to get at the serialized report w
manzagop (departed)
2016/08/12 21:23:22
Documented and linked a bug. Done.
| |
| 84 recovered_serialized_report.resize(report_size_bytes); | |
| 85 ASSERT_EQ(0, fseek(minidump_file.get(), report_rva, SEEK_SET)); | |
| 86 ASSERT_EQ(report_size_bytes, fread(&recovered_serialized_report.at(0), 1, | |
| 87 report_size_bytes, minidump_file.get())); | |
| 88 | |
| 89 // Validate the recovered report. | |
| 90 std::string expected_serialized_report; | |
| 91 expected_report().SerializeToString(&expected_serialized_report); | |
| 92 ASSERT_EQ(expected_serialized_report, recovered_serialized_report); | |
| 93 | |
| 94 StabilityReport recovered_report; | |
| 95 ASSERT_TRUE(recovered_report.ParseFromString(recovered_serialized_report)); | |
| 96 } | |
| 97 | |
| 98 TEST_F(PostmortemMinidumpWriterWinTest, CrashPadCanReadTest) { | |
| 99 // Validate crashpad can read the produced minidump. | |
| 100 crashpad::FileReader minidump_file_reader; | |
| 101 ASSERT_TRUE(minidump_file_reader.Open(minidump_path())); | |
| 102 | |
| 103 crashpad::ProcessSnapshotMinidump minidump_process_snapshot; | |
| 104 ASSERT_TRUE(minidump_process_snapshot.Initialize(&minidump_file_reader)); | |
| 105 | |
| 106 // Validate the crashpadinfo. | |
| 107 UUID client_id; | |
| 108 minidump_process_snapshot.ClientID(&client_id); | |
| 109 ASSERT_EQ(expected_client_id(), client_id); | |
| 110 | |
| 111 UUID report_id; | |
| 112 minidump_process_snapshot.ReportID(&report_id); | |
| 113 ASSERT_EQ(expected_report_id(), report_id); | |
| 114 | |
| 115 std::map<std::string, std::string> parameters = | |
| 116 minidump_process_snapshot.AnnotationsSimpleMap(); | |
| 117 auto it = parameters.find("product"); | |
| 118 ASSERT_NE(parameters.end(), it); | |
| 119 ASSERT_EQ(version_info::GetProductName() + "_Postmortem", it->second); | |
| 120 | |
| 121 it = parameters.find("version"); | |
| 122 ASSERT_NE(parameters.end(), it); | |
| 123 ASSERT_EQ(version_info::GetVersionNumber(), it->second); | |
| 124 } | |
| 125 | |
| 126 } // namespace browser_watcher | |
| OLD | NEW |