tor-browser

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

fake_rtp.cc (2660B)


      1 /*
      2 *  Copyright (c) 2017 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/fake_rtp.h"
     12 
     13 #include <cstdint>
     14 #include <cstring>
     15 #include <vector>
     16 
     17 #include "absl/algorithm/container.h"
     18 #include "rtc_base/checks.h"
     19 #include "test/gtest.h"
     20 
     21 void CompareHeaderExtensions(const char* packet1,
     22                             size_t packet1_size,
     23                             const char* packet2,
     24                             size_t packet2_size,
     25                             const std::vector<int>& encrypted_headers,
     26                             bool expect_equal) {
     27  // Sanity check: packets must be large enough to contain the RTP header and
     28  // extensions header.
     29  RTC_CHECK_GE(packet1_size, 12 + 4);
     30  RTC_CHECK_GE(packet2_size, 12 + 4);
     31  // RTP extension headers are the same.
     32  EXPECT_EQ(0, memcmp(packet1 + 12, packet2 + 12, 4));
     33  // Check for one-byte header extensions.
     34  EXPECT_EQ('\xBE', packet1[12]);
     35  EXPECT_EQ('\xDE', packet1[13]);
     36  // Determine position and size of extension headers.
     37  size_t extension_words = packet1[14] << 8 | packet1[15];
     38  const char* extension_data1 = packet1 + 12 + 4;
     39  const char* extension_end1 = extension_data1 + extension_words * 4;
     40  const char* extension_data2 = packet2 + 12 + 4;
     41  // Sanity check: packets must be large enough to contain the RTP header
     42  // extensions.
     43  RTC_CHECK_GE(packet1_size, 12 + 4 + extension_words * 4);
     44  RTC_CHECK_GE(packet2_size, 12 + 4 + extension_words * 4);
     45  while (extension_data1 < extension_end1) {
     46    uint8_t id = (*extension_data1 & 0xf0) >> 4;
     47    uint8_t len = (*extension_data1 & 0x0f) + 1;
     48    extension_data1++;
     49    extension_data2++;
     50    EXPECT_LE(extension_data1, extension_end1);
     51    if (id == 15) {
     52      // Finished parsing.
     53      break;
     54    }
     55 
     56    // The header extension doesn't get encrypted if the id is not in the
     57    // list of header extensions to encrypt.
     58    if (expect_equal || !absl::c_linear_search(encrypted_headers, id)) {
     59      EXPECT_EQ(0, memcmp(extension_data1, extension_data2, len));
     60    } else {
     61      EXPECT_NE(0, memcmp(extension_data1, extension_data2, len));
     62    }
     63 
     64    extension_data1 += len;
     65    extension_data2 += len;
     66    // Skip padding.
     67    while (extension_data1 < extension_end1 && *extension_data1 == 0) {
     68      extension_data1++;
     69      extension_data2++;
     70    }
     71  }
     72 }