1 /*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
12
13 #include "test/gmock.h"
14 #include "test/gtest.h"
15 #include "test/rtcp_packet_parser.h"
16
17 using ::testing::ElementsAre;
18 using webrtc::rtcp::Bye;
19
20 namespace webrtc {
21 namespace {
22 const uint32_t kSenderSsrc = 0x12345678;
23 const uint32_t kCsrc1 = 0x22232425;
24 const uint32_t kCsrc2 = 0x33343536;
25 } // namespace
26
TEST(RtcpPacketByeTest,CreateAndParseWithoutReason)27 TEST(RtcpPacketByeTest, CreateAndParseWithoutReason) {
28 Bye bye;
29 bye.SetSenderSsrc(kSenderSsrc);
30
31 rtc::Buffer raw = bye.Build();
32 Bye parsed_bye;
33 EXPECT_TRUE(test::ParseSinglePacket(raw, &parsed_bye));
34
35 EXPECT_EQ(kSenderSsrc, parsed_bye.sender_ssrc());
36 EXPECT_TRUE(parsed_bye.csrcs().empty());
37 EXPECT_TRUE(parsed_bye.reason().empty());
38 }
39
TEST(RtcpPacketByeTest,CreateAndParseWithCsrcs)40 TEST(RtcpPacketByeTest, CreateAndParseWithCsrcs) {
41 Bye bye;
42 bye.SetSenderSsrc(kSenderSsrc);
43 EXPECT_TRUE(bye.SetCsrcs({kCsrc1, kCsrc2}));
44 EXPECT_TRUE(bye.reason().empty());
45
46 rtc::Buffer raw = bye.Build();
47 Bye parsed_bye;
48 EXPECT_TRUE(test::ParseSinglePacket(raw, &parsed_bye));
49
50 EXPECT_EQ(kSenderSsrc, parsed_bye.sender_ssrc());
51 EXPECT_THAT(parsed_bye.csrcs(), ElementsAre(kCsrc1, kCsrc2));
52 EXPECT_TRUE(parsed_bye.reason().empty());
53 }
54
TEST(RtcpPacketByeTest,CreateAndParseWithCsrcsAndAReason)55 TEST(RtcpPacketByeTest, CreateAndParseWithCsrcsAndAReason) {
56 Bye bye;
57 const std::string kReason = "Some Reason";
58
59 bye.SetSenderSsrc(kSenderSsrc);
60 EXPECT_TRUE(bye.SetCsrcs({kCsrc1, kCsrc2}));
61 bye.SetReason(kReason);
62
63 rtc::Buffer raw = bye.Build();
64 Bye parsed_bye;
65 EXPECT_TRUE(test::ParseSinglePacket(raw, &parsed_bye));
66
67 EXPECT_EQ(kSenderSsrc, parsed_bye.sender_ssrc());
68 EXPECT_THAT(parsed_bye.csrcs(), ElementsAre(kCsrc1, kCsrc2));
69 EXPECT_EQ(kReason, parsed_bye.reason());
70 }
71
TEST(RtcpPacketByeTest,CreateWithTooManyCsrcs)72 TEST(RtcpPacketByeTest, CreateWithTooManyCsrcs) {
73 Bye bye;
74 bye.SetSenderSsrc(kSenderSsrc);
75 const int kMaxCsrcs = (1 << 5) - 2; // 5 bit len, first item is sender SSRC.
76 EXPECT_TRUE(bye.SetCsrcs(std::vector<uint32_t>(kMaxCsrcs, kCsrc1)));
77 EXPECT_FALSE(bye.SetCsrcs(std::vector<uint32_t>(kMaxCsrcs + 1, kCsrc1)));
78 }
79
TEST(RtcpPacketByeTest,CreateAndParseWithAReason)80 TEST(RtcpPacketByeTest, CreateAndParseWithAReason) {
81 Bye bye;
82 const std::string kReason = "Some Random Reason";
83
84 bye.SetSenderSsrc(kSenderSsrc);
85 bye.SetReason(kReason);
86
87 rtc::Buffer raw = bye.Build();
88 Bye parsed_bye;
89 EXPECT_TRUE(test::ParseSinglePacket(raw, &parsed_bye));
90
91 EXPECT_EQ(kSenderSsrc, parsed_bye.sender_ssrc());
92 EXPECT_TRUE(parsed_bye.csrcs().empty());
93 EXPECT_EQ(kReason, parsed_bye.reason());
94 }
95
TEST(RtcpPacketByeTest,CreateAndParseWithReasons)96 TEST(RtcpPacketByeTest, CreateAndParseWithReasons) {
97 // Test that packet creation/parsing behave with reasons of different length
98 // both when it require padding and when it does not.
99 for (size_t reminder = 0; reminder < 4; ++reminder) {
100 const std::string kReason(4 + reminder, 'a' + reminder);
101 Bye bye;
102 bye.SetSenderSsrc(kSenderSsrc);
103 bye.SetReason(kReason);
104
105 rtc::Buffer raw = bye.Build();
106 Bye parsed_bye;
107 EXPECT_TRUE(test::ParseSinglePacket(raw, &parsed_bye));
108
109 EXPECT_EQ(kReason, parsed_bye.reason());
110 }
111 }
112
TEST(RtcpPacketByeTest,ParseEmptyPacket)113 TEST(RtcpPacketByeTest, ParseEmptyPacket) {
114 uint8_t kEmptyPacket[] = {0x80, Bye::kPacketType, 0, 0};
115 Bye parsed_bye;
116 EXPECT_TRUE(test::ParseSinglePacket(kEmptyPacket, &parsed_bye));
117 EXPECT_EQ(0u, parsed_bye.sender_ssrc());
118 EXPECT_TRUE(parsed_bye.csrcs().empty());
119 EXPECT_TRUE(parsed_bye.reason().empty());
120 }
121
TEST(RtcpPacketByeTest,ParseFailOnInvalidSrcCount)122 TEST(RtcpPacketByeTest, ParseFailOnInvalidSrcCount) {
123 Bye bye;
124 bye.SetSenderSsrc(kSenderSsrc);
125
126 rtc::Buffer raw = bye.Build();
127 raw[0]++; // Damage the packet: increase ssrc count by one.
128
129 Bye parsed_bye;
130 EXPECT_FALSE(test::ParseSinglePacket(raw, &parsed_bye));
131 }
132
TEST(RtcpPacketByeTest,ParseFailOnInvalidReasonLength)133 TEST(RtcpPacketByeTest, ParseFailOnInvalidReasonLength) {
134 Bye bye;
135 bye.SetSenderSsrc(kSenderSsrc);
136 bye.SetReason("18 characters long");
137
138 rtc::Buffer raw = bye.Build();
139 // Damage the packet: decrease payload size by 4 bytes
140 raw[3]--;
141 raw.SetSize(raw.size() - 4);
142
143 Bye parsed_bye;
144 EXPECT_FALSE(test::ParseSinglePacket(raw, &parsed_bye));
145 }
146
147 } // namespace webrtc
148