OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "remoting/jingle_glue/chromium_socket_factory.h" | |
6 | |
7 #include "base/message_loop.h" | |
8 #include "base/run_loop.h" | |
9 #include "testing/gmock/include/gmock/gmock.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 #include "third_party/libjingle/source/talk/base/asyncpacketsocket.h" | |
12 #include "third_party/libjingle/source/talk/base/socketaddress.h" | |
13 | |
14 namespace remoting { | |
15 | |
16 class ChromiumSocketFactoryTest : public testing::Test, | |
17 public sigslot::has_slots<> { | |
18 public: | |
19 virtual void SetUp() OVERRIDE { | |
20 socket_factory_.reset(new ChromiumPacketSocketFactory()); | |
21 | |
22 socket_.reset(socket_factory_->CreateUdpSocket( | |
23 talk_base::SocketAddress("127.0.0.1", 0), 0, 0)); | |
24 ASSERT_TRUE(socket_.get() != NULL); | |
25 EXPECT_EQ(socket_->GetState(), talk_base::AsyncPacketSocket::STATE_BOUND); | |
26 socket_->SignalReadPacket.connect( | |
27 this, &ChromiumSocketFactoryTest::OnPacket); | |
28 } | |
29 | |
30 void OnPacket(talk_base::AsyncPacketSocket* socket, | |
31 const char* data, size_t size, | |
32 const talk_base::SocketAddress& address) { | |
33 EXPECT_EQ(socket, socket_.get()); | |
34 last_packet_.assign(data, data + size); | |
35 last_address_ = address; | |
36 run_loop_.Quit(); | |
37 } | |
38 | |
39 protected: | |
40 MessageLoopForIO message_loop_; | |
41 base::RunLoop run_loop_; | |
42 | |
43 scoped_ptr<talk_base::PacketSocketFactory> socket_factory_; | |
44 scoped_ptr<talk_base::AsyncPacketSocket> socket_; | |
45 | |
46 std::string last_packet_; | |
47 talk_base::SocketAddress last_address_; | |
48 }; | |
49 | |
50 TEST_F(ChromiumSocketFactoryTest, SendAndReceive) { | |
51 // UDP packets may be lost, so we have to retry sending it more than once. | |
52 const int kMaxAttempts = 3; | |
53 const base::TimeDelta kAttemptPeriod = base::TimeDelta::FromSeconds(1); | |
54 | |
55 scoped_ptr<talk_base::AsyncPacketSocket> sending_socket; | |
56 talk_base::SocketAddress address; | |
57 | |
58 sending_socket.reset(socket_factory_->CreateUdpSocket( | |
59 talk_base::SocketAddress("127.0.0.1", 0), 0, 0)); | |
60 ASSERT_TRUE(sending_socket.get() != NULL); | |
61 EXPECT_EQ(sending_socket->GetState(), | |
62 talk_base::AsyncPacketSocket::STATE_BOUND); | |
63 address = sending_socket->GetLocalAddress(); | |
64 | |
65 std::string test_packet("TEST PACKET"); | |
66 int attempts = 0; | |
67 while (last_packet_.empty() && attempts++ < kMaxAttempts) { | |
68 sending_socket->SendTo(test_packet.data(), test_packet.size(), | |
69 socket_->GetLocalAddress()); | |
70 message_loop_.PostDelayedTask(FROM_HERE, run_loop_.QuitClosure(), | |
71 kAttemptPeriod); | |
Wez
2012/07/19 00:40:20
nit: Indentation
Sergey Ulanov
2012/07/19 20:16:39
Done.
| |
72 run_loop_.Run(); | |
73 } | |
74 EXPECT_EQ(test_packet, last_packet_); | |
75 EXPECT_EQ(address, last_address_); | |
76 } | |
77 | |
78 TEST_F(ChromiumSocketFactoryTest, SetOptions) { | |
79 EXPECT_EQ(0, socket_->SetOption(talk_base::Socket::OPT_SNDBUF, 4096)); | |
Wez
2012/07/19 00:40:20
nit: Indentation
Sergey Ulanov
2012/07/19 20:16:39
Done.
| |
80 EXPECT_EQ(0, socket_->SetOption(talk_base::Socket::OPT_RCVBUF, 4096)); | |
81 } | |
82 | |
83 TEST_F(ChromiumSocketFactoryTest, PortRange) { | |
84 const int kMinPort = 12400; | |
85 const int kMaxPort = 12410; | |
86 socket_.reset(socket_factory_->CreateUdpSocket( | |
87 talk_base::SocketAddress("127.0.0.1", 0), kMaxPort, kMaxPort)); | |
88 ASSERT_TRUE(socket_.get() != NULL); | |
89 EXPECT_EQ(socket_->GetState(), talk_base::AsyncPacketSocket::STATE_BOUND); | |
90 EXPECT_GE(socket_->GetLocalAddress().port(), kMinPort); | |
91 EXPECT_LE(socket_->GetLocalAddress().port(), kMaxPort); | |
92 } | |
93 | |
94 } // namespace remoting | |
OLD | NEW |