Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / remoting / host / gnubby_auth_handler_posix_unittest.cc
blobe55ca9a0b74b8f95465b4a83bda0533534ac2a07
1 // Copyright 2014 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.
5 #include "base/files/file_path.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/json/json_writer.h"
8 #include "base/macros.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/timer/mock_timer.h"
13 #include "base/values.h"
14 #include "net/base/io_buffer.h"
15 #include "net/base/net_errors.h"
16 #include "net/base/test_completion_callback.h"
17 #include "net/socket/unix_domain_client_socket_posix.h"
18 #include "remoting/host/gnubby_auth_handler_posix.h"
19 #include "remoting/host/gnubby_socket.h"
20 #include "remoting/proto/internal.pb.h"
21 #include "remoting/protocol/client_stub.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace remoting {
26 namespace {
28 const char kSocketFilename[] = "socket_for_testing";
30 // Test gnubby request data.
31 const unsigned char kRequestData[] = {
32 0x00, 0x00, 0x00, 0x9a, 0x65, 0x1e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
33 0x00, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x60, 0x90,
34 0x24, 0x71, 0xf8, 0xf2, 0xe5, 0xdf, 0x7f, 0x81, 0xc7, 0x49, 0xc4, 0xa3,
35 0x58, 0x5c, 0xf6, 0xcc, 0x40, 0x14, 0x28, 0x0c, 0xa0, 0xfa, 0x03, 0x18,
36 0x38, 0xd8, 0x7d, 0x77, 0x2b, 0x3a, 0x00, 0x00, 0x00, 0x20, 0x64, 0x46,
37 0x47, 0x2f, 0xdf, 0x6e, 0xed, 0x7b, 0xf3, 0xc3, 0x37, 0x20, 0xf2, 0x36,
38 0x67, 0x6c, 0x36, 0xe1, 0xb4, 0x5e, 0xbe, 0x04, 0x85, 0xdb, 0x89, 0xa3,
39 0xcd, 0xfd, 0xd2, 0x4b, 0xd6, 0x9f, 0x00, 0x00, 0x00, 0x40, 0x38, 0x35,
40 0x05, 0x75, 0x1d, 0x13, 0x6e, 0xb3, 0x6b, 0x1d, 0x29, 0xae, 0xd3, 0x43,
41 0xe6, 0x84, 0x8f, 0xa3, 0x9d, 0x65, 0x4e, 0x2f, 0x57, 0xe3, 0xf6, 0xe6,
42 0x20, 0x3c, 0x00, 0xc6, 0xe1, 0x73, 0x34, 0xe2, 0x23, 0x99, 0xc4, 0xfa,
43 0x91, 0xc2, 0xd5, 0x97, 0xc1, 0x8b, 0xd0, 0x3c, 0x13, 0xba, 0xf0, 0xd7,
44 0x5e, 0xa3, 0xbc, 0x02, 0x5b, 0xec, 0xe4, 0x4b, 0xae, 0x0e, 0xf2, 0xbd,
45 0xc8, 0xaa};
47 } // namespace
49 class TestClientStub : public protocol::ClientStub {
50 public:
51 TestClientStub() : loop_(new base::RunLoop) {}
52 ~TestClientStub() override {}
54 // protocol::ClientStub implementation.
55 void SetCapabilities(const protocol::Capabilities& capabilities) override {}
57 void SetPairingResponse(
58 const protocol::PairingResponse& pairing_response) override {}
60 void DeliverHostMessage(const protocol::ExtensionMessage& message) override {
61 message_ = message;
62 loop_->Quit();
65 // protocol::ClipboardStub implementation.
66 void InjectClipboardEvent(const protocol::ClipboardEvent& event) override {}
68 // protocol::CursorShapeStub implementation.
69 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) override {}
71 void WaitForDeliverHostMessage() {
72 loop_->Run();
73 loop_.reset(new base::RunLoop);
76 void CheckHostDataMessage(int id, const std::string& data) {
77 std::string connection_id = base::StringPrintf("\"connectionId\":%d", id);
78 std::string data_message = base::StringPrintf("\"data\":%s", data.c_str());
80 ASSERT_TRUE(message_.type() == "gnubby-auth" ||
81 message_.type() == "auth-v1");
82 ASSERT_NE(message_.data().find("\"type\":\"data\""), std::string::npos);
83 ASSERT_NE(message_.data().find(connection_id), std::string::npos);
84 ASSERT_NE(message_.data().find(data_message), std::string::npos);
87 private:
88 protocol::ExtensionMessage message_;
89 scoped_ptr<base::RunLoop> loop_;
91 DISALLOW_COPY_AND_ASSIGN(TestClientStub);
94 class GnubbyAuthHandlerPosixTest : public testing::Test {
95 public:
96 GnubbyAuthHandlerPosixTest() {
97 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
98 socket_path_ = temp_dir_.path().Append(kSocketFilename);
99 auth_handler_posix_.reset(new GnubbyAuthHandlerPosix(&client_stub_));
100 auth_handler_ = auth_handler_posix_.get();
101 auth_handler_->SetGnubbySocketName(socket_path_);
104 void WriteRequestData(net::UnixDomainClientSocket* client_socket) {
105 int request_len = sizeof(kRequestData);
106 scoped_refptr<net::DrainableIOBuffer> request_buffer(
107 new net::DrainableIOBuffer(
108 new net::WrappedIOBuffer(
109 reinterpret_cast<const char*>(kRequestData)),
110 request_len));
111 net::TestCompletionCallback write_callback;
112 int bytes_written = 0;
113 while (bytes_written < request_len) {
114 int write_result = client_socket->Write(request_buffer.get(),
115 request_buffer->BytesRemaining(),
116 write_callback.callback());
117 write_result = write_callback.GetResult(write_result);
118 ASSERT_GT(write_result, 0);
119 bytes_written += write_result;
120 ASSERT_LE(bytes_written, request_len);
121 request_buffer->DidConsume(write_result);
123 ASSERT_EQ(request_len, bytes_written);
126 void WaitForAndVerifyHostMessage() {
127 client_stub_.WaitForDeliverHostMessage();
128 base::ListValue expected_data;
129 // Skip first four bytes.
130 for (size_t i = 4; i < sizeof(kRequestData); ++i) {
131 expected_data.AppendInteger(kRequestData[i]);
134 std::string expected_data_json;
135 base::JSONWriter::Write(expected_data, &expected_data_json);
136 client_stub_.CheckHostDataMessage(1, expected_data_json);
139 protected:
140 // Object under test.
141 scoped_ptr<GnubbyAuthHandlerPosix> auth_handler_posix_;
143 // GnubbyAuthHandler interface for |auth_handler_posix_|.
144 GnubbyAuthHandler* auth_handler_;
146 base::MessageLoopForIO message_loop_;
147 TestClientStub client_stub_;
148 base::ScopedTempDir temp_dir_;
149 base::FilePath socket_path_;
150 base::Closure accept_callback_;
153 TEST_F(GnubbyAuthHandlerPosixTest, HostDataMessageDelivered) {
154 auth_handler_->DeliverHostDataMessage(42, "test_msg");
155 client_stub_.WaitForDeliverHostMessage();
156 // Expects a JSON array of the ASCII character codes for "test_msg".
157 client_stub_.CheckHostDataMessage(42, "[116,101,115,116,95,109,115,103]");
160 TEST_F(GnubbyAuthHandlerPosixTest, NotClosedAfterRequest) {
161 ASSERT_EQ(0u, auth_handler_posix_->GetActiveSocketsMapSizeForTest());
163 const char message_json[] = "{\"type\":\"control\",\"option\":\"auth-v1\"}";
164 auth_handler_->DeliverClientMessage(message_json);
166 net::UnixDomainClientSocket client_socket(socket_path_.value(), false);
167 net::TestCompletionCallback connect_callback;
169 int rv = client_socket.Connect(connect_callback.callback());
170 ASSERT_EQ(net::OK, connect_callback.GetResult(rv));
172 // Write the request and verify the response.
173 WriteRequestData(&client_socket);
174 WaitForAndVerifyHostMessage();
176 // Verify that completing a request/response cycle didn't close the socket.
177 ASSERT_EQ(1u, auth_handler_posix_->GetActiveSocketsMapSizeForTest());
180 TEST_F(GnubbyAuthHandlerPosixTest, HandleTwoRequests) {
181 ASSERT_EQ(0u, auth_handler_posix_->GetActiveSocketsMapSizeForTest());
183 const char message_json[] = "{\"type\":\"control\",\"option\":\"auth-v1\"}";
184 auth_handler_->DeliverClientMessage(message_json);
186 net::UnixDomainClientSocket client_socket(socket_path_.value(), false);
187 net::TestCompletionCallback connect_callback;
189 int rv = client_socket.Connect(connect_callback.callback());
190 ASSERT_EQ(net::OK, connect_callback.GetResult(rv));
192 // Write the request and verify the response.
193 WriteRequestData(&client_socket);
194 WaitForAndVerifyHostMessage();
196 // Repeat the request/response cycle.
197 WriteRequestData(&client_socket);
198 WaitForAndVerifyHostMessage();
200 // Verify that completing two request/response cycles didn't close the socket.
201 ASSERT_EQ(1u, auth_handler_posix_->GetActiveSocketsMapSizeForTest());
204 TEST_F(GnubbyAuthHandlerPosixTest, DidReadTimeout) {
205 std::string message_json = "{\"type\":\"control\",\"option\":\"auth-v1\"}";
207 ASSERT_EQ(0u, auth_handler_posix_->GetActiveSocketsMapSizeForTest());
208 auth_handler_->DeliverClientMessage(message_json);
209 net::UnixDomainClientSocket client_socket(socket_path_.value(), false);
210 net::TestCompletionCallback connect_callback;
211 int rv = client_socket.Connect(connect_callback.callback());
212 ASSERT_EQ(net::OK, connect_callback.GetResult(rv));
213 auth_handler_posix_->SetRequestTimeoutForTest(base::TimeDelta());
214 ASSERT_EQ(0u, auth_handler_posix_->GetActiveSocketsMapSizeForTest());
217 TEST_F(GnubbyAuthHandlerPosixTest, ClientErrorMessageDelivered) {
218 std::string message_json = "{\"type\":\"control\",\"option\":\"auth-v1\"}";
220 ASSERT_EQ(0u, auth_handler_posix_->GetActiveSocketsMapSizeForTest());
221 auth_handler_->DeliverClientMessage(message_json);
222 net::UnixDomainClientSocket client_socket(socket_path_.value(), false);
223 net::TestCompletionCallback connect_callback;
224 int rv = client_socket.Connect(connect_callback.callback());
225 ASSERT_EQ(net::OK, connect_callback.GetResult(rv));
227 std::string error_json = "{\"type\":\"error\",\"connectionId\":1}";
228 auth_handler_->DeliverClientMessage(error_json);
229 ASSERT_EQ(0u, auth_handler_posix_->GetActiveSocketsMapSizeForTest());
232 } // namespace remoting