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

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

Issue 2611393002: Part two of fallback crash handler for Crashpad handler process. (Closed)
Patch Set: Make CommandLine param to ParseCommandLine const. 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 2017 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/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 =
scottmg 2017/01/10 18:26:28 const
Sigurður Ásgeirsson 2017/01/10 21:21:41 Done.
41 PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_DUP_HANDLE;
42 self_.Set(OpenProcess(kAccessMask, FALSE, GetCurrentProcessId()));
43 ASSERT_EQ(ERROR_SUCCESS, GetLastError());
scottmg 2017/01/10 18:26:28 Does OpenProcess() necessarily set GLE on success?
Sigurður Ásgeirsson 2017/01/10 21:21:41 Good point - so amended.
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_;
scottmg 2017/01/10 18:26:28 context_ and exception_ instead of ctx_ and exc_ p
Sigurður Ásgeirsson 2017/01/10 21:21:41 Done.
63 EXCEPTION_RECORD exc_;
64 EXCEPTION_POINTERS exc_ptrs_;
65
66 base::win::ScopedHandle self_;
scottmg 2017/01/10 18:26:28 Since you have to leak this in all usages, perhaps
Sigurður Ásgeirsson 2017/01/10 21:21:41 Done.
67 base::ScopedTempDir database_dir_;
scottmg 2017/01/10 18:26:28 DISALLOW_COPY_AND_ASSIGN
Sigurður Ásgeirsson 2017/01/10 21:21:41 Done.
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));
scottmg 2017/01/10 18:26:28 I guess this should be ASSERT as otherwise it's tr
Sigurður Ásgeirsson 2017/01/10 21:21:41 Done, though this process is pretty horked either
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
scottmg 2017/01/10 18:26:28 "Because of how..."
Sigurður Ásgeirsson 2017/01/10 21:21:41 Done.
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
scottmg 2017/01/10 18:26:28 MiniDump
Sigurður Ásgeirsson 2017/01/10 21:21:41 Done.
105 // to dump own process.
106 cmd_line.AppendSwitchASCII("process", ProcessAsString());
107 cmd_line.AppendSwitchASCII(
108 "thread", base::UintToString(base::PlatformThread::CurrentId()));
109
110 // Because how handle ownership is guarded, we have to "disown" this before
111 // the handler takes it over.
112 self_.Take();
113
114 ASSERT_TRUE(handler.ParseCommandLine(cmd_line));
115
116 const char kProduct[] = "SomeProduct";
117 const char kVersion[] = "1.2.3.6";
118 const char kChannel[] = "canary";
119 const char kProcessType[] = "Test";
120
121 EXPECT_TRUE(
122 handler.GenerateCrashDump(kProduct, kVersion, kChannel, kProcessType));
123
124 // Validate that the database contains one valid crash dump.
125 std::unique_ptr<crashpad::CrashReportDatabase> database =
126 crashpad::CrashReportDatabase::InitializeWithoutCreating(
127 database_dir_.GetPath());
128
129 std::vector<crashpad::CrashReportDatabase::Report> reports;
130 ASSERT_EQ(crashpad::CrashReportDatabase::kNoError,
131 database->GetPendingReports(&reports));
132
133 EXPECT_EQ(1U, reports.size());
134
135 // Validate crashpad can read the produced minidump.
136 crashpad::FileReader minidump_file_reader;
137 ASSERT_TRUE(minidump_file_reader.Open(reports[0].file_path));
138
139 crashpad::ProcessSnapshotMinidump minidump_process_snapshot;
140 ASSERT_TRUE(minidump_process_snapshot.Initialize(&minidump_file_reader));
141
142 crashpad::UUID expected_client_id;
143 ASSERT_TRUE(database->GetSettings()->GetClientID(&expected_client_id));
144
145 // Validate that the CrashpadInfo in the report contains the same basic
146 // info, as does the database.
147 crashpad::UUID client_id;
148 minidump_process_snapshot.ClientID(&client_id);
149 ASSERT_EQ(expected_client_id, client_id);
scottmg 2017/01/10 18:26:28 All the ASSERT_ from here down should be EXPECT_ I
Sigurður Ásgeirsson 2017/01/10 21:21:41 Done.
150
151 crashpad::UUID report_id;
152 minidump_process_snapshot.ReportID(&report_id);
153 ASSERT_EQ(reports[0].uuid, report_id);
154
155 std::map<std::string, std::string> parameters =
156 minidump_process_snapshot.AnnotationsSimpleMap();
157 auto it = parameters.find("prod");
158 ASSERT_NE(parameters.end(), it);
159 ASSERT_EQ(kProduct, it->second);
160
161 it = parameters.find("ver");
162 ASSERT_NE(parameters.end(), it);
163 ASSERT_EQ(kVersion, it->second);
164
165 it = parameters.find("channel");
166 ASSERT_NE(parameters.end(), it);
167 ASSERT_EQ(kChannel, it->second);
168
169 it = parameters.find("plat");
170 ASSERT_NE(parameters.end(), it);
171 #if defined(ARCH_CPU_64_BITS)
172 ASSERT_EQ("Win64", it->second);
173 #else
174 ASSERT_EQ("Win32", it->second);
175 #endif
176
177 it = parameters.find("ptype");
178 ASSERT_NE(parameters.end(), it);
179 ASSERT_EQ(kProcessType, it->second);
180 }
181
182 } // namespace crash_reporter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698