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/report_block.h"
12 
13 #include <limits>
14 
15 #include "rtc_base/random.h"
16 #include "test/gtest.h"
17 
18 using webrtc::rtcp::ReportBlock;
19 
20 namespace webrtc {
21 namespace {
22 
23 const uint32_t kRemoteSsrc = 0x23456789;
24 const uint8_t kFractionLost = 55;
25 // Use values that are streamed differently LE and BE.
26 const int32_t kCumulativeLost = 0x111213;
27 const uint32_t kExtHighestSeqNum = 0x22232425;
28 const uint32_t kJitter = 0x33343536;
29 const uint32_t kLastSr = 0x44454647;
30 const uint32_t kDelayLastSr = 0x55565758;
31 const size_t kBufferLength = ReportBlock::kLength;
32 
TEST(RtcpPacketReportBlockTest,ParseChecksLength)33 TEST(RtcpPacketReportBlockTest, ParseChecksLength) {
34   uint8_t buffer[kBufferLength];
35   memset(buffer, 0, sizeof(buffer));
36 
37   ReportBlock rb;
38   EXPECT_FALSE(rb.Parse(buffer, kBufferLength - 1));
39   EXPECT_TRUE(rb.Parse(buffer, kBufferLength));
40 }
41 
TEST(RtcpPacketReportBlockTest,ParseAnyData)42 TEST(RtcpPacketReportBlockTest, ParseAnyData) {
43   uint8_t buffer[kBufferLength];
44   // Fill buffer with semi-random data.
45   Random generator(0x256F8A285EC829ull);
46   for (size_t i = 0; i < kBufferLength; ++i)
47     buffer[i] = static_cast<uint8_t>(generator.Rand(0, 0xff));
48 
49   ReportBlock rb;
50   EXPECT_TRUE(rb.Parse(buffer, kBufferLength));
51 }
52 
TEST(RtcpPacketReportBlockTest,ParseMatchCreate)53 TEST(RtcpPacketReportBlockTest, ParseMatchCreate) {
54   ReportBlock rb;
55   rb.SetMediaSsrc(kRemoteSsrc);
56   rb.SetFractionLost(kFractionLost);
57   rb.SetCumulativeLost(kCumulativeLost);
58   rb.SetExtHighestSeqNum(kExtHighestSeqNum);
59   rb.SetJitter(kJitter);
60   rb.SetLastSr(kLastSr);
61   rb.SetDelayLastSr(kDelayLastSr);
62 
63   uint8_t buffer[kBufferLength];
64   rb.Create(buffer);
65 
66   ReportBlock parsed;
67   EXPECT_TRUE(parsed.Parse(buffer, kBufferLength));
68 
69   EXPECT_EQ(kRemoteSsrc, parsed.source_ssrc());
70   EXPECT_EQ(kFractionLost, parsed.fraction_lost());
71   EXPECT_EQ(kCumulativeLost, parsed.cumulative_lost_signed());
72   EXPECT_EQ(kExtHighestSeqNum, parsed.extended_high_seq_num());
73   EXPECT_EQ(kJitter, parsed.jitter());
74   EXPECT_EQ(kLastSr, parsed.last_sr());
75   EXPECT_EQ(kDelayLastSr, parsed.delay_since_last_sr());
76 }
77 
TEST(RtcpPacketReportBlockTest,ValidateCumulativeLost)78 TEST(RtcpPacketReportBlockTest, ValidateCumulativeLost) {
79   // CumulativeLost is a signed 24-bit integer.
80   // However, existing code expects it to be an unsigned integer.
81   // The case of negative values should be unusual; we return 0
82   // when caller wants an unsigned integer.
83   const int32_t kMaxCumulativeLost = 0x7fffff;
84   const int32_t kMinCumulativeLost = -0x800000;
85   ReportBlock rb;
86   EXPECT_FALSE(rb.SetCumulativeLost(kMaxCumulativeLost + 1));
87   EXPECT_TRUE(rb.SetCumulativeLost(kMaxCumulativeLost));
88   EXPECT_FALSE(rb.SetCumulativeLost(kMinCumulativeLost - 1));
89   EXPECT_TRUE(rb.SetCumulativeLost(kMinCumulativeLost));
90   EXPECT_EQ(kMinCumulativeLost, rb.cumulative_lost_signed());
91   EXPECT_EQ(0u, rb.cumulative_lost());
92 }
93 
TEST(RtcpPacketReportBlockTest,ParseNegativeCumulativeLost)94 TEST(RtcpPacketReportBlockTest, ParseNegativeCumulativeLost) {
95   // CumulativeLost is a signed 24-bit integer.
96   const int32_t kNegativeCumulativeLost = -123;
97   ReportBlock rb;
98   EXPECT_TRUE(rb.SetCumulativeLost(kNegativeCumulativeLost));
99 
100   uint8_t buffer[kBufferLength];
101   rb.Create(buffer);
102 
103   ReportBlock parsed;
104   EXPECT_TRUE(parsed.Parse(buffer, kBufferLength));
105 
106   EXPECT_EQ(kNegativeCumulativeLost, parsed.cumulative_lost_signed());
107 }
108 
109 }  // namespace
110 }  // namespace webrtc
111