blob: 70c627714da99cf1c02624892eafb1ad1abb5f16 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/sctp_utils.h"
Yves Gerey3e707812018-11-28 16:47:49 +010012
13#include <stdint.h>
14
Steve Anton10542f22019-01-11 09:11:00 -080015#include "rtc_base/byte_buffer.h"
16#include "rtc_base/copy_on_write_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "test/gtest.h"
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000018
Mirko Bonadei6a489f22019-04-09 15:11:12 +020019class SctpUtilsTest : public ::testing::Test {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000020 public:
ossud4d2f602016-11-08 02:05:32 -080021 void VerifyOpenMessageFormat(const rtc::CopyOnWriteBuffer& packet,
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000022 const std::string& label,
23 const webrtc::DataChannelInit& config) {
Peter Boström0c4e06b2015-10-07 12:23:21 +020024 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;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000030
ossud4d2f602016-11-08 02:05:32 -080031 rtc::ByteBufferReader buffer(packet.data<char>(), packet.size());
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000032 ASSERT_TRUE(buffer.ReadUInt8(&message_type));
33 EXPECT_EQ(0x03, message_type);
34
35 ASSERT_TRUE(buffer.ReadUInt8(&channel_type));
36 if (config.ordered) {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020037 EXPECT_EQ(
38 config.maxRetransmits ? 0x01 : (config.maxRetransmitTime ? 0x02 : 0),
39 channel_type);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000040 } else {
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020041 EXPECT_EQ(config.maxRetransmits
Yves Gerey665174f2018-06-19 15:03:05 +020042 ? 0x81
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020043 : (config.maxRetransmitTime ? 0x82 : 0x80),
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000044 channel_type);
45 }
46
wu@webrtc.org97077a32013-10-25 21:18:33 +000047 ASSERT_TRUE(buffer.ReadUInt16(&priority));
48
49 ASSERT_TRUE(buffer.ReadUInt32(&reliability));
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020050 if (config.maxRetransmits || config.maxRetransmitTime) {
51 EXPECT_EQ(config.maxRetransmits ? *config.maxRetransmits
52 : *config.maxRetransmitTime,
wu@webrtc.org97077a32013-10-25 21:18:33 +000053 static_cast<int>(reliability));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000054 }
55
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000056 ASSERT_TRUE(buffer.ReadUInt16(&label_length));
57 ASSERT_TRUE(buffer.ReadUInt16(&protocol_length));
58 EXPECT_EQ(label.size(), label_length);
59 EXPECT_EQ(config.protocol.size(), protocol_length);
60
61 std::string label_output;
62 ASSERT_TRUE(buffer.ReadString(&label_output, label_length));
63 EXPECT_EQ(label, label_output);
64 std::string protocol_output;
65 ASSERT_TRUE(buffer.ReadString(&protocol_output, protocol_length));
66 EXPECT_EQ(config.protocol, protocol_output);
67 }
68};
69
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000070TEST_F(SctpUtilsTest, WriteParseOpenMessageWithOrderedReliable) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000071 webrtc::DataChannelInit config;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000072 std::string label = "abc";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000073 config.protocol = "y";
74
ossud4d2f602016-11-08 02:05:32 -080075 rtc::CopyOnWriteBuffer packet;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000076 ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000077
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000078 VerifyOpenMessageFormat(packet, label, config);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000079
80 std::string output_label;
81 webrtc::DataChannelInit output_config;
Yves Gerey665174f2018-06-19 15:03:05 +020082 ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
83 &output_config));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000084
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000085 EXPECT_EQ(label, output_label);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000086 EXPECT_EQ(config.protocol, output_config.protocol);
87 EXPECT_EQ(config.ordered, output_config.ordered);
88 EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime);
89 EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits);
90}
91
92TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmitTime) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000093 webrtc::DataChannelInit config;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000094 std::string label = "abc";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000095 config.ordered = false;
96 config.maxRetransmitTime = 10;
97 config.protocol = "y";
98
ossud4d2f602016-11-08 02:05:32 -080099 rtc::CopyOnWriteBuffer packet;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000100 ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000101
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000102 VerifyOpenMessageFormat(packet, label, config);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000103
104 std::string output_label;
105 webrtc::DataChannelInit output_config;
Yves Gerey665174f2018-06-19 15:03:05 +0200106 ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
107 &output_config));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000108
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000109 EXPECT_EQ(label, output_label);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000110 EXPECT_EQ(config.protocol, output_config.protocol);
111 EXPECT_EQ(config.ordered, output_config.ordered);
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200112 EXPECT_EQ(*config.maxRetransmitTime, *output_config.maxRetransmitTime);
113 EXPECT_FALSE(output_config.maxRetransmits);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000114}
115
116TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmits) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000117 webrtc::DataChannelInit config;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000118 std::string label = "abc";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000119 config.maxRetransmits = 10;
120 config.protocol = "y";
121
ossud4d2f602016-11-08 02:05:32 -0800122 rtc::CopyOnWriteBuffer packet;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000123 ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000124
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000125 VerifyOpenMessageFormat(packet, label, config);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000126
127 std::string output_label;
128 webrtc::DataChannelInit output_config;
Yves Gerey665174f2018-06-19 15:03:05 +0200129 ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
130 &output_config));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000131
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000132 EXPECT_EQ(label, output_label);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000133 EXPECT_EQ(config.protocol, output_config.protocol);
134 EXPECT_EQ(config.ordered, output_config.ordered);
135 EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits);
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200136 EXPECT_FALSE(output_config.maxRetransmitTime);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000137}
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000138
139TEST_F(SctpUtilsTest, WriteParseAckMessage) {
ossud4d2f602016-11-08 02:05:32 -0800140 rtc::CopyOnWriteBuffer packet;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000141 webrtc::WriteDataChannelOpenAckMessage(&packet);
142
Peter Boström0c4e06b2015-10-07 12:23:21 +0200143 uint8_t message_type;
ossud4d2f602016-11-08 02:05:32 -0800144 rtc::ByteBufferReader buffer(packet.data<char>(), packet.size());
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000145 ASSERT_TRUE(buffer.ReadUInt8(&message_type));
146 EXPECT_EQ(0x02, message_type);
147
148 EXPECT_TRUE(webrtc::ParseDataChannelOpenAckMessage(packet));
149}
deadbeefab9b2d12015-10-14 11:33:11 -0700150
151TEST_F(SctpUtilsTest, TestIsOpenMessage) {
ossud4d2f602016-11-08 02:05:32 -0800152 rtc::CopyOnWriteBuffer open(1);
153 open[0] = 0x03;
deadbeefab9b2d12015-10-14 11:33:11 -0700154 EXPECT_TRUE(webrtc::IsOpenMessage(open));
155
ossud4d2f602016-11-08 02:05:32 -0800156 rtc::CopyOnWriteBuffer openAck(1);
157 openAck[0] = 0x02;
158 EXPECT_FALSE(webrtc::IsOpenMessage(openAck));
deadbeefab9b2d12015-10-14 11:33:11 -0700159
ossud4d2f602016-11-08 02:05:32 -0800160 rtc::CopyOnWriteBuffer invalid(1);
161 invalid[0] = 0x01;
deadbeefab9b2d12015-10-14 11:33:11 -0700162 EXPECT_FALSE(webrtc::IsOpenMessage(invalid));
163
ossud4d2f602016-11-08 02:05:32 -0800164 rtc::CopyOnWriteBuffer empty;
deadbeefab9b2d12015-10-14 11:33:11 -0700165 EXPECT_FALSE(webrtc::IsOpenMessage(empty));
166}