blob: 1a9951b7cdaec7d8d8200e7eebd91b118db65bac [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
kjellandera96e2d72016-02-04 23:52:28 -080011#include "webrtc/media/base/turnutils.h"
Sergey Ulanovdc305db2016-01-14 17:14:54 -080012
13#include <stddef.h>
14
15#include "webrtc/base/gunit.h"
16
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[] = {
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.
69TEST(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.
94TEST(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
109TEST(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));
116 EXPECT_EQ(4, content_pos);
117 EXPECT_EQ(0, content_size);
118}
119
120} // namespace cricket