xref: /aosp_15_r20/external/webrtc/modules/rtp_rtcp/source/rtcp_packet/app_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2015 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/rtcp_packet/app.h"
12 
13 #include "test/gmock.h"
14 #include "test/gtest.h"
15 #include "test/rtcp_packet_parser.h"
16 
17 namespace webrtc {
18 namespace {
19 
20 using ::testing::ElementsAreArray;
21 using ::testing::make_tuple;
22 using ::webrtc::rtcp::App;
23 
24 constexpr uint32_t kName = ((uint32_t)'n' << 24) | ((uint32_t)'a' << 16) |
25                            ((uint32_t)'m' << 8) | (uint32_t)'e';
26 constexpr uint8_t kSubtype = 0x1e;
27 constexpr uint32_t kSenderSsrc = 0x12345678;
28 constexpr uint8_t kData[] = {'t', 'e', 's', 't', 'd', 'a', 't', 'a'};
29 constexpr uint8_t kVersionBits = 2 << 6;
30 constexpr uint8_t kPaddingBit = 1 << 5;
31 // clang-format off
32 constexpr uint8_t kPacketWithoutData[] = {
33     kVersionBits | kSubtype, App::kPacketType, 0x00, 0x02,
34     0x12, 0x34, 0x56, 0x78,
35     'n',  'a',  'm',  'e'};
36 constexpr uint8_t kPacketWithData[] = {
37     kVersionBits | kSubtype, App::kPacketType, 0x00, 0x04,
38     0x12, 0x34, 0x56, 0x78,
39     'n',  'a',  'm',  'e',
40     't',  'e',  's',  't',
41     'd',  'a',  't',  'a'};
42 constexpr uint8_t kTooSmallPacket[] = {
43     kVersionBits | kSubtype, App::kPacketType, 0x00, 0x01,
44     0x12, 0x34, 0x56, 0x78};
45 constexpr uint8_t kPaddingSize = 1;
46 constexpr uint8_t kPacketWithUnalignedPayload[] = {
47     kVersionBits | kPaddingBit | kSubtype, App::kPacketType, 0x00, 0x03,
48     0x12, 0x34, 0x56, 0x78,
49      'n',  'a',  'm',  'e',
50      'd',  'a',  't', kPaddingSize};
51 // clang-format on
52 }  // namespace
53 
TEST(RtcpPacketAppTest,CreateWithoutData)54 TEST(RtcpPacketAppTest, CreateWithoutData) {
55   App app;
56   app.SetSenderSsrc(kSenderSsrc);
57   app.SetSubType(kSubtype);
58   app.SetName(kName);
59 
60   rtc::Buffer raw = app.Build();
61 
62   EXPECT_THAT(make_tuple(raw.data(), raw.size()),
63               ElementsAreArray(kPacketWithoutData));
64 }
65 
TEST(RtcpPacketAppTest,ParseWithoutData)66 TEST(RtcpPacketAppTest, ParseWithoutData) {
67   App parsed;
68   EXPECT_TRUE(test::ParseSinglePacket(kPacketWithoutData, &parsed));
69 
70   EXPECT_EQ(kSenderSsrc, parsed.sender_ssrc());
71   EXPECT_EQ(kSubtype, parsed.sub_type());
72   EXPECT_EQ(kName, parsed.name());
73   EXPECT_EQ(0u, parsed.data_size());
74 }
75 
TEST(RtcpPacketAppTest,CreateWithData)76 TEST(RtcpPacketAppTest, CreateWithData) {
77   App app;
78   app.SetSenderSsrc(kSenderSsrc);
79   app.SetSubType(kSubtype);
80   app.SetName(kName);
81   app.SetData(kData, sizeof(kData));
82 
83   rtc::Buffer raw = app.Build();
84 
85   EXPECT_THAT(make_tuple(raw.data(), raw.size()),
86               ElementsAreArray(kPacketWithData));
87 }
88 
TEST(RtcpPacketAppTest,ParseWithData)89 TEST(RtcpPacketAppTest, ParseWithData) {
90   App parsed;
91   EXPECT_TRUE(test::ParseSinglePacket(kPacketWithData, &parsed));
92 
93   EXPECT_EQ(kSenderSsrc, parsed.sender_ssrc());
94   EXPECT_EQ(kSubtype, parsed.sub_type());
95   EXPECT_EQ(kName, parsed.name());
96   EXPECT_THAT(make_tuple(parsed.data(), parsed.data_size()),
97               ElementsAreArray(kData));
98 }
99 
TEST(RtcpPacketAppTest,ParseFailsOnTooSmallPacket)100 TEST(RtcpPacketAppTest, ParseFailsOnTooSmallPacket) {
101   App parsed;
102   EXPECT_FALSE(test::ParseSinglePacket(kTooSmallPacket, &parsed));
103 }
104 
TEST(RtcpPacketAppTest,ParseFailsOnUnalignedPayload)105 TEST(RtcpPacketAppTest, ParseFailsOnUnalignedPayload) {
106   App parsed;
107   EXPECT_FALSE(test::ParseSinglePacket(kPacketWithUnalignedPayload, &parsed));
108 }
109 
110 }  // namespace webrtc
111