1 // Copyright (c) 2012 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.
8 #include "base/compiler_specific.h"
9 #include "base/environment.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "breakpad/src/client/windows/crash_generation/client_info.h"
15 #include "breakpad/src/client/windows/crash_generation/crash_generation_server.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
21 // The name of the environment variable used to pass the crash server pipe name
22 // to the crashing child process.
23 const char kPipeVariableName
[] = "REMOTING_BREAKPAD_WIN_DEATH_TEST_PIPE_NAME";
25 // The prefix string used to generate a unique crash server pipe name.
26 // The name has to be unique as multiple test instances can be running
28 const wchar_t kPipeNamePrefix
[] = L
"\\\\.\\pipe\\";
30 class MockCrashServerCallbacks
{
32 MockCrashServerCallbacks();
33 virtual ~MockCrashServerCallbacks();
35 // |google_breakpad::CrashGenerationServer| invokes callbacks from artitrary
36 // thread pool threads. |OnClientDumpRequested| is the only one that happened
37 // to be called in synchronous manner. While it is still called on
38 // a thread pool thread, the crashing process will wait until the server
39 // signals an event after |OnClientDumpRequested| completes (or until 15
40 // seconds timeout expires).
41 MOCK_METHOD0(OnClientDumpRequested
, void());
43 static void OnClientDumpRequestCallback(
45 const google_breakpad::ClientInfo
* client_info
,
46 const std::wstring
* file_path
);
49 MockCrashServerCallbacks::MockCrashServerCallbacks() {
52 MockCrashServerCallbacks::~MockCrashServerCallbacks() {
56 void MockCrashServerCallbacks::OnClientDumpRequestCallback(
58 const google_breakpad::ClientInfo
* /* client_info */,
59 const std::wstring
* /* file_path */) {
60 reinterpret_cast<MockCrashServerCallbacks
*>(context
)->OnClientDumpRequested();
67 void InitializeCrashReportingForTest(const wchar_t* pipe_name
);
69 class BreakpadWinDeathTest
: public testing::Test
{
71 BreakpadWinDeathTest();
72 virtual ~BreakpadWinDeathTest();
74 virtual void SetUp() override
;
77 scoped_ptr
<google_breakpad::CrashGenerationServer
> crash_server_
;
78 scoped_ptr
<MockCrashServerCallbacks
> callbacks_
;
79 std::wstring pipe_name_
;
82 BreakpadWinDeathTest::BreakpadWinDeathTest() {
85 BreakpadWinDeathTest::~BreakpadWinDeathTest() {
88 void BreakpadWinDeathTest::SetUp() {
89 scoped_ptr
<base::Environment
> environment(base::Environment::Create());
90 std::string pipe_name
;
91 if (environment
->GetVar(kPipeVariableName
, &pipe_name
)) {
92 // This is a child process. Initialize crash dump reporting to the crash
94 pipe_name_
= base::UTF8ToWide(pipe_name
);
95 InitializeCrashReportingForTest(pipe_name_
.c_str());
97 // This is the parent process. Generate a unique pipe name and setup
98 // a dummy crash dump server.
100 RPC_STATUS status
= UuidCreate(&guid
);
101 EXPECT_TRUE(status
== RPC_S_OK
|| status
== RPC_S_UUID_LOCAL_ONLY
);
105 L
"%ls%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
118 EXPECT_TRUE(environment
->SetVar(kPipeVariableName
,
119 base::WideToUTF8(pipe_name_
)));
121 // Setup a dummy crash dump server.
122 callbacks_
.reset(new MockCrashServerCallbacks());
124 new google_breakpad::CrashGenerationServer(
129 MockCrashServerCallbacks::OnClientDumpRequestCallback
,
137 ASSERT_TRUE(crash_server_
->Start());
141 TEST_F(BreakpadWinDeathTest
, TestAccessViolation
) {
142 if (callbacks_
.get()) {
143 EXPECT_CALL(*callbacks_
, OnClientDumpRequested());
146 // Generate access violation exception.
147 ASSERT_DEATH(*reinterpret_cast<int*>(NULL
) = 1, "");
150 TEST_F(BreakpadWinDeathTest
, TestInvalidParameter
) {
151 if (callbacks_
.get()) {
152 EXPECT_CALL(*callbacks_
, OnClientDumpRequested());
155 // Cause the invalid parameter callback to be called.
156 ASSERT_EXIT(printf(NULL
), testing::ExitedWithCode(0), "");
159 TEST_F(BreakpadWinDeathTest
, TestDebugbreak
) {
160 if (callbacks_
.get()) {
161 EXPECT_CALL(*callbacks_
, OnClientDumpRequested());
164 // See if __debugbreak() is intercepted.
165 ASSERT_DEATH(__debugbreak(), "");
168 } // namespace remoting