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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 15 | #include "rtc_base/byte_buffer.h" |
| 16 | #include "rtc_base/copy_on_write_buffer.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 17 | #include "test/gtest.h" |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 18 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 19 | class SctpUtilsTest : public ::testing::Test { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 20 | public: |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 21 | void VerifyOpenMessageFormat(const rtc::CopyOnWriteBuffer& packet, |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 22 | const std::string& label, |
| 23 | const webrtc::DataChannelInit& config) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 24 | 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.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 30 | |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 31 | rtc::ByteBufferReader buffer(packet.data<char>(), packet.size()); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 32 | 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 Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 37 | EXPECT_EQ( |
| 38 | config.maxRetransmits ? 0x01 : (config.maxRetransmitTime ? 0x02 : 0), |
| 39 | channel_type); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 40 | } else { |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 41 | EXPECT_EQ(config.maxRetransmits |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 42 | ? 0x81 |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 43 | : (config.maxRetransmitTime ? 0x82 : 0x80), |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 44 | channel_type); |
| 45 | } |
| 46 | |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 47 | ASSERT_TRUE(buffer.ReadUInt16(&priority)); |
| 48 | |
| 49 | ASSERT_TRUE(buffer.ReadUInt32(&reliability)); |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 50 | if (config.maxRetransmits || config.maxRetransmitTime) { |
| 51 | EXPECT_EQ(config.maxRetransmits ? *config.maxRetransmits |
| 52 | : *config.maxRetransmitTime, |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 53 | static_cast<int>(reliability)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 54 | } |
| 55 | |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 56 | 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.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 70 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithOrderedReliable) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 71 | webrtc::DataChannelInit config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 72 | std::string label = "abc"; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 73 | config.protocol = "y"; |
| 74 | |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 75 | rtc::CopyOnWriteBuffer packet; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 76 | ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 77 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 78 | VerifyOpenMessageFormat(packet, label, config); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 79 | |
| 80 | std::string output_label; |
| 81 | webrtc::DataChannelInit output_config; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 82 | ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label, |
| 83 | &output_config)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 84 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 85 | EXPECT_EQ(label, output_label); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 86 | 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 | |
| 92 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmitTime) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 93 | webrtc::DataChannelInit config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 94 | std::string label = "abc"; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 95 | config.ordered = false; |
| 96 | config.maxRetransmitTime = 10; |
| 97 | config.protocol = "y"; |
| 98 | |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 99 | rtc::CopyOnWriteBuffer packet; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 100 | ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 101 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 102 | VerifyOpenMessageFormat(packet, label, config); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 103 | |
| 104 | std::string output_label; |
| 105 | webrtc::DataChannelInit output_config; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 106 | ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label, |
| 107 | &output_config)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 108 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 109 | EXPECT_EQ(label, output_label); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 110 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 111 | EXPECT_EQ(config.ordered, output_config.ordered); |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 112 | EXPECT_EQ(*config.maxRetransmitTime, *output_config.maxRetransmitTime); |
| 113 | EXPECT_FALSE(output_config.maxRetransmits); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmits) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 117 | webrtc::DataChannelInit config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 118 | std::string label = "abc"; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 119 | config.maxRetransmits = 10; |
| 120 | config.protocol = "y"; |
| 121 | |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 122 | rtc::CopyOnWriteBuffer packet; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 123 | ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 124 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 125 | VerifyOpenMessageFormat(packet, label, config); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 126 | |
| 127 | std::string output_label; |
| 128 | webrtc::DataChannelInit output_config; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 129 | ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label, |
| 130 | &output_config)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 131 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 132 | EXPECT_EQ(label, output_label); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 133 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 134 | EXPECT_EQ(config.ordered, output_config.ordered); |
| 135 | EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits); |
Harald Alvestrand | f3736ed | 2019-04-08 13:09:30 +0200 | [diff] [blame] | 136 | EXPECT_FALSE(output_config.maxRetransmitTime); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 137 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 138 | |
| 139 | TEST_F(SctpUtilsTest, WriteParseAckMessage) { |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 140 | rtc::CopyOnWriteBuffer packet; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 141 | webrtc::WriteDataChannelOpenAckMessage(&packet); |
| 142 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 143 | uint8_t message_type; |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 144 | rtc::ByteBufferReader buffer(packet.data<char>(), packet.size()); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 145 | ASSERT_TRUE(buffer.ReadUInt8(&message_type)); |
| 146 | EXPECT_EQ(0x02, message_type); |
| 147 | |
| 148 | EXPECT_TRUE(webrtc::ParseDataChannelOpenAckMessage(packet)); |
| 149 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 150 | |
| 151 | TEST_F(SctpUtilsTest, TestIsOpenMessage) { |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 152 | rtc::CopyOnWriteBuffer open(1); |
| 153 | open[0] = 0x03; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 154 | EXPECT_TRUE(webrtc::IsOpenMessage(open)); |
| 155 | |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 156 | rtc::CopyOnWriteBuffer openAck(1); |
| 157 | openAck[0] = 0x02; |
| 158 | EXPECT_FALSE(webrtc::IsOpenMessage(openAck)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 159 | |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 160 | rtc::CopyOnWriteBuffer invalid(1); |
| 161 | invalid[0] = 0x01; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 162 | EXPECT_FALSE(webrtc::IsOpenMessage(invalid)); |
| 163 | |
ossu | d4d2f60 | 2016-11-08 02:05:32 -0800 | [diff] [blame] | 164 | rtc::CopyOnWriteBuffer empty; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 165 | EXPECT_FALSE(webrtc::IsOpenMessage(empty)); |
| 166 | } |