| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/renderer_host/p2p/socket_host_tcp.h" | 5 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" |
| 6 | 6 |
| 7 #include <deque> | 7 #include <deque> |
| 8 | 8 |
| 9 #include "base/sys_byteorder.h" | 9 #include "base/sys_byteorder.h" |
| 10 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h" | 10 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h" |
| 11 #include "net/socket/stream_socket.h" | 11 #include "net/socket/stream_socket.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 using ::testing::_; | 15 using ::testing::_; |
| 16 using ::testing::DeleteArg; | 16 using ::testing::DeleteArg; |
| 17 using ::testing::DoAll; | 17 using ::testing::DoAll; |
| 18 using ::testing::Return; | 18 using ::testing::Return; |
| 19 | 19 |
| 20 namespace content { | 20 namespace content { |
| 21 | 21 |
| 22 class P2PSocketHostTcpTest : public testing::Test { | 22 class P2PSocketHostTcpTestBase : public testing::Test { |
| 23 protected: | 23 protected: |
| 24 explicit P2PSocketHostTcpTestBase(P2PSocketType type) |
| 25 : socket_type_(type) { |
| 26 } |
| 27 |
| 24 virtual void SetUp() OVERRIDE { | 28 virtual void SetUp() OVERRIDE { |
| 25 EXPECT_CALL(sender_, Send( | 29 EXPECT_CALL(sender_, Send( |
| 26 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID)))) | 30 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID)))) |
| 27 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 31 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 28 | 32 |
| 29 socket_host_.reset(new P2PSocketHostTcp(&sender_, 0)); | 33 if (socket_type_ == P2P_SOCKET_TCP_CLIENT) { |
| 34 socket_host_.reset(new P2PSocketHostTcp(&sender_, 0, NULL)); |
| 35 } else { |
| 36 socket_host_.reset(new P2PSocketHostStunTcp(&sender_, 0, NULL)); |
| 37 } |
| 38 |
| 30 socket_ = new FakeSocket(&sent_data_); | 39 socket_ = new FakeSocket(&sent_data_); |
| 31 socket_->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1)); | 40 socket_->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1)); |
| 32 socket_host_->socket_.reset(socket_); | 41 socket_host_->socket_.reset(socket_); |
| 33 | 42 |
| 34 dest_ = ParseAddress(kTestIpAddress1, kTestPort1); | 43 dest_ = ParseAddress(kTestIpAddress1, kTestPort1); |
| 35 | 44 |
| 36 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); | 45 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); |
| 37 | 46 |
| 38 socket_host_->remote_address_ = dest_; | 47 socket_host_->remote_address_ = dest_; |
| 39 socket_host_->state_ = P2PSocketHost::STATE_CONNECTING; | 48 socket_host_->state_ = P2PSocketHost::STATE_CONNECTING; |
| 40 socket_host_->OnConnected(net::OK); | 49 socket_host_->OnConnected(net::OK); |
| 41 } | 50 } |
| 42 | 51 |
| 43 std::string IntToSize(int size) { | 52 std::string IntToSize(int size) { |
| 44 std::string result; | 53 std::string result; |
| 45 uint16 size16 = base::HostToNet16(size); | 54 uint16 size16 = base::HostToNet16(size); |
| 46 result.resize(sizeof(size16)); | 55 result.resize(sizeof(size16)); |
| 47 memcpy(&result[0], &size16, sizeof(size16)); | 56 memcpy(&result[0], &size16, sizeof(size16)); |
| 48 return result; | 57 return result; |
| 49 } | 58 } |
| 50 | 59 |
| 51 std::string sent_data_; | 60 std::string sent_data_; |
| 52 FakeSocket* socket_; // Owned by |socket_host_|. | 61 FakeSocket* socket_; // Owned by |socket_host_|. |
| 53 scoped_ptr<P2PSocketHostTcp> socket_host_; | 62 scoped_ptr<P2PSocketHostTcpBase> socket_host_; |
| 54 MockIPCSender sender_; | 63 MockIPCSender sender_; |
| 55 | 64 |
| 56 net::IPEndPoint local_address_; | 65 net::IPEndPoint local_address_; |
| 57 | 66 |
| 58 net::IPEndPoint dest_; | 67 net::IPEndPoint dest_; |
| 59 net::IPEndPoint dest2_; | 68 net::IPEndPoint dest2_; |
| 69 |
| 70 P2PSocketType socket_type_; |
| 60 }; | 71 }; |
| 61 | 72 |
| 62 class P2PSocketHostStunTcpTest : public testing::Test { | 73 class P2PSocketHostTcpTest : public P2PSocketHostTcpTestBase { |
| 63 protected: | 74 protected: |
| 64 virtual void SetUp() OVERRIDE { | 75 P2PSocketHostTcpTest() : P2PSocketHostTcpTestBase(P2P_SOCKET_TCP_CLIENT) {} |
| 65 EXPECT_CALL(sender_, Send( | 76 }; |
| 66 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID)))) | |
| 67 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | |
| 68 | 77 |
| 69 socket_host_.reset(new P2PSocketHostStunTcp(&sender_, 0)); | 78 class P2PSocketHostStunTcpTest : public P2PSocketHostTcpTestBase { |
| 70 socket_ = new FakeSocket(&sent_data_); | 79 protected: |
| 71 socket_->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1)); | 80 P2PSocketHostStunTcpTest() |
| 72 socket_host_->socket_.reset(socket_); | 81 : P2PSocketHostTcpTestBase(P2P_SOCKET_STUN_TCP_CLIENT) { |
| 73 | |
| 74 dest_ = ParseAddress(kTestIpAddress1, kTestPort1); | |
| 75 | |
| 76 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); | |
| 77 | |
| 78 socket_host_->remote_address_ = dest_; | |
| 79 socket_host_->state_ = P2PSocketHost::STATE_CONNECTING; | |
| 80 socket_host_->OnConnected(net::OK); | |
| 81 } | 82 } |
| 82 | |
| 83 std::string IntToSize(int size) { | |
| 84 std::string result; | |
| 85 uint16 size16 = base::HostToNet16(size); | |
| 86 result.resize(sizeof(size16)); | |
| 87 memcpy(&result[0], &size16, sizeof(size16)); | |
| 88 return result; | |
| 89 } | |
| 90 | |
| 91 std::string sent_data_; | |
| 92 FakeSocket* socket_; // Owned by |socket_host_|. | |
| 93 scoped_ptr<P2PSocketHostStunTcp> socket_host_; | |
| 94 MockIPCSender sender_; | |
| 95 | |
| 96 net::IPEndPoint local_address_; | |
| 97 | |
| 98 net::IPEndPoint dest_; | |
| 99 net::IPEndPoint dest2_; | |
| 100 }; | 83 }; |
| 101 | 84 |
| 102 // Verify that we can send STUN message and that they are formatted | 85 // Verify that we can send STUN message and that they are formatted |
| 103 // properly. | 86 // properly. |
| 104 TEST_F(P2PSocketHostTcpTest, SendStunNoAuth) { | 87 TEST_F(P2PSocketHostTcpTest, SendStunNoAuth) { |
| 105 EXPECT_CALL(sender_, Send( | 88 EXPECT_CALL(sender_, Send( |
| 106 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 89 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
| 107 .Times(3) | 90 .Times(3) |
| 108 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 91 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
| 109 | 92 |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 message_loop.RunUntilIdle(); | 342 message_loop.RunUntilIdle(); |
| 360 | 343 |
| 361 std::string expected_data; | 344 std::string expected_data; |
| 362 expected_data.append(packet1.begin(), packet1.end()); | 345 expected_data.append(packet1.begin(), packet1.end()); |
| 363 expected_data.append(packet2.begin(), packet2.end()); | 346 expected_data.append(packet2.begin(), packet2.end()); |
| 364 | 347 |
| 365 EXPECT_EQ(expected_data, sent_data_); | 348 EXPECT_EQ(expected_data, sent_data_); |
| 366 } | 349 } |
| 367 | 350 |
| 368 } // namespace content | 351 } // namespace content |
| OLD | NEW |