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) { |
30 P2P_SOCKET_TCP_CLIENT)); | 34 socket_host_.reset(new P2PSocketHostTcp( |
| 35 &sender_, 0, P2P_SOCKET_TCP_CLIENT, NULL)); |
| 36 } else { |
| 37 socket_host_.reset(new P2PSocketHostStunTcp( |
| 38 &sender_, 0, P2P_SOCKET_STUN_TCP_CLIENT, NULL)); |
| 39 } |
| 40 |
31 socket_ = new FakeSocket(&sent_data_); | 41 socket_ = new FakeSocket(&sent_data_); |
32 socket_->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1)); | 42 socket_->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1)); |
33 socket_host_->socket_.reset(socket_); | 43 socket_host_->socket_.reset(socket_); |
34 | 44 |
35 dest_ = ParseAddress(kTestIpAddress1, kTestPort1); | 45 dest_ = ParseAddress(kTestIpAddress1, kTestPort1); |
36 | 46 |
37 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); | 47 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); |
38 | 48 |
39 socket_host_->remote_address_ = dest_; | 49 socket_host_->remote_address_ = dest_; |
40 socket_host_->state_ = P2PSocketHost::STATE_CONNECTING; | 50 socket_host_->state_ = P2PSocketHost::STATE_CONNECTING; |
41 socket_host_->OnConnected(net::OK); | 51 socket_host_->OnConnected(net::OK); |
42 } | 52 } |
43 | 53 |
44 std::string IntToSize(int size) { | 54 std::string IntToSize(int size) { |
45 std::string result; | 55 std::string result; |
46 uint16 size16 = base::HostToNet16(size); | 56 uint16 size16 = base::HostToNet16(size); |
47 result.resize(sizeof(size16)); | 57 result.resize(sizeof(size16)); |
48 memcpy(&result[0], &size16, sizeof(size16)); | 58 memcpy(&result[0], &size16, sizeof(size16)); |
49 return result; | 59 return result; |
50 } | 60 } |
51 | 61 |
52 std::string sent_data_; | 62 std::string sent_data_; |
53 FakeSocket* socket_; // Owned by |socket_host_|. | 63 FakeSocket* socket_; // Owned by |socket_host_|. |
54 scoped_ptr<P2PSocketHostTcp> socket_host_; | 64 scoped_ptr<P2PSocketHostTcpBase> socket_host_; |
55 MockIPCSender sender_; | 65 MockIPCSender sender_; |
56 | 66 |
57 net::IPEndPoint local_address_; | 67 net::IPEndPoint local_address_; |
58 | 68 |
59 net::IPEndPoint dest_; | 69 net::IPEndPoint dest_; |
60 net::IPEndPoint dest2_; | 70 net::IPEndPoint dest2_; |
| 71 |
| 72 P2PSocketType socket_type_; |
61 }; | 73 }; |
62 | 74 |
63 class P2PSocketHostStunTcpTest : public testing::Test { | 75 class P2PSocketHostTcpTest : public P2PSocketHostTcpTestBase { |
64 protected: | 76 protected: |
65 virtual void SetUp() OVERRIDE { | 77 P2PSocketHostTcpTest() : P2PSocketHostTcpTestBase(P2P_SOCKET_TCP_CLIENT) { } |
66 EXPECT_CALL(sender_, Send( | 78 }; |
67 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID)))) | |
68 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | |
69 | 79 |
70 socket_host_.reset(new P2PSocketHostStunTcp(&sender_, 0, | 80 class P2PSocketHostStunTcpTest : public P2PSocketHostTcpTestBase { |
71 P2P_SOCKET_STUN_TCP_CLIENT)); | 81 protected: |
72 socket_ = new FakeSocket(&sent_data_); | 82 P2PSocketHostStunTcpTest() |
73 socket_->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1)); | 83 : P2PSocketHostTcpTestBase(P2P_SOCKET_STUN_TCP_CLIENT) { |
74 socket_host_->socket_.reset(socket_); | |
75 | |
76 dest_ = ParseAddress(kTestIpAddress1, kTestPort1); | |
77 | |
78 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); | |
79 | |
80 socket_host_->remote_address_ = dest_; | |
81 socket_host_->state_ = P2PSocketHost::STATE_CONNECTING; | |
82 socket_host_->OnConnected(net::OK); | |
83 } | 84 } |
84 | |
85 std::string IntToSize(int size) { | |
86 std::string result; | |
87 uint16 size16 = base::HostToNet16(size); | |
88 result.resize(sizeof(size16)); | |
89 memcpy(&result[0], &size16, sizeof(size16)); | |
90 return result; | |
91 } | |
92 | |
93 std::string sent_data_; | |
94 FakeSocket* socket_; // Owned by |socket_host_|. | |
95 scoped_ptr<P2PSocketHostStunTcp> socket_host_; | |
96 MockIPCSender sender_; | |
97 | |
98 net::IPEndPoint local_address_; | |
99 | |
100 net::IPEndPoint dest_; | |
101 net::IPEndPoint dest2_; | |
102 }; | 85 }; |
103 | 86 |
104 // Verify that we can send STUN message and that they are formatted | 87 // Verify that we can send STUN message and that they are formatted |
105 // properly. | 88 // properly. |
106 TEST_F(P2PSocketHostTcpTest, SendStunNoAuth) { | 89 TEST_F(P2PSocketHostTcpTest, SendStunNoAuth) { |
107 EXPECT_CALL(sender_, Send( | 90 EXPECT_CALL(sender_, Send( |
108 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) | 91 MatchMessage(static_cast<uint32>(P2PMsg_OnSendComplete::ID)))) |
109 .Times(3) | 92 .Times(3) |
110 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); | 93 .WillRepeatedly(DoAll(DeleteArg<0>(), Return(true))); |
111 | 94 |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 message_loop.RunUntilIdle(); | 344 message_loop.RunUntilIdle(); |
362 | 345 |
363 std::string expected_data; | 346 std::string expected_data; |
364 expected_data.append(packet1.begin(), packet1.end()); | 347 expected_data.append(packet1.begin(), packet1.end()); |
365 expected_data.append(packet2.begin(), packet2.end()); | 348 expected_data.append(packet2.begin(), packet2.end()); |
366 | 349 |
367 EXPECT_EQ(expected_data, sent_data_); | 350 EXPECT_EQ(expected_data, sent_data_); |
368 } | 351 } |
369 | 352 |
370 } // namespace content | 353 } // namespace content |
OLD | NEW |