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 "net/url_request/url_request_context_getter.h" | |
13 #include "net/url_request/url_request_test_util.h" | |
12 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
14 | 16 |
15 using ::testing::_; | 17 using ::testing::_; |
16 using ::testing::DeleteArg; | 18 using ::testing::DeleteArg; |
17 using ::testing::DoAll; | 19 using ::testing::DoAll; |
18 using ::testing::Return; | 20 using ::testing::Return; |
19 | 21 |
20 namespace content { | 22 namespace content { |
21 | 23 |
22 class P2PSocketHostTcpTest : public testing::Test { | 24 class P2PSocketHostTcpTestBase : public testing::Test { |
23 protected: | 25 protected: |
26 explicit P2PSocketHostTcpTestBase(P2PSocketType type) | |
27 : socket_type_(type) {} | |
28 | |
24 virtual void SetUp() OVERRIDE { | 29 virtual void SetUp() OVERRIDE { |
25 EXPECT_CALL(sender_, Send( | 30 EXPECT_CALL(sender_, Send( |
26 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID)))) | 31 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID)))) |
27 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 32 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
28 | 33 |
29 socket_host_.reset(new P2PSocketHostTcp(&sender_, 0)); | 34 if (socket_type_ == P2P_SOCKET_TCP_CLIENT) |
Sergey Ulanov
2013/06/20 01:50:02
add {} for multi-line if.
Mallinath (Gone from Chromium)
2013/06/20 05:24:15
Done.
| |
35 socket_host_.reset(new P2PSocketHostTcp( | |
36 &sender_, 0, NULL)); | |
Sergey Ulanov
2013/06/20 01:50:02
this can fit on the previous line.
Mallinath (Gone from Chromium)
2013/06/20 05:24:15
Done.
| |
37 else | |
38 socket_host_.reset(new P2PSocketHostStunTcp( | |
39 &sender_, 0, NULL)); | |
30 socket_ = new FakeSocket(&sent_data_); | 40 socket_ = new FakeSocket(&sent_data_); |
31 socket_->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1)); | 41 socket_->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1)); |
32 socket_host_->socket_.reset(socket_); | 42 socket_host_->socket_.reset(socket_); |
33 | 43 |
34 dest_ = ParseAddress(kTestIpAddress1, kTestPort1); | 44 dest_ = ParseAddress(kTestIpAddress1, kTestPort1); |
35 | 45 |
36 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); | 46 local_address_ = ParseAddress(kTestLocalIpAddress, kTestPort1); |
37 | 47 |
38 socket_host_->remote_address_ = dest_; | 48 socket_host_->remote_address_ = dest_; |
39 socket_host_->state_ = P2PSocketHost::STATE_CONNECTING; | 49 socket_host_->state_ = P2PSocketHost::STATE_CONNECTING; |
40 socket_host_->OnConnected(net::OK); | 50 socket_host_->OnConnected(net::OK); |
41 } | 51 } |
42 | 52 |
43 std::string IntToSize(int size) { | 53 std::string IntToSize(int size) { |
44 std::string result; | 54 std::string result; |
45 uint16 size16 = base::HostToNet16(size); | 55 uint16 size16 = base::HostToNet16(size); |
46 result.resize(sizeof(size16)); | 56 result.resize(sizeof(size16)); |
47 memcpy(&result[0], &size16, sizeof(size16)); | 57 memcpy(&result[0], &size16, sizeof(size16)); |
48 return result; | 58 return result; |
49 } | 59 } |
50 | 60 |
51 std::string sent_data_; | 61 std::string sent_data_; |
52 FakeSocket* socket_; // Owned by |socket_host_|. | 62 FakeSocket* socket_; // Owned by |socket_host_|. |
53 scoped_ptr<P2PSocketHostTcp> socket_host_; | 63 scoped_ptr<P2PSocketHostTcpBase> socket_host_; |
54 MockIPCSender sender_; | 64 MockIPCSender sender_; |
55 | 65 |
56 net::IPEndPoint local_address_; | 66 net::IPEndPoint local_address_; |
57 | 67 |
58 net::IPEndPoint dest_; | 68 net::IPEndPoint dest_; |
59 net::IPEndPoint dest2_; | 69 net::IPEndPoint dest2_; |
70 | |
71 P2PSocketType socket_type_; | |
60 }; | 72 }; |
61 | 73 |
62 class P2PSocketHostStunTcpTest : public testing::Test { | 74 class P2PSocketHostTcpTest : public P2PSocketHostTcpTestBase { |
63 protected: | 75 protected: |
64 virtual void SetUp() OVERRIDE { | 76 P2PSocketHostTcpTest() : P2PSocketHostTcpTestBase(P2P_SOCKET_TCP_CLIENT) {} |
65 EXPECT_CALL(sender_, Send( | 77 }; |
66 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID)))) | |
67 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | |
68 | 78 |
69 socket_host_.reset(new P2PSocketHostStunTcp(&sender_, 0)); | 79 class P2PSocketHostStunTcpTest : public P2PSocketHostTcpTestBase { |
70 socket_ = new FakeSocket(&sent_data_); | 80 protected: |
71 socket_->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1)); | 81 P2PSocketHostStunTcpTest() |
72 socket_host_->socket_.reset(socket_); | 82 : P2PSocketHostTcpTestBase(P2P_SOCKET_STUN_TCP_CLIENT) {} |
Sergey Ulanov
2013/06/20 01:50:02
nit: move } to the next line.
Mallinath (Gone from Chromium)
2013/06/20 05:24:15
Done.
| |
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 | |
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 |