blob: 72db9521bb5fdbef210542ee03d78c0cc2a9d5fe [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
19class SctpUtilsTest : public testing::Test {
20 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) {
Yves Gerey665174f2018-06-19 15:03:05 +020037 EXPECT_EQ(config.maxRetransmits > -1
38 ? 0x01
39 : (config.maxRetransmitTime > -1 ? 0x02 : 0),
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000040 channel_type);
41 } else {
Yves Gerey665174f2018-06-19 15:03:05 +020042 EXPECT_EQ(config.maxRetransmits > -1
43 ? 0x81
44 : (config.maxRetransmitTime > -1 ? 0x82 : 0x80),
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000045 channel_type);
46 }
47
wu@webrtc.org97077a32013-10-25 21:18:33 +000048 ASSERT_TRUE(buffer.ReadUInt16(&priority));
49
50 ASSERT_TRUE(buffer.ReadUInt32(&reliability));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000051 if (config.maxRetransmits > -1 || config.maxRetransmitTime > -1) {
Yves Gerey665174f2018-06-19 15:03:05 +020052 EXPECT_EQ(config.maxRetransmits > -1 ? config.maxRetransmits
53 : config.maxRetransmitTime,
wu@webrtc.org97077a32013-10-25 21:18:33 +000054 static_cast<int>(reliability));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000055 }
56
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000057 ASSERT_TRUE(buffer.ReadUInt16(&label_length));
58 ASSERT_TRUE(buffer.ReadUInt16(&protocol_length));
59 EXPECT_EQ(label.size(), label_length);
60 EXPECT_EQ(config.protocol.size(), protocol_length);
61
62 std::string label_output;
63 ASSERT_TRUE(buffer.ReadString(&label_output, label_length));
64 EXPECT_EQ(label, label_output);
65 std::string protocol_output;
66 ASSERT_TRUE(buffer.ReadString(&protocol_output, protocol_length));
67 EXPECT_EQ(config.protocol, protocol_output);
68 }
69};
70
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000071TEST_F(SctpUtilsTest, WriteParseOpenMessageWithOrderedReliable) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000072 webrtc::DataChannelInit config;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000073 std::string label = "abc";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000074 config.protocol = "y";
75
ossud4d2f602016-11-08 02:05:32 -080076 rtc::CopyOnWriteBuffer packet;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000077 ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000078
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000079 VerifyOpenMessageFormat(packet, label, config);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000080
81 std::string output_label;
82 webrtc::DataChannelInit output_config;
Yves Gerey665174f2018-06-19 15:03:05 +020083 ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
84 &output_config));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000085
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000086 EXPECT_EQ(label, output_label);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000087 EXPECT_EQ(config.protocol, output_config.protocol);
88 EXPECT_EQ(config.ordered, output_config.ordered);
89 EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime);
90 EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits);
91}
92
93TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmitTime) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000094 webrtc::DataChannelInit config;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000095 std::string label = "abc";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +000096 config.ordered = false;
97 config.maxRetransmitTime = 10;
98 config.protocol = "y";
99
ossud4d2f602016-11-08 02:05:32 -0800100 rtc::CopyOnWriteBuffer packet;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000101 ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000102
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000103 VerifyOpenMessageFormat(packet, label, config);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000104
105 std::string output_label;
106 webrtc::DataChannelInit output_config;
Yves Gerey665174f2018-06-19 15:03:05 +0200107 ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
108 &output_config));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000109
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000110 EXPECT_EQ(label, output_label);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000111 EXPECT_EQ(config.protocol, output_config.protocol);
112 EXPECT_EQ(config.ordered, output_config.ordered);
113 EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000114 EXPECT_EQ(-1, output_config.maxRetransmits);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000115}
116
117TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmits) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000118 webrtc::DataChannelInit config;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000119 std::string label = "abc";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000120 config.maxRetransmits = 10;
121 config.protocol = "y";
122
ossud4d2f602016-11-08 02:05:32 -0800123 rtc::CopyOnWriteBuffer packet;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000124 ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000125
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000126 VerifyOpenMessageFormat(packet, label, config);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000127
128 std::string output_label;
129 webrtc::DataChannelInit output_config;
Yves Gerey665174f2018-06-19 15:03:05 +0200130 ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
131 &output_config));
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000132
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000133 EXPECT_EQ(label, output_label);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000134 EXPECT_EQ(config.protocol, output_config.protocol);
135 EXPECT_EQ(config.ordered, output_config.ordered);
136 EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000137 EXPECT_EQ(-1, output_config.maxRetransmitTime);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000138}
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000139
140TEST_F(SctpUtilsTest, WriteParseAckMessage) {
ossud4d2f602016-11-08 02:05:32 -0800141 rtc::CopyOnWriteBuffer packet;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000142 webrtc::WriteDataChannelOpenAckMessage(&packet);
143
Peter Boström0c4e06b2015-10-07 12:23:21 +0200144 uint8_t message_type;
ossud4d2f602016-11-08 02:05:32 -0800145 rtc::ByteBufferReader buffer(packet.data<char>(), packet.size());
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000146 ASSERT_TRUE(buffer.ReadUInt8(&message_type));
147 EXPECT_EQ(0x02, message_type);
148
149 EXPECT_TRUE(webrtc::ParseDataChannelOpenAckMessage(packet));
150}
deadbeefab9b2d12015-10-14 11:33:11 -0700151
152TEST_F(SctpUtilsTest, TestIsOpenMessage) {
ossud4d2f602016-11-08 02:05:32 -0800153 rtc::CopyOnWriteBuffer open(1);
154 open[0] = 0x03;
deadbeefab9b2d12015-10-14 11:33:11 -0700155 EXPECT_TRUE(webrtc::IsOpenMessage(open));
156
ossud4d2f602016-11-08 02:05:32 -0800157 rtc::CopyOnWriteBuffer openAck(1);
158 openAck[0] = 0x02;
159 EXPECT_FALSE(webrtc::IsOpenMessage(openAck));
deadbeefab9b2d12015-10-14 11:33:11 -0700160
ossud4d2f602016-11-08 02:05:32 -0800161 rtc::CopyOnWriteBuffer invalid(1);
162 invalid[0] = 0x01;
deadbeefab9b2d12015-10-14 11:33:11 -0700163 EXPECT_FALSE(webrtc::IsOpenMessage(invalid));
164
ossud4d2f602016-11-08 02:05:32 -0800165 rtc::CopyOnWriteBuffer empty;
deadbeefab9b2d12015-10-14 11:33:11 -0700166 EXPECT_FALSE(webrtc::IsOpenMessage(empty));
167}