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

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: Actually add braces Created 6 years, 10 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 // Mock connection factory.
57 scoped_ptr<MockGnubbyConnectionFactory> gnubby_connection_factory_;
58
59 // Mock client stub.
60 MockClientStub client_stub_;
61
62 private:
63 void OnConnect(int result);
64 };
65
66 void GnubbyAuthHandlerTest::SetUp() {
67 gnubby_connection_factory_.reset(new MockGnubbyConnectionFactory());
68 auth_handler_.reset(new GnubbyAuthHandler(
69 gnubby_connection_factory_.get(), &client_stub_));
70 }
71
72 net::MockTCPClientSocket* GnubbyAuthHandlerTest::GetConnectedSocket(
73 net::StaticSocketDataProvider* socket_data) {
74
75 net::AddressList addresses;
76 socket_data->set_connect_data(net::MockConnect(net::SYNCHRONOUS, net::OK));
77 net::MockTCPClientSocket* socket =
78 new net::MockTCPClientSocket(addresses, NULL, socket_data);
79
80 int result = socket->Connect(
81 base::Bind(&GnubbyAuthHandlerTest::OnConnect, base::Unretained(this)));
82 EXPECT_EQ(net::OK, result);
83 return socket;
84 }
85
86 void GnubbyAuthHandlerTest::OnConnect(int result) {}
87
88 MATCHER_P(EqualsCloseMessage, id, "") {
89 std::string message = base::StringPrintf("control close %d", id);
90 return (arg.type() == "gnubby-auth" && arg.data() == message);
91 }
92
93 MATCHER_P2(EqualsDataMessage, id, data, "") {
94 std::string message = base::StringPrintf("data %d %s", id, data.c_str());
95 return (arg.type() == "gnubby-auth" && arg.data() == message);
96 }
97
98 TEST_F(GnubbyAuthHandlerTest, DeliverClientMessage) {
99 std::string encoded_data;
100 base::Base64Encode("test_msg", &encoded_data);
101
102 net::MockWrite write(net::SYNCHRONOUS, "test_msg");
103 net::MockRead read(net::SYNCHRONOUS, net::ERR_IO_PENDING);
104 net::StaticSocketDataProvider socket_data(&read, 1, &write, 1);
105
106 int connection_id = auth_handler_->AddGnubbyConnectionForTesting(
107 GetConnectedSocket(&socket_data));
108
109 std::string message = base::StringPrintf(
110 "[ \"data\", \"%d\", \"%s\" ]", connection_id, encoded_data.c_str());
111 auth_handler_->DeliverClientMessage(message);
112 }
113
114 TEST_F(GnubbyAuthHandlerTest, ConnectionClosed) {
115 net::MockRead read(net::SYNCHRONOUS, net::ERR_IO_PENDING);
116 net::StaticSocketDataProvider socket_data(&read, 1, NULL, 0);
117
118 int connection_id = auth_handler_->AddGnubbyConnectionForTesting(
119 GetConnectedSocket(&socket_data));
120
121 EXPECT_CALL(client_stub_,
122 DeliverHostMessage(EqualsCloseMessage(connection_id)));
123
124 auth_handler_->ConnectionClosed(connection_id);
125 EXPECT_FALSE(auth_handler_->HasGnubbyConnectionForTesting(connection_id));
126 }
127
128 TEST_F(GnubbyAuthHandlerTest, ConnectionError) {
129 net::MockRead read(net::SYNCHRONOUS, net::ERR_IO_PENDING);
130 net::StaticSocketDataProvider socket_data(&read, 1, NULL, 0);
131
132 int connection_id = auth_handler_->AddGnubbyConnectionForTesting(
133 GetConnectedSocket(&socket_data));
134
135 EXPECT_CALL(client_stub_,
136 DeliverHostMessage(EqualsCloseMessage(connection_id)));
137
138 auth_handler_->ConnectionError(connection_id, net::ERR_FAILED);
139 EXPECT_FALSE(auth_handler_->HasGnubbyConnectionForTesting(connection_id));
140 }
141
142 TEST_F(GnubbyAuthHandlerTest, DeliverHostControlMessage) {
143 EXPECT_CALL(client_stub_, DeliverHostMessage(EqualsCloseMessage(42)));
144
145 auth_handler_->DeliverHostControlMessage("close", "42");
146 }
147
148 TEST_F(GnubbyAuthHandlerTest, DeliverHostDataMessage) {
149 std::string encoded_data;
150 base::Base64Encode("test_msg", &encoded_data);
151
152 EXPECT_CALL(client_stub_,
153 DeliverHostMessage(EqualsDataMessage(42, encoded_data)));
154
155 auth_handler_->DeliverHostDataMessage(42, "test_msg");
156 }
157
158 TEST_F(GnubbyAuthHandlerTest, SocketAccepted) {
159 net::StaticSocketDataProvider socket_data(NULL, 0, NULL, 0);
160
161 MockTCPServerSocket* gnubbyd_socket = new MockTCPServerSocket();
162 MockGnubbyConnection* gnubby_connection = new MockGnubbyConnection(
163 auth_handler_.get(), 1, GetConnectedSocket(&socket_data));
164
165 EXPECT_CALL(*gnubbyd_socket, Accept(_, _)).WillOnce(Return(net::OK)).WillOnce(
166 Return(net::ERR_IO_PENDING));
167 EXPECT_CALL(*gnubby_connection_factory_, Create(_, _, _))
168 .WillOnce(Return(gnubby_connection));
169 EXPECT_CALL(*gnubby_connection, Read());
170
171 auth_handler_->SetGnubbydSocketForTesting(gnubbyd_socket);
172 }
173
174 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698