fir_unittest.cc (3284B)
1 /* 2 * Copyright (c) 2016 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/fir.h" 12 13 #include <cstdint> 14 15 #include "rtc_base/buffer.h" 16 #include "test/gmock.h" 17 #include "test/gtest.h" 18 #include "test/rtcp_packet_parser.h" 19 20 using ::testing::AllOf; 21 using ::testing::ElementsAre; 22 using ::testing::ElementsAreArray; 23 using ::testing::Eq; 24 using ::testing::Field; 25 using ::testing::make_tuple; 26 using webrtc::rtcp::Fir; 27 28 namespace webrtc { 29 namespace { 30 31 constexpr uint32_t kSenderSsrc = 0x12345678; 32 constexpr uint32_t kRemoteSsrc = 0x23456789; 33 constexpr uint8_t kSeqNr = 13; 34 // Manually created Fir packet matching constants above. 35 constexpr uint8_t kPacket[] = {0x84, 206, 0x00, 0x04, 0x12, 0x34, 0x56, 36 0x78, 0x00, 0x00, 0x00, 0x00, 0x23, 0x45, 37 0x67, 0x89, 0x0d, 0x00, 0x00, 0x00}; 38 } // namespace 39 40 TEST(RtcpPacketFirTest, Parse) { 41 Fir mutable_parsed; 42 EXPECT_TRUE(test::ParseSinglePacket(kPacket, &mutable_parsed)); 43 const Fir& parsed = mutable_parsed; // Read values from constant object. 44 45 EXPECT_EQ(kSenderSsrc, parsed.sender_ssrc()); 46 EXPECT_THAT(parsed.requests(), 47 ElementsAre(AllOf(Field(&Fir::Request::ssrc, Eq(kRemoteSsrc)), 48 Field(&Fir::Request::seq_nr, Eq(kSeqNr))))); 49 } 50 51 TEST(RtcpPacketFirTest, Create) { 52 Fir fir; 53 fir.SetSenderSsrc(kSenderSsrc); 54 fir.AddRequestTo(kRemoteSsrc, kSeqNr); 55 56 Buffer packet = fir.Build(); 57 58 EXPECT_THAT(make_tuple(packet.data(), packet.size()), 59 ElementsAreArray(kPacket)); 60 } 61 62 TEST(RtcpPacketFirTest, TwoFciEntries) { 63 Fir fir; 64 fir.SetSenderSsrc(kSenderSsrc); 65 fir.AddRequestTo(kRemoteSsrc, kSeqNr); 66 fir.AddRequestTo(kRemoteSsrc + 1, kSeqNr + 1); 67 68 Buffer packet = fir.Build(); 69 Fir parsed; 70 EXPECT_TRUE(test::ParseSinglePacket(packet, &parsed)); 71 72 EXPECT_EQ(kSenderSsrc, parsed.sender_ssrc()); 73 EXPECT_THAT(parsed.requests(), 74 ElementsAre(AllOf(Field(&Fir::Request::ssrc, Eq(kRemoteSsrc)), 75 Field(&Fir::Request::seq_nr, Eq(kSeqNr))), 76 AllOf(Field(&Fir::Request::ssrc, Eq(kRemoteSsrc + 1)), 77 Field(&Fir::Request::seq_nr, Eq(kSeqNr + 1))))); 78 } 79 80 TEST(RtcpPacketFirTest, ParseFailsOnZeroFciEntries) { 81 constexpr uint8_t kPacketWithoutFci[] = {0x84, 206, 0x00, 0x02, 0x12, 0x34, 82 0x56, 0x78, 0x00, 0x00, 0x00, 0x00}; 83 Fir parsed; 84 EXPECT_FALSE(test::ParseSinglePacket(kPacketWithoutFci, &parsed)); 85 } 86 87 TEST(RtcpPacketFirTest, ParseFailsOnFractionalFciEntries) { 88 constexpr uint8_t kPacketWithOneAndHalfFci[] = { 89 0x84, 206, 0x00, 0x05, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00, 0x00, 0x00, 90 0x23, 0x45, 0x67, 0x89, 0x0d, 0x00, 0x00, 0x00, 'h', 'a', 'l', 'f'}; 91 92 Fir parsed; 93 EXPECT_FALSE(test::ParseSinglePacket(kPacketWithOneAndHalfFci, &parsed)); 94 } 95 96 } // namespace webrtc