1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <RtcpHeader.h>
18 #include <gtest/gtest.h>
19 
20 class RtcpHeaderTest : public ::testing::Test
21 {
22 public:
23 protected:
SetUp()24     virtual void SetUp() override {}
25 
TearDown()26     virtual void TearDown() override {}
27 };
28 
TEST_F(RtcpHeaderTest,TestVersion)29 TEST_F(RtcpHeaderTest, TestVersion)
30 {
31     RtcpHeader rtcpHeader;
32 
33     // Check default value
34     EXPECT_EQ(rtcpHeader.getVersion(), 0);
35 
36     rtcpHeader.setVersion(100);
37     EXPECT_EQ(rtcpHeader.getVersion(), 0);
38 
39     rtcpHeader.setVersion(-1);
40     EXPECT_EQ(rtcpHeader.getVersion(), 0);
41 
42     rtcpHeader.setVersion(2);
43     EXPECT_EQ(rtcpHeader.getVersion(), 2);
44 }
45 
TEST_F(RtcpHeaderTest,TestPaddingFlag)46 TEST_F(RtcpHeaderTest, TestPaddingFlag)
47 {
48     RtcpHeader rtcpHeader;
49 
50     // Check default value
51     EXPECT_EQ(rtcpHeader.getPadding(), eRTP_FALSE);
52 
53     // Check true value
54     rtcpHeader.setPadding(eRTP_TRUE);
55     EXPECT_EQ(rtcpHeader.getPadding(), eRTP_TRUE);
56 
57     // Check false value
58     rtcpHeader.setPadding(eRTP_FALSE);
59     EXPECT_EQ(rtcpHeader.getPadding(), eRTP_FALSE);
60 
61     // Check default arg value
62     rtcpHeader.setPadding();
63     EXPECT_EQ(rtcpHeader.getPadding(), eRTP_TRUE);
64 }
65 
TEST_F(RtcpHeaderTest,TestReceptionReportCount)66 TEST_F(RtcpHeaderTest, TestReceptionReportCount)
67 {
68     RtcpHeader rtcpHeader;
69 
70     // Check default value
71     EXPECT_EQ(rtcpHeader.getReceptionReportCount(), 0);
72 
73     // Negative case: value more than max allowed.
74     eRtp_Bool res = rtcpHeader.setReceptionReportCount(MAX_RECEPTION_REPORT_COUNT + 1);
75     EXPECT_EQ(res, eRTP_FALSE);
76     EXPECT_EQ(rtcpHeader.getReceptionReportCount(), 0);
77 
78     // Positive case: value within limits.
79     res = rtcpHeader.setReceptionReportCount(MAX_RECEPTION_REPORT_COUNT);
80     EXPECT_EQ(res, eRTP_TRUE);
81     EXPECT_EQ(rtcpHeader.getReceptionReportCount(), MAX_RECEPTION_REPORT_COUNT);
82 }
83 
TEST_F(RtcpHeaderTest,TestPacketType)84 TEST_F(RtcpHeaderTest, TestPacketType)
85 {
86     RtcpHeader rtcpHeader;
87 
88     // Check default value
89     EXPECT_EQ(rtcpHeader.getPacketType(), 0);
90 
91     rtcpHeader.setPacketType(202);
92     EXPECT_EQ(rtcpHeader.getPacketType(), 202);
93 }
94 
TEST_F(RtcpHeaderTest,TestPacketLength)95 TEST_F(RtcpHeaderTest, TestPacketLength)
96 {
97     RtcpHeader rtcpHeader;
98 
99     // Check default value
100     EXPECT_EQ(rtcpHeader.getLength(), 0);
101 
102     rtcpHeader.setLength(202);
103     EXPECT_EQ(rtcpHeader.getLength(), 202);
104 }
105 
TEST_F(RtcpHeaderTest,TestSSRC)106 TEST_F(RtcpHeaderTest, TestSSRC)
107 {
108     RtcpHeader rtcpHeader;
109 
110     // Check default value
111     EXPECT_EQ(rtcpHeader.getSsrc(), 0);
112 
113     rtcpHeader.setSsrc(202);
114     EXPECT_EQ(rtcpHeader.getSsrc(), 202);
115 }
116 
TEST_F(RtcpHeaderTest,TestDecodeRtcpHeader)117 TEST_F(RtcpHeaderTest, TestDecodeRtcpHeader)
118 {
119     RtcpHeader rtcpHeader;
120 
121     RtpDt_UChar pRTCPBuff[] = {0x81, 0xc8, 0x00, 0x06, 0x59, 0x09, 0x41, 0x02};
122 
123     rtcpHeader.decodeRtcpHeader(pRTCPBuff, 8);
124     EXPECT_EQ(rtcpHeader.getVersion(), 2);
125     EXPECT_EQ(rtcpHeader.getPadding(), eRTP_FALSE);
126     EXPECT_EQ(rtcpHeader.getReceptionReportCount(), 1);
127     EXPECT_EQ(rtcpHeader.getPacketType(), 200);
128     EXPECT_EQ(rtcpHeader.getLength(), 6 * RTP_WORD_SIZE);
129     EXPECT_EQ(rtcpHeader.getSsrc(), 0x59094102);
130 
131     RtpDt_UChar pRTCPBuff2[] = {0xFF, 0xFF, 0xFF, 0xFF, 0x59, 0x09, 0x41, 0x02};
132 
133     rtcpHeader.decodeRtcpHeader(pRTCPBuff2, 8);
134     EXPECT_EQ(rtcpHeader.getVersion(), MAX_RTP_VERSION);
135     EXPECT_EQ(rtcpHeader.getPadding(), eRTP_TRUE);
136     EXPECT_EQ(rtcpHeader.getReceptionReportCount(), MAX_RECEPTION_REPORT_COUNT);
137     EXPECT_EQ(rtcpHeader.getPacketType(), 0xFF);
138     EXPECT_EQ(rtcpHeader.getLength(), 0xFFFF * RTP_WORD_SIZE);
139     EXPECT_EQ(rtcpHeader.getSsrc(), 0x59094102);
140 }
141 
TEST_F(RtcpHeaderTest,TestFormRtcpHeader)142 TEST_F(RtcpHeaderTest, TestFormRtcpHeader)
143 {
144     RtcpHeader rtcpHeader;
145 
146     rtcpHeader.setVersion(2);
147     rtcpHeader.setPadding();
148     rtcpHeader.setReceptionReportCount(5);
149     rtcpHeader.setPacketType(200);
150     rtcpHeader.setLength(28);
151     rtcpHeader.setSsrc(0xFFFFFFFF);
152     RtpBuffer rtpBuffer(16, nullptr);
153     rtpBuffer.setLength(0);
154     rtcpHeader.formRtcpHeader(&rtpBuffer);
155 
156     RtpDt_UChar bExpectedRtcpBuff[] = {0xA5, 0xc8, 0x00, 0x06, 0xFF, 0xFF, 0xFF, 0xFF};
157 
158     EXPECT_EQ(rtpBuffer.getLength(), sizeof(bExpectedRtcpBuff));
159     EXPECT_EQ(memcmp(rtpBuffer.getBuffer(), bExpectedRtcpBuff, sizeof(bExpectedRtcpBuff)), 0);
160 }