| Index: remoting/host/gnubby_auth_handler_unittest.cc
|
| diff --git a/remoting/host/gnubby_auth_handler_unittest.cc b/remoting/host/gnubby_auth_handler_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..2b170f7ed4f4816b599c2e8106163dc5c803d462
|
| --- /dev/null
|
| +++ b/remoting/host/gnubby_auth_handler_unittest.cc
|
| @@ -0,0 +1,178 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "base/base64.h"
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "base/strings/string_number_conversions.h"
|
| +#include "base/strings/stringprintf.h"
|
| +#include "base/test/test_simple_task_runner.h"
|
| +#include "net/base/net_errors.h"
|
| +#include "net/socket/socket_test_util.h"
|
| +#include "remoting/host/gnubby_auth_handler.h"
|
| +#include "remoting/host/host_mock_objects.h"
|
| +#include "remoting/protocol/protocol_mock_objects.h"
|
| +#include "testing/gmock/include/gmock/gmock-matchers.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace remoting {
|
| +
|
| +using protocol::MockClientStub;
|
| +
|
| +using testing::_;
|
| +using testing::Return;
|
| +
|
| +namespace {
|
| +
|
| +class MockTCPServerSocket : public net::TCPServerSocket {
|
| + public:
|
| + explicit MockTCPServerSocket()
|
| + : net::TCPServerSocket(NULL, net::NetLog::Source()) {}
|
| + MOCK_METHOD2(Listen, int(const net::IPEndPoint& address, int backlog));
|
| + MOCK_METHOD2(Accept,
|
| + int(scoped_ptr<net::StreamSocket>* socket,
|
| + const net::CompletionCallback& callback));
|
| +
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(MockTCPServerSocket);
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +class GnubbyAuthHandlerTest : public testing::Test {
|
| + public:
|
| + GnubbyAuthHandlerTest() {}
|
| +
|
| + virtual void SetUp() OVERRIDE;
|
| +
|
| + protected:
|
| + // Create a mock connected socket.
|
| + net::MockTCPClientSocket* GetConnectedSocket(
|
| + net::StaticSocketDataProvider* socket_data);
|
| +
|
| + // Object under test.
|
| + scoped_ptr<GnubbyAuthHandler> auth_handler_;
|
| +
|
| + // Task runner for object under test.
|
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
|
| +
|
| + // Mock connection factory.
|
| + scoped_ptr<MockGnubbyConnectionFactory> gnubby_connection_factory_;
|
| +
|
| + // Mock client stub.
|
| + MockClientStub client_stub_;
|
| +
|
| + private:
|
| + void OnConnect(int result);
|
| +};
|
| +
|
| +void GnubbyAuthHandlerTest::SetUp() {
|
| + task_runner_ = new base::TestSimpleTaskRunner();
|
| + gnubby_connection_factory_.reset(new MockGnubbyConnectionFactory());
|
| + auth_handler_.reset(new GnubbyAuthHandler(
|
| + task_runner_, gnubby_connection_factory_.get(), &client_stub_));
|
| +}
|
| +
|
| +net::MockTCPClientSocket* GnubbyAuthHandlerTest::GetConnectedSocket(
|
| + net::StaticSocketDataProvider* socket_data) {
|
| +
|
| + net::AddressList addresses;
|
| + socket_data->set_connect_data(net::MockConnect(net::SYNCHRONOUS, net::OK));
|
| + net::MockTCPClientSocket* socket =
|
| + new net::MockTCPClientSocket(addresses, NULL, socket_data);
|
| +
|
| + int result = socket->Connect(
|
| + base::Bind(&GnubbyAuthHandlerTest::OnConnect, base::Unretained(this)));
|
| + EXPECT_EQ(net::OK, result);
|
| + return socket;
|
| +}
|
| +
|
| +void GnubbyAuthHandlerTest::OnConnect(int result) {}
|
| +
|
| +MATCHER_P(EqualsCloseMessage, id, "") {
|
| + std::string message = base::StringPrintf("control close %d", id);
|
| + return (arg.type() == "gnubby-auth" && arg.data() == message);
|
| +}
|
| +
|
| +MATCHER_P2(EqualsDataMessage, id, data, "") {
|
| + std::string message = base::StringPrintf("data %d %s", id, data.c_str());
|
| + return (arg.type() == "gnubby-auth" && arg.data() == message);
|
| +}
|
| +
|
| +TEST_F(GnubbyAuthHandlerTest, DeliverClientMessage) {
|
| + std::string encoded_data;
|
| + base::Base64Encode("test_msg", &encoded_data);
|
| +
|
| + net::MockWrite write(net::SYNCHRONOUS, "test_msg");
|
| + net::MockRead read(net::SYNCHRONOUS, net::ERR_IO_PENDING);
|
| + net::StaticSocketDataProvider socket_data(&read, 1, &write, 1);
|
| +
|
| + int connection_id = auth_handler_->AddGnubbyConnectionForTesting(
|
| + GetConnectedSocket(&socket_data));
|
| +
|
| + std::string message = base::StringPrintf(
|
| + "[ \"data\", \"%d\", \"%s\" ]", connection_id, encoded_data.c_str());
|
| + auth_handler_->DeliverClientMessage(message);
|
| +}
|
| +
|
| +TEST_F(GnubbyAuthHandlerTest, ConnectionClosed) {
|
| + net::MockRead read(net::SYNCHRONOUS, net::ERR_IO_PENDING);
|
| + net::StaticSocketDataProvider socket_data(&read, 1, NULL, 0);
|
| +
|
| + int connection_id = auth_handler_->AddGnubbyConnectionForTesting(
|
| + GetConnectedSocket(&socket_data));
|
| +
|
| + EXPECT_CALL(client_stub_,
|
| + DeliverHostMessage(EqualsCloseMessage(connection_id)));
|
| +
|
| + auth_handler_->ConnectionClosed(connection_id);
|
| + EXPECT_FALSE(auth_handler_->HasGnubbyConnectionForTesting(connection_id));
|
| +}
|
| +
|
| +TEST_F(GnubbyAuthHandlerTest, ConnectionError) {
|
| + net::MockRead read(net::SYNCHRONOUS, net::ERR_IO_PENDING);
|
| + net::StaticSocketDataProvider socket_data(&read, 1, NULL, 0);
|
| +
|
| + int connection_id = auth_handler_->AddGnubbyConnectionForTesting(
|
| + GetConnectedSocket(&socket_data));
|
| +
|
| + EXPECT_CALL(client_stub_,
|
| + DeliverHostMessage(EqualsCloseMessage(connection_id)));
|
| +
|
| + auth_handler_->ConnectionError(connection_id, net::ERR_FAILED);
|
| + EXPECT_FALSE(auth_handler_->HasGnubbyConnectionForTesting(connection_id));
|
| +}
|
| +
|
| +TEST_F(GnubbyAuthHandlerTest, DeliverHostControlMessage) {
|
| + EXPECT_CALL(client_stub_, DeliverHostMessage(EqualsCloseMessage(42)));
|
| +
|
| + auth_handler_->DeliverHostControlMessage("close", "42");
|
| +}
|
| +
|
| +TEST_F(GnubbyAuthHandlerTest, DeliverHostDataMessage) {
|
| + std::string encoded_data;
|
| + base::Base64Encode("test_msg", &encoded_data);
|
| +
|
| + EXPECT_CALL(client_stub_,
|
| + DeliverHostMessage(EqualsDataMessage(42, encoded_data)));
|
| +
|
| + auth_handler_->DeliverHostDataMessage(42, "test_msg");
|
| +}
|
| +
|
| +TEST_F(GnubbyAuthHandlerTest, SocketAccepted) {
|
| + net::StaticSocketDataProvider socket_data(NULL, 0, NULL, 0);
|
| +
|
| + MockTCPServerSocket* gnubbyd_socket = new MockTCPServerSocket();
|
| + MockGnubbyConnection* gnubby_connection = new MockGnubbyConnection(
|
| + task_runner_, auth_handler_.get(), 1, GetConnectedSocket(&socket_data));
|
| +
|
| + EXPECT_CALL(*gnubbyd_socket, Accept(_, _)).WillOnce(Return(net::OK)).WillOnce(
|
| + Return(net::ERR_IO_PENDING));
|
| + EXPECT_CALL(*gnubby_connection_factory_, Create(_, _, _, _))
|
| + .WillOnce(Return(gnubby_connection));
|
| + EXPECT_CALL(*gnubby_connection, Read());
|
| +
|
| + auth_handler_->SetGnubbydSocketForTesting(gnubbyd_socket);
|
| +}
|
| +
|
| +} // namespace remoting
|
|
|