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