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