blob: 86cdc47a5b1d5021c7333ec501b0eb5ba5de514d [file] [log] [blame]
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -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.
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/sctputils.h"
12#include "rtc_base/bytebuffer.h"
13#include "rtc_base/copyonwritebuffer.h"
14#include "rtc_base/gunit.h"
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000015
16class SctpUtilsTest : public testing::Test {
17 public:
ossud4d2f602016-11-08 02:05:32 -080018 void VerifyOpenMessageFormat(const rtc::CopyOnWriteBuffer& packet,
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000019 const std::string& label,
20 const webrtc::DataChannelInit& config) {
Peter Boström0c4e06b2015-10-07 12:23:21 +020021 uint8_t message_type;
22 uint8_t channel_type;
23 uint32_t reliability;
24 uint16_t priority;
25 uint16_t label_length;
26 uint16_t protocol_length;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000027
ossud4d2f602016-11-08 02:05:32 -080028 rtc::ByteBufferReader buffer(packet.data<char>(), packet.size());
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000029 ASSERT_TRUE(buffer.ReadUInt8(&message_type));
30 EXPECT_EQ(0x03, message_type);
31
32 ASSERT_TRUE(buffer.ReadUInt8(&channel_type));
33 if (config.ordered) {
34 EXPECT_EQ(config.maxRetransmits > -1 ?
35 0x01 : (config.maxRetransmitTime > -1 ? 0x02 : 0),
36 channel_type);
37 } else {
38 EXPECT_EQ(config.maxRetransmits > -1 ?
39 0x81 : (config.maxRetransmitTime > -1 ? 0x82 : 0x80),
40 channel_type);
41 }
42
wu@webrtc.org97077a32013-10-25 21:18:33 +000043 ASSERT_TRUE(buffer.ReadUInt16(&priority));
44
45 ASSERT_TRUE(buffer.ReadUInt32(&reliability));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000046 if (config.maxRetransmits > -1 || config.maxRetransmitTime > -1) {
47 EXPECT_EQ(config.maxRetransmits > -1 ?
48 config.maxRetransmits : config.maxRetransmitTime,
wu@webrtc.org97077a32013-10-25 21:18:33 +000049 static_cast<int>(reliability));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000050 }
51
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000052 ASSERT_TRUE(buffer.ReadUInt16(&label_length));
53 ASSERT_TRUE(buffer.ReadUInt16(&protocol_length));
54 EXPECT_EQ(label.size(), label_length);
55 EXPECT_EQ(config.protocol.size(), protocol_length);
56
57 std::string label_output;
58 ASSERT_TRUE(buffer.ReadString(&label_output, label_length));
59 EXPECT_EQ(label, label_output);
60 std::string protocol_output;
61 ASSERT_TRUE(buffer.ReadString(&protocol_output, protocol_length));
62 EXPECT_EQ(config.protocol, protocol_output);
63 }
64};
65
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000066TEST_F(SctpUtilsTest, WriteParseOpenMessageWithOrderedReliable) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000067 webrtc::DataChannelInit config;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000068 std::string label = "abc";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000069 config.protocol = "y";
70
ossud4d2f602016-11-08 02:05:32 -080071 rtc::CopyOnWriteBuffer packet;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000072 ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000073
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000074 VerifyOpenMessageFormat(packet, label, config);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000075
76 std::string output_label;
77 webrtc::DataChannelInit output_config;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000078 ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000079 packet, &output_label, &output_config));
80
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000081 EXPECT_EQ(label, output_label);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000082 EXPECT_EQ(config.protocol, output_config.protocol);
83 EXPECT_EQ(config.ordered, output_config.ordered);
84 EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime);
85 EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits);
86}
87
88TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmitTime) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000089 webrtc::DataChannelInit config;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000090 std::string label = "abc";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000091 config.ordered = false;
92 config.maxRetransmitTime = 10;
93 config.protocol = "y";
94
ossud4d2f602016-11-08 02:05:32 -080095 rtc::CopyOnWriteBuffer packet;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000096 ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000097
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000098 VerifyOpenMessageFormat(packet, label, config);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000099
100 std::string output_label;
101 webrtc::DataChannelInit output_config;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000102 ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000103 packet, &output_label, &output_config));
104
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000105 EXPECT_EQ(label, output_label);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000106 EXPECT_EQ(config.protocol, output_config.protocol);
107 EXPECT_EQ(config.ordered, output_config.ordered);
108 EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000109 EXPECT_EQ(-1, output_config.maxRetransmits);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000110}
111
112TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmits) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000113 webrtc::DataChannelInit config;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000114 std::string label = "abc";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000115 config.maxRetransmits = 10;
116 config.protocol = "y";
117
ossud4d2f602016-11-08 02:05:32 -0800118 rtc::CopyOnWriteBuffer packet;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000119 ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000120
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000121 VerifyOpenMessageFormat(packet, label, config);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000122
123 std::string output_label;
124 webrtc::DataChannelInit output_config;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000125 ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000126 packet, &output_label, &output_config));
127
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000128 EXPECT_EQ(label, output_label);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000129 EXPECT_EQ(config.protocol, output_config.protocol);
130 EXPECT_EQ(config.ordered, output_config.ordered);
131 EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000132 EXPECT_EQ(-1, output_config.maxRetransmitTime);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000133}
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000134
135TEST_F(SctpUtilsTest, WriteParseAckMessage) {
ossud4d2f602016-11-08 02:05:32 -0800136 rtc::CopyOnWriteBuffer packet;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000137 webrtc::WriteDataChannelOpenAckMessage(&packet);
138
Peter Boström0c4e06b2015-10-07 12:23:21 +0200139 uint8_t message_type;
ossud4d2f602016-11-08 02:05:32 -0800140 rtc::ByteBufferReader buffer(packet.data<char>(), packet.size());
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000141 ASSERT_TRUE(buffer.ReadUInt8(&message_type));
142 EXPECT_EQ(0x02, message_type);
143
144 EXPECT_TRUE(webrtc::ParseDataChannelOpenAckMessage(packet));
145}
deadbeefab9b2d12015-10-14 11:33:11 -0700146
147TEST_F(SctpUtilsTest, TestIsOpenMessage) {
ossud4d2f602016-11-08 02:05:32 -0800148 rtc::CopyOnWriteBuffer open(1);
149 open[0] = 0x03;
deadbeefab9b2d12015-10-14 11:33:11 -0700150 EXPECT_TRUE(webrtc::IsOpenMessage(open));
151
ossud4d2f602016-11-08 02:05:32 -0800152 rtc::CopyOnWriteBuffer openAck(1);
153 openAck[0] = 0x02;
154 EXPECT_FALSE(webrtc::IsOpenMessage(openAck));
deadbeefab9b2d12015-10-14 11:33:11 -0700155
ossud4d2f602016-11-08 02:05:32 -0800156 rtc::CopyOnWriteBuffer invalid(1);
157 invalid[0] = 0x01;
deadbeefab9b2d12015-10-14 11:33:11 -0700158 EXPECT_FALSE(webrtc::IsOpenMessage(invalid));
159
ossud4d2f602016-11-08 02:05:32 -0800160 rtc::CopyOnWriteBuffer empty;
deadbeefab9b2d12015-10-14 11:33:11 -0700161 EXPECT_FALSE(webrtc::IsOpenMessage(empty));
162}