tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

app_unittest.cc (3433B)


      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/app.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 namespace webrtc {
     21 namespace {
     22 
     23 using rtcp::App;
     24 using ::testing::ElementsAreArray;
     25 using ::testing::make_tuple;
     26 
     27 constexpr uint32_t kName = ((uint32_t)'n' << 24) | ((uint32_t)'a' << 16) |
     28                           ((uint32_t)'m' << 8) | (uint32_t)'e';
     29 constexpr uint8_t kSubtype = 0x1e;
     30 constexpr uint32_t kSenderSsrc = 0x12345678;
     31 constexpr uint8_t kData[] = {'t', 'e', 's', 't', 'd', 'a', 't', 'a'};
     32 constexpr uint8_t kVersionBits = 2 << 6;
     33 constexpr uint8_t kPaddingBit = 1 << 5;
     34 // clang-format off
     35 constexpr uint8_t kPacketWithoutData[] = {
     36    kVersionBits | kSubtype, App::kPacketType, 0x00, 0x02,
     37    0x12, 0x34, 0x56, 0x78,
     38    'n',  'a',  'm',  'e'};
     39 constexpr uint8_t kPacketWithData[] = {
     40    kVersionBits | kSubtype, App::kPacketType, 0x00, 0x04,
     41    0x12, 0x34, 0x56, 0x78,
     42    'n',  'a',  'm',  'e',
     43    't',  'e',  's',  't',
     44    'd',  'a',  't',  'a'};
     45 constexpr uint8_t kTooSmallPacket[] = {
     46    kVersionBits | kSubtype, App::kPacketType, 0x00, 0x01,
     47    0x12, 0x34, 0x56, 0x78};
     48 constexpr uint8_t kPaddingSize = 1;
     49 constexpr uint8_t kPacketWithUnalignedPayload[] = {
     50    kVersionBits | kPaddingBit | kSubtype, App::kPacketType, 0x00, 0x03,
     51    0x12, 0x34, 0x56, 0x78,
     52     'n',  'a',  'm',  'e',
     53     'd',  'a',  't', kPaddingSize};
     54 // clang-format on
     55 }  // namespace
     56 
     57 TEST(RtcpPacketAppTest, CreateWithoutData) {
     58  App app;
     59  app.SetSenderSsrc(kSenderSsrc);
     60  app.SetSubType(kSubtype);
     61  app.SetName(kName);
     62 
     63  Buffer raw = app.Build();
     64 
     65  EXPECT_THAT(make_tuple(raw.data(), raw.size()),
     66              ElementsAreArray(kPacketWithoutData));
     67 }
     68 
     69 TEST(RtcpPacketAppTest, ParseWithoutData) {
     70  App parsed;
     71  EXPECT_TRUE(test::ParseSinglePacket(kPacketWithoutData, &parsed));
     72 
     73  EXPECT_EQ(kSenderSsrc, parsed.sender_ssrc());
     74  EXPECT_EQ(kSubtype, parsed.sub_type());
     75  EXPECT_EQ(kName, parsed.name());
     76  EXPECT_EQ(0u, parsed.data_size());
     77 }
     78 
     79 TEST(RtcpPacketAppTest, CreateWithData) {
     80  App app;
     81  app.SetSenderSsrc(kSenderSsrc);
     82  app.SetSubType(kSubtype);
     83  app.SetName(kName);
     84  app.SetData(kData, sizeof(kData));
     85 
     86  Buffer raw = app.Build();
     87 
     88  EXPECT_THAT(make_tuple(raw.data(), raw.size()),
     89              ElementsAreArray(kPacketWithData));
     90 }
     91 
     92 TEST(RtcpPacketAppTest, ParseWithData) {
     93  App parsed;
     94  EXPECT_TRUE(test::ParseSinglePacket(kPacketWithData, &parsed));
     95 
     96  EXPECT_EQ(kSenderSsrc, parsed.sender_ssrc());
     97  EXPECT_EQ(kSubtype, parsed.sub_type());
     98  EXPECT_EQ(kName, parsed.name());
     99  EXPECT_THAT(make_tuple(parsed.data(), parsed.data_size()),
    100              ElementsAreArray(kData));
    101 }
    102 
    103 TEST(RtcpPacketAppTest, ParseFailsOnTooSmallPacket) {
    104  App parsed;
    105  EXPECT_FALSE(test::ParseSinglePacket(kTooSmallPacket, &parsed));
    106 }
    107 
    108 TEST(RtcpPacketAppTest, ParseFailsOnUnalignedPayload) {
    109  App parsed;
    110  EXPECT_FALSE(test::ParseSinglePacket(kPacketWithUnalignedPayload, &parsed));
    111 }
    112 
    113 }  // namespace webrtc