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