| OLD | NEW |
| (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/process/process_handle.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "base/threading/platform_thread.h" | |
| 13 #include "base/win/scoped_handle.h" | |
| 14 #include "base/win/win_util.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 #include "third_party/crashpad/crashpad/client/crash_report_database.h" | |
| 17 #include "third_party/crashpad/crashpad/client/settings.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 crash_reporter { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 class FallbackCrashHandlerWinTest : public testing::Test { | |
| 27 public: | |
| 28 FallbackCrashHandlerWinTest() : self_handle_(base::kNullProcessHandle) { | |
| 29 RtlCaptureContext(&context_); | |
| 30 memset(&exception_, 0, sizeof(exception_)); | |
| 31 exception_.ExceptionCode = EXCEPTION_ACCESS_VIOLATION; | |
| 32 | |
| 33 exception_ptrs_.ExceptionRecord = &exception_; | |
| 34 exception_ptrs_.ContextRecord = &context_; | |
| 35 } | |
| 36 | |
| 37 void SetUp() override { | |
| 38 ASSERT_TRUE(database_dir_.CreateUniqueTempDir()); | |
| 39 | |
| 40 // Open a handle to our own process. | |
| 41 const DWORD kAccessMask = | |
| 42 PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_DUP_HANDLE; | |
| 43 self_handle_ = OpenProcess(kAccessMask, FALSE, base::GetCurrentProcId()); | |
| 44 DWORD err = GetLastError(); | |
| 45 EXPECT_NE(base::kNullProcessHandle, self_handle_) << "GetLastError: " | |
| 46 << err; | |
| 47 } | |
| 48 | |
| 49 void TearDown() override { | |
| 50 if (self_handle_ != base::kNullProcessHandle) { | |
| 51 CloseHandle(self_handle_); | |
| 52 self_handle_ = base::kNullProcessHandle; | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 std::string ExcPtrsAsString() const { | |
| 57 return base::Uint64ToString(reinterpret_cast<uintptr_t>(&exception_ptrs_)); | |
| 58 }; | |
| 59 | |
| 60 std::string SelfHandleAsString() const { | |
| 61 return base::UintToString(base::win::HandleToUint32(self_handle_)); | |
| 62 }; | |
| 63 | |
| 64 void CreateDatabase() { | |
| 65 std::unique_ptr<crashpad::CrashReportDatabase> database = | |
| 66 crashpad::CrashReportDatabase::InitializeWithoutCreating( | |
| 67 database_dir_.GetPath()); | |
| 68 } | |
| 69 | |
| 70 protected: | |
| 71 CONTEXT context_; | |
| 72 EXCEPTION_RECORD exception_; | |
| 73 EXCEPTION_POINTERS exception_ptrs_; | |
| 74 | |
| 75 base::ProcessHandle self_handle_; | |
| 76 base::ScopedTempDir database_dir_; | |
| 77 | |
| 78 DISALLOW_COPY_AND_ASSIGN(FallbackCrashHandlerWinTest); | |
| 79 }; | |
| 80 | |
| 81 } // namespace | |
| 82 | |
| 83 TEST_F(FallbackCrashHandlerWinTest, ParseCommandLine) { | |
| 84 FallbackCrashHandler handler; | |
| 85 | |
| 86 // An empty command line shouldn't work. | |
| 87 base::CommandLine cmd_line(base::FilePath(L"empty")); | |
| 88 ASSERT_FALSE(handler.ParseCommandLine(cmd_line)); | |
| 89 | |
| 90 cmd_line.AppendSwitchPath("database", database_dir_.GetPath()); | |
| 91 cmd_line.AppendSwitchASCII("exception-pointers", ExcPtrsAsString()); | |
| 92 cmd_line.AppendSwitchASCII("process", SelfHandleAsString()); | |
| 93 | |
| 94 // Thread missing, still should fail. | |
| 95 ASSERT_FALSE(handler.ParseCommandLine(cmd_line)); | |
| 96 | |
| 97 cmd_line.AppendSwitchASCII( | |
| 98 "thread", base::UintToString(base::PlatformThread::CurrentId())); | |
| 99 | |
| 100 // Should succeed with a fully populated command line. | |
| 101 // Because of how handle ownership is guarded, we have to "disown" it before | |
| 102 // the handler takes it over. | |
| 103 EXPECT_TRUE(handler.ParseCommandLine(cmd_line)); | |
| 104 self_handle_ = base::kNullProcessHandle; | |
| 105 } | |
| 106 | |
| 107 TEST_F(FallbackCrashHandlerWinTest, GenerateCrashDump) { | |
| 108 FallbackCrashHandler handler; | |
| 109 | |
| 110 base::CommandLine cmd_line(base::FilePath(L"empty")); | |
| 111 cmd_line.AppendSwitchPath("database", database_dir_.GetPath()); | |
| 112 cmd_line.AppendSwitchASCII("exception-pointers", ExcPtrsAsString()); | |
| 113 | |
| 114 cmd_line.AppendSwitchASCII("process", SelfHandleAsString()); | |
| 115 cmd_line.AppendSwitchASCII( | |
| 116 "thread", base::UintToString(base::PlatformThread::CurrentId())); | |
| 117 | |
| 118 // Because how handle ownership is guarded, we have to "disown" this before | |
| 119 // the handler takes it over. | |
| 120 ASSERT_TRUE(handler.ParseCommandLine(cmd_line)); | |
| 121 self_handle_ = base::kNullProcessHandle; | |
| 122 | |
| 123 const char kProduct[] = "SomeProduct"; | |
| 124 const char kVersion[] = "1.2.3.6"; | |
| 125 const char kChannel[] = "canary"; | |
| 126 const char kProcessType[] = "Test"; | |
| 127 | |
| 128 // TODO(siggi): If this ends up being flakey, spin the testing out to a | |
| 129 // a separate process, which can in turn use the | |
| 130 // FallbackCrashHandlerLauncher class to spin a third process to grab | |
| 131 // a dump. | |
| 132 EXPECT_TRUE( | |
| 133 handler.GenerateCrashDump(kProduct, kVersion, kChannel, kProcessType)); | |
| 134 | |
| 135 // Validate that the database contains one valid crash dump. | |
| 136 std::unique_ptr<crashpad::CrashReportDatabase> database = | |
| 137 crashpad::CrashReportDatabase::InitializeWithoutCreating( | |
| 138 database_dir_.GetPath()); | |
| 139 | |
| 140 std::vector<crashpad::CrashReportDatabase::Report> reports; | |
| 141 ASSERT_EQ(crashpad::CrashReportDatabase::kNoError, | |
| 142 database->GetPendingReports(&reports)); | |
| 143 | |
| 144 EXPECT_EQ(1U, reports.size()); | |
| 145 | |
| 146 // Validate crashpad can read the produced minidump. | |
| 147 crashpad::FileReader minidump_file_reader; | |
| 148 ASSERT_TRUE(minidump_file_reader.Open(reports[0].file_path)); | |
| 149 | |
| 150 crashpad::ProcessSnapshotMinidump minidump_process_snapshot; | |
| 151 ASSERT_TRUE(minidump_process_snapshot.Initialize(&minidump_file_reader)); | |
| 152 | |
| 153 crashpad::UUID expected_client_id; | |
| 154 ASSERT_TRUE(database->GetSettings()->GetClientID(&expected_client_id)); | |
| 155 | |
| 156 // Validate that the CrashpadInfo in the report contains the same basic | |
| 157 // info, as does the database. | |
| 158 crashpad::UUID client_id; | |
| 159 minidump_process_snapshot.ClientID(&client_id); | |
| 160 EXPECT_EQ(expected_client_id, client_id); | |
| 161 | |
| 162 crashpad::UUID report_id; | |
| 163 minidump_process_snapshot.ReportID(&report_id); | |
| 164 EXPECT_EQ(reports[0].uuid, report_id); | |
| 165 | |
| 166 std::map<std::string, std::string> parameters = | |
| 167 minidump_process_snapshot.AnnotationsSimpleMap(); | |
| 168 auto it = parameters.find("prod"); | |
| 169 EXPECT_NE(parameters.end(), it); | |
| 170 EXPECT_EQ(kProduct, it->second); | |
| 171 | |
| 172 it = parameters.find("ver"); | |
| 173 EXPECT_NE(parameters.end(), it); | |
| 174 EXPECT_EQ(kVersion, it->second); | |
| 175 | |
| 176 it = parameters.find("channel"); | |
| 177 EXPECT_NE(parameters.end(), it); | |
| 178 EXPECT_EQ(kChannel, it->second); | |
| 179 | |
| 180 it = parameters.find("plat"); | |
| 181 EXPECT_NE(parameters.end(), it); | |
| 182 #if defined(ARCH_CPU_64_BITS) | |
| 183 EXPECT_EQ("Win64", it->second); | |
| 184 #else | |
| 185 EXPECT_EQ("Win32", it->second); | |
| 186 #endif | |
| 187 | |
| 188 it = parameters.find("ptype"); | |
| 189 EXPECT_NE(parameters.end(), it); | |
| 190 EXPECT_EQ(kProcessType, it->second); | |
| 191 } | |
| 192 | |
| 193 } // namespace crash_reporter | |
| OLD | NEW |