Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / cast / net / udp_transport_unittest.cc
blobb0b32a4e1fd7e527497aacd435953219478f4c27
1 // Copyright 2014 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.
5 #include "media/cast/net/udp_transport.h"
7 #include <algorithm>
8 #include <string>
9 #include <vector>
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h"
15 #include "media/cast/net/cast_transport_config.h"
16 #include "media/cast/test/utility/net_utility.h"
17 #include "net/base/net_util.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 namespace media {
21 namespace cast {
23 class MockPacketReceiver {
24 public:
25 MockPacketReceiver(const base::Closure& callback)
26 : packet_callback_(callback) {}
28 bool ReceivedPacket(scoped_ptr<Packet> packet) {
29 packet_ = std::string(packet->size(), '\0');
30 std::copy(packet->begin(), packet->end(), packet_.begin());
31 packet_callback_.Run();
32 return true;
35 std::string packet() const { return packet_; }
36 PacketReceiverCallbackWithStatus packet_receiver() {
37 return base::Bind(&MockPacketReceiver::ReceivedPacket,
38 base::Unretained(this));
41 private:
42 std::string packet_;
43 base::Closure packet_callback_;
45 DISALLOW_COPY_AND_ASSIGN(MockPacketReceiver);
48 void SendPacket(UdpTransport* transport, Packet packet) {
49 base::Closure cb;
50 transport->SendPacket(new base::RefCountedData<Packet>(packet), cb);
53 static void UpdateCastTransportStatus(CastTransportStatus status) {
54 NOTREACHED();
57 TEST(UdpTransport, SendAndReceive) {
58 base::MessageLoopForIO message_loop;
60 net::IPEndPoint free_local_port1 = test::GetFreeLocalPort();
61 net::IPEndPoint free_local_port2 = test::GetFreeLocalPort();
62 net::IPAddressNumber empty_addr_number;
63 net::ParseIPLiteralToNumber("0.0.0.0", &empty_addr_number);
65 UdpTransport send_transport(NULL,
66 message_loop.task_runner(),
67 free_local_port1,
68 free_local_port2,
69 65536,
70 base::Bind(&UpdateCastTransportStatus));
71 UdpTransport recv_transport(NULL,
72 message_loop.task_runner(),
73 free_local_port2,
74 net::IPEndPoint(empty_addr_number, 0),
75 65536,
76 base::Bind(&UpdateCastTransportStatus));
78 Packet packet;
79 packet.push_back('t');
80 packet.push_back('e');
81 packet.push_back('s');
82 packet.push_back('t');
84 base::RunLoop run_loop;
85 MockPacketReceiver receiver1(run_loop.QuitClosure());
86 MockPacketReceiver receiver2(
87 base::Bind(&SendPacket, &recv_transport, packet));
88 send_transport.StartReceiving(receiver1.packet_receiver());
89 recv_transport.StartReceiving(receiver2.packet_receiver());
91 base::Closure cb;
92 send_transport.SendPacket(new base::RefCountedData<Packet>(packet), cb);
93 run_loop.Run();
94 EXPECT_TRUE(
95 std::equal(packet.begin(), packet.end(), receiver1.packet().begin()));
96 EXPECT_TRUE(
97 std::equal(packet.begin(), packet.end(), receiver2.packet().begin()));
100 } // namespace cast
101 } // namespace media