OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/win/exception_handler_server.h" |
| 16 |
| 17 #include <windows.h> |
| 18 |
| 19 #include <string> |
| 20 #include <vector> |
| 21 |
| 22 #include "base/basictypes.h" |
| 23 #include "base/strings/stringprintf.h" |
| 24 #include "client/crashpad_client.h" |
| 25 #include "gtest/gtest.h" |
| 26 #include "test/win/win_child_process.h" |
| 27 #include "util/thread/thread.h" |
| 28 #include "util/win/address_types.h" |
| 29 #include "util/win/registration_protocol_win.h" |
| 30 #include "util/win/scoped_handle.h" |
| 31 |
| 32 namespace crashpad { |
| 33 namespace test { |
| 34 namespace { |
| 35 |
| 36 // Runs the ExceptionHandlerServer on a background thread. |
| 37 class RunServerThread : public Thread { |
| 38 public: |
| 39 // Instantiates a thread which will invoke server->Run(pipe_name). |
| 40 RunServerThread(ExceptionHandlerServer* server, const std::string& pipe_name) |
| 41 : server_(server), pipe_name_(pipe_name) {} |
| 42 ~RunServerThread() override {} |
| 43 |
| 44 private: |
| 45 // Thread: |
| 46 void ThreadMain() override { server_->Run(pipe_name_); } |
| 47 |
| 48 ExceptionHandlerServer* server_; |
| 49 std::string pipe_name_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(RunServerThread); |
| 52 }; |
| 53 |
| 54 class TestDelegate : public ExceptionHandlerServer::Delegate { |
| 55 public: |
| 56 explicit TestDelegate(HANDLE server_ready) : server_ready_(server_ready) {} |
| 57 ~TestDelegate() override {} |
| 58 |
| 59 void ExceptionHandlerServerStarted() override { |
| 60 SetEvent(server_ready_); |
| 61 } |
| 62 unsigned int ExceptionHandlerServerException( |
| 63 HANDLE process, |
| 64 WinVMAddress exception_information_address) override { |
| 65 return 0; |
| 66 } |
| 67 |
| 68 void WaitForStart() { WaitForSingleObject(server_ready_, INFINITE); } |
| 69 |
| 70 private: |
| 71 HANDLE server_ready_; // weak |
| 72 bool started_; |
| 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| 75 }; |
| 76 |
| 77 class ExceptionHandlerServerTest : public testing::Test { |
| 78 public: |
| 79 ExceptionHandlerServerTest() |
| 80 : pipe_name_("\\\\.\\pipe\\exception_handler_server_test_pipe_" + |
| 81 base::StringPrintf("%08x", GetCurrentProcessId())), |
| 82 server_ready_(CreateEvent(nullptr, false, false, nullptr)), |
| 83 delegate_(server_ready_.get()), |
| 84 server_(&delegate_), |
| 85 server_thread_(&server_, pipe_name_) {} |
| 86 |
| 87 TestDelegate& delegate() { return delegate_; } |
| 88 ExceptionHandlerServer& server() { return server_; } |
| 89 Thread& server_thread() { return server_thread_; } |
| 90 const std::string& pipe_name() const { return pipe_name_; } |
| 91 |
| 92 private: |
| 93 std::string pipe_name_; |
| 94 ScopedKernelHANDLE server_ready_; |
| 95 TestDelegate delegate_; |
| 96 ExceptionHandlerServer server_; |
| 97 RunServerThread server_thread_; |
| 98 |
| 99 DISALLOW_COPY_AND_ASSIGN(ExceptionHandlerServerTest); |
| 100 }; |
| 101 |
| 102 // During destruction, ensures that the server is stopped and the background |
| 103 // thread joined. |
| 104 class ScopedStopServerAndJoinThread { |
| 105 public: |
| 106 ScopedStopServerAndJoinThread(ExceptionHandlerServer* server, Thread* thread) |
| 107 : server_(server), thread_(thread) {} |
| 108 ~ScopedStopServerAndJoinThread() { |
| 109 server_->Stop(); |
| 110 thread_->Join(); |
| 111 } |
| 112 |
| 113 private: |
| 114 ExceptionHandlerServer* server_; |
| 115 Thread* thread_; |
| 116 DISALLOW_COPY_AND_ASSIGN(ScopedStopServerAndJoinThread); |
| 117 }; |
| 118 |
| 119 TEST_F(ExceptionHandlerServerTest, Instantiate) { |
| 120 } |
| 121 |
| 122 TEST_F(ExceptionHandlerServerTest, StartAndStop) { |
| 123 server_thread().Start(); |
| 124 ScopedStopServerAndJoinThread scoped_stop_server_and_join_thread( |
| 125 &server(), &server_thread()); |
| 126 ASSERT_NO_FATAL_FAILURE(delegate().WaitForStart()); |
| 127 } |
| 128 |
| 129 TEST_F(ExceptionHandlerServerTest, StopWhileConnected) { |
| 130 server_thread().Start(); |
| 131 ScopedStopServerAndJoinThread scoped_stop_server_and_join_thread( |
| 132 &server(), &server_thread()); |
| 133 ASSERT_NO_FATAL_FAILURE(delegate().WaitForStart()); |
| 134 CrashpadClient client; |
| 135 client.SetHandler(pipe_name()); // Connect to server. |
| 136 // Leaving this scope causes the server to be stopped, while the connection |
| 137 // is still open. |
| 138 } |
| 139 |
| 140 std::string ReadString(FileHandle handle) { |
| 141 size_t length = 0; |
| 142 EXPECT_TRUE(LoggingReadFile(handle, &length, sizeof(length))); |
| 143 scoped_ptr<char[]> buffer(new char[length]); |
| 144 EXPECT_TRUE(LoggingReadFile(handle, &buffer[0], length)); |
| 145 return std::string(&buffer[0], length); |
| 146 } |
| 147 |
| 148 void WriteString(FileHandle handle, const std::string& str) { |
| 149 size_t length = str.size(); |
| 150 EXPECT_TRUE(LoggingWriteFile(handle, &length, sizeof(length))); |
| 151 EXPECT_TRUE(LoggingWriteFile(handle, &str[0], length)); |
| 152 } |
| 153 |
| 154 class TestClient final : public WinChildProcess { |
| 155 public: |
| 156 TestClient() : WinChildProcess() {} |
| 157 |
| 158 ~TestClient() {} |
| 159 |
| 160 private: |
| 161 int Run() override { |
| 162 std::string pipe_name = ReadString(ReadPipeHandle()); |
| 163 CrashpadClient client; |
| 164 if (!client.SetHandler(pipe_name)) { |
| 165 ADD_FAILURE(); |
| 166 return EXIT_FAILURE; |
| 167 } |
| 168 if (!client.UseHandler()) { |
| 169 ADD_FAILURE(); |
| 170 return EXIT_FAILURE; |
| 171 } |
| 172 WriteString(WritePipeHandle(), "OK"); |
| 173 return EXIT_SUCCESS; |
| 174 } |
| 175 |
| 176 DISALLOW_COPY_AND_ASSIGN(TestClient); |
| 177 }; |
| 178 |
| 179 TEST_F(ExceptionHandlerServerTest, MultipleConnections) { |
| 180 WinChildProcess::EntryPoint<TestClient>(); |
| 181 |
| 182 scoped_ptr<WinChildProcess::Handles> handles_1 = WinChildProcess::Launch(); |
| 183 scoped_ptr<WinChildProcess::Handles> handles_2 = WinChildProcess::Launch(); |
| 184 scoped_ptr<WinChildProcess::Handles> handles_3 = WinChildProcess::Launch(); |
| 185 |
| 186 // Must ensure the delegate outlasts the server. |
| 187 { |
| 188 server_thread().Start(); |
| 189 ScopedStopServerAndJoinThread scoped_stop_server_and_join_thread( |
| 190 &server(), &server_thread()); |
| 191 ASSERT_NO_FATAL_FAILURE(delegate().WaitForStart()); |
| 192 |
| 193 // Tell all the children where to connect. |
| 194 WriteString(handles_1->write.get(), pipe_name()); |
| 195 WriteString(handles_2->write.get(), pipe_name()); |
| 196 WriteString(handles_3->write.get(), pipe_name()); |
| 197 |
| 198 ASSERT_EQ("OK", ReadString(handles_3->read.get())); |
| 199 ASSERT_EQ("OK", ReadString(handles_2->read.get())); |
| 200 ASSERT_EQ("OK", ReadString(handles_1->read.get())); |
| 201 } |
| 202 } |
| 203 |
| 204 } // namespace |
| 205 } // namespace test |
| 206 } // namespace crashpad |
OLD | NEW |