OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 * | 9 * |
10 * This file includes unit tests for the RtcpPacket. | 10 * This file includes unit tests for the RtcpPacket. |
11 */ | 11 */ |
12 | 12 |
13 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h" | 16 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h" |
17 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/app.h" | |
18 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/bye.h" | |
19 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/receiver_report.h" | 17 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/receiver_report.h" |
20 #include "webrtc/test/rtcp_packet_parser.h" | |
21 | 18 |
22 using ::testing::ElementsAre; | |
23 | |
24 using webrtc::rtcp::App; | |
25 using webrtc::rtcp::Bye; | |
26 using webrtc::rtcp::RawPacket; | |
27 using webrtc::rtcp::ReceiverReport; | 19 using webrtc::rtcp::ReceiverReport; |
28 using webrtc::rtcp::ReportBlock; | 20 using webrtc::rtcp::ReportBlock; |
29 using webrtc::rtcp::SenderReport; | |
30 using webrtc::test::RtcpPacketParser; | |
31 | 21 |
32 namespace webrtc { | 22 namespace webrtc { |
33 | 23 |
34 const uint32_t kSenderSsrc = 0x12345678; | 24 const uint32_t kSenderSsrc = 0x12345678; |
35 const uint32_t kRemoteSsrc = 0x23456789; | |
36 | |
37 TEST(RtcpPacketTest, Sr) { | |
38 SenderReport sr; | |
39 sr.From(kSenderSsrc); | |
40 sr.WithNtpSec(0x11111111); | |
41 sr.WithNtpFrac(0x22222222); | |
42 sr.WithRtpTimestamp(0x33333333); | |
43 sr.WithPacketCount(0x44444444); | |
44 sr.WithOctetCount(0x55555555); | |
45 | |
46 rtc::scoped_ptr<RawPacket> packet(sr.Build()); | |
47 RtcpPacketParser parser; | |
48 parser.Parse(packet->Buffer(), packet->Length()); | |
49 | |
50 EXPECT_EQ(1, parser.sender_report()->num_packets()); | |
51 EXPECT_EQ(kSenderSsrc, parser.sender_report()->Ssrc()); | |
52 EXPECT_EQ(0x11111111U, parser.sender_report()->NtpSec()); | |
53 EXPECT_EQ(0x22222222U, parser.sender_report()->NtpFrac()); | |
54 EXPECT_EQ(0x33333333U, parser.sender_report()->RtpTimestamp()); | |
55 EXPECT_EQ(0x44444444U, parser.sender_report()->PacketCount()); | |
56 EXPECT_EQ(0x55555555U, parser.sender_report()->OctetCount()); | |
57 EXPECT_EQ(0, parser.report_block()->num_packets()); | |
58 } | |
59 | |
60 TEST(RtcpPacketTest, SrWithOneReportBlock) { | |
61 ReportBlock rb; | |
62 rb.To(kRemoteSsrc); | |
63 | |
64 SenderReport sr; | |
65 sr.From(kSenderSsrc); | |
66 EXPECT_TRUE(sr.WithReportBlock(rb)); | |
67 | |
68 rtc::scoped_ptr<RawPacket> packet(sr.Build()); | |
69 RtcpPacketParser parser; | |
70 parser.Parse(packet->Buffer(), packet->Length()); | |
71 EXPECT_EQ(1, parser.sender_report()->num_packets()); | |
72 EXPECT_EQ(kSenderSsrc, parser.sender_report()->Ssrc()); | |
73 EXPECT_EQ(1, parser.report_block()->num_packets()); | |
74 EXPECT_EQ(kRemoteSsrc, parser.report_block()->Ssrc()); | |
75 } | |
76 | |
77 TEST(RtcpPacketTest, SrWithTwoReportBlocks) { | |
78 ReportBlock rb1; | |
79 rb1.To(kRemoteSsrc); | |
80 ReportBlock rb2; | |
81 rb2.To(kRemoteSsrc + 1); | |
82 | |
83 SenderReport sr; | |
84 sr.From(kSenderSsrc); | |
85 EXPECT_TRUE(sr.WithReportBlock(rb1)); | |
86 EXPECT_TRUE(sr.WithReportBlock(rb2)); | |
87 | |
88 rtc::scoped_ptr<RawPacket> packet(sr.Build()); | |
89 RtcpPacketParser parser; | |
90 parser.Parse(packet->Buffer(), packet->Length()); | |
91 EXPECT_EQ(1, parser.sender_report()->num_packets()); | |
92 EXPECT_EQ(kSenderSsrc, parser.sender_report()->Ssrc()); | |
93 EXPECT_EQ(2, parser.report_block()->num_packets()); | |
94 EXPECT_EQ(1, parser.report_blocks_per_ssrc(kRemoteSsrc)); | |
95 EXPECT_EQ(1, parser.report_blocks_per_ssrc(kRemoteSsrc + 1)); | |
96 } | |
97 | |
98 TEST(RtcpPacketTest, SrWithTooManyReportBlocks) { | |
99 SenderReport sr; | |
100 sr.From(kSenderSsrc); | |
101 const int kMaxReportBlocks = (1 << 5) - 1; | |
102 ReportBlock rb; | |
103 for (int i = 0; i < kMaxReportBlocks; ++i) { | |
104 rb.To(kRemoteSsrc + i); | |
105 EXPECT_TRUE(sr.WithReportBlock(rb)); | |
106 } | |
107 rb.To(kRemoteSsrc + kMaxReportBlocks); | |
108 EXPECT_FALSE(sr.WithReportBlock(rb)); | |
109 } | |
110 | |
111 TEST(RtcpPacketTest, AppWithNoData) { | |
112 App app; | |
113 app.WithSubType(30); | |
114 uint32_t name = 'n' << 24; | |
115 name += 'a' << 16; | |
116 name += 'm' << 8; | |
117 name += 'e'; | |
118 app.WithName(name); | |
119 | |
120 rtc::scoped_ptr<RawPacket> packet(app.Build()); | |
121 RtcpPacketParser parser; | |
122 parser.Parse(packet->Buffer(), packet->Length()); | |
123 EXPECT_EQ(1, parser.app()->num_packets()); | |
124 EXPECT_EQ(30U, parser.app()->SubType()); | |
125 EXPECT_EQ(name, parser.app()->Name()); | |
126 EXPECT_EQ(0, parser.app_item()->num_packets()); | |
127 } | |
128 | |
129 TEST(RtcpPacketTest, App) { | |
130 App app; | |
131 app.From(kSenderSsrc); | |
132 app.WithSubType(30); | |
133 uint32_t name = 'n' << 24; | |
134 name += 'a' << 16; | |
135 name += 'm' << 8; | |
136 name += 'e'; | |
137 app.WithName(name); | |
138 const char kData[] = {'t', 'e', 's', 't', 'd', 'a', 't', 'a'}; | |
139 const size_t kDataLength = sizeof(kData) / sizeof(kData[0]); | |
140 app.WithData((const uint8_t*)kData, kDataLength); | |
141 | |
142 rtc::scoped_ptr<RawPacket> packet(app.Build()); | |
143 RtcpPacketParser parser; | |
144 parser.Parse(packet->Buffer(), packet->Length()); | |
145 EXPECT_EQ(1, parser.app()->num_packets()); | |
146 EXPECT_EQ(30U, parser.app()->SubType()); | |
147 EXPECT_EQ(name, parser.app()->Name()); | |
148 EXPECT_EQ(1, parser.app_item()->num_packets()); | |
149 EXPECT_EQ(kDataLength, parser.app_item()->DataLength()); | |
150 EXPECT_EQ(0, strncmp(kData, (const char*)parser.app_item()->Data(), | |
151 parser.app_item()->DataLength())); | |
152 } | |
153 | 25 |
154 TEST(RtcpPacketTest, BuildWithTooSmallBuffer) { | 26 TEST(RtcpPacketTest, BuildWithTooSmallBuffer) { |
155 ReportBlock rb; | 27 ReportBlock rb; |
156 ReceiverReport rr; | 28 ReceiverReport rr; |
157 rr.From(kSenderSsrc); | 29 rr.From(kSenderSsrc); |
158 EXPECT_TRUE(rr.WithReportBlock(rb)); | 30 EXPECT_TRUE(rr.WithReportBlock(rb)); |
159 | 31 |
160 const size_t kRrLength = 8; | 32 const size_t kRrLength = 8; |
161 const size_t kReportBlockLength = 24; | 33 const size_t kReportBlockLength = 24; |
162 | 34 |
163 // No packet. | 35 // No packet. |
164 class Verifier : public rtcp::RtcpPacket::PacketReadyCallback { | 36 class Verifier : public rtcp::RtcpPacket::PacketReadyCallback { |
165 void OnPacketReady(uint8_t* data, size_t length) override { | 37 void OnPacketReady(uint8_t* data, size_t length) override { |
166 ADD_FAILURE() << "Packet should not fit within max size."; | 38 ADD_FAILURE() << "Packet should not fit within max size."; |
167 } | 39 } |
168 } verifier; | 40 } verifier; |
169 const size_t kBufferSize = kRrLength + kReportBlockLength - 1; | 41 const size_t kBufferSize = kRrLength + kReportBlockLength - 1; |
170 uint8_t buffer[kBufferSize]; | 42 uint8_t buffer[kBufferSize]; |
171 EXPECT_FALSE(rr.BuildExternalBuffer(buffer, kBufferSize, &verifier)); | 43 EXPECT_FALSE(rr.BuildExternalBuffer(buffer, kBufferSize, &verifier)); |
172 } | 44 } |
173 } // namespace webrtc | 45 } // namespace webrtc |
OLD | NEW |