wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2013 Google Inc |
| 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 | |
| 28 | #include "talk/app/webrtc/datachannelinterface.h" |
| 29 | #include "talk/base/bytebuffer.h" |
| 30 | #include "talk/base/gunit.h" |
| 31 | #include "talk/media/sctp/sctputils.h" |
| 32 | |
| 33 | class SctpUtilsTest : public testing::Test { |
| 34 | public: |
| 35 | void VerifyOpenMessageFormat(const talk_base::Buffer& packet, |
| 36 | const std::string& label, |
| 37 | const webrtc::DataChannelInit& config) { |
| 38 | uint8 message_type; |
| 39 | uint8 channel_type; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 40 | uint32 reliability; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 41 | uint16 priority; |
| 42 | uint16 label_length; |
| 43 | uint16 protocol_length; |
| 44 | |
| 45 | talk_base::ByteBuffer buffer(packet.data(), packet.length()); |
| 46 | ASSERT_TRUE(buffer.ReadUInt8(&message_type)); |
| 47 | EXPECT_EQ(0x03, message_type); |
| 48 | |
| 49 | ASSERT_TRUE(buffer.ReadUInt8(&channel_type)); |
| 50 | if (config.ordered) { |
| 51 | EXPECT_EQ(config.maxRetransmits > -1 ? |
| 52 | 0x01 : (config.maxRetransmitTime > -1 ? 0x02 : 0), |
| 53 | channel_type); |
| 54 | } else { |
| 55 | EXPECT_EQ(config.maxRetransmits > -1 ? |
| 56 | 0x81 : (config.maxRetransmitTime > -1 ? 0x82 : 0x80), |
| 57 | channel_type); |
| 58 | } |
| 59 | |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 60 | ASSERT_TRUE(buffer.ReadUInt16(&priority)); |
| 61 | |
| 62 | ASSERT_TRUE(buffer.ReadUInt32(&reliability)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 63 | if (config.maxRetransmits > -1 || config.maxRetransmitTime > -1) { |
| 64 | EXPECT_EQ(config.maxRetransmits > -1 ? |
| 65 | config.maxRetransmits : config.maxRetransmitTime, |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 66 | static_cast<int>(reliability)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 67 | } |
| 68 | |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 69 | ASSERT_TRUE(buffer.ReadUInt16(&label_length)); |
| 70 | ASSERT_TRUE(buffer.ReadUInt16(&protocol_length)); |
| 71 | EXPECT_EQ(label.size(), label_length); |
| 72 | EXPECT_EQ(config.protocol.size(), protocol_length); |
| 73 | |
| 74 | std::string label_output; |
| 75 | ASSERT_TRUE(buffer.ReadString(&label_output, label_length)); |
| 76 | EXPECT_EQ(label, label_output); |
| 77 | std::string protocol_output; |
| 78 | ASSERT_TRUE(buffer.ReadString(&protocol_output, protocol_length)); |
| 79 | EXPECT_EQ(config.protocol, protocol_output); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | TEST_F(SctpUtilsTest, WriteParseMessageWithOrderedReliable) { |
| 84 | std::string input_label = "abc"; |
| 85 | webrtc::DataChannelInit config; |
| 86 | config.protocol = "y"; |
| 87 | |
| 88 | talk_base::Buffer packet; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 89 | ASSERT_TRUE( |
| 90 | cricket::WriteDataChannelOpenMessage(input_label, config, &packet)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 91 | |
| 92 | VerifyOpenMessageFormat(packet, input_label, config); |
| 93 | |
| 94 | std::string output_label; |
| 95 | webrtc::DataChannelInit output_config; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 96 | ASSERT_TRUE(cricket::ParseDataChannelOpenMessage( |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 97 | packet, &output_label, &output_config)); |
| 98 | |
| 99 | EXPECT_EQ(input_label, output_label); |
| 100 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 101 | EXPECT_EQ(config.ordered, output_config.ordered); |
| 102 | EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime); |
| 103 | EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits); |
| 104 | } |
| 105 | |
| 106 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmitTime) { |
| 107 | std::string input_label = "abc"; |
| 108 | webrtc::DataChannelInit config; |
| 109 | config.ordered = false; |
| 110 | config.maxRetransmitTime = 10; |
| 111 | config.protocol = "y"; |
| 112 | |
| 113 | talk_base::Buffer packet; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 114 | ASSERT_TRUE( |
| 115 | cricket::WriteDataChannelOpenMessage(input_label, config, &packet)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 116 | |
| 117 | VerifyOpenMessageFormat(packet, input_label, config); |
| 118 | |
| 119 | std::string output_label; |
| 120 | webrtc::DataChannelInit output_config; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 121 | ASSERT_TRUE(cricket::ParseDataChannelOpenMessage( |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 122 | packet, &output_label, &output_config)); |
| 123 | |
| 124 | EXPECT_EQ(input_label, output_label); |
| 125 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 126 | EXPECT_EQ(config.ordered, output_config.ordered); |
| 127 | EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime); |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 128 | EXPECT_EQ(-1, output_config.maxRetransmits); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmits) { |
| 132 | std::string input_label = "abc"; |
| 133 | webrtc::DataChannelInit config; |
| 134 | config.maxRetransmits = 10; |
| 135 | config.protocol = "y"; |
| 136 | |
| 137 | talk_base::Buffer packet; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 138 | ASSERT_TRUE( |
| 139 | cricket::WriteDataChannelOpenMessage(input_label, config, &packet)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 140 | |
| 141 | VerifyOpenMessageFormat(packet, input_label, config); |
| 142 | |
| 143 | std::string output_label; |
| 144 | webrtc::DataChannelInit output_config; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 145 | ASSERT_TRUE(cricket::ParseDataChannelOpenMessage( |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 146 | packet, &output_label, &output_config)); |
| 147 | |
| 148 | EXPECT_EQ(input_label, output_label); |
| 149 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 150 | EXPECT_EQ(config.ordered, output_config.ordered); |
| 151 | EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits); |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 152 | EXPECT_EQ(-1, output_config.maxRetransmitTime); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 153 | } |