xref: /aosp_15_r20/external/webrtc/modules/rtp_rtcp/source/rtp_util_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 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 "modules/rtp_rtcp/source/rtp_util.h"
12 
13 #include "test/gmock.h"
14 
15 namespace webrtc {
16 namespace {
17 
TEST(RtpUtilTest,IsRtpPacket)18 TEST(RtpUtilTest, IsRtpPacket) {
19   constexpr uint8_t kMinimalisticRtpPacket[] = {0x80, 97, 0, 0,  //
20                                                 0,    0,  0, 0,  //
21                                                 0,    0,  0, 0};
22   EXPECT_TRUE(IsRtpPacket(kMinimalisticRtpPacket));
23 
24   constexpr uint8_t kWrongRtpVersion[] = {0xc0, 97, 0, 0,  //
25                                           0,    0,  0, 0,  //
26                                           0,    0,  0, 0};
27   EXPECT_FALSE(IsRtpPacket(kWrongRtpVersion));
28 
29   constexpr uint8_t kPacketWithPayloadForRtcp[] = {0x80, 200, 0, 0,  //
30                                                    0,    0,   0, 0,  //
31                                                    0,    0,   0, 0};
32   EXPECT_FALSE(IsRtpPacket(kPacketWithPayloadForRtcp));
33 
34   constexpr uint8_t kTooSmallRtpPacket[] = {0x80, 97, 0, 0,  //
35                                             0,    0,  0, 0,  //
36                                             0,    0,  0};
37   EXPECT_FALSE(IsRtpPacket(kTooSmallRtpPacket));
38 
39   EXPECT_FALSE(IsRtpPacket({}));
40 }
41 
TEST(RtpUtilTest,IsRtcpPacket)42 TEST(RtpUtilTest, IsRtcpPacket) {
43   constexpr uint8_t kMinimalisticRtcpPacket[] = {0x80, 202, 0, 0};
44   EXPECT_TRUE(IsRtcpPacket(kMinimalisticRtcpPacket));
45 
46   constexpr uint8_t kWrongRtpVersion[] = {0xc0, 202, 0, 0};
47   EXPECT_FALSE(IsRtcpPacket(kWrongRtpVersion));
48 
49   constexpr uint8_t kPacketWithPayloadForRtp[] = {0x80, 225, 0, 0};
50   EXPECT_FALSE(IsRtcpPacket(kPacketWithPayloadForRtp));
51 
52   constexpr uint8_t kTooSmallRtcpPacket[] = {0x80, 202, 0};
53   EXPECT_FALSE(IsRtcpPacket(kTooSmallRtcpPacket));
54 
55   EXPECT_FALSE(IsRtcpPacket({}));
56 }
57 
TEST(RtpUtilTest,ParseRtpPayloadType)58 TEST(RtpUtilTest, ParseRtpPayloadType) {
59   constexpr uint8_t kMinimalisticRtpPacket[] = {0x80, 97,   0,    0,  //
60                                                 0,    0,    0,    0,  //
61                                                 0x12, 0x34, 0x56, 0x78};
62   EXPECT_EQ(ParseRtpPayloadType(kMinimalisticRtpPacket), 97);
63 
64   constexpr uint8_t kMinimalisticRtpPacketWithMarker[] = {
65       0x80, 0x80 | 97, 0,    0,  //
66       0,    0,         0,    0,  //
67       0x12, 0x34,      0x56, 0x78};
68   EXPECT_EQ(ParseRtpPayloadType(kMinimalisticRtpPacketWithMarker), 97);
69 }
70 
TEST(RtpUtilTest,ParseRtpSequenceNumber)71 TEST(RtpUtilTest, ParseRtpSequenceNumber) {
72   constexpr uint8_t kMinimalisticRtpPacket[] = {0x80, 97, 0x12, 0x34,  //
73                                                 0,    0,  0,    0,     //
74                                                 0,    0,  0,    0};
75   EXPECT_EQ(ParseRtpSequenceNumber(kMinimalisticRtpPacket), 0x1234);
76 }
77 
TEST(RtpUtilTest,ParseRtpSsrc)78 TEST(RtpUtilTest, ParseRtpSsrc) {
79   constexpr uint8_t kMinimalisticRtpPacket[] = {0x80, 97,   0,    0,  //
80                                                 0,    0,    0,    0,  //
81                                                 0x12, 0x34, 0x56, 0x78};
82   EXPECT_EQ(ParseRtpSsrc(kMinimalisticRtpPacket), 0x12345678u);
83 }
84 
85 }  // namespace
86 }  // namespace webrtc
87