tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

rtp_utils_unittest.cc (11806B)


      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 <cstdint>
     14 #include <cstring>
     15 #include <vector>
     16 
     17 #include "api/array_view.h"
     18 #include "media/base/fake_rtp.h"
     19 #include "rtc_base/async_packet_socket.h"
     20 #include "test/gtest.h"
     21 
     22 namespace webrtc {
     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 ArrayView<const uint8_t> kPcmuFrameArrayView =
     76    MakeArrayView(kPcmuFrame, sizeof(kPcmuFrame));
     77 static const ArrayView<const uint8_t> kRtcpReportArrayView =
     78    MakeArrayView(kRtcpReport, sizeof(kRtcpReport));
     79 static const ArrayView<const uint8_t> kInvalidPacketArrayView =
     80    MakeArrayView(kInvalidPacket, sizeof(kInvalidPacket));
     81 
     82 TEST(RtpUtilsTest, GetRtcp) {
     83  int pt;
     84  EXPECT_TRUE(GetRtcpType(kRtcpReport, sizeof(kRtcpReport), &pt));
     85  EXPECT_EQ(0xc9, pt);
     86 
     87  EXPECT_FALSE(GetRtcpType(kInvalidPacket, sizeof(kInvalidPacket), &pt));
     88 
     89  uint32_t ssrc;
     90  EXPECT_TRUE(GetRtcpSsrc(kNonCompoundRtcpPliFeedbackPacket,
     91                          sizeof(kNonCompoundRtcpPliFeedbackPacket), &ssrc));
     92  EXPECT_TRUE(GetRtcpSsrc(kNonCompoundRtcpAppPacket,
     93                          sizeof(kNonCompoundRtcpAppPacket), &ssrc));
     94  EXPECT_FALSE(GetRtcpSsrc(kNonCompoundRtcpSDESPacket,
     95                           sizeof(kNonCompoundRtcpSDESPacket), &ssrc));
     96 }
     97 
     98 // Invalid RTP packets.
     99 TEST(RtpUtilsTest, InvalidRtpHeader) {
    100  // Rtp message with invalid length.
    101  const uint8_t kRtpMsgWithInvalidLength[] = {
    102      // clang-format off
    103      // clang formatting doesn't respect inline comments.
    104      0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    105      0xAA, 0xBB, 0xCC, 0XDD,  // SSRC
    106      0xDD, 0xCC, 0xBB, 0xAA,  // Only 1 CSRC, but CC count is 4.
    107      // clang-format on
    108  };
    109  EXPECT_FALSE(ValidateRtpHeader(kRtpMsgWithInvalidLength, nullptr));
    110 
    111  // Rtp message with single byte header extension, invalid extension length.
    112  const uint8_t kRtpMsgWithInvalidExtnLength[] = {
    113      0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    114      0x00, 0x00, 0x00, 0x00, 0xBE, 0xDE, 0x0A, 0x00,  // Extn length - 0x0A00
    115  };
    116  EXPECT_FALSE(ValidateRtpHeader(kRtpMsgWithInvalidExtnLength,
    117                                 nullptr));
    118 }
    119 
    120 // Valid RTP packet with a 2byte header extension.
    121 TEST(RtpUtilsTest, Valid2ByteExtnHdrRtpMessage) {
    122  EXPECT_TRUE(ValidateRtpHeader(kRtpMsgWith2ByteExtnHeader, nullptr));
    123 }
    124 
    125 // Valid RTP packet which has 1 byte header AbsSendTime extension in it.
    126 TEST(RtpUtilsTest, ValidRtpPacketWithOneByteAbsSendTimeExtension) {
    127  EXPECT_TRUE(ValidateRtpHeader(kRtpMsgWithOneByteAbsSendTimeExtension,
    128                                nullptr));
    129 }
    130 
    131 // Valid RTP packet which has 2 byte header AbsSendTime extension in it.
    132 TEST(RtpUtilsTest, ValidRtpPacketWithTwoByteAbsSendTimeExtension) {
    133  EXPECT_TRUE(ValidateRtpHeader(kRtpMsgWithTwoByteAbsSendTimeExtension,
    134                                nullptr));
    135 }
    136 
    137 // Verify finding an extension ID in the TURN send indication message.
    138 TEST(RtpUtilsTest, UpdateAbsSendTimeExtensionInTurnSendIndication) {
    139  // A valid STUN indication message with a valid RTP header in data attribute
    140  // payload field and no extension bit set.
    141  uint8_t message_without_extension[] = {
    142      // clang-format off
    143      // clang formatting doesn't respect inline comments.
    144      0x00, 0x16, 0x00, 0x18,  // length of
    145      0x21, 0x12, 0xA4, 0x42,  // magic cookie
    146      '0',  '1',  '2',  '3',   // transaction id
    147      '4',  '5',  '6',  '7',
    148      '8',  '9',  'a',  'b',
    149      0x00, 0x20, 0x00, 0x04,  // Mapped address.
    150      0x00, 0x00, 0x00, 0x00,
    151      0x00, 0x13, 0x00, 0x0C,  // Data attribute.
    152      0x80, 0x00, 0x00, 0x00,  // RTP packet.
    153      0x00, 0x00, 0x00, 0x00,
    154      0x00, 0x00, 0x00, 0x00,
    155      // clang-format on
    156  };
    157  EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(message_without_extension, 3, 0));
    158 
    159  // A valid STUN indication message with a valid RTP header and a extension
    160  // header.
    161  uint8_t message[] = {
    162      // clang-format off
    163      // clang formatting doesn't respect inline comments.
    164      0x00, 0x16, 0x00, 0x24,  // length of
    165      0x21, 0x12, 0xA4, 0x42,  // magic cookie
    166      '0',  '1',  '2',  '3',   // transaction id
    167      '4',  '5',  '6',  '7',
    168      '8',  '9',  'a',  'b',
    169      0x00, 0x20, 0x00, 0x04,  // Mapped address.
    170      0x00, 0x00, 0x00, 0x00,
    171      0x00, 0x13, 0x00, 0x18,  // Data attribute.
    172      0x90, 0x00, 0x00, 0x00,  // RTP packet.
    173      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xDE,
    174      0x00, 0x02, 0x22, 0xaa, 0xbb, 0xcc, 0x32, 0xaa, 0xbb, 0xcc,
    175      // clang-format on
    176  };
    177  EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(message, 3, 0));
    178 }
    179 
    180 // Test without any packet options variables set. This method should return
    181 // without HMAC value in the packet.
    182 TEST(RtpUtilsTest, ApplyPacketOptionsWithDefaultValues) {
    183  PacketTimeUpdateParams packet_time_params;
    184  std::vector<uint8_t> rtp_packet(
    185      kRtpMsgWithOneByteAbsSendTimeExtension,
    186      kRtpMsgWithOneByteAbsSendTimeExtension +
    187          sizeof(kRtpMsgWithOneByteAbsSendTimeExtension));
    188  rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag));
    189  EXPECT_TRUE(ApplyPacketOptions(rtp_packet, packet_time_params, 0));
    190 
    191  // Making sure HMAC wasn't updated..
    192  EXPECT_EQ(0,
    193            memcmp(&rtp_packet[sizeof(kRtpMsgWithOneByteAbsSendTimeExtension)],
    194                   kFakeTag, 4));
    195 
    196  // Verify AbsouluteSendTime extension field wasn't modified.
    197  EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInOneByteRtpMsg], kTestAstValue,
    198                      sizeof(kTestAstValue)));
    199 }
    200 
    201 // Veirfy HMAC is updated when packet option parameters are set.
    202 TEST(RtpUtilsTest, ApplyPacketOptionsWithAuthParams) {
    203  PacketTimeUpdateParams packet_time_params;
    204  packet_time_params.srtp_auth_key.assign(kTestKey,
    205                                          kTestKey + sizeof(kTestKey));
    206  packet_time_params.srtp_auth_tag_len = 4;
    207 
    208  std::vector<uint8_t> rtp_packet(
    209      kRtpMsgWithOneByteAbsSendTimeExtension,
    210      kRtpMsgWithOneByteAbsSendTimeExtension +
    211          sizeof(kRtpMsgWithOneByteAbsSendTimeExtension));
    212  rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag));
    213  EXPECT_TRUE(ApplyPacketOptions(rtp_packet, packet_time_params, 0));
    214 
    215  uint8_t kExpectedTag[] = {0xc1, 0x7a, 0x8c, 0xa0};
    216  EXPECT_EQ(0,
    217            memcmp(&rtp_packet[sizeof(kRtpMsgWithOneByteAbsSendTimeExtension)],
    218                   kExpectedTag, sizeof(kExpectedTag)));
    219 
    220  // Verify AbsouluteSendTime extension field is not modified.
    221  EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInOneByteRtpMsg], kTestAstValue,
    222                      sizeof(kTestAstValue)));
    223 }
    224 
    225 // Verify finding an extension ID in a raw rtp message.
    226 TEST(RtpUtilsTest, UpdateOneByteAbsSendTimeExtensionInRtpPacket) {
    227  std::vector<uint8_t> rtp_packet(
    228      kRtpMsgWithOneByteAbsSendTimeExtension,
    229      kRtpMsgWithOneByteAbsSendTimeExtension +
    230          sizeof(kRtpMsgWithOneByteAbsSendTimeExtension));
    231 
    232  EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(rtp_packet, 3, 51183266));
    233 
    234  // Verify that the timestamp was updated.
    235  const uint8_t kExpectedTimestamp[3] = {0xcc, 0xbb, 0xaa};
    236  EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInOneByteRtpMsg], kExpectedTimestamp,
    237                      sizeof(kExpectedTimestamp)));
    238 }
    239 
    240 // Verify finding an extension ID in a raw rtp message.
    241 TEST(RtpUtilsTest, UpdateTwoByteAbsSendTimeExtensionInRtpPacket) {
    242  std::vector<uint8_t> rtp_packet(
    243      kRtpMsgWithTwoByteAbsSendTimeExtension,
    244      kRtpMsgWithTwoByteAbsSendTimeExtension +
    245          sizeof(kRtpMsgWithTwoByteAbsSendTimeExtension));
    246 
    247  EXPECT_TRUE(UpdateRtpAbsSendTimeExtension(rtp_packet, 3, 51183266));
    248 
    249  // Verify that the timestamp was updated.
    250  const uint8_t kExpectedTimestamp[3] = {0xcc, 0xbb, 0xaa};
    251  EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInTwoByteRtpMsg], kExpectedTimestamp,
    252                      sizeof(kExpectedTimestamp)));
    253 }
    254 
    255 // Verify we update both AbsSendTime extension header and HMAC.
    256 TEST(RtpUtilsTest, ApplyPacketOptionsWithAuthParamsAndAbsSendTime) {
    257  PacketTimeUpdateParams packet_time_params;
    258  packet_time_params.srtp_auth_key.assign(kTestKey,
    259                                          kTestKey + sizeof(kTestKey));
    260  packet_time_params.srtp_auth_tag_len = 4;
    261  packet_time_params.rtp_sendtime_extension_id = 3;
    262  // 3 is also present in the test message.
    263 
    264  std::vector<uint8_t> rtp_packet(
    265      kRtpMsgWithOneByteAbsSendTimeExtension,
    266      kRtpMsgWithOneByteAbsSendTimeExtension +
    267          sizeof(kRtpMsgWithOneByteAbsSendTimeExtension));
    268  rtp_packet.insert(rtp_packet.end(), kFakeTag, kFakeTag + sizeof(kFakeTag));
    269  EXPECT_TRUE(ApplyPacketOptions(rtp_packet, packet_time_params, 51183266));
    270 
    271  const uint8_t kExpectedTag[] = {0x81, 0xd1, 0x2c, 0x0e};
    272  EXPECT_EQ(0,
    273            memcmp(&rtp_packet[sizeof(kRtpMsgWithOneByteAbsSendTimeExtension)],
    274                   kExpectedTag, sizeof(kExpectedTag)));
    275 
    276  // Verify that the timestamp was updated.
    277  const uint8_t kExpectedTimestamp[3] = {0xcc, 0xbb, 0xaa};
    278  EXPECT_EQ(0, memcmp(&rtp_packet[kAstIndexInOneByteRtpMsg], kExpectedTimestamp,
    279                      sizeof(kExpectedTimestamp)));
    280 }
    281 
    282 TEST(RtpUtilsTest, InferRtpPacketType) {
    283  EXPECT_EQ(RtpPacketType::kRtp, InferRtpPacketType(kPcmuFrameArrayView));
    284  EXPECT_EQ(RtpPacketType::kRtcp, InferRtpPacketType(kRtcpReportArrayView));
    285  EXPECT_EQ(RtpPacketType::kUnknown,
    286            InferRtpPacketType(kInvalidPacketArrayView));
    287 }
    288 
    289 }  // namespace webrtc