| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 1 | /* |
| kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 3 | * |
| kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 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. |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 9 | */ |
| 10 | |
| Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "pc/sctp_utils.h" |
| Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 12 | |
| Philipp Hancke | b2ea936 | 2025-07-18 17:47:39 | [diff] [blame] | 13 | #include <cstdint> |
| Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 14 | #include <limits> |
| Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 15 | #include <optional> |
| Evan Shrubsole | daf96cf | 2025-03-31 12:34:02 | [diff] [blame] | 16 | #include <string> |
| Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 17 | |
| Evan Shrubsole | daf96cf | 2025-03-31 12:34:02 | [diff] [blame] | 18 | #include "absl/strings/string_view.h" |
| Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 19 | #include "api/priority.h" |
| Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 20 | #include "media/sctp/sctp_transport_internal.h" |
| Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 21 | #include "rtc_base/byte_buffer.h" |
| 22 | #include "rtc_base/copy_on_write_buffer.h" |
| Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 23 | #include "test/gtest.h" |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 24 | |
| Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 25 | using webrtc::StreamId; |
| 26 | |
| Mirko Bonadei | 6a489f2 | 2019-04-09 13:11:12 | [diff] [blame] | 27 | class SctpUtilsTest : public ::testing::Test { |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 28 | public: |
| Evan Shrubsole | e6a1f70 | 2025-04-15 14:55:42 | [diff] [blame] | 29 | void VerifyOpenMessageFormat(const webrtc::CopyOnWriteBuffer& packet, |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 30 | const std::string& label, |
| 31 | const webrtc::DataChannelInit& config) { |
| Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 32 | 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] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 38 | |
| Evan Shrubsole | daf96cf | 2025-03-31 12:34:02 | [diff] [blame] | 39 | webrtc::ByteBufferReader buffer(packet); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 40 | 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 Alvestrand | f3736ed | 2019-04-08 11:09:30 | [diff] [blame] | 45 | EXPECT_EQ( |
| 46 | config.maxRetransmits ? 0x01 : (config.maxRetransmitTime ? 0x02 : 0), |
| 47 | channel_type); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 48 | } else { |
| Harald Alvestrand | f3736ed | 2019-04-08 11:09:30 | [diff] [blame] | 49 | EXPECT_EQ(config.maxRetransmits |
| Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 50 | ? 0x81 |
| Harald Alvestrand | f3736ed | 2019-04-08 11:09:30 | [diff] [blame] | 51 | : (config.maxRetransmitTime ? 0x82 : 0x80), |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 52 | channel_type); |
| 53 | } |
| 54 | |
| [email protected] | 97077a3 | 2013-10-25 21:18:33 | [diff] [blame] | 55 | ASSERT_TRUE(buffer.ReadUInt16(&priority)); |
| Harald Alvestrand | fd5ae7f | 2020-05-16 06:37:49 | [diff] [blame] | 56 | if (config.priority) { |
| 57 | // Exact values are checked by round-trip conversion, but |
| 58 | // all values defined are greater than zero. |
| Florent Castelli | 0012bfa | 2024-07-26 16:16:41 | [diff] [blame] | 59 | EXPECT_EQ(priority, config.priority->value()); |
| Harald Alvestrand | fd5ae7f | 2020-05-16 06:37:49 | [diff] [blame] | 60 | } else { |
| Florent Castelli | 0012bfa | 2024-07-26 16:16:41 | [diff] [blame] | 61 | EXPECT_EQ(priority, |
| 62 | webrtc::PriorityValue(webrtc::Priority::kLow).value()); |
| Harald Alvestrand | fd5ae7f | 2020-05-16 06:37:49 | [diff] [blame] | 63 | } |
| [email protected] | 97077a3 | 2013-10-25 21:18:33 | [diff] [blame] | 64 | |
| 65 | ASSERT_TRUE(buffer.ReadUInt32(&reliability)); |
| Harald Alvestrand | f3736ed | 2019-04-08 11:09:30 | [diff] [blame] | 66 | if (config.maxRetransmits || config.maxRetransmitTime) { |
| 67 | EXPECT_EQ(config.maxRetransmits ? *config.maxRetransmits |
| 68 | : *config.maxRetransmitTime, |
| [email protected] | 97077a3 | 2013-10-25 21:18:33 | [diff] [blame] | 69 | static_cast<int>(reliability)); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 70 | } |
| 71 | |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 72 | 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 | |
| Tommi | eb4a314 | 2024-01-15 20:06:05 | [diff] [blame] | 77 | absl::string_view label_output; |
| 78 | ASSERT_TRUE(buffer.ReadStringView(&label_output, label_length)); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 79 | EXPECT_EQ(label, label_output); |
| Tommi | eb4a314 | 2024-01-15 20:06:05 | [diff] [blame] | 80 | absl::string_view protocol_output; |
| 81 | ASSERT_TRUE(buffer.ReadStringView(&protocol_output, protocol_length)); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 82 | EXPECT_EQ(config.protocol, protocol_output); |
| 83 | } |
| 84 | }; |
| 85 | |
| [email protected] | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 86 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithOrderedReliable) { |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 87 | webrtc::DataChannelInit config; |
| [email protected] | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 88 | std::string label = "abc"; |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 89 | config.protocol = "y"; |
| 90 | |
| Evan Shrubsole | e6a1f70 | 2025-04-15 14:55:42 | [diff] [blame] | 91 | webrtc::CopyOnWriteBuffer packet; |
| [email protected] | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 92 | ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet)); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 93 | |
| [email protected] | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 94 | VerifyOpenMessageFormat(packet, label, config); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 95 | |
| 96 | std::string output_label; |
| 97 | webrtc::DataChannelInit output_config; |
| Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 98 | ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label, |
| 99 | &output_config)); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 100 | |
| [email protected] | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 101 | EXPECT_EQ(label, output_label); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 102 | 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 | |
| 108 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmitTime) { |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 109 | webrtc::DataChannelInit config; |
| [email protected] | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 110 | std::string label = "abc"; |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 111 | config.ordered = false; |
| 112 | config.maxRetransmitTime = 10; |
| 113 | config.protocol = "y"; |
| 114 | |
| Evan Shrubsole | e6a1f70 | 2025-04-15 14:55:42 | [diff] [blame] | 115 | webrtc::CopyOnWriteBuffer packet; |
| [email protected] | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 116 | ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet)); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 117 | |
| [email protected] | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 118 | VerifyOpenMessageFormat(packet, label, config); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 119 | |
| 120 | std::string output_label; |
| 121 | webrtc::DataChannelInit output_config; |
| Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 122 | ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label, |
| 123 | &output_config)); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 124 | |
| [email protected] | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 125 | EXPECT_EQ(label, output_label); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 126 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 127 | EXPECT_EQ(config.ordered, output_config.ordered); |
| Harald Alvestrand | f3736ed | 2019-04-08 11:09:30 | [diff] [blame] | 128 | EXPECT_EQ(*config.maxRetransmitTime, *output_config.maxRetransmitTime); |
| 129 | EXPECT_FALSE(output_config.maxRetransmits); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmits) { |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 133 | webrtc::DataChannelInit config; |
| [email protected] | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 134 | std::string label = "abc"; |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 135 | config.maxRetransmits = 10; |
| 136 | config.protocol = "y"; |
| 137 | |
| Evan Shrubsole | e6a1f70 | 2025-04-15 14:55:42 | [diff] [blame] | 138 | webrtc::CopyOnWriteBuffer packet; |
| [email protected] | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 139 | ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet)); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 140 | |
| [email protected] | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 141 | VerifyOpenMessageFormat(packet, label, config); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 142 | |
| 143 | std::string output_label; |
| 144 | webrtc::DataChannelInit output_config; |
| Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 145 | ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label, |
| 146 | &output_config)); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 147 | |
| [email protected] | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 148 | EXPECT_EQ(label, output_label); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 149 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 150 | EXPECT_EQ(config.ordered, output_config.ordered); |
| 151 | EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits); |
| Harald Alvestrand | f3736ed | 2019-04-08 11:09:30 | [diff] [blame] | 152 | EXPECT_FALSE(output_config.maxRetransmitTime); |
| [email protected] | 1d1ffc9 | 2013-10-16 18:12:02 | [diff] [blame] | 153 | } |
| [email protected] | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 154 | |
| Harald Alvestrand | fd5ae7f | 2020-05-16 06:37:49 | [diff] [blame] | 155 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithPriority) { |
| 156 | webrtc::DataChannelInit config; |
| 157 | std::string label = "abc"; |
| 158 | config.protocol = "y"; |
| Florent Castelli | 0012bfa | 2024-07-26 16:16:41 | [diff] [blame] | 159 | config.priority = webrtc::PriorityValue(webrtc::Priority::kVeryLow); |
| Harald Alvestrand | fd5ae7f | 2020-05-16 06:37:49 | [diff] [blame] | 160 | |
| Evan Shrubsole | e6a1f70 | 2025-04-15 14:55:42 | [diff] [blame] | 161 | webrtc::CopyOnWriteBuffer packet; |
| Harald Alvestrand | fd5ae7f | 2020-05-16 06:37:49 | [diff] [blame] | 162 | 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] | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 176 | TEST_F(SctpUtilsTest, WriteParseAckMessage) { |
| Evan Shrubsole | e6a1f70 | 2025-04-15 14:55:42 | [diff] [blame] | 177 | webrtc::CopyOnWriteBuffer packet; |
| [email protected] | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 178 | webrtc::WriteDataChannelOpenAckMessage(&packet); |
| 179 | |
| Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 180 | uint8_t message_type; |
| Evan Shrubsole | daf96cf | 2025-03-31 12:34:02 | [diff] [blame] | 181 | webrtc::ByteBufferReader buffer(packet); |
| [email protected] | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 182 | ASSERT_TRUE(buffer.ReadUInt8(&message_type)); |
| 183 | EXPECT_EQ(0x02, message_type); |
| 184 | |
| 185 | EXPECT_TRUE(webrtc::ParseDataChannelOpenAckMessage(packet)); |
| 186 | } |
| deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 187 | |
| 188 | TEST_F(SctpUtilsTest, TestIsOpenMessage) { |
| Evan Shrubsole | e6a1f70 | 2025-04-15 14:55:42 | [diff] [blame] | 189 | webrtc::CopyOnWriteBuffer open(1); |
| Danil Chapovalov | e15dc58 | 2021-01-07 14:24:05 | [diff] [blame] | 190 | open.MutableData()[0] = 0x03; |
| deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 191 | EXPECT_TRUE(webrtc::IsOpenMessage(open)); |
| 192 | |
| Evan Shrubsole | e6a1f70 | 2025-04-15 14:55:42 | [diff] [blame] | 193 | webrtc::CopyOnWriteBuffer openAck(1); |
| Danil Chapovalov | e15dc58 | 2021-01-07 14:24:05 | [diff] [blame] | 194 | openAck.MutableData()[0] = 0x02; |
| ossu | d4d2f60 | 2016-11-08 10:05:32 | [diff] [blame] | 195 | EXPECT_FALSE(webrtc::IsOpenMessage(openAck)); |
| deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 196 | |
| Evan Shrubsole | e6a1f70 | 2025-04-15 14:55:42 | [diff] [blame] | 197 | webrtc::CopyOnWriteBuffer invalid(1); |
| Danil Chapovalov | e15dc58 | 2021-01-07 14:24:05 | [diff] [blame] | 198 | invalid.MutableData()[0] = 0x01; |
| deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 199 | EXPECT_FALSE(webrtc::IsOpenMessage(invalid)); |
| 200 | |
| Evan Shrubsole | e6a1f70 | 2025-04-15 14:55:42 | [diff] [blame] | 201 | webrtc::CopyOnWriteBuffer empty; |
| deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 202 | EXPECT_FALSE(webrtc::IsOpenMessage(empty)); |
| 203 | } |
| Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 204 | |
| 205 | TEST(SctpSidTest, Basics) { |
| 206 | // These static asserts are mostly here to aid with readability (i.e. knowing |
| 207 | // what these constants represent). |
| Evan Shrubsole | 945e517 | 2025-04-08 14:11:45 | [diff] [blame] | 208 | 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"); |
| Tommi | 492296c | 2023-03-12 15:59:25 | [diff] [blame] | 212 | } |