[email protected] | 3a5b82c | 2014-02-20 13:28:14 | [diff] [blame] | 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 | |
[email protected] | f66432db | 2014-03-24 02:33:35 | [diff] [blame] | 5 | #include "base/message_loop/message_loop.h" |
[email protected] | 3a5b82c | 2014-02-20 13:28:14 | [diff] [blame] | 6 | #include "base/strings/stringprintf.h" |
[email protected] | f66432db | 2014-03-24 02:33:35 | [diff] [blame] | 7 | #include "base/timer/mock_timer.h" |
[email protected] | 3a5b82c | 2014-02-20 13:28:14 | [diff] [blame] | 8 | #include "net/socket/stream_listen_socket.h" |
| 9 | #include "remoting/host/gnubby_auth_handler_posix.h" |
[email protected] | f66432db | 2014-03-24 02:33:35 | [diff] [blame] | 10 | #include "remoting/host/gnubby_socket.h" |
[email protected] | 3a5b82c | 2014-02-20 13:28:14 | [diff] [blame] | 11 | #include "remoting/protocol/protocol_mock_objects.h" |
| 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace remoting { |
| 15 | |
| 16 | using protocol::MockClientStub; |
| 17 | |
| 18 | using testing::_; |
| 19 | using testing::Return; |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | // Test gnubby request data. |
[email protected] | 7771529 | 2014-02-27 22:30:32 | [diff] [blame] | 24 | const unsigned char request_data[] = { |
[email protected] | 3a5b82c | 2014-02-20 13:28:14 | [diff] [blame] | 25 | 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 | |
| 40 | class MockStreamListenSocket : public net::StreamListenSocket { |
| 41 | public: |
| 42 | explicit MockStreamListenSocket(net::StreamListenSocket::Delegate* delegate) |
| 43 | : StreamListenSocket(net::kInvalidSocket, delegate) {} |
| 44 | |
dcheng | 562aba5 | 2014-10-21 12:30:14 | [diff] [blame] | 45 | void Accept() override { NOTREACHED(); } |
[email protected] | 3a5b82c | 2014-02-20 13:28:14 | [diff] [blame] | 46 | |
| 47 | private: |
dcheng | 562aba5 | 2014-10-21 12:30:14 | [diff] [blame] | 48 | ~MockStreamListenSocket() override {} |
[email protected] | 3a5b82c | 2014-02-20 13:28:14 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | } // namespace |
| 52 | |
| 53 | class GnubbyAuthHandlerPosixTest : public testing::Test { |
| 54 | public: |
| 55 | GnubbyAuthHandlerPosixTest() {} |
| 56 | |
dcheng | 440d8e1c | 2014-10-28 01:23:15 | [diff] [blame] | 57 | void SetUp() override; |
[email protected] | 3a5b82c | 2014-02-20 13:28:14 | [diff] [blame] | 58 | |
| 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] | f66432db | 2014-03-24 02:33:35 | [diff] [blame] | 72 | base::MessageLoop message_loop_; |
| 73 | |
[email protected] | 3a5b82c | 2014-02-20 13:28:14 | [diff] [blame] | 74 | private: |
| 75 | void OnConnect(int result); |
| 76 | }; |
| 77 | |
| 78 | void 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 | |
| 84 | MATCHER_P2(EqualsDataMessage, id, data, "") { |
| 85 | std::string connection_id = base::StringPrintf("\"connectionId\":%d", id); |
[email protected] | f66432db | 2014-03-24 02:33:35 | [diff] [blame] | 86 | std::string data_message = base::StringPrintf("\"data\":%s", data); |
[email protected] | 3a5b82c | 2014-02-20 13:28:14 | [diff] [blame] | 87 | |
| 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] | f66432db | 2014-03-24 02:33:35 | [diff] [blame] | 91 | arg.data().find(data_message) != std::string::npos); |
[email protected] | 3a5b82c | 2014-02-20 13:28:14 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | TEST_F(GnubbyAuthHandlerPosixTest, HostDataMessageDelivered) { |
[email protected] | f66432db | 2014-03-24 02:33:35 | [diff] [blame] | 95 | // Expects a JSON array of the ASCII character codes for "test_msg". |
[email protected] | 3a5b82c | 2014-02-20 13:28:14 | [diff] [blame] | 96 | EXPECT_CALL(client_stub_, |
[email protected] | f66432db | 2014-03-24 02:33:35 | [diff] [blame] | 97 | DeliverHostMessage( |
| 98 | EqualsDataMessage(42, "[116,101,115,116,95,109,115,103]"))); |
[email protected] | 3a5b82c | 2014-02-20 13:28:14 | [diff] [blame] | 99 | |
| 100 | auth_handler_->DeliverHostDataMessage(42, "test_msg"); |
| 101 | } |
| 102 | |
| 103 | TEST_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 | |
| 113 | TEST_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] | f66432db | 2014-03-24 02:33:35 | [diff] [blame] | 119 | delegate_->DidRead(socket, |
| 120 | reinterpret_cast<const char*>(request_data), |
[email protected] | 7771529 | 2014-02-27 22:30:32 | [diff] [blame] | 121 | sizeof(request_data)); |
[email protected] | 3a5b82c | 2014-02-20 13:28:14 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | TEST_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] | f66432db | 2014-03-24 02:33:35 | [diff] [blame] | 131 | delegate_->DidRead( |
| 132 | socket, reinterpret_cast<const char*>(request_data + i), 1); |
[email protected] | 3a5b82c | 2014-02-20 13:28:14 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
[email protected] | f66432db | 2014-03-24 02:33:35 | [diff] [blame] | 136 | TEST_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) |
sergeyu | 2d69088 | 2014-10-01 02:36:43 | [diff] [blame] | 144 | ->SetTimerForTesting(make_scoped_ptr(mock_timer)); |
[email protected] | f66432db | 2014-03-24 02:33:35 | [diff] [blame] | 145 | 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 | |
| 151 | TEST_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] | 3a5b82c | 2014-02-20 13:28:14 | [diff] [blame] | 165 | } // namespace remoting |