1 /*
2 * Copyright 2013 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 "pc/sctp_utils.h"
12
13 #include <stdint.h>
14
15 #include "rtc_base/byte_buffer.h"
16 #include "rtc_base/copy_on_write_buffer.h"
17 #include "test/gtest.h"
18
19 class SctpUtilsTest : public ::testing::Test {
20 public:
VerifyOpenMessageFormat(const rtc::CopyOnWriteBuffer & packet,const std::string & label,const webrtc::DataChannelInit & config)21 void VerifyOpenMessageFormat(const rtc::CopyOnWriteBuffer& packet,
22 const std::string& label,
23 const webrtc::DataChannelInit& config) {
24 uint8_t message_type;
25 uint8_t channel_type;
26 uint32_t reliability;
27 uint16_t priority;
28 uint16_t label_length;
29 uint16_t protocol_length;
30
31 rtc::ByteBufferReader buffer(packet.data<char>(), packet.size());
32 ASSERT_TRUE(buffer.ReadUInt8(&message_type));
33 EXPECT_EQ(0x03, message_type);
34
35 ASSERT_TRUE(buffer.ReadUInt8(&channel_type));
36 if (config.ordered) {
37 EXPECT_EQ(
38 config.maxRetransmits ? 0x01 : (config.maxRetransmitTime ? 0x02 : 0),
39 channel_type);
40 } else {
41 EXPECT_EQ(config.maxRetransmits
42 ? 0x81
43 : (config.maxRetransmitTime ? 0x82 : 0x80),
44 channel_type);
45 }
46
47 ASSERT_TRUE(buffer.ReadUInt16(&priority));
48 if (config.priority) {
49 // Exact values are checked by round-trip conversion, but
50 // all values defined are greater than zero.
51 EXPECT_GT(priority, 0);
52 } else {
53 EXPECT_EQ(priority, 0);
54 }
55
56 ASSERT_TRUE(buffer.ReadUInt32(&reliability));
57 if (config.maxRetransmits || config.maxRetransmitTime) {
58 EXPECT_EQ(config.maxRetransmits ? *config.maxRetransmits
59 : *config.maxRetransmitTime,
60 static_cast<int>(reliability));
61 }
62
63 ASSERT_TRUE(buffer.ReadUInt16(&label_length));
64 ASSERT_TRUE(buffer.ReadUInt16(&protocol_length));
65 EXPECT_EQ(label.size(), label_length);
66 EXPECT_EQ(config.protocol.size(), protocol_length);
67
68 std::string label_output;
69 ASSERT_TRUE(buffer.ReadString(&label_output, label_length));
70 EXPECT_EQ(label, label_output);
71 std::string protocol_output;
72 ASSERT_TRUE(buffer.ReadString(&protocol_output, protocol_length));
73 EXPECT_EQ(config.protocol, protocol_output);
74 }
75 };
76
TEST_F(SctpUtilsTest,WriteParseOpenMessageWithOrderedReliable)77 TEST_F(SctpUtilsTest, WriteParseOpenMessageWithOrderedReliable) {
78 webrtc::DataChannelInit config;
79 std::string label = "abc";
80 config.protocol = "y";
81
82 rtc::CopyOnWriteBuffer packet;
83 ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
84
85 VerifyOpenMessageFormat(packet, label, config);
86
87 std::string output_label;
88 webrtc::DataChannelInit output_config;
89 ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
90 &output_config));
91
92 EXPECT_EQ(label, output_label);
93 EXPECT_EQ(config.protocol, output_config.protocol);
94 EXPECT_EQ(config.ordered, output_config.ordered);
95 EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime);
96 EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits);
97 }
98
TEST_F(SctpUtilsTest,WriteParseOpenMessageWithMaxRetransmitTime)99 TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmitTime) {
100 webrtc::DataChannelInit config;
101 std::string label = "abc";
102 config.ordered = false;
103 config.maxRetransmitTime = 10;
104 config.protocol = "y";
105
106 rtc::CopyOnWriteBuffer packet;
107 ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
108
109 VerifyOpenMessageFormat(packet, label, config);
110
111 std::string output_label;
112 webrtc::DataChannelInit output_config;
113 ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
114 &output_config));
115
116 EXPECT_EQ(label, output_label);
117 EXPECT_EQ(config.protocol, output_config.protocol);
118 EXPECT_EQ(config.ordered, output_config.ordered);
119 EXPECT_EQ(*config.maxRetransmitTime, *output_config.maxRetransmitTime);
120 EXPECT_FALSE(output_config.maxRetransmits);
121 }
122
TEST_F(SctpUtilsTest,WriteParseOpenMessageWithMaxRetransmits)123 TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmits) {
124 webrtc::DataChannelInit config;
125 std::string label = "abc";
126 config.maxRetransmits = 10;
127 config.protocol = "y";
128
129 rtc::CopyOnWriteBuffer packet;
130 ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
131
132 VerifyOpenMessageFormat(packet, label, config);
133
134 std::string output_label;
135 webrtc::DataChannelInit output_config;
136 ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
137 &output_config));
138
139 EXPECT_EQ(label, output_label);
140 EXPECT_EQ(config.protocol, output_config.protocol);
141 EXPECT_EQ(config.ordered, output_config.ordered);
142 EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits);
143 EXPECT_FALSE(output_config.maxRetransmitTime);
144 }
145
TEST_F(SctpUtilsTest,WriteParseOpenMessageWithPriority)146 TEST_F(SctpUtilsTest, WriteParseOpenMessageWithPriority) {
147 webrtc::DataChannelInit config;
148 std::string label = "abc";
149 config.protocol = "y";
150 config.priority = webrtc::Priority::kVeryLow;
151
152 rtc::CopyOnWriteBuffer packet;
153 ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
154
155 VerifyOpenMessageFormat(packet, label, config);
156
157 std::string output_label;
158 webrtc::DataChannelInit output_config;
159 ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
160 &output_config));
161
162 EXPECT_EQ(label, output_label);
163 ASSERT_TRUE(output_config.priority);
164 EXPECT_EQ(*config.priority, *output_config.priority);
165 }
166
TEST_F(SctpUtilsTest,WriteParseAckMessage)167 TEST_F(SctpUtilsTest, WriteParseAckMessage) {
168 rtc::CopyOnWriteBuffer packet;
169 webrtc::WriteDataChannelOpenAckMessage(&packet);
170
171 uint8_t message_type;
172 rtc::ByteBufferReader buffer(packet.data<char>(), packet.size());
173 ASSERT_TRUE(buffer.ReadUInt8(&message_type));
174 EXPECT_EQ(0x02, message_type);
175
176 EXPECT_TRUE(webrtc::ParseDataChannelOpenAckMessage(packet));
177 }
178
TEST_F(SctpUtilsTest,TestIsOpenMessage)179 TEST_F(SctpUtilsTest, TestIsOpenMessage) {
180 rtc::CopyOnWriteBuffer open(1);
181 open[0] = 0x03;
182 EXPECT_TRUE(webrtc::IsOpenMessage(open));
183
184 rtc::CopyOnWriteBuffer openAck(1);
185 openAck[0] = 0x02;
186 EXPECT_FALSE(webrtc::IsOpenMessage(openAck));
187
188 rtc::CopyOnWriteBuffer invalid(1);
189 invalid[0] = 0x01;
190 EXPECT_FALSE(webrtc::IsOpenMessage(invalid));
191
192 rtc::CopyOnWriteBuffer empty;
193 EXPECT_FALSE(webrtc::IsOpenMessage(empty));
194 }
195