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"
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,
49 class TestClientStub
: public protocol::ClientStub
{
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
{
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() { loop_
.Run(); }
73 void CheckHostDataMessage(int id
, const std::string
& data
) {
74 std::string connection_id
= base::StringPrintf("\"connectionId\":%d", id
);
75 std::string data_message
= base::StringPrintf("\"data\":%s", data
.c_str());
77 ASSERT_TRUE(message_
.type() == "gnubby-auth" ||
78 message_
.type() == "auth-v1");
79 ASSERT_NE(message_
.data().find("\"type\":\"data\""), std::string::npos
);
80 ASSERT_NE(message_
.data().find(connection_id
), std::string::npos
);
81 ASSERT_NE(message_
.data().find(data_message
), std::string::npos
);
85 protocol::ExtensionMessage message_
;
88 DISALLOW_COPY_AND_ASSIGN(TestClientStub
);
91 class GnubbyAuthHandlerPosixTest
: public testing::Test
{
93 GnubbyAuthHandlerPosixTest() {
94 EXPECT_TRUE(temp_dir_
.CreateUniqueTempDir());
95 socket_path_
= temp_dir_
.path().Append(kSocketFilename
);
96 auth_handler_posix_
.reset(new GnubbyAuthHandlerPosix(&client_stub_
));
97 auth_handler_
= auth_handler_posix_
.get();
98 auth_handler_
->SetGnubbySocketName(socket_path_
);
102 // Object under test.
103 scoped_ptr
<GnubbyAuthHandlerPosix
> auth_handler_posix_
;
105 // GnubbyAuthHandler interface for |auth_handler_posix_|.
106 GnubbyAuthHandler
* auth_handler_
;
108 base::MessageLoopForIO message_loop_
;
109 TestClientStub client_stub_
;
110 base::ScopedTempDir temp_dir_
;
111 base::FilePath socket_path_
;
112 base::Closure accept_callback_
;
115 TEST_F(GnubbyAuthHandlerPosixTest
, HostDataMessageDelivered
) {
116 auth_handler_
->DeliverHostDataMessage(42, "test_msg");
117 client_stub_
.WaitForDeliverHostMessage();
118 // Expects a JSON array of the ASCII character codes for "test_msg".
119 client_stub_
.CheckHostDataMessage(42, "[116,101,115,116,95,109,115,103]");
122 TEST_F(GnubbyAuthHandlerPosixTest
, DidReadAndClose
) {
123 std::string message_json
= "{\"type\":\"control\",\"option\":\"auth-v1\"}";
125 ASSERT_EQ(0u, auth_handler_posix_
->GetActiveSocketsMapSizeForTest());
126 auth_handler_
->DeliverClientMessage(message_json
);
127 net::UnixDomainClientSocket
client_socket(socket_path_
.value(), false);
128 net::TestCompletionCallback connect_callback
;
129 int rv
= client_socket
.Connect(connect_callback
.callback());
130 ASSERT_EQ(net::OK
, connect_callback
.GetResult(rv
));
131 int request_len
= sizeof(kRequestData
);
132 scoped_refptr
<net::DrainableIOBuffer
> request_buffer(
133 new net::DrainableIOBuffer(
134 new net::WrappedIOBuffer(reinterpret_cast<const char*>(kRequestData
)),
136 net::TestCompletionCallback write_callback
;
137 int bytes_written
= 0;
138 while (bytes_written
< request_len
) {
139 int write_result
= client_socket
.Write(request_buffer
.get(),
140 request_buffer
->BytesRemaining(),
141 write_callback
.callback());
142 write_result
= write_callback
.GetResult(write_result
);
143 ASSERT_GT(write_result
, 0);
144 bytes_written
+= write_result
;
145 ASSERT_LE(bytes_written
, request_len
);
146 request_buffer
->DidConsume(write_result
);
148 ASSERT_EQ(request_len
, bytes_written
);
150 client_stub_
.WaitForDeliverHostMessage();
151 base::ListValue expected_data
;
152 // Skip first four bytes.
153 for (size_t i
= 4; i
< sizeof(kRequestData
); ++i
) {
154 expected_data
.AppendInteger(kRequestData
[i
]);
157 std::string expected_data_json
;
158 base::JSONWriter::Write(expected_data
, &expected_data_json
);
159 client_stub_
.CheckHostDataMessage(1, expected_data_json
);
160 ASSERT_EQ(0u, auth_handler_posix_
->GetActiveSocketsMapSizeForTest());
163 TEST_F(GnubbyAuthHandlerPosixTest
, DidReadTimeout
) {
164 std::string message_json
= "{\"type\":\"control\",\"option\":\"auth-v1\"}";
166 ASSERT_EQ(0u, auth_handler_posix_
->GetActiveSocketsMapSizeForTest());
167 auth_handler_
->DeliverClientMessage(message_json
);
168 net::UnixDomainClientSocket
client_socket(socket_path_
.value(), false);
169 net::TestCompletionCallback connect_callback
;
170 int rv
= client_socket
.Connect(connect_callback
.callback());
171 ASSERT_EQ(net::OK
, connect_callback
.GetResult(rv
));
172 auth_handler_posix_
->SetRequestTimeoutForTest(base::TimeDelta());
173 ASSERT_EQ(0u, auth_handler_posix_
->GetActiveSocketsMapSizeForTest());
176 TEST_F(GnubbyAuthHandlerPosixTest
, ClientErrorMessageDelivered
) {
177 std::string message_json
= "{\"type\":\"control\",\"option\":\"auth-v1\"}";
179 ASSERT_EQ(0u, auth_handler_posix_
->GetActiveSocketsMapSizeForTest());
180 auth_handler_
->DeliverClientMessage(message_json
);
181 net::UnixDomainClientSocket
client_socket(socket_path_
.value(), false);
182 net::TestCompletionCallback connect_callback
;
183 int rv
= client_socket
.Connect(connect_callback
.callback());
184 ASSERT_EQ(net::OK
, connect_callback
.GetResult(rv
));
186 std::string error_json
= "{\"type\":\"error\",\"connectionId\":1}";
187 auth_handler_
->DeliverClientMessage(error_json
);
188 ASSERT_EQ(0u, auth_handler_posix_
->GetActiveSocketsMapSizeForTest());
191 } // namespace remoting