blob: e953c2519c1b46e000ea9b8ae6d8c6e0c14708e3 [file] [log] [blame]
Sergey Ulanovdc305db2016-01-14 17:14:54 -08001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
Sergey Ulanovdc305db2016-01-14 17:14:54 -08003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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 Ulanovdc305db2016-01-14 17:14:54 -08009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "media/base/turnutils.h"
Sergey Ulanovdc305db2016-01-14 17:14:54 -080012
13#include <stddef.h>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/gunit.h"
Sergey Ulanovdc305db2016-01-14 17:14:54 -080016
17namespace cricket {
18
19// Invalid TURN send indication messages. Messages are proper STUN
20// messages with incorrect values in attributes.
21TEST(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[] = {
Yves Gerey665174f2018-06-19 15:03:05 +020053 // clang-format off
54 // clang formatting doesn't respect inline comments.
Sergey Ulanovdc305db2016-01-14 17:14:54 -080055 0x00, 0x16, 0x00, 0x08, // length of
56 0x21, 0x12, 0xA4, 0x42, // magic cookie
57 '0', '1', '2', '3', // transaction id
58 '4', '5', '6', '7', '8', '9', 'a', 'b',
59 0x00, 0x20, 0x00, 0x04, // Mapped address.
60 0x00, 0x00, 0x00, 0x00,
Yves Gerey665174f2018-06-19 15:03:05 +020061 // clang-format on
Sergey Ulanovdc305db2016-01-14 17:14:54 -080062 };
63 EXPECT_FALSE(
64 UnwrapTurnPacket(kTurnSendIndicatinMsgWithNoDataAttribute,
65 sizeof(kTurnSendIndicatinMsgWithNoDataAttribute),
66 &content_pos, &content_size));
67 EXPECT_EQ(SIZE_MAX, content_pos);
68 EXPECT_EQ(SIZE_MAX, content_size);
69}
70
71// Valid TURN Send Indication messages.
72TEST(TurnUtilsTest, ValidTurnSendIndicationMessage) {
73 size_t content_pos = SIZE_MAX;
74 size_t content_size = SIZE_MAX;
75 // A valid STUN indication message with a valid RTP header in data attribute
76 // payload field and no extension bit set.
77 const uint8_t kTurnSendIndicationMsgWithoutRtpExtension[] = {
Yves Gerey665174f2018-06-19 15:03:05 +020078 // clang-format off
79 // clang formatting doesn't respect inline comments.
Sergey Ulanovdc305db2016-01-14 17:14:54 -080080 0x00, 0x16, 0x00, 0x18, // length of
81 0x21, 0x12, 0xA4, 0x42, // magic cookie
82 '0', '1', '2', '3', // transaction id
83 '4', '5', '6', '7', '8', '9', 'a', 'b',
84 0x00, 0x20, 0x00, 0x04, // Mapped address.
85 0x00, 0x00, 0x00, 0x00,
86 0x00, 0x13, 0x00, 0x0C, // Data attribute.
87 0x80, 0x00, 0x00, 0x00, // RTP packet.
88 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Yves Gerey665174f2018-06-19 15:03:05 +020089 // clang-format on
Sergey Ulanovdc305db2016-01-14 17:14:54 -080090 };
Yves Gerey665174f2018-06-19 15:03:05 +020091 EXPECT_TRUE(
92 UnwrapTurnPacket(kTurnSendIndicationMsgWithoutRtpExtension,
93 sizeof(kTurnSendIndicationMsgWithoutRtpExtension),
94 &content_pos, &content_size));
Sergey Ulanovdc305db2016-01-14 17:14:54 -080095 EXPECT_EQ(12U, content_size);
96 EXPECT_EQ(32U, content_pos);
97}
98
99// Verify that parsing of valid TURN Channel Messages.
100TEST(TurnUtilsTest, ValidTurnChannelMessages) {
101 const uint8_t kTurnChannelMsgWithRtpPacket[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200102 // clang-format off
103 // clang formatting doesn't respect inline comments.
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800104 0x40, 0x00, 0x00, 0x0C,
105 0x80, 0x00, 0x00, 0x00, // RTP packet.
106 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Yves Gerey665174f2018-06-19 15:03:05 +0200107 // clang-format on
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800108 };
109
110 size_t content_pos = 0, content_size = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200111 EXPECT_TRUE(UnwrapTurnPacket(kTurnChannelMsgWithRtpPacket,
112 sizeof(kTurnChannelMsgWithRtpPacket),
113 &content_pos, &content_size));
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800114 EXPECT_EQ(12U, content_size);
115 EXPECT_EQ(4U, content_pos);
116}
117
118TEST(TurnUtilsTest, ChannelMessageZeroLength) {
119 const uint8_t kTurnChannelMsgWithZeroLength[] = {0x40, 0x00, 0x00, 0x00};
120 size_t content_pos = SIZE_MAX;
121 size_t content_size = SIZE_MAX;
122 EXPECT_TRUE(UnwrapTurnPacket(kTurnChannelMsgWithZeroLength,
123 sizeof(kTurnChannelMsgWithZeroLength),
124 &content_pos, &content_size));
Mirko Bonadeif859e552018-05-30 15:31:29 +0200125 EXPECT_EQ(4u, content_pos);
126 EXPECT_EQ(0u, content_size);
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800127}
128
129} // namespace cricket