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