| Index: remoting/host/gnubby_connection_unittest.cc
|
| diff --git a/remoting/host/gnubby_connection_unittest.cc b/remoting/host/gnubby_connection_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a6932ce3e1c9dfeedff32c9ca1f63f6dcdcb6b23
|
| --- /dev/null
|
| +++ b/remoting/host/gnubby_connection_unittest.cc
|
| @@ -0,0 +1,128 @@
|
| +// 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/test/test_simple_task_runner.h"
|
| +#include "net/base/net_errors.h"
|
| +#include "net/socket/socket_test_util.h"
|
| +#include "remoting/host/gnubby_connection.h"
|
| +#include "remoting/host/host_mock_objects.h"
|
| +#include "remoting/protocol/protocol_mock_objects.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace remoting {
|
| +
|
| +using protocol::MockClientStub;
|
| +
|
| +using testing::_;
|
| +
|
| +class GnubbyConnectionTest : public testing::Test {
|
| + public:
|
| + GnubbyConnectionTest() {}
|
| +
|
| + virtual void SetUp() OVERRIDE;
|
| +
|
| + protected:
|
| + // Create a mock connected socket.
|
| + net::MockTCPClientSocket* GetConnectedSocket(
|
| + net::StaticSocketDataProvider* socket_data);
|
| +
|
| + // Object under test.
|
| + scoped_ptr<GnubbyConnection> connection_;
|
| +
|
| + // Task runner for object under test.
|
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
|
| +
|
| + // Mock gnubby auth handler that receives connection callbacks.
|
| + scoped_ptr<MockGnubbyAuthHandler> gnubby_auth_handler_;
|
| +
|
| + // Mock gnubby connection factory for mock gnubby auth handler.
|
| + scoped_ptr<MockGnubbyConnectionFactory> gnubby_connection_factory_;
|
| +
|
| + // Mock client stub.
|
| + MockClientStub client_stub_;
|
| +
|
| + private:
|
| + void OnConnect(int result);
|
| +};
|
| +
|
| +void GnubbyConnectionTest::SetUp() {
|
| + task_runner_ = new base::TestSimpleTaskRunner();
|
| +
|
| + gnubby_connection_factory_.reset(new MockGnubbyConnectionFactory());
|
| + gnubby_auth_handler_.reset(new MockGnubbyAuthHandler(
|
| + task_runner_, gnubby_connection_factory_.get(), &client_stub_));
|
| +}
|
| +
|
| +net::MockTCPClientSocket* GnubbyConnectionTest::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(&GnubbyConnectionTest::OnConnect, base::Unretained(this)));
|
| + EXPECT_EQ(net::OK, result);
|
| + return socket;
|
| +}
|
| +
|
| +void GnubbyConnectionTest::OnConnect(int result) {}
|
| +
|
| +TEST_F(GnubbyConnectionTest, Read) {
|
| + EXPECT_CALL(*gnubby_auth_handler_, DeliverHostDataMessage(42, "test_msg"));
|
| + EXPECT_CALL(*gnubby_auth_handler_, ConnectionClosed(42));
|
| +
|
| + net::MockRead read(net::SYNCHRONOUS, "test_msg");
|
| + net::MockRead close(net::SYNCHRONOUS, net::OK, "");
|
| + net::MockRead* read_and_close[] = {&read, &close};
|
| +
|
| + net::StaticSocketDataProvider socket_data(
|
| + read_and_close[0], sizeof(read_and_close), NULL, 0);
|
| +
|
| + connection_.reset(new GnubbyConnection(task_runner_,
|
| + gnubby_auth_handler_.get(),
|
| + 42,
|
| + GetConnectedSocket(&socket_data)));
|
| + connection_->Read();
|
| +}
|
| +
|
| +TEST_F(GnubbyConnectionTest, ReadFailed) {
|
| + EXPECT_CALL(*gnubby_auth_handler_, ConnectionError(42, net::ERR_FAILED));
|
| +
|
| + net::MockRead read_fail(net::SYNCHRONOUS, net::ERR_FAILED);
|
| + net::StaticSocketDataProvider socket_data(&read_fail, 1, NULL, 0);
|
| +
|
| + connection_.reset(new GnubbyConnection(task_runner_,
|
| + gnubby_auth_handler_.get(),
|
| + 42,
|
| + GetConnectedSocket(&socket_data)));
|
| + connection_->Read();
|
| +}
|
| +
|
| +TEST_F(GnubbyConnectionTest, WriteFailed) {
|
| + EXPECT_CALL(*gnubby_auth_handler_, ConnectionError(42, net::ERR_FAILED));
|
| +
|
| + net::MockWrite write_fail(net::SYNCHRONOUS, net::ERR_FAILED);
|
| + net::StaticSocketDataProvider socket_data(NULL, 0, &write_fail, 1);
|
| +
|
| + connection_.reset(new GnubbyConnection(task_runner_,
|
| + gnubby_auth_handler_.get(),
|
| + 42,
|
| + GetConnectedSocket(&socket_data)));
|
| + connection_->Write("test_msg");
|
| +}
|
| +
|
| +TEST_F(GnubbyConnectionTest, Write) {
|
| + net::MockWrite write(net::SYNCHRONOUS, "test_msg");
|
| + net::StaticSocketDataProvider socket_data(NULL, 0, &write, 1);
|
| +
|
| + connection_.reset(new GnubbyConnection(task_runner_,
|
| + gnubby_auth_handler_.get(),
|
| + 42,
|
| + GetConnectedSocket(&socket_data)));
|
| + connection_->Write("test_msg");
|
| +}
|
| +
|
| +} // namespace remoting
|
|
|