OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include <stdio.h> |
| 6 |
| 7 #include "base/compiler_specific.h" |
| 8 #include "base/environment.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/string16.h" |
| 12 #include "base/stringprintf.h" |
| 13 #include "base/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" |
| 18 |
| 19 namespace remoting { |
| 20 |
| 21 namespace { |
| 22 |
| 23 // The name of the environment variable used to pass the crash server pipe name |
| 24 // to the crashing child process. |
| 25 const char kPipeVariableName[] = "REMOTING_BREAKPAD_WIN_DEATH_TEST_PIPE_NAME"; |
| 26 |
| 27 // The prefix string used to generate a unique crash server pipe name. |
| 28 // The name has to be unique as multiple test instances can be running |
| 29 // simultaneously. |
| 30 const wchar_t kPipeNamePrefix[] = L"\\\\.\\pipe\\"; |
| 31 |
| 32 class MockCrashServerCallbacks { |
| 33 public: |
| 34 MockCrashServerCallbacks() {} |
| 35 virtual ~MockCrashServerCallbacks() {} |
| 36 |
| 37 // |google_breakpad::CrashGenerationServer| invokes callbacks from artitrary |
| 38 // thread pool threads. |OnClientDumpRequested| is the only one that happened |
| 39 // to be called in synchronous manner. While it is still called on |
| 40 // a thread pool thread, the crashing process will wait until the server |
| 41 // signals an event after |OnClientDumpRequested| completes (or until 15 |
| 42 // seconds timeout expires). |
| 43 MOCK_METHOD0(OnClientDumpRequested, void()); |
| 44 |
| 45 static void OnClientDumpRequestCallback( |
| 46 void* context, |
| 47 const google_breakpad::ClientInfo* client_info, |
| 48 const string16* file_path) { |
| 49 reinterpret_cast<MockCrashServerCallbacks*>(context)-> |
| 50 OnClientDumpRequested(); |
| 51 } |
| 52 }; |
| 53 |
| 54 } // namespace |
| 55 |
| 56 void InitializeCrashReportingForTest(const wchar_t*); |
| 57 |
| 58 class BreakpadWinDeathTest : public testing::Test { |
| 59 public: |
| 60 BreakpadWinDeathTest() {} |
| 61 virtual void SetUp() OVERRIDE { |
| 62 scoped_ptr<base::Environment> environment(base::Environment::Create()); |
| 63 std::string pipe_name; |
| 64 if (environment->GetVar(kPipeVariableName, &pipe_name)) { |
| 65 // This is a child process. Initialize crash dump reporting to the crash |
| 66 // dump server. |
| 67 pipe_name_ = UTF8ToUTF16(pipe_name); |
| 68 ::remoting::InitializeCrashReportingForTest(pipe_name_.c_str()); |
| 69 } else { |
| 70 // This is the parent process. Generate a unique pipe name and setup |
| 71 // a dummy crash dump server. |
| 72 UUID guid = {0}; |
| 73 RPC_STATUS status = ::UuidCreate(&guid); |
| 74 EXPECT_TRUE(status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY); |
| 75 |
| 76 pipe_name_ = kPipeNamePrefix + |
| 77 base::StringPrintf( |
| 78 L"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
| 79 guid.Data1, guid.Data2, guid.Data3, |
| 80 guid.Data4[0], guid.Data4[1], guid.Data4[2], |
| 81 guid.Data4[3], guid.Data4[4], guid.Data4[5], |
| 82 guid.Data4[6], guid.Data4[7]); |
| 83 bool result = environment->SetVar(kPipeVariableName, |
| 84 UTF16ToUTF8(pipe_name_)); |
| 85 EXPECT_TRUE(result); |
| 86 |
| 87 // Setup a dummy crash dump server. |
| 88 callbacks_.reset(new MockCrashServerCallbacks()); |
| 89 crash_server_.reset( |
| 90 new google_breakpad::CrashGenerationServer( |
| 91 pipe_name_, NULL, |
| 92 NULL, 0, |
| 93 MockCrashServerCallbacks::OnClientDumpRequestCallback, |
| 94 callbacks_.get(), |
| 95 NULL, 0, |
| 96 false, NULL)); |
| 97 |
| 98 result = crash_server_->Start(); |
| 99 ASSERT_TRUE(result); |
| 100 } |
| 101 } |
| 102 |
| 103 protected: |
| 104 scoped_ptr<google_breakpad::CrashGenerationServer> crash_server_; |
| 105 scoped_ptr<MockCrashServerCallbacks> callbacks_; |
| 106 string16 pipe_name_; |
| 107 }; |
| 108 |
| 109 TEST_F(BreakpadWinDeathTest, TestAccessViolation) { |
| 110 if (callbacks_.get()) { |
| 111 EXPECT_CALL(*callbacks_, OnClientDumpRequested()); |
| 112 } |
| 113 |
| 114 // Generate access violation exception. |
| 115 ASSERT_DEATH(*reinterpret_cast<int*>(0) = 1, ""); |
| 116 } |
| 117 |
| 118 TEST_F(BreakpadWinDeathTest, TestInvalidParameter) { |
| 119 if (callbacks_.get()) { |
| 120 EXPECT_CALL(*callbacks_, OnClientDumpRequested()); |
| 121 } |
| 122 |
| 123 // Cause the invalid parameter callback to be called. |
| 124 ASSERT_EXIT(printf(NULL), ::testing::ExitedWithCode(0), ""); |
| 125 } |
| 126 |
| 127 TEST_F(BreakpadWinDeathTest, TestDebugbreak) { |
| 128 if (callbacks_.get()) { |
| 129 EXPECT_CALL(*callbacks_, OnClientDumpRequested()); |
| 130 } |
| 131 |
| 132 // See if __debugbreak() is intercepted. |
| 133 ASSERT_DEATH(__debugbreak(), ""); |
| 134 } |
| 135 |
| 136 } // namespace remoting |
OLD | NEW |