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 "base/win/scoped_handle.h" | |
15 #include "components/browser_watcher/stability_report.pb.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 const char kProductName[] = "some-product"; | |
26 const char kExpectedProductName[] = "some-product_Postmortem"; | |
27 const char kVersion[] = "51.0.2704.106"; | |
28 | |
29 class WritePostmortemDumpTest : public testing::Test { | |
30 public: | |
31 void SetUp() override { | |
32 testing::Test::SetUp(); | |
33 | |
34 expected_client_id_ = UUID(UUID::InitializeWithNewTag{}); | |
35 expected_report_id_ = UUID(UUID::InitializeWithNewTag{}); | |
36 | |
37 // Create a stability report. | |
38 // TODO(manzagop): flesh out the report once proto is more detailed. | |
39 ProcessState* process_state = expected_report_.add_process_states(); | |
40 CodeModule* module = process_state->add_modules(); | |
41 module->set_base_address(1024); | |
42 module->set_code_file("some_code_file.dll"); | |
43 | |
44 // Write the minidump. | |
45 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
46 minidump_path_ = temp_dir_.path().AppendASCII("minidump.dmp"); | |
47 } | |
48 | |
49 bool WriteDump() { | |
50 base::win::ScopedHandle file_handle(::CreateFile( | |
51 minidump_path_.value().c_str(), GENERIC_READ | GENERIC_WRITE, | |
52 FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, CREATE_NEW, | |
53 FILE_ATTRIBUTE_NORMAL, nullptr)); | |
54 if (!file_handle.IsValid()) | |
55 return false; | |
56 | |
57 MinidumpInfo mindump_info; | |
58 mindump_info.client_id = expected_client_id_; | |
59 mindump_info.report_id = expected_report_id_; | |
60 mindump_info.product_name = kProductName; | |
61 mindump_info.version_number = kVersion; | |
62 | |
63 return WritePostmortemDump(file_handle.Get(), expected_report_, | |
64 mindump_info); | |
65 } | |
66 | |
67 const base::FilePath& minidump_path() { return minidump_path_; } | |
68 const UUID& expected_client_id() { return expected_client_id_; } | |
69 const UUID& expected_report_id() { return expected_report_id_; } | |
70 const StabilityReport& expected_report() { return expected_report_; } | |
71 | |
72 private: | |
73 base::ScopedTempDir temp_dir_; | |
74 base::FilePath minidump_path_; | |
75 UUID expected_client_id_; | |
76 UUID expected_report_id_; | |
77 StabilityReport expected_report_; | |
78 }; | |
79 | |
80 TEST_F(WritePostmortemDumpTest, ValidateStabilityReportTest) { | |
81 ASSERT_TRUE(WriteDump()); | |
82 | |
83 // Read back the minidump to extract the proto. | |
84 // TODO(manzagop): rely on crashpad for reading the proto once crashpad | |
85 // supports it (https://crashpad.chromium.org/bug/10). | |
86 base::ScopedFILE minidump_file; | |
87 minidump_file.reset(base::OpenFile(minidump_path(), "rb")); | |
88 ASSERT_TRUE(minidump_file.get()); | |
89 | |
90 MINIDUMP_HEADER header = {}; | |
91 ASSERT_EQ(1, fread(&header, sizeof(header), 1, minidump_file.get())); | |
92 ASSERT_EQ(MINIDUMP_SIGNATURE, header.Signature); | |
93 ASSERT_EQ(2, header.NumberOfStreams); | |
94 RVA directory_rva = header.StreamDirectoryRva; | |
95 | |
96 MINIDUMP_DIRECTORY directory = {}; | |
97 ASSERT_EQ(0, fseek(minidump_file.get(), directory_rva, SEEK_SET)); | |
98 ASSERT_EQ(1, fread(&directory, sizeof(directory), 1, minidump_file.get())); | |
99 ASSERT_EQ(0x4B6B0002, directory.StreamType); | |
100 RVA report_rva = directory.Location.Rva; | |
101 ULONG32 report_size_bytes = directory.Location.DataSize; | |
102 | |
103 std::string recovered_serialized_report; | |
104 recovered_serialized_report.resize(report_size_bytes); | |
105 ASSERT_EQ(0, fseek(minidump_file.get(), report_rva, SEEK_SET)); | |
106 ASSERT_EQ(report_size_bytes, fread(&recovered_serialized_report.at(0), 1, | |
107 report_size_bytes, minidump_file.get())); | |
108 | |
109 // Validate the recovered report. | |
110 std::string expected_serialized_report; | |
111 expected_report().SerializeToString(&expected_serialized_report); | |
112 ASSERT_EQ(expected_serialized_report, recovered_serialized_report); | |
113 | |
114 StabilityReport recovered_report; | |
115 ASSERT_TRUE(recovered_report.ParseFromString(recovered_serialized_report)); | |
116 } | |
117 | |
118 TEST_F(WritePostmortemDumpTest, CrashPadCanReadTest) { | |
scottmg
2016/09/13 21:32:37
one more Pad here :)
manzagop (departed)
2016/09/14 13:44:36
Can't sneak one past you! ;) Done.
| |
119 ASSERT_TRUE(WriteDump()); | |
120 | |
121 // Validate crashpad can read the produced minidump. | |
122 crashpad::FileReader minidump_file_reader; | |
123 ASSERT_TRUE(minidump_file_reader.Open(minidump_path())); | |
124 | |
125 crashpad::ProcessSnapshotMinidump minidump_process_snapshot; | |
126 ASSERT_TRUE(minidump_process_snapshot.Initialize(&minidump_file_reader)); | |
127 | |
128 // Validate the crashpadinfo. | |
129 UUID client_id; | |
130 minidump_process_snapshot.ClientID(&client_id); | |
131 ASSERT_EQ(expected_client_id(), client_id); | |
132 | |
133 UUID report_id; | |
134 minidump_process_snapshot.ReportID(&report_id); | |
135 ASSERT_EQ(expected_report_id(), report_id); | |
136 | |
137 std::map<std::string, std::string> parameters = | |
138 minidump_process_snapshot.AnnotationsSimpleMap(); | |
139 auto it = parameters.find("product"); | |
140 ASSERT_NE(parameters.end(), it); | |
141 ASSERT_EQ(kExpectedProductName, it->second); | |
142 | |
143 it = parameters.find("version"); | |
144 ASSERT_NE(parameters.end(), it); | |
145 ASSERT_EQ(kVersion, it->second); | |
146 } | |
147 | |
148 } // namespace browser_watcher | |
OLD | NEW |