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 "test/gtest.h"
14
15 namespace cricket {
16
17 // Invalid TURN send indication messages. Messages are proper STUN
18 // messages with incorrect values in attributes.
TEST(TurnUtilsTest,InvalidTurnSendIndicationMessages)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[] = {
51 // clang-format off
52 // clang formatting doesn't respect inline comments.
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 // clang-format on
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.
TEST(TurnUtilsTest,ValidTurnSendIndicationMessage)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[] = {
76 // clang-format off
77 // clang formatting doesn't respect inline comments.
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,
87 // clang-format on
88 };
89 EXPECT_TRUE(
90 UnwrapTurnPacket(kTurnSendIndicationMsgWithoutRtpExtension,
91 sizeof(kTurnSendIndicationMsgWithoutRtpExtension),
92 &content_pos, &content_size));
93 EXPECT_EQ(12U, content_size);
94 EXPECT_EQ(32U, content_pos);
95 }
96
97 // Verify that parsing of valid TURN Channel Messages.
TEST(TurnUtilsTest,ValidTurnChannelMessages)98 TEST(TurnUtilsTest, ValidTurnChannelMessages) {
99 const uint8_t kTurnChannelMsgWithRtpPacket[] = {
100 // clang-format off
101 // clang formatting doesn't respect inline comments.
102 0x40, 0x00, 0x00, 0x0C,
103 0x80, 0x00, 0x00, 0x00, // RTP packet.
104 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 // clang-format on
106 };
107
108 size_t content_pos = 0, content_size = 0;
109 EXPECT_TRUE(UnwrapTurnPacket(kTurnChannelMsgWithRtpPacket,
110 sizeof(kTurnChannelMsgWithRtpPacket),
111 &content_pos, &content_size));
112 EXPECT_EQ(12U, content_size);
113 EXPECT_EQ(4U, content_pos);
114 }
115
TEST(TurnUtilsTest,ChannelMessageZeroLength)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));
123 EXPECT_EQ(4u, content_pos);
124 EXPECT_EQ(0u, content_size);
125 }
126
127 } // namespace cricket
128