blob: d3e522a6975e114e4543a3b7264a541a2a490f76 [file] [log] [blame]
[email protected]3a5b82c2014-02-20 13:28:141// 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
[email protected]f66432db2014-03-24 02:33:355#include "base/message_loop/message_loop.h"
[email protected]3a5b82c2014-02-20 13:28:146#include "base/strings/stringprintf.h"
[email protected]f66432db2014-03-24 02:33:357#include "base/timer/mock_timer.h"
[email protected]3a5b82c2014-02-20 13:28:148#include "net/socket/stream_listen_socket.h"
9#include "remoting/host/gnubby_auth_handler_posix.h"
[email protected]f66432db2014-03-24 02:33:3510#include "remoting/host/gnubby_socket.h"
[email protected]3a5b82c2014-02-20 13:28:1411#include "remoting/protocol/protocol_mock_objects.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14namespace remoting {
15
16using protocol::MockClientStub;
17
18using testing::_;
19using testing::Return;
20
21namespace {
22
23// Test gnubby request data.
[email protected]77715292014-02-27 22:30:3224const unsigned char request_data[] = {
[email protected]3a5b82c2014-02-20 13:28:1425 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
40class MockStreamListenSocket : public net::StreamListenSocket {
41 public:
42 explicit MockStreamListenSocket(net::StreamListenSocket::Delegate* delegate)
43 : StreamListenSocket(net::kInvalidSocket, delegate) {}
44
dcheng562aba52014-10-21 12:30:1445 void Accept() override { NOTREACHED(); }
[email protected]3a5b82c2014-02-20 13:28:1446
47 private:
dcheng562aba52014-10-21 12:30:1448 ~MockStreamListenSocket() override {}
[email protected]3a5b82c2014-02-20 13:28:1449};
50
51} // namespace
52
53class GnubbyAuthHandlerPosixTest : public testing::Test {
54 public:
55 GnubbyAuthHandlerPosixTest() {}
56
dcheng440d8e1c2014-10-28 01:23:1557 void SetUp() override;
[email protected]3a5b82c2014-02-20 13:28:1458
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
[email protected]f66432db2014-03-24 02:33:3572 base::MessageLoop message_loop_;
73
[email protected]3a5b82c2014-02-20 13:28:1474 private:
75 void OnConnect(int result);
76};
77
78void 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
84MATCHER_P2(EqualsDataMessage, id, data, "") {
85 std::string connection_id = base::StringPrintf("\"connectionId\":%d", id);
[email protected]f66432db2014-03-24 02:33:3586 std::string data_message = base::StringPrintf("\"data\":%s", data);
[email protected]3a5b82c2014-02-20 13:28:1487
88 return (arg.type() == "gnubby-auth" &&
89 arg.data().find("\"type\":\"data\"") != std::string::npos &&
90 arg.data().find(connection_id) != std::string::npos &&
[email protected]f66432db2014-03-24 02:33:3591 arg.data().find(data_message) != std::string::npos);
[email protected]3a5b82c2014-02-20 13:28:1492}
93
94TEST_F(GnubbyAuthHandlerPosixTest, HostDataMessageDelivered) {
[email protected]f66432db2014-03-24 02:33:3595 // Expects a JSON array of the ASCII character codes for "test_msg".
[email protected]3a5b82c2014-02-20 13:28:1496 EXPECT_CALL(client_stub_,
[email protected]f66432db2014-03-24 02:33:3597 DeliverHostMessage(
98 EqualsDataMessage(42, "[116,101,115,116,95,109,115,103]")));
[email protected]3a5b82c2014-02-20 13:28:1499
100 auth_handler_->DeliverHostDataMessage(42, "test_msg");
101}
102
103TEST_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
113TEST_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));
[email protected]f66432db2014-03-24 02:33:35119 delegate_->DidRead(socket,
120 reinterpret_cast<const char*>(request_data),
[email protected]77715292014-02-27 22:30:32121 sizeof(request_data));
[email protected]3a5b82c2014-02-20 13:28:14122}
123
124TEST_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) {
[email protected]f66432db2014-03-24 02:33:35131 delegate_->DidRead(
132 socket, reinterpret_cast<const char*>(request_data + i), 1);
[email protected]3a5b82c2014-02-20 13:28:14133 }
134}
135
[email protected]f66432db2014-03-24 02:33:35136TEST_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)
sergeyu2d690882014-10-01 02:36:43144 ->SetTimerForTesting(make_scoped_ptr(mock_timer));
[email protected]f66432db2014-03-24 02:33:35145 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
151TEST_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
[email protected]3a5b82c2014-02-20 13:28:14165} // namespace remoting