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