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