Chromium Code Reviews| Index: handler/win/registration_server_test.cc |
| diff --git a/handler/win/registration_server_test.cc b/handler/win/registration_server_test.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..021122509d83ff2ee85230d9de3367ca922ec816 |
| --- /dev/null |
| +++ b/handler/win/registration_server_test.cc |
| @@ -0,0 +1,414 @@ |
| +// Copyright 2015 The Crashpad Authors. All rights reserved. |
| +// |
| +// Licensed under the Apache License, Version 2.0 (the "License"); |
| +// you may not use this file except in compliance with the License. |
| +// You may obtain a copy of the License at |
| +// |
| +// http://www.apache.org/licenses/LICENSE-2.0 |
| +// |
| +// Unless required by applicable law or agreed to in writing, software |
| +// distributed under the License is distributed on an "AS IS" BASIS, |
| +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| +// See the License for the specific language governing permissions and |
| +// limitations under the License. |
| + |
| +#include "handler/win/registration_server.h" |
| + |
| +#include <vector> |
| + |
| +#include <windows.h> |
|
scottmg
2015/05/20 18:53:10
C headers before C++ headers.
erikwright (departed)
2015/05/20 20:54:15
Done.
|
| +#include "base/basictypes.h" |
|
scottmg
2015/05/20 18:53:10
Blank line before.
erikwright (departed)
2015/05/20 20:54:15
Acknowledged.
|
| +#include "base/memory/scoped_ptr.h" |
|
scottmg
2015/05/20 18:53:10
unused?
erikwright (departed)
2015/05/20 20:54:15
Done.
|
| +#include "base/strings/string16.h" |
| +#include "base/strings/stringprintf.h" |
|
scottmg
2015/05/20 18:53:10
unused?
erikwright (departed)
2015/05/20 20:54:14
line 159
|
| +#include "base/strings/utf_string_conversions.h" |
| +#include "client/crashpad_info.h" |
| +#include "client/registration_protocol_win.h" |
| +#include "gtest/gtest.h" |
| +#include "util/stdlib/pointer_container.h" |
|
scottmg
2015/05/20 18:53:10
unused?
erikwright (departed)
2015/05/20 20:54:15
used for PointerVector
scottmg
2015/05/21 02:32:36
I don't see a PointerVector in this file?
erikwright (departed)
2015/05/21 15:12:37
MockDelegate::registered_processes_
|
| +#include "util/thread/thread.h" |
|
scottmg
2015/05/20 18:53:10
Looks like it could have stayed in test/ after all
erikwright (departed)
2015/05/20 20:54:15
Yeah, my initial implementation used this in Regis
|
| +#include "util/win/address_types.h" |
| +#include "util/win/scoped_handle.h" |
| + |
| +namespace crashpad { |
| +namespace test { |
| +namespace { |
| + |
| +// Simulates a registrar to collect requests from and feed responses to the |
| +// RegistrationServer. |
| +class MockDelegate : public RegistrationServer::Delegate { |
| + public: |
| + // Records a single simulated client registration. |
| + struct Entry { |
| + Entry(ScopedKernelHANDLE client_process, |
| + WinVMAddress crashpad_info_address, |
| + HANDLE fake_request_dump_event, |
| + HANDLE fake_dump_complete_event) |
| + : client_process(client_process.Pass()), |
| + crashpad_info_address(crashpad_info_address), |
| + fake_request_dump_event_handle(fake_request_dump_event), |
| + fake_dump_complete_event_handle(fake_dump_complete_event) {} |
| + |
| + ScopedKernelHANDLE client_process; |
| + WinVMAddress crashpad_info_address; |
| + HANDLE fake_request_dump_event_handle; |
| + HANDLE fake_dump_complete_event_handle; |
| + }; |
| + |
| + MockDelegate() |
| + : started_event_(CreateEvent(nullptr, TRUE, FALSE, nullptr)), |
| + registered_processes_(), |
| + next_fake_handle_(1), |
| + fail_(false) { |
| + EXPECT_TRUE(started_event_.is_valid()); |
| + } |
| + |
| + ~MockDelegate() override {} |
| + |
| + // Blocks until RegistrationServer::Delegate::OnStarted is invoked. |
| + void WaitForStart() { |
| + DWORD wait_result = WaitForSingleObject(started_event_.get(), INFINITE); |
| + if (wait_result == WAIT_FAILED) |
| + PLOG(ERROR); |
| + ASSERT_EQ(wait_result, WAIT_OBJECT_0); |
| + } |
| + |
| + // RegistrationServer::Delegate: |
| + void OnStarted() override { SetEvent(started_event_.get()); } |
| + |
| + bool RegisterClient(ScopedKernelHANDLE client_process, |
| + WinVMAddress crashpad_info_address, |
| + HANDLE* request_dump_event, |
| + HANDLE* dump_complete_event) override { |
| + if (fail_) |
| + return false; |
| + |
| + if (!request_dump_event || !dump_complete_event) { |
| + ADD_FAILURE() << "NULL 'out' parameter."; |
| + return false; |
| + } |
| + *request_dump_event = reinterpret_cast<HANDLE>(next_fake_handle_++); |
| + *dump_complete_event = reinterpret_cast<HANDLE>(next_fake_handle_++); |
| + |
| + registered_processes_.push_back(new Entry(client_process.Pass(), |
| + crashpad_info_address, |
| + *request_dump_event, |
| + *dump_complete_event)); |
| + return true; |
| + } |
| + |
| + // Provides access to the registered process data. |
| + const std::vector<Entry*> registered_processes() { |
| + return registered_processes_; |
| + } |
| + |
| + // If true, causes RegisterClient to simulate registration failure. |
| + void set_fail_mode(bool fail) { fail_ = fail; } |
| + |
| + private: |
| + ScopedKernelHANDLE started_event_; |
| + PointerVector<Entry> registered_processes_; |
| + int next_fake_handle_; |
| + bool fail_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MockDelegate); |
| +}; |
| + |
| +// Verifies that the request and response match what was received and sent by |
| +// the MockDelegate. |
| +void VerifyRegistration(const MockDelegate::Entry& registered_process, |
| + const RegistrationRequest& request, |
| + const RegistrationResponse& response) { |
| + EXPECT_EQ(request.crashpad_info_address, |
| + registered_process.crashpad_info_address); |
| + EXPECT_EQ(registered_process.fake_request_dump_event_handle, |
| + response.request_report_event); |
| + EXPECT_EQ(registered_process.fake_dump_complete_event_handle, |
| + response.report_complete_event); |
| + EXPECT_EQ(request.client_process_id, |
| + GetProcessId(registered_process.client_process.get())); |
| +} |
| + |
| +// Runs the RegistrationServer on a background thread. |
| +class RunServerThread : public Thread { |
| + public: |
| + // Instantiates a thread which will invoke server->Run(pipe_name, delegate). |
| + RunServerThread(RegistrationServer* server, |
| + const base::string16& pipe_name, |
| + RegistrationServer::Delegate* delegate) |
| + : server_(server), pipe_name_(pipe_name), delegate_(delegate) {} |
| + ~RunServerThread() override {} |
| + |
| + private: |
| + // Thread : |
|
scottmg
2015/05/20 18:53:10
no space before :
erikwright (departed)
2015/05/20 20:54:14
Done.
|
| + void ThreadMain() override { server_->Run(pipe_name_, delegate_); } |
| + |
| + RegistrationServer* server_; |
| + base::string16 pipe_name_; |
| + RegistrationServer::Delegate* delegate_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(RunServerThread); |
| +}; |
| + |
| +class RegistrationServerTest : public testing::Test { |
| + public: |
| + RegistrationServerTest() |
| + : server_(), |
| + pipe_name_(L"\\\\.\\pipe\\registration_server_test_pipe_" + |
| + base::UTF8ToUTF16( |
| + base::StringPrintf("%08x", GetCurrentProcessId()))), |
| + delegate_(), |
| + server_thread_(&server_, pipe_name_, &delegate_) {} |
| + |
| + RegistrationServer& server() { return server_; } |
| + MockDelegate& delegate() { return delegate_; } |
| + Thread& server_thread() { return server_thread_; } |
| + |
| + // Returns a pipe handle connected to the RegistrationServer. |
| + ScopedFileHANDLE Connect() { |
| + ScopedFileHANDLE pipe; |
| + const int kMaxRetries = 5; |
| + for (int retries = 0; !pipe.is_valid() && retries < kMaxRetries; ++retries) { |
|
scottmg
2015/05/20 18:53:10
80 col
erikwright (departed)
2015/05/20 20:54:15
Done.
|
| + if (!::WaitNamedPipe(pipe_name_.c_str(), NMPWAIT_WAIT_FOREVER)) |
|
scottmg
2015/05/20 18:53:10
No leading :: unless there's a name conflict (and
erikwright (departed)
2015/05/20 20:54:15
Done.
|
| + break; |
| + pipe.reset(::CreateFile(pipe_name_.c_str(), |
| + GENERIC_READ | GENERIC_WRITE, |
| + 0, |
| + NULL, |
| + OPEN_EXISTING, |
| + SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION, |
| + NULL)); |
| + } |
| + EXPECT_TRUE(pipe.is_valid()); |
| + return pipe.Pass(); |
| + } |
| + |
| + // Sends the provided request and receives a response via the provided pipe. |
| + bool SendRequest(ScopedFileHANDLE pipe, |
| + const void* request_buffer, |
| + size_t request_size, |
| + RegistrationResponse* response) { |
| + DWORD mode = PIPE_READMODE_MESSAGE; |
| + ::SetNamedPipeHandleState(pipe.get(), &mode, NULL, NULL); |
| + DWORD bytes_read = 0; |
| + if (::TransactNamedPipe(pipe.get(), |
| + const_cast<void*>(request_buffer), |
| + static_cast<DWORD>(request_size), |
| + response, |
| + sizeof(*response), |
| + &bytes_read, |
| + NULL)) { |
| + if (bytes_read == sizeof(*response)) |
| + return true; |
| + } |
| + return false; |
| + } |
| + |
| + private: |
| + RegistrationServer server_; |
| + base::string16 pipe_name_; |
| + MockDelegate delegate_; |
| + RunServerThread server_thread_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(RegistrationServerTest); |
| +}; |
| + |
| +// During destruction, ensures that the server is stopped and the background |
| +// thread joined. |
| +class ScopedStopServerAndJoinThread { |
| + public: |
| + explicit ScopedStopServerAndJoinThread(RegistrationServer* server, |
| + Thread* thread) |
| + : server_(server), thread_(thread) {} |
| + ~ScopedStopServerAndJoinThread() { |
| + server_->Stop(); |
| + thread_->Join(); |
| + } |
| + |
| + private: |
| + RegistrationServer* server_; |
| + Thread* thread_; |
| + DISALLOW_COPY_AND_ASSIGN(ScopedStopServerAndJoinThread); |
| +}; |
| + |
| +TEST_F(RegistrationServerTest, Instantiate) { |
| +} |
| + |
| +TEST_F(RegistrationServerTest, StartAndStop) { |
| + server_thread().Start(); |
| + ScopedStopServerAndJoinThread scoped_stop_server_and_join_thread( |
| + &server(), &server_thread()); |
| + ASSERT_NO_FATAL_FAILURE(delegate().WaitForStart()); |
| +} |
| + |
| +TEST_F(RegistrationServerTest, StopWhileConnected) { |
| + ScopedFileHANDLE connection; |
| + { |
| + server_thread().Start(); |
| + ScopedStopServerAndJoinThread scoped_stop_server_and_join_thread( |
| + &server(), &server_thread()); |
| + ASSERT_NO_FATAL_FAILURE(delegate().WaitForStart()); |
| + connection = Connect(); |
| + ASSERT_TRUE(connection.is_valid()); |
| + // Leaving this scope causes the server to be stopped, while the connection |
| + // is still open. |
| + } |
| +} |
| + |
| +TEST_F(RegistrationServerTest, Register) { |
| + RegistrationRequest request = {0}; |
| + RegistrationResponse response = {0}; |
| + CrashpadInfo crashpad_info; |
| + request.client_process_id = GetCurrentProcessId(); |
| + request.crashpad_info_address = |
| + reinterpret_cast<WinVMAddress>(&crashpad_info); |
| + |
| + server_thread().Start(); |
| + ScopedStopServerAndJoinThread scoped_stop_server_and_join_thread( |
| + &server(), &server_thread()); |
| + |
| + ASSERT_NO_FATAL_FAILURE(delegate().WaitForStart()); |
| + |
| + ASSERT_TRUE(SendRequest(Connect(), &request, sizeof(request), &response)); |
| + |
| + ASSERT_EQ(1, delegate().registered_processes().size()); |
| + VerifyRegistration(*delegate().registered_processes()[0], request, response); |
| +} |
| + |
| +TEST_F(RegistrationServerTest, ForgedClientId) { |
| + // Skip this test on pre-Vista as the forged PID detection is not supported |
| + // there. |
| + OSVERSIONINFO vi = {0}; |
| + vi.dwOSVersionInfoSize = sizeof(vi); |
| + GetVersionEx(&vi); |
| + if (vi.dwMajorVersion < 6) |
| + return; |
| + |
| + RegistrationRequest request = {0}; |
| + RegistrationResponse response = {0}; |
| + CrashpadInfo crashpad_info; |
| + // Note that we forge the PID here. |
| + request.client_process_id = GetCurrentProcessId() + 1; |
| + request.crashpad_info_address = |
| + reinterpret_cast<WinVMAddress>(&crashpad_info); |
| + |
| + server_thread().Start(); |
| + ScopedStopServerAndJoinThread scoped_stop_server_and_join_thread( |
| + &server(), &server_thread()); |
| + |
| + ASSERT_NO_FATAL_FAILURE(delegate().WaitForStart()); |
| + |
| + ASSERT_FALSE(SendRequest(Connect(), &request, sizeof(request), &response)); |
| + ASSERT_EQ(0, delegate().registered_processes().size()); |
| + |
| + // Correct the PID and verify that this was the only reason we failed. |
| + request.client_process_id = GetCurrentProcessId(); |
| + ASSERT_TRUE(SendRequest(Connect(), &request, sizeof(request), &response)); |
| + ASSERT_EQ(1, delegate().registered_processes().size()); |
| + VerifyRegistration(*delegate().registered_processes()[0], request, response); |
| +} |
| + |
| +TEST_F(RegistrationServerTest, RegisterClientFails) { |
| + RegistrationRequest request = {0}; |
| + RegistrationResponse response = {0}; |
| + CrashpadInfo crashpad_info; |
| + request.client_process_id = GetCurrentProcessId(); |
| + request.crashpad_info_address = |
| + reinterpret_cast<WinVMAddress>(&crashpad_info); |
| + |
| + server_thread().Start(); |
| + ScopedStopServerAndJoinThread scoped_stop_server_and_join_thread( |
| + &server(), &server_thread()); |
| + |
| + ASSERT_NO_FATAL_FAILURE(delegate().WaitForStart()); |
| + |
| + // Simulate some failures |
| + delegate().set_fail_mode(true); |
| + for (int i = 0; i < 10; ++i) { |
| + ASSERT_FALSE(SendRequest(Connect(), &request, sizeof(request), &response)); |
| + ASSERT_EQ(0, delegate().registered_processes().size()); |
| + } |
| + |
| + // Now verify that a valid response may still be processed. |
| + delegate().set_fail_mode(false); |
| + ASSERT_TRUE(SendRequest(Connect(), &request, sizeof(request), &response)); |
| + |
| + ASSERT_EQ(1, delegate().registered_processes().size()); |
| + VerifyRegistration(*delegate().registered_processes()[0], request, response); |
| +} |
| + |
| +TEST_F(RegistrationServerTest, BadRequests) { |
| + server_thread().Start(); |
| + ScopedStopServerAndJoinThread scoped_stop_server_and_join_thread( |
| + &server(), &server_thread()); |
| + ASSERT_NO_FATAL_FAILURE(delegate().WaitForStart()); |
| + |
| + RegistrationRequest request = {0}; |
| + RegistrationResponse response = {0}; |
| + CrashpadInfo crashpad_info; |
| + request.client_process_id = GetCurrentProcessId(); |
| + request.crashpad_info_address = |
| + reinterpret_cast<WinVMAddress>(&crashpad_info); |
| + |
| + // Concatenate a valid request with a single byte of garbage. |
| + std::vector<char> extra_long; |
| + extra_long.insert(extra_long.begin(), |
| + reinterpret_cast<char*>(&request), |
| + reinterpret_cast<char*>(&request) + sizeof(request)); |
| + extra_long.push_back('x'); |
| + |
| + for (int i = 0; i < 10; ++i) { |
| + ASSERT_FALSE(SendRequest(Connect(), "a", 1, &response)); |
| + ASSERT_FALSE(SendRequest( |
| + Connect(), extra_long.data(), extra_long.size(), &response)); |
| + ASSERT_TRUE(Connect().is_valid()); |
| + } |
| + |
| + // Now verify that a valid response may still be processed. |
| + |
| + ASSERT_TRUE(SendRequest(Connect(), &request, sizeof(request), &response)); |
| + |
| + ASSERT_EQ(1, delegate().registered_processes().size()); |
| + VerifyRegistration(*delegate().registered_processes()[0], request, response); |
| +} |
| + |
| +TEST_F(RegistrationServerTest, OverlappingRequests) { |
| + server_thread().Start(); |
| + ScopedStopServerAndJoinThread scoped_stop_server_and_join_thread( |
| + &server(), &server_thread()); |
| + ASSERT_NO_FATAL_FAILURE(delegate().WaitForStart()); |
| + |
| + RegistrationRequest request = {0}; |
| + RegistrationResponse response_1 = {0}; |
| + RegistrationResponse response_2 = {0}; |
| + RegistrationResponse response_3 = {0}; |
| + CrashpadInfo crashpad_info; |
| + request.client_process_id = GetCurrentProcessId(); |
| + request.crashpad_info_address = |
| + reinterpret_cast<WinVMAddress>(&crashpad_info); |
| + |
| + ScopedFileHANDLE connection_1 = Connect(); |
| + ASSERT_TRUE(connection_1.is_valid()); |
| + ScopedFileHANDLE connection_2 = Connect(); |
| + ASSERT_TRUE(connection_2.is_valid()); |
| + ScopedFileHANDLE connection_3 = Connect(); |
| + ASSERT_TRUE(connection_3.is_valid()); |
| + |
| + ASSERT_FALSE(SendRequest(connection_1.Pass(), "a", 1, &response_1)); |
| + |
| + ASSERT_TRUE( |
| + SendRequest(connection_2.Pass(), &request, sizeof(request), &response_2)); |
| + |
| + ASSERT_TRUE(Connect().is_valid()); |
| + |
| + ASSERT_TRUE( |
| + SendRequest(connection_3.Pass(), &request, sizeof(request), &response_3)); |
| + |
| + ASSERT_EQ(2, delegate().registered_processes().size()); |
| + VerifyRegistration(*delegate().registered_processes()[0], request, response_2); |
|
scottmg
2015/05/20 18:53:10
80 col
erikwright (departed)
2015/05/20 20:54:15
Done.
|
| + VerifyRegistration(*delegate().registered_processes()[1], request, response_3); |
| +} |
| + |
| +} // namespace |
| +} // namespace test |
| +} // namespace crashpad |