wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [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. |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/sctp_utils.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 12 | |
| 13 | #include <stdint.h> |
| 14 | |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 +0000 | [diff] [blame] | 15 | #include "absl/types/optional.h" |
| 16 | #include "api/priority.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 17 | #include "rtc_base/byte_buffer.h" |
| 18 | #include "rtc_base/copy_on_write_buffer.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 19 | #include "test/gtest.h" |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 20 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 21 | class SctpUtilsTest : public ::testing::Test { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 22 | public: |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 23 | void VerifyOpenMessageFormat(const rtc::CopyOnWriteBuffer& packet, |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 24 | const std::string& label, |
| 25 | const webrtc::DataChannelInit& config) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 26 | uint8_t message_type; |
| 27 | uint8_t channel_type; |
| 28 | uint32_t reliability; |
| 29 | uint16_t priority; |
| 30 | uint16_t label_length; |
| 31 | uint16_t protocol_length; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 32 | |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 33 | rtc::ByteBufferReader buffer(packet.data<char>(), packet.size()); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 34 | ASSERT_TRUE(buffer.ReadUInt8(&message_type)); |
| 35 | EXPECT_EQ(0x03, message_type); |
| 36 | |
| 37 | ASSERT_TRUE(buffer.ReadUInt8(&channel_type)); |
| 38 | if (config.ordered) { |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 39 | EXPECT_EQ( |
| 40 | config.maxRetransmits ? 0x01 : (config.maxRetransmitTime ? 0x02 : 0), |
| 41 | channel_type); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 42 | } else { |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 43 | EXPECT_EQ(config.maxRetransmits |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 44 | ? 0x81 |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 45 | : (config.maxRetransmitTime ? 0x82 : 0x80), |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 46 | channel_type); |
| 47 | } |
| 48 | |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 49 | ASSERT_TRUE(buffer.ReadUInt16(&priority)); |
Harald Alvestrand | fd5ae7f | 2020-05-16 08:37:49 +0200 | [diff] [blame] | 50 | if (config.priority) { |
| 51 | // Exact values are checked by round-trip conversion, but |
| 52 | // all values defined are greater than zero. |
| 53 | EXPECT_GT(priority, 0); |
| 54 | } else { |
| 55 | EXPECT_EQ(priority, 0); |
| 56 | } |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 57 | |
| 58 | ASSERT_TRUE(buffer.ReadUInt32(&reliability)); |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 59 | if (config.maxRetransmits || config.maxRetransmitTime) { |
| 60 | EXPECT_EQ(config.maxRetransmits ? *config.maxRetransmits |
| 61 | : *config.maxRetransmitTime, |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 62 | static_cast<int>(reliability)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 63 | } |
| 64 | |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 65 | ASSERT_TRUE(buffer.ReadUInt16(&label_length)); |
| 66 | ASSERT_TRUE(buffer.ReadUInt16(&protocol_length)); |
| 67 | EXPECT_EQ(label.size(), label_length); |
| 68 | EXPECT_EQ(config.protocol.size(), protocol_length); |
| 69 | |
| 70 | std::string label_output; |
| 71 | ASSERT_TRUE(buffer.ReadString(&label_output, label_length)); |
| 72 | EXPECT_EQ(label, label_output); |
| 73 | std::string protocol_output; |
| 74 | ASSERT_TRUE(buffer.ReadString(&protocol_output, protocol_length)); |
| 75 | EXPECT_EQ(config.protocol, protocol_output); |
| 76 | } |
| 77 | }; |
| 78 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 79 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithOrderedReliable) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 80 | webrtc::DataChannelInit config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 81 | std::string label = "abc"; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 82 | config.protocol = "y"; |
| 83 | |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 84 | rtc::CopyOnWriteBuffer packet; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 85 | ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 86 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 87 | VerifyOpenMessageFormat(packet, label, config); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 88 | |
| 89 | std::string output_label; |
| 90 | webrtc::DataChannelInit output_config; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 91 | ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label, |
| 92 | &output_config)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 93 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 94 | EXPECT_EQ(label, output_label); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 95 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 96 | EXPECT_EQ(config.ordered, output_config.ordered); |
| 97 | EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime); |
| 98 | EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits); |
| 99 | } |
| 100 | |
| 101 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmitTime) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 102 | webrtc::DataChannelInit config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 103 | std::string label = "abc"; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 104 | config.ordered = false; |
| 105 | config.maxRetransmitTime = 10; |
| 106 | config.protocol = "y"; |
| 107 | |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 108 | rtc::CopyOnWriteBuffer packet; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 109 | ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 110 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 111 | VerifyOpenMessageFormat(packet, label, config); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 112 | |
| 113 | std::string output_label; |
| 114 | webrtc::DataChannelInit output_config; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 115 | ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label, |
| 116 | &output_config)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 117 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 118 | EXPECT_EQ(label, output_label); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 119 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 120 | EXPECT_EQ(config.ordered, output_config.ordered); |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 121 | EXPECT_EQ(*config.maxRetransmitTime, *output_config.maxRetransmitTime); |
| 122 | EXPECT_FALSE(output_config.maxRetransmits); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmits) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 126 | webrtc::DataChannelInit config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 127 | std::string label = "abc"; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 128 | config.maxRetransmits = 10; |
| 129 | config.protocol = "y"; |
| 130 | |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 131 | rtc::CopyOnWriteBuffer packet; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 132 | ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 133 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 134 | VerifyOpenMessageFormat(packet, label, config); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 135 | |
| 136 | std::string output_label; |
| 137 | webrtc::DataChannelInit output_config; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 138 | ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label, |
| 139 | &output_config)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 140 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 141 | EXPECT_EQ(label, output_label); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 142 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 143 | EXPECT_EQ(config.ordered, output_config.ordered); |
| 144 | EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits); |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 145 | EXPECT_FALSE(output_config.maxRetransmitTime); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 146 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 147 | |
Harald Alvestrand | fd5ae7f | 2020-05-16 08:37:49 +0200 | [diff] [blame] | 148 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithPriority) { |
| 149 | webrtc::DataChannelInit config; |
| 150 | std::string label = "abc"; |
| 151 | config.protocol = "y"; |
| 152 | config.priority = webrtc::Priority::kVeryLow; |
| 153 | |
| 154 | rtc::CopyOnWriteBuffer packet; |
| 155 | ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet)); |
| 156 | |
| 157 | VerifyOpenMessageFormat(packet, label, config); |
| 158 | |
| 159 | std::string output_label; |
| 160 | webrtc::DataChannelInit output_config; |
| 161 | ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label, |
| 162 | &output_config)); |
| 163 | |
| 164 | EXPECT_EQ(label, output_label); |
| 165 | ASSERT_TRUE(output_config.priority); |
| 166 | EXPECT_EQ(*config.priority, *output_config.priority); |
| 167 | } |
| 168 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 169 | TEST_F(SctpUtilsTest, WriteParseAckMessage) { |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 170 | rtc::CopyOnWriteBuffer packet; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 171 | webrtc::WriteDataChannelOpenAckMessage(&packet); |
| 172 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 173 | uint8_t message_type; |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 174 | rtc::ByteBufferReader buffer(packet.data<char>(), packet.size()); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 175 | ASSERT_TRUE(buffer.ReadUInt8(&message_type)); |
| 176 | EXPECT_EQ(0x02, message_type); |
| 177 | |
| 178 | EXPECT_TRUE(webrtc::ParseDataChannelOpenAckMessage(packet)); |
| 179 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 180 | |
| 181 | TEST_F(SctpUtilsTest, TestIsOpenMessage) { |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 182 | rtc::CopyOnWriteBuffer open(1); |
Danil Chapovalov | e15dc58 | 2021-01-07 15:24:05 +0100 | [diff] [blame] | 183 | open.MutableData()[0] = 0x03; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 184 | EXPECT_TRUE(webrtc::IsOpenMessage(open)); |
| 185 | |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 186 | rtc::CopyOnWriteBuffer openAck(1); |
Danil Chapovalov | e15dc58 | 2021-01-07 15:24:05 +0100 | [diff] [blame] | 187 | openAck.MutableData()[0] = 0x02; |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 188 | EXPECT_FALSE(webrtc::IsOpenMessage(openAck)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 189 | |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 190 | rtc::CopyOnWriteBuffer invalid(1); |
Danil Chapovalov | e15dc58 | 2021-01-07 15:24:05 +0100 | [diff] [blame] | 191 | invalid.MutableData()[0] = 0x01; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 192 | EXPECT_FALSE(webrtc::IsOpenMessage(invalid)); |
| 193 | |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 194 | rtc::CopyOnWriteBuffer empty; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 195 | EXPECT_FALSE(webrtc::IsOpenMessage(empty)); |
| 196 | } |