| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/message_loop/message_loop.h" |
| 5 #include "base/strings/stringprintf.h" | 6 #include "base/strings/stringprintf.h" |
| 7 #include "base/timer/mock_timer.h" |
| 6 #include "net/socket/stream_listen_socket.h" | 8 #include "net/socket/stream_listen_socket.h" |
| 7 #include "remoting/host/gnubby_auth_handler_posix.h" | 9 #include "remoting/host/gnubby_auth_handler_posix.h" |
| 10 #include "remoting/host/gnubby_socket.h" |
| 8 #include "remoting/protocol/protocol_mock_objects.h" | 11 #include "remoting/protocol/protocol_mock_objects.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 13 |
| 11 namespace remoting { | 14 namespace remoting { |
| 12 | 15 |
| 13 using protocol::MockClientStub; | 16 using protocol::MockClientStub; |
| 14 | 17 |
| 15 using testing::_; | 18 using testing::_; |
| 16 using testing::Return; | 19 using testing::Return; |
| 17 | 20 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 | 62 |
| 60 // GnubbyAuthHandler interface for |auth_handler_posix_|. | 63 // GnubbyAuthHandler interface for |auth_handler_posix_|. |
| 61 GnubbyAuthHandler* auth_handler_; | 64 GnubbyAuthHandler* auth_handler_; |
| 62 | 65 |
| 63 // Stream delegate interface for |auth_handler_posix_|. | 66 // Stream delegate interface for |auth_handler_posix_|. |
| 64 net::StreamListenSocket::Delegate* delegate_; | 67 net::StreamListenSocket::Delegate* delegate_; |
| 65 | 68 |
| 66 // Mock client stub. | 69 // Mock client stub. |
| 67 MockClientStub client_stub_; | 70 MockClientStub client_stub_; |
| 68 | 71 |
| 72 base::MessageLoop message_loop_; |
| 73 |
| 69 private: | 74 private: |
| 70 void OnConnect(int result); | 75 void OnConnect(int result); |
| 71 }; | 76 }; |
| 72 | 77 |
| 73 void GnubbyAuthHandlerPosixTest::SetUp() { | 78 void GnubbyAuthHandlerPosixTest::SetUp() { |
| 74 auth_handler_posix_.reset(new GnubbyAuthHandlerPosix(&client_stub_)); | 79 auth_handler_posix_.reset(new GnubbyAuthHandlerPosix(&client_stub_)); |
| 75 auth_handler_ = auth_handler_posix_.get(); | 80 auth_handler_ = auth_handler_posix_.get(); |
| 76 delegate_ = auth_handler_posix_.get(); | 81 delegate_ = auth_handler_posix_.get(); |
| 77 } | 82 } |
| 78 | 83 |
| 79 MATCHER_P2(EqualsDataMessage, id, data, "") { | 84 MATCHER_P2(EqualsDataMessage, id, data, "") { |
| 80 std::string connection_id = base::StringPrintf("\"connectionId\":%d", id); | 85 std::string connection_id = base::StringPrintf("\"connectionId\":%d", id); |
| 81 std::string json_message = base::StringPrintf("\"jsonMessage\":\"%s\"", data); | 86 std::string data_message = base::StringPrintf("\"data\":%s", data); |
| 82 | 87 |
| 83 return (arg.type() == "gnubby-auth" && | 88 return (arg.type() == "gnubby-auth" && |
| 84 arg.data().find("\"type\":\"data\"") != std::string::npos && | 89 arg.data().find("\"type\":\"data\"") != std::string::npos && |
| 85 arg.data().find(connection_id) != std::string::npos && | 90 arg.data().find(connection_id) != std::string::npos && |
| 86 arg.data().find(json_message) != std::string::npos); | 91 arg.data().find(data_message) != std::string::npos); |
| 87 } | 92 } |
| 88 | 93 |
| 89 TEST_F(GnubbyAuthHandlerPosixTest, HostDataMessageDelivered) { | 94 TEST_F(GnubbyAuthHandlerPosixTest, HostDataMessageDelivered) { |
| 95 // Expects a JSON array of the ASCII character codes for "test_msg". |
| 90 EXPECT_CALL(client_stub_, | 96 EXPECT_CALL(client_stub_, |
| 91 DeliverHostMessage(EqualsDataMessage(42, "test_msg"))); | 97 DeliverHostMessage( |
| 98 EqualsDataMessage(42, "[116,101,115,116,95,109,115,103]"))); |
| 92 | 99 |
| 93 auth_handler_->DeliverHostDataMessage(42, "test_msg"); | 100 auth_handler_->DeliverHostDataMessage(42, "test_msg"); |
| 94 } | 101 } |
| 95 | 102 |
| 96 TEST_F(GnubbyAuthHandlerPosixTest, DidClose) { | 103 TEST_F(GnubbyAuthHandlerPosixTest, DidClose) { |
| 97 net::StreamListenSocket* socket = new MockStreamListenSocket(delegate_); | 104 net::StreamListenSocket* socket = new MockStreamListenSocket(delegate_); |
| 98 | 105 |
| 99 delegate_->DidAccept(NULL, make_scoped_ptr(socket)); | 106 delegate_->DidAccept(NULL, make_scoped_ptr(socket)); |
| 100 ASSERT_TRUE(auth_handler_posix_->HasActiveSocketForTesting(socket)); | 107 ASSERT_TRUE(auth_handler_posix_->HasActiveSocketForTesting(socket)); |
| 101 | 108 |
| 102 delegate_->DidClose(socket); | 109 delegate_->DidClose(socket); |
| 103 ASSERT_FALSE(auth_handler_posix_->HasActiveSocketForTesting(socket)); | 110 ASSERT_FALSE(auth_handler_posix_->HasActiveSocketForTesting(socket)); |
| 104 } | 111 } |
| 105 | 112 |
| 106 TEST_F(GnubbyAuthHandlerPosixTest, DidRead) { | 113 TEST_F(GnubbyAuthHandlerPosixTest, DidRead) { |
| 107 EXPECT_CALL(client_stub_, DeliverHostMessage(_)); | 114 EXPECT_CALL(client_stub_, DeliverHostMessage(_)); |
| 108 | 115 |
| 109 net::StreamListenSocket* socket = new MockStreamListenSocket(delegate_); | 116 net::StreamListenSocket* socket = new MockStreamListenSocket(delegate_); |
| 110 | 117 |
| 111 delegate_->DidAccept(NULL, make_scoped_ptr(socket)); | 118 delegate_->DidAccept(NULL, make_scoped_ptr(socket)); |
| 112 delegate_->DidRead(socket, reinterpret_cast<const char*>(request_data), | 119 delegate_->DidRead(socket, |
| 120 reinterpret_cast<const char*>(request_data), |
| 113 sizeof(request_data)); | 121 sizeof(request_data)); |
| 114 } | 122 } |
| 115 | 123 |
| 116 TEST_F(GnubbyAuthHandlerPosixTest, DidReadByteByByte) { | 124 TEST_F(GnubbyAuthHandlerPosixTest, DidReadByteByByte) { |
| 117 EXPECT_CALL(client_stub_, DeliverHostMessage(_)); | 125 EXPECT_CALL(client_stub_, DeliverHostMessage(_)); |
| 118 | 126 |
| 119 net::StreamListenSocket* socket = new MockStreamListenSocket(delegate_); | 127 net::StreamListenSocket* socket = new MockStreamListenSocket(delegate_); |
| 120 | 128 |
| 121 delegate_->DidAccept(NULL, make_scoped_ptr(socket)); | 129 delegate_->DidAccept(NULL, make_scoped_ptr(socket)); |
| 122 for (unsigned int i = 0; i < sizeof(request_data); ++i) { | 130 for (unsigned int i = 0; i < sizeof(request_data); ++i) { |
| 123 delegate_->DidRead(socket, | 131 delegate_->DidRead( |
| 124 reinterpret_cast<const char *>(request_data + i), 1); | 132 socket, reinterpret_cast<const char*>(request_data + i), 1); |
| 125 } | 133 } |
| 126 } | 134 } |
| 127 | 135 |
| 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) |
| 144 ->SetTimerForTesting(scoped_ptr<base::Timer>(mock_timer)); |
| 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 |
| 128 } // namespace remoting | 165 } // namespace remoting |
| OLD | NEW |