blob: f7bbf8b8d4308da7c2535f905977ac092bb4e5de [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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "media/base/turn_utils.h"
Sergey Ulanovdc305db2016-01-14 17:14:54 -080012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include "test/gtest.h"
Sergey Ulanovdc305db2016-01-14 17:14:54 -080014
15namespace cricket {
16
17// Invalid TURN send indication messages. Messages are proper STUN
18// messages with incorrect values in attributes.
19TEST(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 Gerey665174f2018-06-19 15:03:05 +020051 // clang-format off
52 // clang formatting doesn't respect inline comments.
Sergey Ulanovdc305db2016-01-14 17:14:54 -080053 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 Gerey665174f2018-06-19 15:03:05 +020059 // clang-format on
Sergey Ulanovdc305db2016-01-14 17:14:54 -080060 };
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.
70TEST(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 Gerey665174f2018-06-19 15:03:05 +020076 // clang-format off
77 // clang formatting doesn't respect inline comments.
Sergey Ulanovdc305db2016-01-14 17:14:54 -080078 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 Gerey665174f2018-06-19 15:03:05 +020087 // clang-format on
Sergey Ulanovdc305db2016-01-14 17:14:54 -080088 };
Yves Gerey665174f2018-06-19 15:03:05 +020089 EXPECT_TRUE(
90 UnwrapTurnPacket(kTurnSendIndicationMsgWithoutRtpExtension,
91 sizeof(kTurnSendIndicationMsgWithoutRtpExtension),
92 &content_pos, &content_size));
Sergey Ulanovdc305db2016-01-14 17:14:54 -080093 EXPECT_EQ(12U, content_size);
94 EXPECT_EQ(32U, content_pos);
95}
96
97// Verify that parsing of valid TURN Channel Messages.
98TEST(TurnUtilsTest, ValidTurnChannelMessages) {
99 const uint8_t kTurnChannelMsgWithRtpPacket[] = {
Yves Gerey665174f2018-06-19 15:03:05 +0200100 // clang-format off
101 // clang formatting doesn't respect inline comments.
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800102 0x40, 0x00, 0x00, 0x0C,
103 0x80, 0x00, 0x00, 0x00, // RTP packet.
104 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Yves Gerey665174f2018-06-19 15:03:05 +0200105 // clang-format on
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800106 };
107
108 size_t content_pos = 0, content_size = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200109 EXPECT_TRUE(UnwrapTurnPacket(kTurnChannelMsgWithRtpPacket,
110 sizeof(kTurnChannelMsgWithRtpPacket),
111 &content_pos, &content_size));
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800112 EXPECT_EQ(12U, content_size);
113 EXPECT_EQ(4U, content_pos);
114}
115
116TEST(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 Bonadeif859e552018-05-30 15:31:29 +0200123 EXPECT_EQ(4u, content_pos);
124 EXPECT_EQ(0u, content_size);
Sergey Ulanovdc305db2016-01-14 17:14:54 -0800125}
126
127} // namespace cricket