dlrr_unittest.cc (3027B)
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/dlrr.h" 12 13 #include <cstddef> 14 #include <cstdint> 15 #include <cstring> 16 17 #include "modules/rtp_rtcp/source/byte_io.h" 18 #include "test/gtest.h" 19 20 using webrtc::rtcp::Dlrr; 21 using webrtc::rtcp::ReceiveTimeInfo; 22 23 namespace webrtc { 24 namespace { 25 constexpr uint32_t kSsrc = 0x12345678; 26 constexpr uint32_t kLastRR = 0x23344556; 27 constexpr uint32_t kDelay = 0x33343536; 28 constexpr uint8_t kBlock[] = {0x05, 0x00, 0x00, 0x03, 0x12, 0x34, 0x56, 0x78, 29 0x23, 0x34, 0x45, 0x56, 0x33, 0x34, 0x35, 0x36}; 30 constexpr size_t kBlockSizeBytes = sizeof(kBlock); 31 } // namespace 32 33 TEST(RtcpPacketDlrrTest, Empty) { 34 Dlrr dlrr; 35 36 EXPECT_EQ(0u, dlrr.BlockLength()); 37 } 38 39 TEST(RtcpPacketDlrrTest, Create) { 40 Dlrr dlrr; 41 dlrr.AddDlrrItem(ReceiveTimeInfo(kSsrc, kLastRR, kDelay)); 42 43 ASSERT_EQ(kBlockSizeBytes, dlrr.BlockLength()); 44 uint8_t buffer[kBlockSizeBytes]; 45 46 dlrr.Create(buffer); 47 EXPECT_EQ(0, memcmp(buffer, kBlock, kBlockSizeBytes)); 48 } 49 50 TEST(RtcpPacketDlrrTest, Parse) { 51 Dlrr dlrr; 52 uint16_t block_length = ByteReader<uint16_t>::ReadBigEndian(&kBlock[2]); 53 EXPECT_TRUE(dlrr.Parse(kBlock, block_length)); 54 55 EXPECT_EQ(1u, dlrr.sub_blocks().size()); 56 const ReceiveTimeInfo& block = dlrr.sub_blocks().front(); 57 EXPECT_EQ(kSsrc, block.ssrc); 58 EXPECT_EQ(kLastRR, block.last_rr); 59 EXPECT_EQ(kDelay, block.delay_since_last_rr); 60 } 61 62 TEST(RtcpPacketDlrrTest, ParseFailsOnBadSize) { 63 const size_t kBigBufferSize = 0x100; // More than enough. 64 uint8_t buffer[kBigBufferSize]; 65 buffer[0] = Dlrr::kBlockType; 66 buffer[1] = 0; // Reserved. 67 buffer[2] = 0; // Most significant size byte. 68 for (uint8_t size = 3; size < 6; ++size) { 69 buffer[3] = size; 70 Dlrr dlrr; 71 // Parse should be successful only when size is multiple of 3. 72 EXPECT_EQ(size % 3 == 0, dlrr.Parse(buffer, static_cast<uint16_t>(size))); 73 } 74 } 75 76 TEST(RtcpPacketDlrrTest, CreateAndParseManySubBlocks) { 77 const size_t kBufferSize = 0x1000; // More than enough. 78 const size_t kManyDlrrItems = 50; 79 uint8_t buffer[kBufferSize]; 80 81 // Create. 82 Dlrr dlrr; 83 for (size_t i = 1; i <= kManyDlrrItems; ++i) 84 dlrr.AddDlrrItem(ReceiveTimeInfo(kSsrc + i, kLastRR + i, kDelay + i)); 85 size_t used_buffer_size = dlrr.BlockLength(); 86 ASSERT_LE(used_buffer_size, kBufferSize); 87 dlrr.Create(buffer); 88 89 // Parse. 90 Dlrr parsed; 91 uint16_t block_length = ByteReader<uint16_t>::ReadBigEndian(&buffer[2]); 92 EXPECT_EQ(used_buffer_size, (block_length + 1) * 4u); 93 EXPECT_TRUE(parsed.Parse(buffer, block_length)); 94 EXPECT_EQ(kManyDlrrItems, parsed.sub_blocks().size()); 95 } 96 } // namespace webrtc