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"
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"
23 class MockPacketReceiver
{
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();
35 std::string
packet() const { return packet_
; }
36 PacketReceiverCallbackWithStatus
packet_receiver() {
37 return base::Bind(&MockPacketReceiver::ReceivedPacket
,
38 base::Unretained(this));
43 base::Closure packet_callback_
;
45 DISALLOW_COPY_AND_ASSIGN(MockPacketReceiver
);
48 void SendPacket(UdpTransport
* transport
, Packet packet
) {
50 transport
->SendPacket(new base::RefCountedData
<Packet
>(packet
), cb
);
53 static void UpdateCastTransportStatus(CastTransportStatus status
) {
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(),
70 base::Bind(&UpdateCastTransportStatus
));
71 UdpTransport
recv_transport(NULL
,
72 message_loop
.task_runner(),
74 net::IPEndPoint(empty_addr_number
, 0),
76 base::Bind(&UpdateCastTransportStatus
));
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());
92 send_transport
.SendPacket(new base::RefCountedData
<Packet
>(packet
), cb
);
95 std::equal(packet
.begin(), packet
.end(), receiver1
.packet().begin()));
97 std::equal(packet
.begin(), packet
.end(), receiver2
.packet().begin()));