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.
4
5 #include "base/message_loop/message_loop.h"
6 #include "base/strings/stringprintf.h"
7 #include "base/timer/mock_timer.h"
8 #include "net/socket/stream_listen_socket.h"
9 #include "remoting/host/gnubby_auth_handler_posix.h"
10 #include "remoting/host/gnubby_socket.h"
11 #include "remoting/protocol/protocol_mock_objects.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace remoting {
15
16 using protocol::MockClientStub;
17
18 using testing::_;
19 using testing::Return;
20
21 namespace {
22
23 // Test gnubby request data.
24 const unsigned char request_data[] = {
25 0x00, 0x00, 0x00, 0x9a, 0x65, 0x1e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
26 0x00, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x60, 0x90,
27 0x24, 0x71, 0xf8, 0xf2, 0xe5, 0xdf, 0x7f, 0x81, 0xc7, 0x49, 0xc4, 0xa3,
28 0x58, 0x5c, 0xf6, 0xcc, 0x40, 0x14, 0x28, 0x0c, 0xa0, 0xfa, 0x03, 0x18,
29 0x38, 0xd8, 0x7d, 0x77, 0x2b, 0x3a, 0x00, 0x00, 0x00, 0x20, 0x64, 0x46,
30 0x47, 0x2f, 0xdf, 0x6e, 0xed, 0x7b, 0xf3, 0xc3, 0x37, 0x20, 0xf2, 0x36,
31 0x67, 0x6c, 0x36, 0xe1, 0xb4, 0x5e, 0xbe, 0x04, 0x85, 0xdb, 0x89, 0xa3,
32 0xcd, 0xfd, 0xd2, 0x4b, 0xd6, 0x9f, 0x00, 0x00, 0x00, 0x40, 0x38, 0x35,
33 0x05, 0x75, 0x1d, 0x13, 0x6e, 0xb3, 0x6b, 0x1d, 0x29, 0xae, 0xd3, 0x43,
34 0xe6, 0x84, 0x8f, 0xa3, 0x9d, 0x65, 0x4e, 0x2f, 0x57, 0xe3, 0xf6, 0xe6,
35 0x20, 0x3c, 0x00, 0xc6, 0xe1, 0x73, 0x34, 0xe2, 0x23, 0x99, 0xc4, 0xfa,
36 0x91, 0xc2, 0xd5, 0x97, 0xc1, 0x8b, 0xd0, 0x3c, 0x13, 0xba, 0xf0, 0xd7,
37 0x5e, 0xa3, 0xbc, 0x02, 0x5b, 0xec, 0xe4, 0x4b, 0xae, 0x0e, 0xf2, 0xbd,
38 0xc8, 0xaa};
39
40 class MockStreamListenSocket : public net::StreamListenSocket {
41 public:
MockStreamListenSocket(net::StreamListenSocket::Delegate * delegate)42 explicit MockStreamListenSocket(net::StreamListenSocket::Delegate* delegate)
43 : StreamListenSocket(net::kInvalidSocket, delegate) {}
44
Accept()45 virtual void Accept() OVERRIDE { NOTREACHED(); }
46
47 private:
~MockStreamListenSocket()48 virtual ~MockStreamListenSocket() {}
49 };
50
51 } // namespace
52
53 class GnubbyAuthHandlerPosixTest : public testing::Test {
54 public:
GnubbyAuthHandlerPosixTest()55 GnubbyAuthHandlerPosixTest() {}
56
57 virtual void SetUp() OVERRIDE;
58
59 protected:
60 // Object under test.
61 scoped_ptr<GnubbyAuthHandlerPosix> auth_handler_posix_;
62
63 // GnubbyAuthHandler interface for |auth_handler_posix_|.
64 GnubbyAuthHandler* auth_handler_;
65
66 // Stream delegate interface for |auth_handler_posix_|.
67 net::StreamListenSocket::Delegate* delegate_;
68
69 // Mock client stub.
70 MockClientStub client_stub_;
71
72 base::MessageLoop message_loop_;
73
74 private:
75 void OnConnect(int result);
76 };
77
SetUp()78 void GnubbyAuthHandlerPosixTest::SetUp() {
79 auth_handler_posix_.reset(new GnubbyAuthHandlerPosix(&client_stub_));
80 auth_handler_ = auth_handler_posix_.get();
81 delegate_ = auth_handler_posix_.get();
82 }
83
84 MATCHER_P2(EqualsDataMessage, id, data, "") {
85 std::string connection_id = base::StringPrintf("\"connectionId\":%d", id);
86 std::string data_message = base::StringPrintf("\"data\":%s", data);
87
88 return (arg.type() == "gnubby-auth" &&
89 arg.data().find("\"type\":\"data\"") != std::string::npos &&
90 arg.data().find(connection_id) != std::string::npos &&
91 arg.data().find(data_message) != std::string::npos);
92 }
93
TEST_F(GnubbyAuthHandlerPosixTest,HostDataMessageDelivered)94 TEST_F(GnubbyAuthHandlerPosixTest, HostDataMessageDelivered) {
95 // Expects a JSON array of the ASCII character codes for "test_msg".
96 EXPECT_CALL(client_stub_,
97 DeliverHostMessage(
98 EqualsDataMessage(42, "[116,101,115,116,95,109,115,103]")));
99
100 auth_handler_->DeliverHostDataMessage(42, "test_msg");
101 }
102
TEST_F(GnubbyAuthHandlerPosixTest,DidClose)103 TEST_F(GnubbyAuthHandlerPosixTest, DidClose) {
104 net::StreamListenSocket* socket = new MockStreamListenSocket(delegate_);
105
106 delegate_->DidAccept(NULL, make_scoped_ptr(socket));
107 ASSERT_TRUE(auth_handler_posix_->HasActiveSocketForTesting(socket));
108
109 delegate_->DidClose(socket);
110 ASSERT_FALSE(auth_handler_posix_->HasActiveSocketForTesting(socket));
111 }
112
TEST_F(GnubbyAuthHandlerPosixTest,DidRead)113 TEST_F(GnubbyAuthHandlerPosixTest, DidRead) {
114 EXPECT_CALL(client_stub_, DeliverHostMessage(_));
115
116 net::StreamListenSocket* socket = new MockStreamListenSocket(delegate_);
117
118 delegate_->DidAccept(NULL, make_scoped_ptr(socket));
119 delegate_->DidRead(socket,
120 reinterpret_cast<const char*>(request_data),
121 sizeof(request_data));
122 }
123
TEST_F(GnubbyAuthHandlerPosixTest,DidReadByteByByte)124 TEST_F(GnubbyAuthHandlerPosixTest, DidReadByteByByte) {
125 EXPECT_CALL(client_stub_, DeliverHostMessage(_));
126
127 net::StreamListenSocket* socket = new MockStreamListenSocket(delegate_);
128
129 delegate_->DidAccept(NULL, make_scoped_ptr(socket));
130 for (unsigned int i = 0; i < sizeof(request_data); ++i) {
131 delegate_->DidRead(
132 socket, reinterpret_cast<const char*>(request_data + i), 1);
133 }
134 }
135
TEST_F(GnubbyAuthHandlerPosixTest,DidReadTimeout)136 TEST_F(GnubbyAuthHandlerPosixTest, DidReadTimeout) {
137 net::StreamListenSocket* socket = new MockStreamListenSocket(delegate_);
138
139 delegate_->DidAccept(NULL, make_scoped_ptr(socket));
140 ASSERT_TRUE(auth_handler_posix_->HasActiveSocketForTesting(socket));
141
142 base::MockTimer* mock_timer = new base::MockTimer(false, false);
143 auth_handler_posix_->GetGnubbySocketForTesting(socket)
144 ->SetTimerForTesting(scoped_ptr<base::Timer>(mock_timer));
145 delegate_->DidRead(socket, reinterpret_cast<const char*>(request_data), 1);
146 mock_timer->Fire();
147
148 ASSERT_FALSE(auth_handler_posix_->HasActiveSocketForTesting(socket));
149 }
150
TEST_F(GnubbyAuthHandlerPosixTest,ClientErrorMessageDelivered)151 TEST_F(GnubbyAuthHandlerPosixTest, ClientErrorMessageDelivered) {
152 net::StreamListenSocket* socket = new MockStreamListenSocket(delegate_);
153
154 delegate_->DidAccept(NULL, make_scoped_ptr(socket));
155
156 std::string error_json = base::StringPrintf(
157 "{\"type\":\"error\",\"connectionId\":%d}",
158 auth_handler_posix_->GetConnectionIdForTesting(socket));
159
160 ASSERT_TRUE(auth_handler_posix_->HasActiveSocketForTesting(socket));
161 auth_handler_->DeliverClientMessage(error_json);
162 ASSERT_FALSE(auth_handler_posix_->HasActiveSocketForTesting(socket));
163 }
164
165 } // namespace remoting
166