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; |
| 40 | uint16 reliability; |
| 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 | |
| 60 | ASSERT_TRUE(buffer.ReadUInt16(&reliability)); |
| 61 | if (config.maxRetransmits > -1 || config.maxRetransmitTime > -1) { |
| 62 | EXPECT_EQ(config.maxRetransmits > -1 ? |
| 63 | config.maxRetransmits : config.maxRetransmitTime, |
| 64 | reliability); |
| 65 | } |
| 66 | |
| 67 | ASSERT_TRUE(buffer.ReadUInt16(&priority)); |
| 68 | |
| 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; |
| 89 | ASSERT(cricket::WriteDataChannelOpenMessage(input_label, config, &packet)); |
| 90 | |
| 91 | VerifyOpenMessageFormat(packet, input_label, config); |
| 92 | |
| 93 | std::string output_label; |
| 94 | webrtc::DataChannelInit output_config; |
| 95 | ASSERT(cricket::ParseDataChannelOpenMessage( |
| 96 | packet, &output_label, &output_config)); |
| 97 | |
| 98 | EXPECT_EQ(input_label, output_label); |
| 99 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 100 | EXPECT_EQ(config.ordered, output_config.ordered); |
| 101 | EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime); |
| 102 | EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits); |
| 103 | } |
| 104 | |
| 105 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmitTime) { |
| 106 | std::string input_label = "abc"; |
| 107 | webrtc::DataChannelInit config; |
| 108 | config.ordered = false; |
| 109 | config.maxRetransmitTime = 10; |
| 110 | config.protocol = "y"; |
| 111 | |
| 112 | talk_base::Buffer packet; |
| 113 | ASSERT(cricket::WriteDataChannelOpenMessage(input_label, config, &packet)); |
| 114 | |
| 115 | VerifyOpenMessageFormat(packet, input_label, config); |
| 116 | |
| 117 | std::string output_label; |
| 118 | webrtc::DataChannelInit output_config; |
| 119 | ASSERT(cricket::ParseDataChannelOpenMessage( |
| 120 | packet, &output_label, &output_config)); |
| 121 | |
| 122 | EXPECT_EQ(input_label, output_label); |
| 123 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 124 | EXPECT_EQ(config.ordered, output_config.ordered); |
| 125 | EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime); |
| 126 | } |
| 127 | |
| 128 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmits) { |
| 129 | std::string input_label = "abc"; |
| 130 | webrtc::DataChannelInit config; |
| 131 | config.maxRetransmits = 10; |
| 132 | config.protocol = "y"; |
| 133 | |
| 134 | talk_base::Buffer packet; |
| 135 | ASSERT(cricket::WriteDataChannelOpenMessage(input_label, config, &packet)); |
| 136 | |
| 137 | VerifyOpenMessageFormat(packet, input_label, config); |
| 138 | |
| 139 | std::string output_label; |
| 140 | webrtc::DataChannelInit output_config; |
| 141 | ASSERT(cricket::ParseDataChannelOpenMessage( |
| 142 | packet, &output_label, &output_config)); |
| 143 | |
| 144 | EXPECT_EQ(input_label, output_label); |
| 145 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 146 | EXPECT_EQ(config.ordered, output_config.ordered); |
| 147 | EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits); |
| 148 | } |