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

Side by Side Diff: components/crash/content/app/fallback_crash_handler_win_unittest.cc

Issue 2596463002: A simple, practically zero cost fallback crash handler for Crashpad handler process. (Closed)
Patch Set: Tweak comments for moar better confusion-less reading. Created 3 years, 11 months 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
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
scottmg 2017/01/06 18:29:04 2017
Sigurður Ásgeirsson 2017/01/06 20:59:11 Done.
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/crash/content/app/fallback_crash_handler_win.h"
6
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/threading/platform_thread.h"
12 #include "base/win/scoped_handle.h"
13 #include "base/win/win_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/crashpad/crashpad/client/crash_report_database.h"
16 #include "third_party/crashpad/crashpad/client/settings.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 crash_reporter {
22
23 namespace {
24
25 class FallbackCrashHandlerWinTest : public testing::Test {
26 public:
27 FallbackCrashHandlerWinTest() {
28 RtlCaptureContext(&ctx_);
29 memset(&exc_, 0, sizeof(exc_));
30 exc_.ExceptionCode = EXCEPTION_ACCESS_VIOLATION;
31
32 exc_ptrs_.ExceptionRecord = &exc_;
33 exc_ptrs_.ContextRecord = &ctx_;
34 }
35
36 void SetUp() override {
37 ASSERT_TRUE(database_dir_.CreateUniqueTempDir());
38
39 // Open a handle to our own process.
40 DWORD kAccessMask =
41 PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_DUP_HANDLE;
42 self_.Set(OpenProcess(kAccessMask, FALSE, GetCurrentProcessId()));
43 ASSERT_EQ(ERROR_SUCCESS, GetLastError());
44 EXPECT_TRUE(self_.IsValid());
45 }
46
47 std::string ExcPtrsAsString() const {
48 return base::UintToString(reinterpret_cast<uintptr_t>(&exc_ptrs_));
49 };
50
51 std::string ProcessAsString() const {
52 return base::UintToString(base::win::HandleToUint32(self_.Get()));
53 };
54
55 void CreateDatabase() {
56 std::unique_ptr<crashpad::CrashReportDatabase> database =
57 crashpad::CrashReportDatabase::InitializeWithoutCreating(
58 database_dir_.GetPath());
59 }
60
61 protected:
62 CONTEXT ctx_;
63 EXCEPTION_RECORD exc_;
64 EXCEPTION_POINTERS exc_ptrs_;
65
66 base::win::ScopedHandle self_;
67 base::ScopedTempDir database_dir_;
68 };
69
70 } // namespace
71
72 TEST_F(FallbackCrashHandlerWinTest, ParseCommandLine) {
73 FallbackCrashHandler handler;
74
75 // An empty command line shouldn't work.
76 base::CommandLine cmd_line(base::FilePath(L"empty"));
77 EXPECT_FALSE(handler.ParseCommandLine(&cmd_line));
78
79 cmd_line.AppendSwitchPath("database", database_dir_.GetPath());
80 cmd_line.AppendSwitchASCII("exception-pointers", ExcPtrsAsString());
81 cmd_line.AppendSwitchASCII("process", ProcessAsString());
82
83 // Thread missing, still should fail.
84 EXPECT_FALSE(handler.ParseCommandLine(&cmd_line));
85
86 cmd_line.AppendSwitchASCII(
87 "thread", base::UintToString(base::PlatformThread::CurrentId()));
88
89 // Should succeed with a fully populated command line.
90 // Because how handle ownership is guarded, we have to "disown" it before
91 // the handler takes it over.
92 self_.Take();
93 EXPECT_TRUE(handler.ParseCommandLine(&cmd_line));
94 }
95
96 TEST_F(FallbackCrashHandlerWinTest, GenerateCrashDump) {
97 FallbackCrashHandler handler;
98
99 base::CommandLine cmd_line(base::FilePath(L"empty"));
100 cmd_line.AppendSwitchPath("database", database_dir_.GetPath());
101 cmd_line.AppendSwitchASCII("exception-pointers", ExcPtrsAsString());
102
103 // TODO(siggi): It's probably safer to spawn a sacrificial process and then
104 // terminate it. MinidumpWriteDump is alleged to occasionally hang if used
105 // to dump own process.
106 cmd_line.AppendSwitchASCII("process", ProcessAsString());
107 cmd_line.AppendSwitchASCII(
108 "thread", base::UintToString(base::PlatformThread::CurrentId()));
109
110 self_.Take();
111 ASSERT_TRUE(handler.ParseCommandLine(&cmd_line));
112 EXPECT_TRUE(handler.GenerateCrashDump());
113
114 // Validate that the database contains one valid crash dump.
115 std::unique_ptr<crashpad::CrashReportDatabase> database =
116 crashpad::CrashReportDatabase::InitializeWithoutCreating(
117 database_dir_.GetPath());
118
119 std::vector<crashpad::CrashReportDatabase::Report> reports;
120 ASSERT_EQ(crashpad::CrashReportDatabase::kNoError,
121 database->GetPendingReports(&reports));
122
123 EXPECT_EQ(1U, reports.size());
124
125 // Validate crashpad can read the produced minidump.
126 crashpad::FileReader minidump_file_reader;
127 ASSERT_TRUE(minidump_file_reader.Open(reports[0].file_path));
128
129 crashpad::ProcessSnapshotMinidump minidump_process_snapshot;
130 ASSERT_TRUE(minidump_process_snapshot.Initialize(&minidump_file_reader));
131
132 crashpad::UUID expected_client_id;
133 ASSERT_TRUE(database->GetSettings()->GetClientID(&expected_client_id));
134
135 // Validate that the CrashpadInfo in the report contains the same basic
136 // info, as does the database.
137 crashpad::UUID client_id;
138 minidump_process_snapshot.ClientID(&client_id);
139 ASSERT_EQ(expected_client_id, client_id);
140
141 crashpad::UUID report_id;
142 minidump_process_snapshot.ReportID(&report_id);
143 ASSERT_EQ(reports[0].uuid, report_id);
144
145 std::map<std::string, std::string> parameters =
146 minidump_process_snapshot.AnnotationsSimpleMap();
147 auto it = parameters.find("prod");
148 ASSERT_NE(parameters.end(), it);
149 // ASSERT_EQ(kExpectedProductName, it->second);
150
151 it = parameters.find("ver");
152 ASSERT_NE(parameters.end(), it);
153 // ASSERT_EQ(kVersion, it->second);
154
155 it = parameters.find("channel");
156 ASSERT_NE(parameters.end(), it);
157 // ASSERT_EQ(kChannel, it->second);
158
159 it = parameters.find("plat");
160 ASSERT_NE(parameters.end(), it);
161 // ASSERT_EQ(kPlatform, it->second);
162 }
163
164 } // namespace crash_reporter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698