tor-browser

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

turn_utils_unittest.cc (5125B)


      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 "media/base/turn_utils.h"
     12 
     13 #include <cstddef>
     14 #include <cstdint>
     15 
     16 #include "test/gtest.h"
     17 
     18 namespace webrtc {
     19 
     20 // Invalid TURN send indication messages. Messages are proper STUN
     21 // messages with incorrect values in attributes.
     22 TEST(TurnUtilsTest, InvalidTurnSendIndicationMessages) {
     23  size_t content_pos = SIZE_MAX;
     24  size_t content_size = SIZE_MAX;
     25 
     26  // Stun Indication message with Zero length
     27  uint8_t kTurnSendIndicationMsgWithNoAttributes[] = {
     28      0x00, 0x16, 0x00, 0x00,  // Zero length
     29      0x21, 0x12, 0xA4, 0x42,  // magic cookie
     30      '0',  '1',  '2',  '3',   // transaction id
     31      '4',  '5',  '6',  '7',  '8', '9', 'a', 'b',
     32  };
     33  EXPECT_FALSE(UnwrapTurnPacket(kTurnSendIndicationMsgWithNoAttributes,
     34                                sizeof(kTurnSendIndicationMsgWithNoAttributes),
     35                                &content_pos, &content_size));
     36  EXPECT_EQ(SIZE_MAX, content_pos);
     37  EXPECT_EQ(SIZE_MAX, content_size);
     38 
     39  // Stun Send Indication message with invalid length in stun header.
     40  const uint8_t kTurnSendIndicationMsgWithInvalidLength[] = {
     41      0x00, 0x16, 0xFF, 0x00,  // length of 0xFF00
     42      0x21, 0x12, 0xA4, 0x42,  // magic cookie
     43      '0',  '1',  '2',  '3',   // transaction id
     44      '4',  '5',  '6',  '7',  '8', '9', 'a', 'b',
     45  };
     46  EXPECT_FALSE(UnwrapTurnPacket(kTurnSendIndicationMsgWithInvalidLength,
     47                                sizeof(kTurnSendIndicationMsgWithInvalidLength),
     48                                &content_pos, &content_size));
     49  EXPECT_EQ(SIZE_MAX, content_pos);
     50  EXPECT_EQ(SIZE_MAX, content_size);
     51 
     52  // Stun Send Indication message with no DATA attribute in message.
     53  const uint8_t kTurnSendIndicatinMsgWithNoDataAttribute[] = {
     54      // clang-format off
     55      // clang formatting doesn't respect inline comments.
     56      0x00, 0x16, 0x00, 0x08,  // length of
     57      0x21, 0x12, 0xA4, 0x42,  // magic cookie
     58      '0',  '1',  '2',  '3',   // transaction id
     59      '4',  '5',  '6',  '7',  '8',  '9', 'a',  'b',
     60      0x00, 0x20, 0x00, 0x04,  // Mapped address.
     61      0x00, 0x00, 0x00, 0x00,
     62      // clang-format on
     63  };
     64  EXPECT_FALSE(
     65      UnwrapTurnPacket(kTurnSendIndicatinMsgWithNoDataAttribute,
     66                       sizeof(kTurnSendIndicatinMsgWithNoDataAttribute),
     67                       &content_pos, &content_size));
     68  EXPECT_EQ(SIZE_MAX, content_pos);
     69  EXPECT_EQ(SIZE_MAX, content_size);
     70 }
     71 
     72 // Valid TURN Send Indication messages.
     73 TEST(TurnUtilsTest, ValidTurnSendIndicationMessage) {
     74  size_t content_pos = SIZE_MAX;
     75  size_t content_size = SIZE_MAX;
     76  // A valid STUN indication message with a valid RTP header in data attribute
     77  // payload field and no extension bit set.
     78  const uint8_t kTurnSendIndicationMsgWithoutRtpExtension[] = {
     79      // clang-format off
     80      // clang formatting doesn't respect inline comments.
     81      0x00, 0x16, 0x00, 0x18,  // length of
     82      0x21, 0x12, 0xA4, 0x42,  // magic cookie
     83      '0',  '1',  '2',  '3',   // transaction id
     84      '4',  '5',  '6',  '7',  '8',  '9',  'a',  'b',
     85      0x00, 0x20, 0x00, 0x04,  // Mapped address.
     86      0x00, 0x00, 0x00, 0x00,
     87      0x00, 0x13, 0x00, 0x0C,  // Data attribute.
     88      0x80, 0x00, 0x00, 0x00,  // RTP packet.
     89      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     90      // clang-format on
     91  };
     92  EXPECT_TRUE(
     93      UnwrapTurnPacket(kTurnSendIndicationMsgWithoutRtpExtension,
     94                       sizeof(kTurnSendIndicationMsgWithoutRtpExtension),
     95                       &content_pos, &content_size));
     96  EXPECT_EQ(12U, content_size);
     97  EXPECT_EQ(32U, content_pos);
     98 }
     99 
    100 // Verify that parsing of valid TURN Channel Messages.
    101 TEST(TurnUtilsTest, ValidTurnChannelMessages) {
    102  const uint8_t kTurnChannelMsgWithRtpPacket[] = {
    103      // clang-format off
    104      // clang formatting doesn't respect inline comments.
    105      0x40, 0x00, 0x00, 0x0C,
    106      0x80, 0x00, 0x00, 0x00,  // RTP packet.
    107      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    108      // clang-format on
    109  };
    110 
    111  size_t content_pos = 0, content_size = 0;
    112  EXPECT_TRUE(UnwrapTurnPacket(kTurnChannelMsgWithRtpPacket,
    113                               sizeof(kTurnChannelMsgWithRtpPacket),
    114                               &content_pos, &content_size));
    115  EXPECT_EQ(12U, content_size);
    116  EXPECT_EQ(4U, content_pos);
    117 }
    118 
    119 TEST(TurnUtilsTest, ChannelMessageZeroLength) {
    120  const uint8_t kTurnChannelMsgWithZeroLength[] = {0x40, 0x00, 0x00, 0x00};
    121  size_t content_pos = SIZE_MAX;
    122  size_t content_size = SIZE_MAX;
    123  EXPECT_TRUE(UnwrapTurnPacket(kTurnChannelMsgWithZeroLength,
    124                               sizeof(kTurnChannelMsgWithZeroLength),
    125                               &content_pos, &content_size));
    126  EXPECT_EQ(4u, content_pos);
    127  EXPECT_EQ(0u, content_size);
    128 }
    129 
    130 }  // namespace webrtc