Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1345)

Side by Side Diff: remoting/host/gnubby_auth_handler_unittest.cc

Issue 138753005: Add gnubby authentication to remoting host (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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/base64.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/test/test_simple_task_runner.h"
10 #include "net/base/net_errors.h"
11 #include "net/socket/socket_test_util.h"
12 #include "remoting/host/gnubby_auth_handler.h"
13 #include "remoting/host/host_mock_objects.h"
14 #include "remoting/protocol/protocol_mock_objects.h"
15 #include "testing/gmock/include/gmock/gmock-matchers.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace remoting {
19
20 using protocol::MockClientStub;
21
22 using testing::_;
23 using testing::Return;
24
25 namespace {
26
27 class MockTCPServerSocket : public net::TCPServerSocket {
28 public:
29 explicit MockTCPServerSocket()
30 : net::TCPServerSocket(NULL, net::NetLog::Source()) {}
31 MOCK_METHOD2(Listen, int(const net::IPEndPoint& address, int backlog));
32 MOCK_METHOD2(Accept,
33 int(scoped_ptr<net::StreamSocket>* socket,
34 const net::CompletionCallback& callback));
35
36 private:
37 DISALLOW_COPY_AND_ASSIGN(MockTCPServerSocket);
38 };
39
40 } // namespace
41
42 class GnubbyAuthHandlerTest : public testing::Test {
43 public:
44 GnubbyAuthHandlerTest() {}
45
46 virtual void SetUp() OVERRIDE;
47
48 protected:
49 // Create a mock connected socket.
50 net::MockTCPClientSocket* GetConnectedSocket(
51 net::StaticSocketDataProvider* socket_data);
52
53 // Object under test.
54 scoped_ptr<GnubbyAuthHandler> auth_handler_;
55
56 // Task runner for object under test.
57 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
58
59 // Mock connection factory.
60 scoped_ptr<MockGnubbyConnectionFactory> gnubby_connection_factory_;
61
62 // Mock client stub.
63 MockClientStub client_stub_;
64
65 private:
66 void OnConnect(int result);
67 };
68
69 void GnubbyAuthHandlerTest::SetUp() {
70 task_runner_ = new base::TestSimpleTaskRunner();
71 gnubby_connection_factory_.reset(new MockGnubbyConnectionFactory());
72 auth_handler_.reset(new GnubbyAuthHandler(
73 task_runner_, gnubby_connection_factory_.get(), &client_stub_));
74 }
75
76 net::MockTCPClientSocket* GnubbyAuthHandlerTest::GetConnectedSocket(
77 net::StaticSocketDataProvider* socket_data) {
78
79 net::AddressList addresses;
80 socket_data->set_connect_data(net::MockConnect(net::SYNCHRONOUS, net::OK));
81 net::MockTCPClientSocket* socket =
82 new net::MockTCPClientSocket(addresses, NULL, socket_data);
83
84 int result = socket->Connect(
85 base::Bind(&GnubbyAuthHandlerTest::OnConnect, base::Unretained(this)));
86 EXPECT_EQ(net::OK, result);
87 return socket;
88 }
89
90 void GnubbyAuthHandlerTest::OnConnect(int result) {}
91
92 MATCHER_P(EqualsCloseMessage, id, "") {
93 std::string message = base::StringPrintf("control close %d", id);
94 return (arg.type() == "gnubby-auth" && arg.data() == message);
95 }
96
97 MATCHER_P2(EqualsDataMessage, id, data, "") {
98 std::string message = base::StringPrintf("data %d %s", id, data.c_str());
99 return (arg.type() == "gnubby-auth" && arg.data() == message);
100 }
101
102 TEST_F(GnubbyAuthHandlerTest, DeliverClientMessage) {
103 std::string encoded_data;
104 base::Base64Encode("test_msg", &encoded_data);
105
106 net::MockWrite write(net::SYNCHRONOUS, "test_msg");
107 net::MockRead read(net::SYNCHRONOUS, net::ERR_IO_PENDING);
108 net::StaticSocketDataProvider socket_data(&read, 1, &write, 1);
109
110 int connection_id = auth_handler_->AddGnubbyConnectionForTesting(
111 GetConnectedSocket(&socket_data));
112
113 std::string message = base::StringPrintf(
114 "[ \"data\", \"%d\", \"%s\" ]", connection_id, encoded_data.c_str());
115 auth_handler_->DeliverClientMessage(message);
116 }
117
118 TEST_F(GnubbyAuthHandlerTest, ConnectionClosed) {
119 net::MockRead read(net::SYNCHRONOUS, net::ERR_IO_PENDING);
120 net::StaticSocketDataProvider socket_data(&read, 1, NULL, 0);
121
122 int connection_id = auth_handler_->AddGnubbyConnectionForTesting(
123 GetConnectedSocket(&socket_data));
124
125 EXPECT_CALL(client_stub_,
126 DeliverHostMessage(EqualsCloseMessage(connection_id)));
127
128 auth_handler_->ConnectionClosed(connection_id);
129 EXPECT_FALSE(auth_handler_->HasGnubbyConnectionForTesting(connection_id));
130 }
131
132 TEST_F(GnubbyAuthHandlerTest, ConnectionError) {
133 net::MockRead read(net::SYNCHRONOUS, net::ERR_IO_PENDING);
134 net::StaticSocketDataProvider socket_data(&read, 1, NULL, 0);
135
136 int connection_id = auth_handler_->AddGnubbyConnectionForTesting(
137 GetConnectedSocket(&socket_data));
138
139 EXPECT_CALL(client_stub_,
140 DeliverHostMessage(EqualsCloseMessage(connection_id)));
141
142 auth_handler_->ConnectionError(connection_id, net::ERR_FAILED);
143 EXPECT_FALSE(auth_handler_->HasGnubbyConnectionForTesting(connection_id));
144 }
145
146 TEST_F(GnubbyAuthHandlerTest, DeliverHostControlMessage) {
147 EXPECT_CALL(client_stub_, DeliverHostMessage(EqualsCloseMessage(42)));
148
149 auth_handler_->DeliverHostControlMessage("close", "42");
150 }
151
152 TEST_F(GnubbyAuthHandlerTest, DeliverHostDataMessage) {
153 std::string encoded_data;
154 base::Base64Encode("test_msg", &encoded_data);
155
156 EXPECT_CALL(client_stub_,
157 DeliverHostMessage(EqualsDataMessage(42, encoded_data)));
158
159 auth_handler_->DeliverHostDataMessage(42, "test_msg");
160 }
161
162 TEST_F(GnubbyAuthHandlerTest, SocketAccepted) {
163 net::StaticSocketDataProvider socket_data(NULL, 0, NULL, 0);
164
165 MockTCPServerSocket* gnubbyd_socket = new MockTCPServerSocket();
166 MockGnubbyConnection* gnubby_connection = new MockGnubbyConnection(
167 task_runner_, auth_handler_.get(), 1, GetConnectedSocket(&socket_data));
168
169 EXPECT_CALL(*gnubbyd_socket, Accept(_, _)).WillOnce(Return(net::OK)).WillOnce(
170 Return(net::ERR_IO_PENDING));
171 EXPECT_CALL(*gnubby_connection_factory_, Create(_, _, _, _))
172 .WillOnce(Return(gnubby_connection));
173 EXPECT_CALL(*gnubby_connection, Read());
174
175 auth_handler_->SetGnubbydSocketForTesting(gnubbyd_socket);
176 }
177
178 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698