henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "media/base/rtp_utils.h" |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include <cstdint> |
Steve Anton | e78bcb9 | 2017-10-31 09:53:08 -0700 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "media/base/fake_rtp.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/async_packet_socket.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 20 | #include "test/gtest.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | |
| 22 | namespace cricket { |
| 23 | |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 24 | static const uint8_t kRtpPacketWithMarker[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 25 | 0x80, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 26 | // 3 CSRCs (0x01020304, 0x12345678, 0xAABBCCDD) |
| 27 | // Extension (0xBEDE, 0x1122334455667788) |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 28 | static const uint8_t kRtpPacketWithMarkerAndCsrcAndExtension[] = { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | 0x93, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
| 30 | 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC, 0xDD, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 31 | 0xBE, 0xDE, 0x00, 0x02, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; |
| 32 | static const uint8_t kInvalidPacket[] = {0x80, 0x00}; |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 33 | static const uint8_t kInvalidPacketWithCsrc[] = { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | 0x83, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 35 | 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC}; |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 36 | static const uint8_t kInvalidPacketWithCsrcAndExtension1[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 37 | 0x93, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 38 | 0x00, 0x00, 0x01, 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, |
| 39 | 0x56, 0x78, 0xAA, 0xBB, 0xCC, 0xDD, 0xBE, 0xDE, 0x00}; |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 40 | static const uint8_t kInvalidPacketWithCsrcAndExtension2[] = { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 41 | 0x93, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
| 42 | 0x01, 0x02, 0x03, 0x04, 0x12, 0x34, 0x56, 0x78, 0xAA, 0xBB, 0xCC, 0xDD, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 43 | 0xBE, 0xDE, 0x00, 0x02, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 44 | |
| 45 | // PT = 206, FMT = 1, Sender SSRC = 0x1111, Media SSRC = 0x1111 |
| 46 | // No FCI information is needed for PLI. |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 47 | static const uint8_t kNonCompoundRtcpPliFeedbackPacket[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 48 | 0x81, 0xCE, 0x00, 0x0C, 0x00, 0x00, 0x11, 0x11, 0x00, 0x00, 0x11, 0x11}; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 49 | |
| 50 | // Packet has only mandatory fixed RTCP header |
| 51 | // PT = 204, SSRC = 0x1111 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 52 | static const uint8_t kNonCompoundRtcpAppPacket[] = {0x81, 0xCC, 0x00, 0x0C, |
| 53 | 0x00, 0x00, 0x11, 0x11}; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | |
| 55 | // PT = 202, Source count = 0 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 56 | static const uint8_t kNonCompoundRtcpSDESPacket[] = {0x80, 0xCA, 0x00, 0x00}; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 57 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 58 | static uint8_t kFakeTag[4] = {0xba, 0xdd, 0xba, 0xdd}; |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 59 | static uint8_t kTestKey[] = "12345678901234567890"; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 60 | static uint8_t kTestAstValue[3] = {0xaa, 0xbb, 0xcc}; |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 61 | |
| 62 | // Valid rtp Message with 2 byte header extension. |
| 63 | static uint8_t kRtpMsgWith2ByteExtnHeader[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 64 | // clang-format off |
| 65 | // clang formatting doesn't respect inline comments. |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 66 | 0x90, 0x00, 0x00, 0x00, |
| 67 | 0x00, 0x00, 0x00, 0x00, |
| 68 | 0xAA, 0xBB, 0xCC, 0XDD, // SSRC |
| 69 | 0x10, 0x00, 0x00, 0x01, // 2 Byte header extension |
| 70 | 0x01, 0x00, 0x00, 0x00 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 71 | // clang-format on |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | // RTP packet with single byte extension header of length 4 bytes. |
| 75 | // Extension id = 3 and length = 3 |
| 76 | static uint8_t kRtpMsgWithAbsSendTimeExtension[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 77 | 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 78 | 0xBE, 0xDE, 0x00, 0x02, 0x22, 0x00, 0x02, 0x1c, 0x32, 0xaa, 0xbb, 0xcc, |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | // Index of AbsSendTimeExtn data in message |kRtpMsgWithAbsSendTimeExtension|. |
| 82 | static const int kAstIndexInRtpMsg = 21; |
| 83 | |
Amit Hilbuch | edd2054 | 2019-03-18 12:33:43 -0700 | [diff] [blame] | 84 | static const rtc::ArrayView<const char> kPcmuFrameArrayView = |
| 85 | rtc::MakeArrayView(reinterpret_cast<const char*>(kPcmuFrame), |
| 86 | sizeof(kPcmuFrame)); |
| 87 | static const rtc::ArrayView<const char> kRtcpReportArrayView = |
| 88 | rtc::MakeArrayView(reinterpret_cast<const char*>(kRtcpReport), |
| 89 | sizeof(kRtcpReport)); |
| 90 | static const rtc::ArrayView<const char> kInvalidPacketArrayView = |
| 91 | rtc::MakeArrayView(reinterpret_cast<const char*>(kInvalidPacket), |
| 92 | sizeof(kInvalidPacket)); |
| 93 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | TEST(RtpUtilsTest, GetRtp) { |
Amit Hilbuch | edd2054 | 2019-03-18 12:33:43 -0700 | [diff] [blame] | 95 | EXPECT_TRUE(IsRtpPacket(kPcmuFrameArrayView)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 96 | |
| 97 | int pt; |
| 98 | EXPECT_TRUE(GetRtpPayloadType(kPcmuFrame, sizeof(kPcmuFrame), &pt)); |
| 99 | EXPECT_EQ(0, pt); |
| 100 | EXPECT_TRUE(GetRtpPayloadType(kRtpPacketWithMarker, |
| 101 | sizeof(kRtpPacketWithMarker), &pt)); |
| 102 | EXPECT_EQ(0, pt); |
| 103 | |
| 104 | int seq_num; |
| 105 | EXPECT_TRUE(GetRtpSeqNum(kPcmuFrame, sizeof(kPcmuFrame), &seq_num)); |
| 106 | EXPECT_EQ(1, seq_num); |
| 107 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 108 | uint32_t ts; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | EXPECT_TRUE(GetRtpTimestamp(kPcmuFrame, sizeof(kPcmuFrame), &ts)); |
| 110 | EXPECT_EQ(0u, ts); |
| 111 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 112 | uint32_t ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | EXPECT_TRUE(GetRtpSsrc(kPcmuFrame, sizeof(kPcmuFrame), &ssrc)); |
| 114 | EXPECT_EQ(1u, ssrc); |
| 115 | |
| 116 | RtpHeader header; |
| 117 | EXPECT_TRUE(GetRtpHeader(kPcmuFrame, sizeof(kPcmuFrame), &header)); |
| 118 | EXPECT_EQ(0, header.payload_type); |
| 119 | EXPECT_EQ(1, header.seq_num); |
| 120 | EXPECT_EQ(0u, header.timestamp); |
| 121 | EXPECT_EQ(1u, header.ssrc); |
| 122 | |
| 123 | EXPECT_FALSE(GetRtpPayloadType(kInvalidPacket, sizeof(kInvalidPacket), &pt)); |
| 124 | EXPECT_FALSE(GetRtpSeqNum(kInvalidPacket, sizeof(kInvalidPacket), &seq_num)); |
| 125 | EXPECT_FALSE(GetRtpTimestamp(kInvalidPacket, sizeof(kInvalidPacket), &ts)); |
| 126 | EXPECT_FALSE(GetRtpSsrc(kInvalidPacket, sizeof(kInvalidPacket), &ssrc)); |
| 127 | } |
| 128 | |
pkasting@chromium.org | 0e81fdf | 2015-02-02 23:54:03 +0000 | [diff] [blame] | 129 | TEST(RtpUtilsTest, SetRtpHeader) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 130 | uint8_t packet[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 131 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 132 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 133 | RtpHeader header = {9, 1111, 2222u, 3333u}; |
pkasting@chromium.org | 0e81fdf | 2015-02-02 23:54:03 +0000 | [diff] [blame] | 134 | EXPECT_TRUE(SetRtpHeader(packet, sizeof(packet), header)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 135 | |
| 136 | // Bits: 10 0 0 0000 |
| 137 | EXPECT_EQ(128u, packet[0]); |
| 138 | size_t len; |
| 139 | EXPECT_TRUE(GetRtpHeaderLen(packet, sizeof(packet), &len)); |
| 140 | EXPECT_EQ(12U, len); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 141 | EXPECT_TRUE(GetRtpHeader(packet, sizeof(packet), &header)); |
| 142 | EXPECT_EQ(9, header.payload_type); |
| 143 | EXPECT_EQ(1111, header.seq_num); |
| 144 | EXPECT_EQ(2222u, header.timestamp); |
| 145 | EXPECT_EQ(3333u, header.ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | TEST(RtpUtilsTest, GetRtpHeaderLen) { |
| 149 | size_t len; |
| 150 | EXPECT_TRUE(GetRtpHeaderLen(kPcmuFrame, sizeof(kPcmuFrame), &len)); |
| 151 | EXPECT_EQ(12U, len); |
| 152 | |
| 153 | EXPECT_TRUE(GetRtpHeaderLen(kRtpPacketWithMarkerAndCsrcAndExtension, |
| 154 | sizeof(kRtpPacketWithMarkerAndCsrcAndExtension), |
| 155 | &len)); |
| 156 | EXPECT_EQ(sizeof(kRtpPacketWithMarkerAndCsrcAndExtension), len); |
| 157 | |
| 158 | EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacket, sizeof(kInvalidPacket), &len)); |
| 159 | EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrc, |
| 160 | sizeof(kInvalidPacketWithCsrc), &len)); |
| 161 | EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrcAndExtension1, |
| 162 | sizeof(kInvalidPacketWithCsrcAndExtension1), |
| 163 | &len)); |
| 164 | EXPECT_FALSE(GetRtpHeaderLen(kInvalidPacketWithCsrcAndExtension2, |
| 165 | sizeof(kInvalidPacketWithCsrcAndExtension2), |
| 166 | &len)); |
| 167 | } |
| 168 | |
| 169 | TEST(RtpUtilsTest, GetRtcp) { |
| 170 | int pt; |
| 171 | EXPECT_TRUE(GetRtcpType(kRtcpReport, sizeof(kRtcpReport), &pt)); |
| 172 | EXPECT_EQ(0xc9, pt); |
| 173 | |
| 174 | EXPECT_FALSE(GetRtcpType(kInvalidPacket, sizeof(kInvalidPacket), &pt)); |
| 175 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 176 | uint32_t ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 177 | EXPECT_TRUE(GetRtcpSsrc(kNonCompoundRtcpPliFeedbackPacket, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 178 | sizeof(kNonCompoundRtcpPliFeedbackPacket), &ssrc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 179 | EXPECT_TRUE(GetRtcpSsrc(kNonCompoundRtcpAppPacket, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 180 | sizeof(kNonCompoundRtcpAppPacket), &ssrc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 181 | EXPECT_FALSE(GetRtcpSsrc(kNonCompoundRtcpSDESPacket, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 182 | sizeof(kNonCompoundRtcpSDESPacket), &ssrc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 185 | // Invalid RTP packets. |
| 186 | TEST(RtpUtilsTest, InvalidRtpHeader) { |
| 187 | // Rtp message with invalid length. |
| 188 | const uint8_t kRtpMsgWithInvalidLength[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 189 | // clang-format off |
| 190 | // clang formatting doesn't respect inline comments. |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 191 | 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 192 | 0xAA, 0xBB, 0xCC, 0XDD, // SSRC |
| 193 | 0xDD, 0xCC, 0xBB, 0xAA, // Only 1 CSRC, but CC count is 4. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 194 | // clang-format on |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 195 | }; |
| 196 | EXPECT_FALSE(ValidateRtpHeader(kRtpMsgWithInvalidLength, |
| 197 | sizeof(kRtpMsgWithInvalidLength), nullptr)); |
| 198 | |
| 199 | // Rtp message with single byte header extension, invalid extension length. |
| 200 | const uint8_t kRtpMsgWithInvalidExtnLength[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 201 | 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 202 | 0x00, 0x00, 0x00, 0x00, 0xBE, 0xDE, 0x0A, 0x00, // Extn length - 0x0A00 |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 203 | }; |
| 204 | EXPECT_FALSE(ValidateRtpHeader(kRtpMsgWithInvalidExtnLength, |
| 205 | sizeof(kRtpMsgWithInvalidExtnLength), |
| 206 | nullptr)); |
| 207 | } |
| 208 | |
| 209 | // Valid RTP packet with a 2byte header extension. |
| 210 | TEST(RtpUtilsTest, Valid2ByteExtnHdrRtpMessage) { |
| 211 | EXPECT_TRUE(ValidateRtpHeader(kRtpMsgWith2ByteExtnHeader, |
| 212 | sizeof(kRtpMsgWith2ByteExtnHeader), nullptr)); |
| 213 | } |
| 214 | |
| 215 | // Valid RTP packet which has 1 byte header AbsSendTime extension in it. |
| 216 | TEST(RtpUtilsTest, ValidRtpPacketWithAbsSendTimeExtension) { |
| 217 | EXPECT_TRUE(ValidateRtpHeader(kRtpMsgWithAbsSendTimeExtension, |
| 218 | sizeof(kRtpMsgWithAbsSendTimeExtension), |
| 219 | nullptr)); |
| 220 | } |
| 221 | |
| 222 | // Verify handling of a 2 byte extension header RTP messsage. Currently these |
| 223 | // messages are not supported. |
| 224 | TEST(RtpUtilsTest, UpdateAbsSendTimeExtensionIn2ByteHeaderExtn) { |
| 225 | std::vector<uint8_t> data( |
| 226 | kRtpMsgWith2ByteExtnHeader, |
| 227 | kRtpMsgWith2ByteExtnHeader + sizeof(kRtpMsgWith2ByteExtnHeader)); |
| 228 | EXPECT_FALSE(UpdateRtpAbsSendTimeExtension(&data[0], data.size(), 3, 0)); |
| 229 | } |
| 230 | |
| 231 | // Verify finding an extension ID in the TURN send indication message. |
| 232 | TEST(RtpUtilsTest, UpdateAbsSendTimeExtensionInTurnSendIndication) { |
| 233 | // A valid STUN indication message with a valid RTP header in data attribute |
| 234 | // payload field and no extension bit set. |
| 235 | uint8_t message_without_extension[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 236 | // clang-format off |
| 237 | // clang formatting doesn't respect inline comments. |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 238 | 0x00, 0x16, 0x00, 0x18, // length of |
| 239 | 0x21, 0x12, 0xA4, 0x42, // magic cookie |
| 240 | '0', '1', '2', '3', // transaction id |
| 241 | '4', '5', '6', '7', |
| 242 | '8', '9', 'a', 'b', |
| 243 | 0x00, 0x20, 0x00, 0x04, // Mapped address. |
| 244 | 0x00, 0x00, 0x00, 0x00, |
| 245 | 0x00, 0x13, 0x00, 0x0C, // Data attribute. |
| 246 | 0x80, 0x00, 0x00, 0x00, // RTP packet. |
| 247 | 0x00, 0x00, 0x00, 0x00, |
| 248 | 0x00, 0x00, 0x00, 0x00, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 249 | // clang-format on |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 250 | }; |
| 251 | EXPECT_TRUE(UpdateRtpAbsSendTimeExtension( |
| 252 | message_without_extension, sizeof(message_without_extension), 3, 0)); |
| 253 | |
| 254 | // A valid STUN indication message with a valid RTP header and a extension |
| 255 | // header. |
| 256 | uint8_t message[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 257 | // clang-format off |
| 258 | // clang formatting doesn't respect inline comments. |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 259 | 0x00, 0x16, 0x00, 0x24, // length of |
| 260 | 0x21, 0x12, 0xA4, 0x42, // magic cookie |
| 261 | '0', '1', '2', '3', // transaction id |
| 262 | '4', '5', '6', '7', |
| 263 | '8', '9', 'a', 'b', |
| 264 | 0x00, 0x20, 0x00, 0x04, // Mapped address. |
| 265 | 0x00, 0x00, 0x00, 0x00, |
| 266 | 0x00, 0x13, 0x00, 0x18, // Data attribute. |
| 267 | 0x90, 0x00, 0x00, 0x00, // RTP packet. |
| 268 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xDE, |
| 269 | 0x00, 0x02, 0x22, 0xaa, 0xbb, 0xcc, 0x32, 0xaa, 0xbb, 0xcc, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 270 | // clang-format on |
Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame] | 271 | }; |
| 272 | EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(message, sizeof(message), 3, 0)); |
| 273 | } |
| 274 | |
| 275 | // Test without any packet options variables set. This method should return |
| 276 | // without HMAC value in the packet. |
| 277 | TEST(RtpUtilsTest, ApplyPacketOptionsWithDefaultValues) { |
| 278 | rtc::PacketTimeUpdateParams packet_time_params; |
| 279 | std::vector<uint8_t> rtp_packet(kRtpMsgWithAbsSendTimeExtension, |
| 280 | kRtpMsgWithAbsSendTimeExtension + |
| 281 | sizeof(kRtpMsgWithAbsSendTimeExtension)); |
| 282 | rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag)); |
| 283 | EXPECT_TRUE(ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(), |
| 284 | packet_time_params, 0)); |
| 285 | |
| 286 | // Making sure HMAC wasn't updated.. |
| 287 | EXPECT_EQ(0, memcmp(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)], |
| 288 | kFakeTag, 4)); |
| 289 | |
| 290 | // Verify AbsouluteSendTime extension field wasn't modified. |
| 291 | EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kTestAstValue, |
| 292 | sizeof(kTestAstValue))); |
| 293 | } |
| 294 | |
| 295 | // Veirfy HMAC is updated when packet option parameters are set. |
| 296 | TEST(RtpUtilsTest, ApplyPacketOptionsWithAuthParams) { |
| 297 | rtc::PacketTimeUpdateParams packet_time_params; |
| 298 | packet_time_params.srtp_auth_key.assign(kTestKey, |
| 299 | kTestKey + sizeof(kTestKey)); |
| 300 | packet_time_params.srtp_auth_tag_len = 4; |
| 301 | |
| 302 | std::vector<uint8_t> rtp_packet(kRtpMsgWithAbsSendTimeExtension, |
| 303 | kRtpMsgWithAbsSendTimeExtension + |
| 304 | sizeof(kRtpMsgWithAbsSendTimeExtension)); |
| 305 | rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag)); |
| 306 | EXPECT_TRUE(ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(), |
| 307 | packet_time_params, 0)); |
| 308 | |
| 309 | uint8_t kExpectedTag[] = {0xc1, 0x7a, 0x8c, 0xa0}; |
| 310 | EXPECT_EQ(0, memcmp(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)], |
| 311 | kExpectedTag, sizeof(kExpectedTag))); |
| 312 | |
| 313 | // Verify AbsouluteSendTime extension field is not modified. |
| 314 | EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kTestAstValue, |
| 315 | sizeof(kTestAstValue))); |
| 316 | } |
| 317 | |
| 318 | // Verify finding an extension ID in a raw rtp message. |
| 319 | TEST(RtpUtilsTest, UpdateAbsSendTimeExtensionInRtpPacket) { |
| 320 | std::vector<uint8_t> rtp_packet(kRtpMsgWithAbsSendTimeExtension, |
| 321 | kRtpMsgWithAbsSendTimeExtension + |
| 322 | sizeof(kRtpMsgWithAbsSendTimeExtension)); |
| 323 | |
| 324 | EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(&rtp_packet[0], rtp_packet.size(), |
| 325 | 3, 51183266)); |
| 326 | |
| 327 | // Verify that the timestamp was updated. |
| 328 | const uint8_t kExpectedTimestamp[3] = {0xcc, 0xbb, 0xaa}; |
| 329 | EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kExpectedTimestamp, |
| 330 | sizeof(kExpectedTimestamp))); |
| 331 | } |
| 332 | |
| 333 | // Verify we update both AbsSendTime extension header and HMAC. |
| 334 | TEST(RtpUtilsTest, ApplyPacketOptionsWithAuthParamsAndAbsSendTime) { |
| 335 | rtc::PacketTimeUpdateParams packet_time_params; |
| 336 | packet_time_params.srtp_auth_key.assign(kTestKey, |
| 337 | kTestKey + sizeof(kTestKey)); |
| 338 | packet_time_params.srtp_auth_tag_len = 4; |
| 339 | packet_time_params.rtp_sendtime_extension_id = 3; |
| 340 | // 3 is also present in the test message. |
| 341 | |
| 342 | std::vector<uint8_t> rtp_packet(kRtpMsgWithAbsSendTimeExtension, |
| 343 | kRtpMsgWithAbsSendTimeExtension + |
| 344 | sizeof(kRtpMsgWithAbsSendTimeExtension)); |
| 345 | rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag)); |
| 346 | EXPECT_TRUE(ApplyPacketOptions(&rtp_packet[0], rtp_packet.size(), |
| 347 | packet_time_params, 51183266)); |
| 348 | |
| 349 | const uint8_t kExpectedTag[] = {0x81, 0xd1, 0x2c, 0x0e}; |
| 350 | EXPECT_EQ(0, memcmp(&rtp_packet[sizeof(kRtpMsgWithAbsSendTimeExtension)], |
| 351 | kExpectedTag, sizeof(kExpectedTag))); |
| 352 | |
| 353 | // Verify that the timestamp was updated. |
| 354 | const uint8_t kExpectedTimestamp[3] = {0xcc, 0xbb, 0xaa}; |
| 355 | EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInRtpMsg], kExpectedTimestamp, |
| 356 | sizeof(kExpectedTimestamp))); |
| 357 | } |
| 358 | |
Amit Hilbuch | edd2054 | 2019-03-18 12:33:43 -0700 | [diff] [blame] | 359 | TEST(RtpUtilsTest, InferRtpPacketType) { |
| 360 | EXPECT_EQ(RtpPacketType::kRtp, InferRtpPacketType(kPcmuFrameArrayView)); |
| 361 | EXPECT_EQ(RtpPacketType::kRtcp, InferRtpPacketType(kRtcpReportArrayView)); |
| 362 | EXPECT_EQ(RtpPacketType::kUnknown, |
| 363 | InferRtpPacketType(kInvalidPacketArrayView)); |
| 364 | } |
| 365 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 366 | } // namespace cricket |