| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdio.h> | 5 #include <stdio.h> |
| 6 #include <string> |
| 6 | 7 |
| 7 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 8 #include "base/environment.h" | 9 #include "base/environment.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/string16.h" | |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "breakpad/src/client/windows/crash_generation/client_info.h" | 14 #include "breakpad/src/client/windows/crash_generation/client_info.h" |
| 15 #include "breakpad/src/client/windows/crash_generation/crash_generation_server.h
" | 15 #include "breakpad/src/client/windows/crash_generation/crash_generation_server.h
" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace remoting { | |
| 20 | |
| 21 namespace { | 19 namespace { |
| 22 | 20 |
| 23 // The name of the environment variable used to pass the crash server pipe name | 21 // The name of the environment variable used to pass the crash server pipe name |
| 24 // to the crashing child process. | 22 // to the crashing child process. |
| 25 const char kPipeVariableName[] = "REMOTING_BREAKPAD_WIN_DEATH_TEST_PIPE_NAME"; | 23 const char kPipeVariableName[] = "REMOTING_BREAKPAD_WIN_DEATH_TEST_PIPE_NAME"; |
| 26 | 24 |
| 27 // The prefix string used to generate a unique crash server pipe name. | 25 // 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 | 26 // The name has to be unique as multiple test instances can be running |
| 29 // simultaneously. | 27 // simultaneously. |
| 30 const wchar_t kPipeNamePrefix[] = L"\\\\.\\pipe\\"; | 28 const wchar_t kPipeNamePrefix[] = L"\\\\.\\pipe\\"; |
| 31 | 29 |
| 32 class MockCrashServerCallbacks { | 30 class MockCrashServerCallbacks { |
| 33 public: | 31 public: |
| 34 MockCrashServerCallbacks() {} | 32 MockCrashServerCallbacks(); |
| 35 virtual ~MockCrashServerCallbacks() {} | 33 virtual ~MockCrashServerCallbacks(); |
| 36 | 34 |
| 37 // |google_breakpad::CrashGenerationServer| invokes callbacks from artitrary | 35 // |google_breakpad::CrashGenerationServer| invokes callbacks from artitrary |
| 38 // thread pool threads. |OnClientDumpRequested| is the only one that happened | 36 // thread pool threads. |OnClientDumpRequested| is the only one that happened |
| 39 // to be called in synchronous manner. While it is still called on | 37 // 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 | 38 // a thread pool thread, the crashing process will wait until the server |
| 41 // signals an event after |OnClientDumpRequested| completes (or until 15 | 39 // signals an event after |OnClientDumpRequested| completes (or until 15 |
| 42 // seconds timeout expires). | 40 // seconds timeout expires). |
| 43 MOCK_METHOD0(OnClientDumpRequested, void()); | 41 MOCK_METHOD0(OnClientDumpRequested, void()); |
| 44 | 42 |
| 45 static void OnClientDumpRequestCallback( | 43 static void OnClientDumpRequestCallback( |
| 46 void* context, | 44 void* context, |
| 47 const google_breakpad::ClientInfo* client_info, | 45 const google_breakpad::ClientInfo* client_info, |
| 48 const string16* file_path) { | 46 const std::wstring* file_path); |
| 49 reinterpret_cast<MockCrashServerCallbacks*>(context)-> | |
| 50 OnClientDumpRequested(); | |
| 51 } | |
| 52 }; | 47 }; |
| 53 | 48 |
| 49 MockCrashServerCallbacks::MockCrashServerCallbacks() { |
| 50 } |
| 51 |
| 52 MockCrashServerCallbacks::~MockCrashServerCallbacks() { |
| 53 } |
| 54 |
| 55 // static |
| 56 void MockCrashServerCallbacks::OnClientDumpRequestCallback( |
| 57 void* context, |
| 58 const google_breakpad::ClientInfo* /* client_info */, |
| 59 const std::wstring* /* file_path */) { |
| 60 reinterpret_cast<MockCrashServerCallbacks*>(context)->OnClientDumpRequested(); |
| 61 } |
| 62 |
| 54 } // namespace | 63 } // namespace |
| 55 | 64 |
| 56 void InitializeCrashReportingForTest(const wchar_t*); | 65 namespace remoting { |
| 66 |
| 67 void InitializeCrashReportingForTest(const wchar_t* pipe_name); |
| 57 | 68 |
| 58 class BreakpadWinDeathTest : public testing::Test { | 69 class BreakpadWinDeathTest : public testing::Test { |
| 59 public: | 70 public: |
| 60 BreakpadWinDeathTest() {} | 71 BreakpadWinDeathTest(); |
| 61 virtual void SetUp() OVERRIDE { | 72 virtual ~BreakpadWinDeathTest(); |
| 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 | 73 |
| 76 pipe_name_ = kPipeNamePrefix + | 74 virtual void SetUp() OVERRIDE; |
| 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, NULL, | |
| 93 MockCrashServerCallbacks::OnClientDumpRequestCallback, | |
| 94 callbacks_.get(), | |
| 95 NULL, NULL, | |
| 96 NULL, NULL, | |
| 97 false, NULL)); | |
| 98 | |
| 99 result = crash_server_->Start(); | |
| 100 ASSERT_TRUE(result); | |
| 101 } | |
| 102 } | |
| 103 | 75 |
| 104 protected: | 76 protected: |
| 105 scoped_ptr<google_breakpad::CrashGenerationServer> crash_server_; | 77 scoped_ptr<google_breakpad::CrashGenerationServer> crash_server_; |
| 106 scoped_ptr<MockCrashServerCallbacks> callbacks_; | 78 scoped_ptr<MockCrashServerCallbacks> callbacks_; |
| 107 string16 pipe_name_; | 79 std::wstring pipe_name_; |
| 108 }; | 80 }; |
| 109 | 81 |
| 82 BreakpadWinDeathTest::BreakpadWinDeathTest() { |
| 83 } |
| 84 |
| 85 BreakpadWinDeathTest::~BreakpadWinDeathTest() { |
| 86 } |
| 87 |
| 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 |
| 93 // dump server. |
| 94 pipe_name_ = UTF8ToWide(pipe_name); |
| 95 InitializeCrashReportingForTest(pipe_name_.c_str()); |
| 96 } else { |
| 97 // This is the parent process. Generate a unique pipe name and setup |
| 98 // a dummy crash dump server. |
| 99 UUID guid = {0}; |
| 100 RPC_STATUS status = UuidCreate(&guid); |
| 101 EXPECT_TRUE(status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY); |
| 102 |
| 103 pipe_name_ = |
| 104 base::StringPrintf( |
| 105 L"%ls%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
| 106 kPipeNamePrefix, |
| 107 guid.Data1, |
| 108 guid.Data2, |
| 109 guid.Data3, |
| 110 guid.Data4[0], |
| 111 guid.Data4[1], |
| 112 guid.Data4[2], |
| 113 guid.Data4[3], |
| 114 guid.Data4[4], |
| 115 guid.Data4[5], |
| 116 guid.Data4[6], |
| 117 guid.Data4[7]); |
| 118 EXPECT_TRUE(environment->SetVar(kPipeVariableName, |
| 119 WideToUTF8(pipe_name_))); |
| 120 |
| 121 // Setup a dummy crash dump server. |
| 122 callbacks_.reset(new MockCrashServerCallbacks()); |
| 123 crash_server_.reset( |
| 124 new google_breakpad::CrashGenerationServer( |
| 125 pipe_name_, |
| 126 NULL, |
| 127 NULL, |
| 128 NULL, |
| 129 MockCrashServerCallbacks::OnClientDumpRequestCallback, |
| 130 callbacks_.get(), |
| 131 NULL, |
| 132 NULL, |
| 133 NULL, |
| 134 NULL, |
| 135 false, |
| 136 NULL)); |
| 137 ASSERT_TRUE(crash_server_->Start()); |
| 138 } |
| 139 } |
| 140 |
| 110 TEST_F(BreakpadWinDeathTest, TestAccessViolation) { | 141 TEST_F(BreakpadWinDeathTest, TestAccessViolation) { |
| 111 if (callbacks_.get()) { | 142 if (callbacks_.get()) { |
| 112 EXPECT_CALL(*callbacks_, OnClientDumpRequested()); | 143 EXPECT_CALL(*callbacks_, OnClientDumpRequested()); |
| 113 } | 144 } |
| 114 | 145 |
| 115 // Generate access violation exception. | 146 // Generate access violation exception. |
| 116 ASSERT_DEATH(*reinterpret_cast<int*>(0) = 1, ""); | 147 ASSERT_DEATH(*reinterpret_cast<int*>(NULL) = 1, ""); |
| 117 } | 148 } |
| 118 | 149 |
| 119 TEST_F(BreakpadWinDeathTest, TestInvalidParameter) { | 150 TEST_F(BreakpadWinDeathTest, TestInvalidParameter) { |
| 120 if (callbacks_.get()) { | 151 if (callbacks_.get()) { |
| 121 EXPECT_CALL(*callbacks_, OnClientDumpRequested()); | 152 EXPECT_CALL(*callbacks_, OnClientDumpRequested()); |
| 122 } | 153 } |
| 123 | 154 |
| 124 // Cause the invalid parameter callback to be called. | 155 // Cause the invalid parameter callback to be called. |
| 125 ASSERT_EXIT(printf(NULL), ::testing::ExitedWithCode(0), ""); | 156 ASSERT_EXIT(printf(NULL), testing::ExitedWithCode(0), ""); |
| 126 } | 157 } |
| 127 | 158 |
| 128 TEST_F(BreakpadWinDeathTest, TestDebugbreak) { | 159 TEST_F(BreakpadWinDeathTest, TestDebugbreak) { |
| 129 if (callbacks_.get()) { | 160 if (callbacks_.get()) { |
| 130 EXPECT_CALL(*callbacks_, OnClientDumpRequested()); | 161 EXPECT_CALL(*callbacks_, OnClientDumpRequested()); |
| 131 } | 162 } |
| 132 | 163 |
| 133 // See if __debugbreak() is intercepted. | 164 // See if __debugbreak() is intercepted. |
| 134 ASSERT_DEATH(__debugbreak(), ""); | 165 ASSERT_DEATH(__debugbreak(), ""); |
| 135 } | 166 } |
| 136 | 167 |
| 137 } // namespace remoting | 168 } // namespace remoting |
| OLD | NEW |