1 /*
2 * Copyright (c) 2004 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 "media/base/rtp_utils.h"
12
13 #include <string.h>
14
15 #include <cstdint>
16 #include <vector>
17
18 #include "media/base/fake_rtp.h"
19 #include "rtc_base/async_packet_socket.h"
20 #include "test/gtest.h"
21
22 namespace cricket {
23
24 static const uint8_t kInvalidPacket[] = {0x80, 0x00};
25
26 // PT = 206, FMT = 1, Sender SSRC = 0x1111, Media SSRC = 0x1111
27 // No FCI information is needed for PLI.
28 static const uint8_t kNonCompoundRtcpPliFeedbackPacket[] = {
29 0x81, 0xCE, 0x00, 0x0C, 0x00, 0x00, 0x11, 0x11, 0x00, 0x00, 0x11, 0x11};
30
31 // Packet has only mandatory fixed RTCP header
32 // PT = 204, SSRC = 0x1111
33 static const uint8_t kNonCompoundRtcpAppPacket[] = {0x81, 0xCC, 0x00, 0x0C,
34 0x00, 0x00, 0x11, 0x11};
35
36 // PT = 202, Source count = 0
37 static const uint8_t kNonCompoundRtcpSDESPacket[] = {0x80, 0xCA, 0x00, 0x00};
38
39 static uint8_t kFakeTag[4] = {0xba, 0xdd, 0xba, 0xdd};
40 static uint8_t kTestKey[] = "12345678901234567890";
41 static uint8_t kTestAstValue[3] = {0xaa, 0xbb, 0xcc};
42
43 // Valid rtp Message with 2 byte header extension.
44 static uint8_t kRtpMsgWith2ByteExtnHeader[] = {
45 // clang-format off
46 // clang formatting doesn't respect inline comments.
47 0x90, 0x00, 0x00, 0x00,
48 0x00, 0x00, 0x00, 0x00,
49 0xAA, 0xBB, 0xCC, 0XDD, // SSRC
50 0x10, 0x00, 0x00, 0x01, // 2 Byte header extension
51 0x01, 0x00, 0x00, 0x00
52 // clang-format on
53 };
54
55 // RTP packet with two one-byte header extensions. The last 4 bytes consist of
56 // abs-send-time with extension id = 3 and length = 3.
57 static uint8_t kRtpMsgWithOneByteAbsSendTimeExtension[] = {
58 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 0xBE, 0xDE, 0x00, 0x02, 0x22, 0x00, 0x02, 0x1c, 0x32, 0xaa, 0xbb, 0xcc,
60 };
61
62 // RTP packet with two two-byte header extensions. The last 5 bytes consist of
63 // abs-send-time with extension id = 3 and length = 3.
64 static uint8_t kRtpMsgWithTwoByteAbsSendTimeExtension[] = {
65 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x10, 0x00, 0x00, 0x02, 0x02, 0x01, 0x02, 0x03, 0x03, 0xaa, 0xbb, 0xcc,
67 };
68
69 // Index of AbsSendTimeExtn data in message
70 // `kRtpMsgWithOneByteAbsSendTimeExtension`.
71 static const int kAstIndexInOneByteRtpMsg = 21;
72 // and in message `kRtpMsgWithTwoByteAbsSendTimeExtension`.
73 static const int kAstIndexInTwoByteRtpMsg = 21;
74
75 static const rtc::ArrayView<const char> kPcmuFrameArrayView =
76 rtc::MakeArrayView(reinterpret_cast<const char*>(kPcmuFrame),
77 sizeof(kPcmuFrame));
78 static const rtc::ArrayView<const char> kRtcpReportArrayView =
79 rtc::MakeArrayView(reinterpret_cast<const char*>(kRtcpReport),
80 sizeof(kRtcpReport));
81 static const rtc::ArrayView<const char> kInvalidPacketArrayView =
82 rtc::MakeArrayView(reinterpret_cast<const char*>(kInvalidPacket),
83 sizeof(kInvalidPacket));
84
TEST(RtpUtilsTest,GetRtcp)85 TEST(RtpUtilsTest, GetRtcp) {
86 int pt;
87 EXPECT_TRUE(GetRtcpType(kRtcpReport, sizeof(kRtcpReport), &pt));
88 EXPECT_EQ(0xc9, pt);
89
90 EXPECT_FALSE(GetRtcpType(kInvalidPacket, sizeof(kInvalidPacket), &pt));
91
92 uint32_t ssrc;
93 EXPECT_TRUE(GetRtcpSsrc(kNonCompoundRtcpPliFeedbackPacket,
94 sizeof(kNonCompoundRtcpPliFeedbackPacket), &ssrc));
95 EXPECT_TRUE(GetRtcpSsrc(kNonCompoundRtcpAppPacket,
96 sizeof(kNonCompoundRtcpAppPacket), &ssrc));
97 EXPECT_FALSE(GetRtcpSsrc(kNonCompoundRtcpSDESPacket,
98 sizeof(kNonCompoundRtcpSDESPacket), &ssrc));
99 }
100
101 // Invalid RTP packets.
TEST(RtpUtilsTest,InvalidRtpHeader)102 TEST(RtpUtilsTest, InvalidRtpHeader) {
103 // Rtp message with invalid length.
104 const uint8_t kRtpMsgWithInvalidLength[] = {
105 // clang-format off
106 // clang formatting doesn't respect inline comments.
107 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108 0xAA, 0xBB, 0xCC, 0XDD, // SSRC
109 0xDD, 0xCC, 0xBB, 0xAA, // Only 1 CSRC, but CC count is 4.
110 // clang-format on
111 };
112 EXPECT_FALSE(ValidateRtpHeader(kRtpMsgWithInvalidLength,
113 sizeof(kRtpMsgWithInvalidLength), nullptr));
114
115 // Rtp message with single byte header extension, invalid extension length.
116 const uint8_t kRtpMsgWithInvalidExtnLength[] = {
117 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
118 0x00, 0x00, 0x00, 0x00, 0xBE, 0xDE, 0x0A, 0x00, // Extn length - 0x0A00
119 };
120 EXPECT_FALSE(ValidateRtpHeader(kRtpMsgWithInvalidExtnLength,
121 sizeof(kRtpMsgWithInvalidExtnLength),
122 nullptr));
123 }
124
125 // Valid RTP packet with a 2byte header extension.
TEST(RtpUtilsTest,Valid2ByteExtnHdrRtpMessage)126 TEST(RtpUtilsTest, Valid2ByteExtnHdrRtpMessage) {
127 EXPECT_TRUE(ValidateRtpHeader(kRtpMsgWith2ByteExtnHeader,
128 sizeof(kRtpMsgWith2ByteExtnHeader), nullptr));
129 }
130
131 // Valid RTP packet which has 1 byte header AbsSendTime extension in it.
TEST(RtpUtilsTest,ValidRtpPacketWithOneByteAbsSendTimeExtension)132 TEST(RtpUtilsTest, ValidRtpPacketWithOneByteAbsSendTimeExtension) {
133 EXPECT_TRUE(ValidateRtpHeader(kRtpMsgWithOneByteAbsSendTimeExtension,
134 sizeof(kRtpMsgWithOneByteAbsSendTimeExtension),
135 nullptr));
136 }
137
138 // Valid RTP packet which has 2 byte header AbsSendTime extension in it.
TEST(RtpUtilsTest,ValidRtpPacketWithTwoByteAbsSendTimeExtension)139 TEST(RtpUtilsTest, ValidRtpPacketWithTwoByteAbsSendTimeExtension) {
140 EXPECT_TRUE(ValidateRtpHeader(kRtpMsgWithTwoByteAbsSendTimeExtension,
141 sizeof(kRtpMsgWithTwoByteAbsSendTimeExtension),
142 nullptr));
143 }
144
145 // Verify finding an extension ID in the TURN send indication message.
TEST(RtpUtilsTest,UpdateAbsSendTimeExtensionInTurnSendIndication)146 TEST(RtpUtilsTest, UpdateAbsSendTimeExtensionInTurnSendIndication) {
147 // A valid STUN indication message with a valid RTP header in data attribute
148 // payload field and no extension bit set.
149 uint8_t message_without_extension[] = {
150 // clang-format off
151 // clang formatting doesn't respect inline comments.
152 0x00, 0x16, 0x00, 0x18, // length of
153 0x21, 0x12, 0xA4, 0x42, // magic cookie
154 '0', '1', '2', '3', // transaction id
155 '4', '5', '6', '7',
156 '8', '9', 'a', 'b',
157 0x00, 0x20, 0x00, 0x04, // Mapped address.
158 0x00, 0x00, 0x00, 0x00,
159 0x00, 0x13, 0x00, 0x0C, // Data attribute.
160 0x80, 0x00, 0x00, 0x00, // RTP packet.
161 0x00, 0x00, 0x00, 0x00,
162 0x00, 0x00, 0x00, 0x00,
163 // clang-format on
164 };
165 EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(
166 message_without_extension, sizeof(message_without_extension), 3, 0));
167
168 // A valid STUN indication message with a valid RTP header and a extension
169 // header.
170 uint8_t message[] = {
171 // clang-format off
172 // clang formatting doesn't respect inline comments.
173 0x00, 0x16, 0x00, 0x24, // length of
174 0x21, 0x12, 0xA4, 0x42, // magic cookie
175 '0', '1', '2', '3', // transaction id
176 '4', '5', '6', '7',
177 '8', '9', 'a', 'b',
178 0x00, 0x20, 0x00, 0x04, // Mapped address.
179 0x00, 0x00, 0x00, 0x00,
180 0x00, 0x13, 0x00, 0x18, // Data attribute.
181 0x90, 0x00, 0x00, 0x00, // RTP packet.
182 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xDE,
183 0x00, 0x02, 0x22, 0xaa, 0xbb, 0xcc, 0x32, 0xaa, 0xbb, 0xcc,
184 // clang-format on
185 };
186 EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(message, sizeof(message), 3, 0));
187 }
188
189 // Test without any packet options variables set. This method should return
190 // without HMAC value in the packet.
TEST(RtpUtilsTest,ApplyPacketOptionsWithDefaultValues)191 TEST(RtpUtilsTest, ApplyPacketOptionsWithDefaultValues) {
192 rtc::PacketTimeUpdateParams packet_time_params;
193 std::vector<uint8_t> rtp_packet(
194 kRtpMsgWithOneByteAbsSendTimeExtension,
195 kRtpMsgWithOneByteAbsSendTimeExtension +
196 sizeof(kRtpMsgWithOneByteAbsSendTimeExtension));
197 rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag));
198 EXPECT_TRUE(ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(),
199 packet_time_params, 0));
200
201 // Making sure HMAC wasn't updated..
202 EXPECT_EQ(0,
203 memcmp(&rtp_packet[sizeof(kRtpMsgWithOneByteAbsSendTimeExtension)],
204 kFakeTag, 4));
205
206 // Verify AbsouluteSendTime extension field wasn't modified.
207 EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInOneByteRtpMsg], kTestAstValue,
208 sizeof(kTestAstValue)));
209 }
210
211 // Veirfy HMAC is updated when packet option parameters are set.
TEST(RtpUtilsTest,ApplyPacketOptionsWithAuthParams)212 TEST(RtpUtilsTest, ApplyPacketOptionsWithAuthParams) {
213 rtc::PacketTimeUpdateParams packet_time_params;
214 packet_time_params.srtp_auth_key.assign(kTestKey,
215 kTestKey + sizeof(kTestKey));
216 packet_time_params.srtp_auth_tag_len = 4;
217
218 std::vector<uint8_t> rtp_packet(
219 kRtpMsgWithOneByteAbsSendTimeExtension,
220 kRtpMsgWithOneByteAbsSendTimeExtension +
221 sizeof(kRtpMsgWithOneByteAbsSendTimeExtension));
222 rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag));
223 EXPECT_TRUE(ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(),
224 packet_time_params, 0));
225
226 uint8_t kExpectedTag[] = {0xc1, 0x7a, 0x8c, 0xa0};
227 EXPECT_EQ(0,
228 memcmp(&rtp_packet[sizeof(kRtpMsgWithOneByteAbsSendTimeExtension)],
229 kExpectedTag, sizeof(kExpectedTag)));
230
231 // Verify AbsouluteSendTime extension field is not modified.
232 EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInOneByteRtpMsg], kTestAstValue,
233 sizeof(kTestAstValue)));
234 }
235
236 // Verify finding an extension ID in a raw rtp message.
TEST(RtpUtilsTest,UpdateOneByteAbsSendTimeExtensionInRtpPacket)237 TEST(RtpUtilsTest, UpdateOneByteAbsSendTimeExtensionInRtpPacket) {
238 std::vector<uint8_t> rtp_packet(
239 kRtpMsgWithOneByteAbsSendTimeExtension,
240 kRtpMsgWithOneByteAbsSendTimeExtension +
241 sizeof(kRtpMsgWithOneByteAbsSendTimeExtension));
242
243 EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(&rtp_packet[0], rtp_packet.size(),
244 3, 51183266));
245
246 // Verify that the timestamp was updated.
247 const uint8_t kExpectedTimestamp[3] = {0xcc, 0xbb, 0xaa};
248 EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInOneByteRtpMsg], kExpectedTimestamp,
249 sizeof(kExpectedTimestamp)));
250 }
251
252 // Verify finding an extension ID in a raw rtp message.
TEST(RtpUtilsTest,UpdateTwoByteAbsSendTimeExtensionInRtpPacket)253 TEST(RtpUtilsTest, UpdateTwoByteAbsSendTimeExtensionInRtpPacket) {
254 std::vector<uint8_t> rtp_packet(
255 kRtpMsgWithTwoByteAbsSendTimeExtension,
256 kRtpMsgWithTwoByteAbsSendTimeExtension +
257 sizeof(kRtpMsgWithTwoByteAbsSendTimeExtension));
258
259 EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(&rtp_packet[0], rtp_packet.size(),
260 3, 51183266));
261
262 // Verify that the timestamp was updated.
263 const uint8_t kExpectedTimestamp[3] = {0xcc, 0xbb, 0xaa};
264 EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInTwoByteRtpMsg], kExpectedTimestamp,
265 sizeof(kExpectedTimestamp)));
266 }
267
268 // Verify we update both AbsSendTime extension header and HMAC.
TEST(RtpUtilsTest,ApplyPacketOptionsWithAuthParamsAndAbsSendTime)269 TEST(RtpUtilsTest, ApplyPacketOptionsWithAuthParamsAndAbsSendTime) {
270 rtc::PacketTimeUpdateParams packet_time_params;
271 packet_time_params.srtp_auth_key.assign(kTestKey,
272 kTestKey + sizeof(kTestKey));
273 packet_time_params.srtp_auth_tag_len = 4;
274 packet_time_params.rtp_sendtime_extension_id = 3;
275 // 3 is also present in the test message.
276
277 std::vector<uint8_t> rtp_packet(
278 kRtpMsgWithOneByteAbsSendTimeExtension,
279 kRtpMsgWithOneByteAbsSendTimeExtension +
280 sizeof(kRtpMsgWithOneByteAbsSendTimeExtension));
281 rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag));
282 EXPECT_TRUE(ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(),
283 packet_time_params, 51183266));
284
285 const uint8_t kExpectedTag[] = {0x81, 0xd1, 0x2c, 0x0e};
286 EXPECT_EQ(0,
287 memcmp(&rtp_packet[sizeof(kRtpMsgWithOneByteAbsSendTimeExtension)],
288 kExpectedTag, sizeof(kExpectedTag)));
289
290 // Verify that the timestamp was updated.
291 const uint8_t kExpectedTimestamp[3] = {0xcc, 0xbb, 0xaa};
292 EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInOneByteRtpMsg], kExpectedTimestamp,
293 sizeof(kExpectedTimestamp)));
294 }
295
TEST(RtpUtilsTest,InferRtpPacketType)296 TEST(RtpUtilsTest, InferRtpPacketType) {
297 EXPECT_EQ(RtpPacketType::kRtp, InferRtpPacketType(kPcmuFrameArrayView));
298 EXPECT_EQ(RtpPacketType::kRtcp, InferRtpPacketType(kRtcpReportArrayView));
299 EXPECT_EQ(RtpPacketType::kUnknown,
300 InferRtpPacketType(kInvalidPacketArrayView));
301 }
302
303 } // namespace cricket
304