Thanks to visit codestin.com
Credit goes to webrtc.googlesource.com

blob: df02229ad5ab27f7cebcb3f16033eafa98dc60aa [file] [log] [blame]
[email protected]1d1ffc92013-10-16 18:12:021/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
[email protected]1d1ffc92013-10-16 18:12:023 *
kjellanderb24317b2016-02-10 15:54:434 * 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.
[email protected]1d1ffc92013-10-16 18:12:029 */
10
Steve Anton10542f22019-01-11 17:11:0011#include "pc/sctp_utils.h"
Yves Gerey3e707812018-11-28 15:47:4912
Philipp Hanckeb2ea9362025-07-18 17:47:3913#include <cstdint>
Tommi492296c2023-03-12 15:59:2514#include <limits>
Florent Castelli8037fc62024-08-29 13:00:4015#include <optional>
Evan Shrubsoledaf96cf2025-03-31 12:34:0216#include <string>
Tommi492296c2023-03-12 15:59:2517
Evan Shrubsoledaf96cf2025-03-31 12:34:0218#include "absl/strings/string_view.h"
Harald Alvestrandc24a2182022-02-23 13:44:5919#include "api/priority.h"
Tommi492296c2023-03-12 15:59:2520#include "media/sctp/sctp_transport_internal.h"
Steve Anton10542f22019-01-11 17:11:0021#include "rtc_base/byte_buffer.h"
22#include "rtc_base/copy_on_write_buffer.h"
Yves Gerey3e707812018-11-28 15:47:4923#include "test/gtest.h"
[email protected]1d1ffc92013-10-16 18:12:0224
Tommi492296c2023-03-12 15:59:2525using webrtc::StreamId;
26
Mirko Bonadei6a489f22019-04-09 13:11:1227class SctpUtilsTest : public ::testing::Test {
[email protected]1d1ffc92013-10-16 18:12:0228 public:
Evan Shrubsolee6a1f702025-04-15 14:55:4229 void VerifyOpenMessageFormat(const webrtc::CopyOnWriteBuffer& packet,
[email protected]1d1ffc92013-10-16 18:12:0230 const std::string& label,
31 const webrtc::DataChannelInit& config) {
Peter Boström0c4e06b2015-10-07 10:23:2132 uint8_t message_type;
33 uint8_t channel_type;
34 uint32_t reliability;
35 uint16_t priority;
36 uint16_t label_length;
37 uint16_t protocol_length;
[email protected]1d1ffc92013-10-16 18:12:0238
Evan Shrubsoledaf96cf2025-03-31 12:34:0239 webrtc::ByteBufferReader buffer(packet);
[email protected]1d1ffc92013-10-16 18:12:0240 ASSERT_TRUE(buffer.ReadUInt8(&message_type));
41 EXPECT_EQ(0x03, message_type);
42
43 ASSERT_TRUE(buffer.ReadUInt8(&channel_type));
44 if (config.ordered) {
Harald Alvestrandf3736ed2019-04-08 11:09:3045 EXPECT_EQ(
46 config.maxRetransmits ? 0x01 : (config.maxRetransmitTime ? 0x02 : 0),
47 channel_type);
[email protected]1d1ffc92013-10-16 18:12:0248 } else {
Harald Alvestrandf3736ed2019-04-08 11:09:3049 EXPECT_EQ(config.maxRetransmits
Yves Gerey665174f2018-06-19 13:03:0550 ? 0x81
Harald Alvestrandf3736ed2019-04-08 11:09:3051 : (config.maxRetransmitTime ? 0x82 : 0x80),
[email protected]1d1ffc92013-10-16 18:12:0252 channel_type);
53 }
54
[email protected]97077a32013-10-25 21:18:3355 ASSERT_TRUE(buffer.ReadUInt16(&priority));
Harald Alvestrandfd5ae7f2020-05-16 06:37:4956 if (config.priority) {
57 // Exact values are checked by round-trip conversion, but
58 // all values defined are greater than zero.
Florent Castelli0012bfa2024-07-26 16:16:4159 EXPECT_EQ(priority, config.priority->value());
Harald Alvestrandfd5ae7f2020-05-16 06:37:4960 } else {
Florent Castelli0012bfa2024-07-26 16:16:4161 EXPECT_EQ(priority,
62 webrtc::PriorityValue(webrtc::Priority::kLow).value());
Harald Alvestrandfd5ae7f2020-05-16 06:37:4963 }
[email protected]97077a32013-10-25 21:18:3364
65 ASSERT_TRUE(buffer.ReadUInt32(&reliability));
Harald Alvestrandf3736ed2019-04-08 11:09:3066 if (config.maxRetransmits || config.maxRetransmitTime) {
67 EXPECT_EQ(config.maxRetransmits ? *config.maxRetransmits
68 : *config.maxRetransmitTime,
[email protected]97077a32013-10-25 21:18:3369 static_cast<int>(reliability));
[email protected]1d1ffc92013-10-16 18:12:0270 }
71
[email protected]1d1ffc92013-10-16 18:12:0272 ASSERT_TRUE(buffer.ReadUInt16(&label_length));
73 ASSERT_TRUE(buffer.ReadUInt16(&protocol_length));
74 EXPECT_EQ(label.size(), label_length);
75 EXPECT_EQ(config.protocol.size(), protocol_length);
76
Tommieb4a3142024-01-15 20:06:0577 absl::string_view label_output;
78 ASSERT_TRUE(buffer.ReadStringView(&label_output, label_length));
[email protected]1d1ffc92013-10-16 18:12:0279 EXPECT_EQ(label, label_output);
Tommieb4a3142024-01-15 20:06:0580 absl::string_view protocol_output;
81 ASSERT_TRUE(buffer.ReadStringView(&protocol_output, protocol_length));
[email protected]1d1ffc92013-10-16 18:12:0282 EXPECT_EQ(config.protocol, protocol_output);
83 }
84};
85
[email protected]aebb1ad2014-01-14 10:00:5886TEST_F(SctpUtilsTest, WriteParseOpenMessageWithOrderedReliable) {
[email protected]1d1ffc92013-10-16 18:12:0287 webrtc::DataChannelInit config;
[email protected]aebb1ad2014-01-14 10:00:5888 std::string label = "abc";
[email protected]1d1ffc92013-10-16 18:12:0289 config.protocol = "y";
90
Evan Shrubsolee6a1f702025-04-15 14:55:4291 webrtc::CopyOnWriteBuffer packet;
[email protected]aebb1ad2014-01-14 10:00:5892 ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
[email protected]1d1ffc92013-10-16 18:12:0293
[email protected]aebb1ad2014-01-14 10:00:5894 VerifyOpenMessageFormat(packet, label, config);
[email protected]1d1ffc92013-10-16 18:12:0295
96 std::string output_label;
97 webrtc::DataChannelInit output_config;
Yves Gerey665174f2018-06-19 13:03:0598 ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
99 &output_config));
[email protected]1d1ffc92013-10-16 18:12:02100
[email protected]aebb1ad2014-01-14 10:00:58101 EXPECT_EQ(label, output_label);
[email protected]1d1ffc92013-10-16 18:12:02102 EXPECT_EQ(config.protocol, output_config.protocol);
103 EXPECT_EQ(config.ordered, output_config.ordered);
104 EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime);
105 EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits);
106}
107
108TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmitTime) {
[email protected]1d1ffc92013-10-16 18:12:02109 webrtc::DataChannelInit config;
[email protected]aebb1ad2014-01-14 10:00:58110 std::string label = "abc";
[email protected]1d1ffc92013-10-16 18:12:02111 config.ordered = false;
112 config.maxRetransmitTime = 10;
113 config.protocol = "y";
114
Evan Shrubsolee6a1f702025-04-15 14:55:42115 webrtc::CopyOnWriteBuffer packet;
[email protected]aebb1ad2014-01-14 10:00:58116 ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
[email protected]1d1ffc92013-10-16 18:12:02117
[email protected]aebb1ad2014-01-14 10:00:58118 VerifyOpenMessageFormat(packet, label, config);
[email protected]1d1ffc92013-10-16 18:12:02119
120 std::string output_label;
121 webrtc::DataChannelInit output_config;
Yves Gerey665174f2018-06-19 13:03:05122 ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
123 &output_config));
[email protected]1d1ffc92013-10-16 18:12:02124
[email protected]aebb1ad2014-01-14 10:00:58125 EXPECT_EQ(label, output_label);
[email protected]1d1ffc92013-10-16 18:12:02126 EXPECT_EQ(config.protocol, output_config.protocol);
127 EXPECT_EQ(config.ordered, output_config.ordered);
Harald Alvestrandf3736ed2019-04-08 11:09:30128 EXPECT_EQ(*config.maxRetransmitTime, *output_config.maxRetransmitTime);
129 EXPECT_FALSE(output_config.maxRetransmits);
[email protected]1d1ffc92013-10-16 18:12:02130}
131
132TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmits) {
[email protected]1d1ffc92013-10-16 18:12:02133 webrtc::DataChannelInit config;
[email protected]aebb1ad2014-01-14 10:00:58134 std::string label = "abc";
[email protected]1d1ffc92013-10-16 18:12:02135 config.maxRetransmits = 10;
136 config.protocol = "y";
137
Evan Shrubsolee6a1f702025-04-15 14:55:42138 webrtc::CopyOnWriteBuffer packet;
[email protected]aebb1ad2014-01-14 10:00:58139 ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
[email protected]1d1ffc92013-10-16 18:12:02140
[email protected]aebb1ad2014-01-14 10:00:58141 VerifyOpenMessageFormat(packet, label, config);
[email protected]1d1ffc92013-10-16 18:12:02142
143 std::string output_label;
144 webrtc::DataChannelInit output_config;
Yves Gerey665174f2018-06-19 13:03:05145 ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
146 &output_config));
[email protected]1d1ffc92013-10-16 18:12:02147
[email protected]aebb1ad2014-01-14 10:00:58148 EXPECT_EQ(label, output_label);
[email protected]1d1ffc92013-10-16 18:12:02149 EXPECT_EQ(config.protocol, output_config.protocol);
150 EXPECT_EQ(config.ordered, output_config.ordered);
151 EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits);
Harald Alvestrandf3736ed2019-04-08 11:09:30152 EXPECT_FALSE(output_config.maxRetransmitTime);
[email protected]1d1ffc92013-10-16 18:12:02153}
[email protected]aebb1ad2014-01-14 10:00:58154
Harald Alvestrandfd5ae7f2020-05-16 06:37:49155TEST_F(SctpUtilsTest, WriteParseOpenMessageWithPriority) {
156 webrtc::DataChannelInit config;
157 std::string label = "abc";
158 config.protocol = "y";
Florent Castelli0012bfa2024-07-26 16:16:41159 config.priority = webrtc::PriorityValue(webrtc::Priority::kVeryLow);
Harald Alvestrandfd5ae7f2020-05-16 06:37:49160
Evan Shrubsolee6a1f702025-04-15 14:55:42161 webrtc::CopyOnWriteBuffer packet;
Harald Alvestrandfd5ae7f2020-05-16 06:37:49162 ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
163
164 VerifyOpenMessageFormat(packet, label, config);
165
166 std::string output_label;
167 webrtc::DataChannelInit output_config;
168 ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
169 &output_config));
170
171 EXPECT_EQ(label, output_label);
172 ASSERT_TRUE(output_config.priority);
173 EXPECT_EQ(*config.priority, *output_config.priority);
174}
175
[email protected]aebb1ad2014-01-14 10:00:58176TEST_F(SctpUtilsTest, WriteParseAckMessage) {
Evan Shrubsolee6a1f702025-04-15 14:55:42177 webrtc::CopyOnWriteBuffer packet;
[email protected]aebb1ad2014-01-14 10:00:58178 webrtc::WriteDataChannelOpenAckMessage(&packet);
179
Peter Boström0c4e06b2015-10-07 10:23:21180 uint8_t message_type;
Evan Shrubsoledaf96cf2025-03-31 12:34:02181 webrtc::ByteBufferReader buffer(packet);
[email protected]aebb1ad2014-01-14 10:00:58182 ASSERT_TRUE(buffer.ReadUInt8(&message_type));
183 EXPECT_EQ(0x02, message_type);
184
185 EXPECT_TRUE(webrtc::ParseDataChannelOpenAckMessage(packet));
186}
deadbeefab9b2d12015-10-14 18:33:11187
188TEST_F(SctpUtilsTest, TestIsOpenMessage) {
Evan Shrubsolee6a1f702025-04-15 14:55:42189 webrtc::CopyOnWriteBuffer open(1);
Danil Chapovalove15dc582021-01-07 14:24:05190 open.MutableData()[0] = 0x03;
deadbeefab9b2d12015-10-14 18:33:11191 EXPECT_TRUE(webrtc::IsOpenMessage(open));
192
Evan Shrubsolee6a1f702025-04-15 14:55:42193 webrtc::CopyOnWriteBuffer openAck(1);
Danil Chapovalove15dc582021-01-07 14:24:05194 openAck.MutableData()[0] = 0x02;
ossud4d2f602016-11-08 10:05:32195 EXPECT_FALSE(webrtc::IsOpenMessage(openAck));
deadbeefab9b2d12015-10-14 18:33:11196
Evan Shrubsolee6a1f702025-04-15 14:55:42197 webrtc::CopyOnWriteBuffer invalid(1);
Danil Chapovalove15dc582021-01-07 14:24:05198 invalid.MutableData()[0] = 0x01;
deadbeefab9b2d12015-10-14 18:33:11199 EXPECT_FALSE(webrtc::IsOpenMessage(invalid));
200
Evan Shrubsolee6a1f702025-04-15 14:55:42201 webrtc::CopyOnWriteBuffer empty;
deadbeefab9b2d12015-10-14 18:33:11202 EXPECT_FALSE(webrtc::IsOpenMessage(empty));
203}
Tommi492296c2023-03-12 15:59:25204
205TEST(SctpSidTest, Basics) {
206 // These static asserts are mostly here to aid with readability (i.e. knowing
207 // what these constants represent).
Evan Shrubsole945e5172025-04-08 14:11:45208 static_assert(webrtc::kMinSctpSid == 0, "Min stream id should be 0");
209 static_assert(webrtc::kMaxSctpSid <= webrtc::kSpecMaxSctpSid, "");
210 static_assert(webrtc::kSpecMaxSctpSid == std::numeric_limits<uint16_t>::max(),
211 "Max legal sctp stream value should be 0xffff");
Tommi492296c2023-03-12 15:59:25212}