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