wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3 | * Copyright 2013 Google Inc. |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame^] | 28 | #include "webrtc/api/sctputils.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 29 | #include "webrtc/base/bytebuffer.h" |
| 30 | #include "webrtc/base/gunit.h" |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 31 | |
| 32 | class SctpUtilsTest : public testing::Test { |
| 33 | public: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 34 | void VerifyOpenMessageFormat(const rtc::Buffer& packet, |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 35 | const std::string& label, |
| 36 | const webrtc::DataChannelInit& config) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 37 | uint8_t message_type; |
| 38 | uint8_t channel_type; |
| 39 | uint32_t reliability; |
| 40 | uint16_t priority; |
| 41 | uint16_t label_length; |
| 42 | uint16_t protocol_length; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 43 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 44 | rtc::ByteBuffer buffer(packet.data(), packet.length()); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 45 | ASSERT_TRUE(buffer.ReadUInt8(&message_type)); |
| 46 | EXPECT_EQ(0x03, message_type); |
| 47 | |
| 48 | ASSERT_TRUE(buffer.ReadUInt8(&channel_type)); |
| 49 | if (config.ordered) { |
| 50 | EXPECT_EQ(config.maxRetransmits > -1 ? |
| 51 | 0x01 : (config.maxRetransmitTime > -1 ? 0x02 : 0), |
| 52 | channel_type); |
| 53 | } else { |
| 54 | EXPECT_EQ(config.maxRetransmits > -1 ? |
| 55 | 0x81 : (config.maxRetransmitTime > -1 ? 0x82 : 0x80), |
| 56 | channel_type); |
| 57 | } |
| 58 | |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 59 | ASSERT_TRUE(buffer.ReadUInt16(&priority)); |
| 60 | |
| 61 | ASSERT_TRUE(buffer.ReadUInt32(&reliability)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 62 | if (config.maxRetransmits > -1 || config.maxRetransmitTime > -1) { |
| 63 | EXPECT_EQ(config.maxRetransmits > -1 ? |
| 64 | config.maxRetransmits : config.maxRetransmitTime, |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 65 | static_cast<int>(reliability)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 66 | } |
| 67 | |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 68 | ASSERT_TRUE(buffer.ReadUInt16(&label_length)); |
| 69 | ASSERT_TRUE(buffer.ReadUInt16(&protocol_length)); |
| 70 | EXPECT_EQ(label.size(), label_length); |
| 71 | EXPECT_EQ(config.protocol.size(), protocol_length); |
| 72 | |
| 73 | std::string label_output; |
| 74 | ASSERT_TRUE(buffer.ReadString(&label_output, label_length)); |
| 75 | EXPECT_EQ(label, label_output); |
| 76 | std::string protocol_output; |
| 77 | ASSERT_TRUE(buffer.ReadString(&protocol_output, protocol_length)); |
| 78 | EXPECT_EQ(config.protocol, protocol_output); |
| 79 | } |
| 80 | }; |
| 81 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 82 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithOrderedReliable) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 83 | webrtc::DataChannelInit config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 84 | std::string label = "abc"; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 85 | config.protocol = "y"; |
| 86 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 87 | rtc::Buffer packet; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 88 | ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 89 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 90 | VerifyOpenMessageFormat(packet, label, config); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 91 | |
| 92 | std::string output_label; |
| 93 | webrtc::DataChannelInit output_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 94 | ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage( |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 95 | packet, &output_label, &output_config)); |
| 96 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 97 | EXPECT_EQ(label, output_label); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 98 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 99 | EXPECT_EQ(config.ordered, output_config.ordered); |
| 100 | EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime); |
| 101 | EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits); |
| 102 | } |
| 103 | |
| 104 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmitTime) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 105 | webrtc::DataChannelInit config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 106 | std::string label = "abc"; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 107 | config.ordered = false; |
| 108 | config.maxRetransmitTime = 10; |
| 109 | config.protocol = "y"; |
| 110 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 111 | rtc::Buffer packet; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 112 | ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 113 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 114 | VerifyOpenMessageFormat(packet, label, config); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 115 | |
| 116 | std::string output_label; |
| 117 | webrtc::DataChannelInit output_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 118 | ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage( |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 119 | packet, &output_label, &output_config)); |
| 120 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 121 | EXPECT_EQ(label, output_label); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 122 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 123 | EXPECT_EQ(config.ordered, output_config.ordered); |
| 124 | EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime); |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 125 | EXPECT_EQ(-1, output_config.maxRetransmits); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmits) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 129 | webrtc::DataChannelInit config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 130 | std::string label = "abc"; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 131 | config.maxRetransmits = 10; |
| 132 | config.protocol = "y"; |
| 133 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 134 | rtc::Buffer packet; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 135 | ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 136 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 137 | VerifyOpenMessageFormat(packet, label, config); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 138 | |
| 139 | std::string output_label; |
| 140 | webrtc::DataChannelInit output_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 141 | ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage( |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 142 | packet, &output_label, &output_config)); |
| 143 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 144 | EXPECT_EQ(label, output_label); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 145 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 146 | EXPECT_EQ(config.ordered, output_config.ordered); |
| 147 | EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits); |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 148 | EXPECT_EQ(-1, output_config.maxRetransmitTime); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 149 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 150 | |
| 151 | TEST_F(SctpUtilsTest, WriteParseAckMessage) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 152 | rtc::Buffer packet; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 153 | webrtc::WriteDataChannelOpenAckMessage(&packet); |
| 154 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 155 | uint8_t message_type; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 156 | rtc::ByteBuffer buffer(packet.data(), packet.length()); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 157 | ASSERT_TRUE(buffer.ReadUInt8(&message_type)); |
| 158 | EXPECT_EQ(0x02, message_type); |
| 159 | |
| 160 | EXPECT_TRUE(webrtc::ParseDataChannelOpenAckMessage(packet)); |
| 161 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 162 | |
| 163 | TEST_F(SctpUtilsTest, TestIsOpenMessage) { |
| 164 | rtc::ByteBuffer open; |
| 165 | open.WriteUInt8(0x03); |
| 166 | EXPECT_TRUE(webrtc::IsOpenMessage(open)); |
| 167 | |
| 168 | rtc::ByteBuffer openAck; |
| 169 | openAck.WriteUInt8(0x02); |
| 170 | EXPECT_FALSE(webrtc::IsOpenMessage(open)); |
| 171 | |
| 172 | rtc::ByteBuffer invalid; |
| 173 | openAck.WriteUInt8(0x01); |
| 174 | EXPECT_FALSE(webrtc::IsOpenMessage(invalid)); |
| 175 | |
| 176 | rtc::ByteBuffer empty; |
| 177 | EXPECT_FALSE(webrtc::IsOpenMessage(empty)); |
| 178 | } |