Chromium Code Reviews| Index: components/browser_watcher/postmortem_minidump_writer_win_unittest.cc |
| diff --git a/components/browser_watcher/postmortem_minidump_writer_win_unittest.cc b/components/browser_watcher/postmortem_minidump_writer_win_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8a5961eb927efc600f13e7c29ce9b81ce9e27381 |
| --- /dev/null |
| +++ b/components/browser_watcher/postmortem_minidump_writer_win_unittest.cc |
| @@ -0,0 +1,126 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/browser_watcher/postmortem_minidump_writer.h" |
| + |
| +#include <windows.h> // NOLINT |
| +#include <dbghelp.h> |
| + |
| +#include "base/files/file_path.h" |
| +#include "base/files/file_util.h" |
| +#include "base/files/scoped_file.h" |
| +#include "base/files/scoped_temp_dir.h" |
| +#include "components/browser_watcher/stability_report.pb.h" |
| +#include "components/version_info/version_info.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "third_party/crashpad/crashpad/snapshot/minidump/process_snapshot_minidump.h" |
| +#include "third_party/crashpad/crashpad/util/file/file_reader.h" |
| +#include "third_party/crashpad/crashpad/util/misc/uuid.h" |
| + |
| +namespace browser_watcher { |
| + |
| +using crashpad::UUID; |
| + |
| +class PostmortemMinidumpWriterWinTest : public testing::Test { |
| + public: |
| + void SetUp() override { |
| + testing::Test::SetUp(); |
| + |
| + expected_client_id_ = UUID(UUID::InitializeWithNewTag{}); |
| + expected_report_id_ = UUID(UUID::InitializeWithNewTag{}); |
| + |
| + // Create a stability report. |
| + // TODO(manzagop): flesh out the report once proto is more detailed. |
| + ProcessState* process_state = expected_report_.add_process_states(); |
| + CodeModule* module = process_state->add_modules(); |
| + module->set_base_address(1024); |
| + module->set_code_file("some_code_file.dll"); |
| + |
| + // Write the minidump. |
| + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| + minidump_path_ = temp_dir_.path().AppendASCII("minidump.dmp"); |
| + base::ScopedFILE file(base::OpenFile(minidump_path_, "wb")); |
| + ASSERT_TRUE(file.get()); |
| + |
| + 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.
|
| + 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.
|
| + expected_report_id_)); |
| + } |
| + |
| + const base::FilePath& minidump_path() { return minidump_path_; } |
| + const UUID& expected_client_id() { return expected_client_id_; } |
| + const UUID& expected_report_id() { return expected_report_id_; } |
| + const StabilityReport& expected_report() { return expected_report_; } |
| + |
| + private: |
| + base::ScopedTempDir temp_dir_; |
| + base::FilePath minidump_path_; |
| + UUID expected_client_id_; |
| + UUID expected_report_id_; |
| + StabilityReport expected_report_; |
| +}; |
| + |
| +TEST_F(PostmortemMinidumpWriterWinTest, ValidateStabilityReportTest) { |
| + // Read back the minidump. |
| + base::ScopedFILE minidump_file; |
| + minidump_file.reset(base::OpenFile(minidump_path(), "rb")); |
| + ASSERT_TRUE(minidump_file.get()); |
| + |
| + MINIDUMP_HEADER header = {}; |
| + ASSERT_EQ(1, fread(&header, sizeof(header), 1, minidump_file.get())); |
| + ASSERT_EQ(MINIDUMP_SIGNATURE, header.Signature); |
| + ASSERT_EQ(2, header.NumberOfStreams); |
| + RVA directory_rva = header.StreamDirectoryRva; |
| + |
| + MINIDUMP_DIRECTORY directory = {}; |
| + ASSERT_EQ(0, fseek(minidump_file.get(), directory_rva, SEEK_SET)); |
| + ASSERT_EQ(1, fread(&directory, sizeof(directory), 1, minidump_file.get())); |
| + ASSERT_EQ(0x4B6B0002, directory.StreamType); |
| + RVA report_rva = directory.Location.Rva; |
| + ULONG32 report_size_bytes = directory.Location.DataSize; |
| + |
| + 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.
|
| + recovered_serialized_report.resize(report_size_bytes); |
| + ASSERT_EQ(0, fseek(minidump_file.get(), report_rva, SEEK_SET)); |
| + ASSERT_EQ(report_size_bytes, fread(&recovered_serialized_report.at(0), 1, |
| + report_size_bytes, minidump_file.get())); |
| + |
| + // Validate the recovered report. |
| + std::string expected_serialized_report; |
| + expected_report().SerializeToString(&expected_serialized_report); |
| + ASSERT_EQ(expected_serialized_report, recovered_serialized_report); |
| + |
| + StabilityReport recovered_report; |
| + ASSERT_TRUE(recovered_report.ParseFromString(recovered_serialized_report)); |
| +} |
| + |
| +TEST_F(PostmortemMinidumpWriterWinTest, CrashPadCanReadTest) { |
| + // Validate crashpad can read the produced minidump. |
| + crashpad::FileReader minidump_file_reader; |
| + ASSERT_TRUE(minidump_file_reader.Open(minidump_path())); |
| + |
| + crashpad::ProcessSnapshotMinidump minidump_process_snapshot; |
| + ASSERT_TRUE(minidump_process_snapshot.Initialize(&minidump_file_reader)); |
| + |
| + // Validate the crashpadinfo. |
| + UUID client_id; |
| + minidump_process_snapshot.ClientID(&client_id); |
| + ASSERT_EQ(expected_client_id(), client_id); |
| + |
| + UUID report_id; |
| + minidump_process_snapshot.ReportID(&report_id); |
| + ASSERT_EQ(expected_report_id(), report_id); |
| + |
| + std::map<std::string, std::string> parameters = |
| + minidump_process_snapshot.AnnotationsSimpleMap(); |
| + auto it = parameters.find("product"); |
| + ASSERT_NE(parameters.end(), it); |
| + ASSERT_EQ(version_info::GetProductName() + "_Postmortem", it->second); |
| + |
| + it = parameters.find("version"); |
| + ASSERT_NE(parameters.end(), it); |
| + ASSERT_EQ(version_info::GetVersionNumber(), it->second); |
| +} |
| + |
| +} // namespace browser_watcher |