Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: util/win/exception_handler_server_test.cc

Issue 1301853002: win: Crash handler server (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: fixes and move some things around Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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)
57 : server_ready_(server_ready),
58 started_(false),
59 got_exception_(false),
60 shutdown_(false) {}
61 ~TestDelegate() override {}
62
63 void OnStarted() override {
64 started_ = true;
65 SetEvent(server_ready_);
66 }
67 void OnException(HANDLE process,
68 WinVMAddress exception_information_address) override {
69 got_exception_ = true;
70 }
71 void OnShutdown() override { shutdown_ = true; }
72
73 void WaitForStart() { WaitForSingleObject(server_ready_, INFINITE); }
74
75 private:
76 HANDLE server_ready_; // weak
77 bool started_;
78 bool got_exception_;
79 bool shutdown_;
80
81 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
82 };
83
84 class ExceptionHandlerServerTest : public testing::Test {
85 public:
86 ExceptionHandlerServerTest()
87 : pipe_name_("\\\\.\\pipe\\exception_handler_server_test_pipe_" +
88 base::StringPrintf("%08x", GetCurrentProcessId())),
89 server_ready_(CreateEvent(nullptr, false, false, nullptr)),
90 delegate_(server_ready_.get()),
91 server_(&delegate_),
92 server_thread_(&server_, pipe_name_) {}
93
94 TestDelegate& delegate() { return delegate_; }
95 ExceptionHandlerServer& server() { return server_; }
96 Thread& server_thread() { return server_thread_; }
97 const std::string& pipe_name() const { return pipe_name_; }
98
99 private:
100 std::string pipe_name_;
101 ScopedKernelHANDLE server_ready_;
102 TestDelegate delegate_;
103 ExceptionHandlerServer server_;
104 RunServerThread server_thread_;
105
106 DISALLOW_COPY_AND_ASSIGN(ExceptionHandlerServerTest);
107 };
108
109 // During destruction, ensures that the server is stopped and the background
110 // thread joined.
111 class ScopedStopServerAndJoinThread {
112 public:
113 ScopedStopServerAndJoinThread(ExceptionHandlerServer* server, Thread* thread)
114 : server_(server), thread_(thread) {}
115 ~ScopedStopServerAndJoinThread() {
116 server_->Stop();
117 thread_->Join();
118 }
119
120 private:
121 ExceptionHandlerServer* server_;
122 Thread* thread_;
123 DISALLOW_COPY_AND_ASSIGN(ScopedStopServerAndJoinThread);
124 };
125
126 TEST_F(ExceptionHandlerServerTest, Instantiate) {
127 }
128
129 TEST_F(ExceptionHandlerServerTest, StartAndStop) {
130 server_thread().Start();
131 ScopedStopServerAndJoinThread scoped_stop_server_and_join_thread(
132 &server(), &server_thread());
133 ASSERT_NO_FATAL_FAILURE(delegate().WaitForStart());
134 }
135
136 TEST_F(ExceptionHandlerServerTest, StopWhileConnected) {
137 server_thread().Start();
138 ScopedStopServerAndJoinThread scoped_stop_server_and_join_thread(
139 &server(), &server_thread());
140 ASSERT_NO_FATAL_FAILURE(delegate().WaitForStart());
141 CrashpadClient client;
142 client.SetHandler(pipe_name()); // Connect to server.
143 // Leaving this scope causes the server to be stopped, while the connection
144 // is still open.
145 }
146
147 std::string ReadString(FileHandle handle) {
148 size_t length = 0;
149 EXPECT_TRUE(LoggingReadFile(handle, &length, sizeof(length)));
150 scoped_ptr<char[]> buffer(new char[length]);
151 EXPECT_TRUE(LoggingReadFile(handle, &buffer[0], length));
152 return std::string(&buffer[0], length);
153 }
154
155 void WriteString(FileHandle handle, const std::string& str) {
156 size_t length = str.size();
157 EXPECT_TRUE(LoggingWriteFile(handle, &length, sizeof(length)));
158 EXPECT_TRUE(LoggingWriteFile(handle, &str[0], length));
159 }
160
161 class TestClient final : public WinChildProcess {
162 public:
163 TestClient() : WinChildProcess() {}
164
165 ~TestClient() {}
166
167 private:
168 int Run() override {
169 std::string pipe_name = ReadString(ReadPipeHandle());
170 CrashpadClient client;
171 EXPECT_TRUE(client.SetHandler(pipe_name));
172 EXPECT_TRUE(client.UseHandler());
173 WriteString(WritePipeHandle(), "OK");
174 return EXIT_SUCCESS;
175 }
176
177 DISALLOW_COPY_AND_ASSIGN(TestClient);
178 };
179
180 TEST_F(ExceptionHandlerServerTest, MultipleConnections) {
181 WinChildProcess::EntryPoint<TestClient>();
182
183 scoped_ptr<WinChildProcess::Handles> handles_1 = WinChildProcess::Launch();
184 scoped_ptr<WinChildProcess::Handles> handles_2 = WinChildProcess::Launch();
185 scoped_ptr<WinChildProcess::Handles> handles_3 = WinChildProcess::Launch();
186
187 // Must ensure the delegate outlasts the server.
188 {
189 server_thread().Start();
190 ScopedStopServerAndJoinThread scoped_stop_server_and_join_thread(
191 &server(), &server_thread());
192 ASSERT_NO_FATAL_FAILURE(delegate().WaitForStart());
193
194 // Tell all the children where to connect.
195 WriteString(handles_1->write.get(), pipe_name());
196 WriteString(handles_2->write.get(), pipe_name());
197 WriteString(handles_3->write.get(), pipe_name());
198
199 ASSERT_EQ("OK", ReadString(handles_3->read.get()));
200 ASSERT_EQ("OK", ReadString(handles_2->read.get()));
201 ASSERT_EQ("OK", ReadString(handles_1->read.get()));
202 }
203 }
204
205 } // namespace
206 } // namespace test
207 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698