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 "remoting/jingle_glue/chromium_socket_factory.h" | 5 #include "remoting/jingle_glue/chromium_socket_factory.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 19 matching lines...) Expand all Loading... | |
30 void OnPacket(talk_base::AsyncPacketSocket* socket, | 30 void OnPacket(talk_base::AsyncPacketSocket* socket, |
31 const char* data, size_t size, | 31 const char* data, size_t size, |
32 const talk_base::SocketAddress& address, | 32 const talk_base::SocketAddress& address, |
33 const talk_base::PacketTime& packet_time) { | 33 const talk_base::PacketTime& packet_time) { |
34 EXPECT_EQ(socket, socket_.get()); | 34 EXPECT_EQ(socket, socket_.get()); |
35 last_packet_.assign(data, data + size); | 35 last_packet_.assign(data, data + size); |
36 last_address_ = address; | 36 last_address_ = address; |
37 run_loop_.Quit(); | 37 run_loop_.Quit(); |
38 } | 38 } |
39 | 39 |
40 void VerifyCanSendAndReceive(talk_base::AsyncPacketSocket* sender) { | |
41 // UDP packets may be lost, so we have to retry sending it more than once. | |
42 const int kMaxAttempts = 3; | |
43 const base::TimeDelta kAttemptPeriod = base::TimeDelta::FromSeconds(1); | |
44 std::string test_packet("TEST PACKET"); | |
45 int attempts = 0; | |
46 talk_base::PacketOptions options; | |
47 while (last_packet_.empty() && attempts++ < kMaxAttempts) { | |
48 sender->SendTo(test_packet.data(), test_packet.size(), | |
49 socket_->GetLocalAddress(), options); | |
50 message_loop_.PostDelayedTask(FROM_HERE, run_loop_.QuitClosure(), | |
51 kAttemptPeriod); | |
52 run_loop_.Run(); | |
53 } | |
54 EXPECT_EQ(test_packet, last_packet_); | |
55 EXPECT_EQ(sender->GetLocalAddress(), last_address_); | |
56 } | |
57 | |
40 protected: | 58 protected: |
41 base::MessageLoopForIO message_loop_; | 59 base::MessageLoopForIO message_loop_; |
42 base::RunLoop run_loop_; | 60 base::RunLoop run_loop_; |
43 | 61 |
44 scoped_ptr<talk_base::PacketSocketFactory> socket_factory_; | 62 scoped_ptr<talk_base::PacketSocketFactory> socket_factory_; |
45 scoped_ptr<talk_base::AsyncPacketSocket> socket_; | 63 scoped_ptr<talk_base::AsyncPacketSocket> socket_; |
46 | 64 |
47 std::string last_packet_; | 65 std::string last_packet_; |
48 talk_base::SocketAddress last_address_; | 66 talk_base::SocketAddress last_address_; |
49 }; | 67 }; |
50 | 68 |
51 TEST_F(ChromiumSocketFactoryTest, SendAndReceive) { | 69 TEST_F(ChromiumSocketFactoryTest, SendAndReceive) { |
52 // UDP packets may be lost, so we have to retry sending it more than once. | 70 scoped_ptr<talk_base::AsyncPacketSocket> sending_socket( |
53 const int kMaxAttempts = 3; | 71 socket_factory_->CreateUdpSocket( |
54 const base::TimeDelta kAttemptPeriod = base::TimeDelta::FromSeconds(1); | 72 talk_base::SocketAddress("127.0.0.1", 0), 0, 0)); |
55 | |
56 scoped_ptr<talk_base::AsyncPacketSocket> sending_socket; | |
57 talk_base::SocketAddress address; | |
58 | |
59 sending_socket.reset(socket_factory_->CreateUdpSocket( | |
60 talk_base::SocketAddress("127.0.0.1", 0), 0, 0)); | |
61 ASSERT_TRUE(sending_socket.get() != NULL); | 73 ASSERT_TRUE(sending_socket.get() != NULL); |
62 EXPECT_EQ(sending_socket->GetState(), | 74 EXPECT_EQ(sending_socket->GetState(), |
63 talk_base::AsyncPacketSocket::STATE_BOUND); | 75 talk_base::AsyncPacketSocket::STATE_BOUND); |
64 address = sending_socket->GetLocalAddress(); | |
65 | 76 |
66 std::string test_packet("TEST PACKET"); | 77 VerifyCanSendAndReceive(sending_socket.get()); |
67 int attempts = 0; | |
68 talk_base::PacketOptions options; | |
69 while (last_packet_.empty() && attempts++ < kMaxAttempts) { | |
70 sending_socket->SendTo(test_packet.data(), test_packet.size(), | |
71 socket_->GetLocalAddress(), options); | |
72 message_loop_.PostDelayedTask(FROM_HERE, run_loop_.QuitClosure(), | |
73 kAttemptPeriod); | |
74 run_loop_.Run(); | |
75 } | |
76 EXPECT_EQ(test_packet, last_packet_); | |
77 EXPECT_EQ(address, last_address_); | |
78 } | 78 } |
79 | 79 |
80 TEST_F(ChromiumSocketFactoryTest, SetOptions) { | 80 TEST_F(ChromiumSocketFactoryTest, SetOptions) { |
81 EXPECT_EQ(0, socket_->SetOption(talk_base::Socket::OPT_SNDBUF, 4096)); | 81 EXPECT_EQ(0, socket_->SetOption(talk_base::Socket::OPT_SNDBUF, 4096)); |
82 EXPECT_EQ(0, socket_->SetOption(talk_base::Socket::OPT_RCVBUF, 4096)); | 82 EXPECT_EQ(0, socket_->SetOption(talk_base::Socket::OPT_RCVBUF, 4096)); |
83 } | 83 } |
84 | 84 |
85 TEST_F(ChromiumSocketFactoryTest, PortRange) { | 85 TEST_F(ChromiumSocketFactoryTest, PortRange) { |
86 const int kMinPort = 12400; | 86 const int kMinPort = 12400; |
87 const int kMaxPort = 12410; | 87 const int kMaxPort = 12410; |
88 socket_.reset(socket_factory_->CreateUdpSocket( | 88 socket_.reset(socket_factory_->CreateUdpSocket( |
89 talk_base::SocketAddress("127.0.0.1", 0), kMaxPort, kMaxPort)); | 89 talk_base::SocketAddress("127.0.0.1", 0), kMaxPort, kMaxPort)); |
90 ASSERT_TRUE(socket_.get() != NULL); | 90 ASSERT_TRUE(socket_.get() != NULL); |
91 EXPECT_EQ(socket_->GetState(), talk_base::AsyncPacketSocket::STATE_BOUND); | 91 EXPECT_EQ(socket_->GetState(), talk_base::AsyncPacketSocket::STATE_BOUND); |
92 EXPECT_GE(socket_->GetLocalAddress().port(), kMinPort); | 92 EXPECT_GE(socket_->GetLocalAddress().port(), kMinPort); |
93 EXPECT_LE(socket_->GetLocalAddress().port(), kMaxPort); | 93 EXPECT_LE(socket_->GetLocalAddress().port(), kMaxPort); |
94 } | 94 } |
95 | 95 |
96 TEST_F(ChromiumSocketFactoryTest, TransientError) { | |
97 scoped_ptr<talk_base::AsyncPacketSocket> sending_socket( | |
98 socket_factory_->CreateUdpSocket( | |
99 talk_base::SocketAddress("127.0.0.1", 0), 0, 0)); | |
100 std::string test_packet("TEST"); | |
101 | |
102 // Try sending a packet to an IPv6 address from a socket that's bound to an | |
103 // IPv4 address. This send is expected to fail internally. | |
Wez
2014/06/17 03:27:54
Fail internally? You mean fail without reporting a
Sergey Ulanov
2014/06/17 22:32:04
Done.
| |
104 sending_socket->SendTo(test_packet.data(), test_packet.size(), | |
105 talk_base::SocketAddress("::1", 0), | |
106 talk_base::PacketOptions()); | |
107 | |
108 // Verify that socket is still usable. | |
109 VerifyCanSendAndReceive(sending_socket.get()); | |
110 } | |
111 | |
96 } // namespace remoting | 112 } // namespace remoting |
OLD | NEW |