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