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/sctputils.h"
29#include "webrtc/base/bytebuffer.h"
30#include "webrtc/base/gunit.h"
31
32class SctpUtilsTest : public testing::Test {
33 public:
34  void VerifyOpenMessageFormat(const rtc::Buffer& packet,
35                               const std::string& label,
36                               const webrtc::DataChannelInit& config) {
37    uint8 message_type;
38    uint8 channel_type;
39    uint32 reliability;
40    uint16 priority;
41    uint16 label_length;
42    uint16 protocol_length;
43
44    rtc::ByteBuffer buffer(packet.data(), packet.length());
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
59    ASSERT_TRUE(buffer.ReadUInt16(&priority));
60
61    ASSERT_TRUE(buffer.ReadUInt32(&reliability));
62    if (config.maxRetransmits > -1 || config.maxRetransmitTime > -1) {
63      EXPECT_EQ(config.maxRetransmits > -1 ?
64                    config.maxRetransmits : config.maxRetransmitTime,
65                static_cast<int>(reliability));
66    }
67
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
82TEST_F(SctpUtilsTest, WriteParseOpenMessageWithOrderedReliable) {
83  webrtc::DataChannelInit config;
84  std::string label = "abc";
85  config.protocol = "y";
86
87  rtc::Buffer packet;
88  ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
89
90  VerifyOpenMessageFormat(packet, label, config);
91
92  std::string output_label;
93  webrtc::DataChannelInit output_config;
94  ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(
95      packet, &output_label, &output_config));
96
97  EXPECT_EQ(label, output_label);
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
104TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmitTime) {
105  webrtc::DataChannelInit config;
106  std::string label = "abc";
107  config.ordered = false;
108  config.maxRetransmitTime = 10;
109  config.protocol = "y";
110
111  rtc::Buffer packet;
112  ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
113
114  VerifyOpenMessageFormat(packet, label, config);
115
116  std::string output_label;
117  webrtc::DataChannelInit output_config;
118  ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(
119      packet, &output_label, &output_config));
120
121  EXPECT_EQ(label, output_label);
122  EXPECT_EQ(config.protocol, output_config.protocol);
123  EXPECT_EQ(config.ordered, output_config.ordered);
124  EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime);
125  EXPECT_EQ(-1, output_config.maxRetransmits);
126}
127
128TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmits) {
129  webrtc::DataChannelInit config;
130  std::string label = "abc";
131  config.maxRetransmits = 10;
132  config.protocol = "y";
133
134  rtc::Buffer packet;
135  ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
136
137  VerifyOpenMessageFormat(packet, label, config);
138
139  std::string output_label;
140  webrtc::DataChannelInit output_config;
141  ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(
142      packet, &output_label, &output_config));
143
144  EXPECT_EQ(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  EXPECT_EQ(-1, output_config.maxRetransmitTime);
149}
150
151TEST_F(SctpUtilsTest, WriteParseAckMessage) {
152  rtc::Buffer packet;
153  webrtc::WriteDataChannelOpenAckMessage(&packet);
154
155  uint8 message_type;
156  rtc::ByteBuffer buffer(packet.data(), packet.length());
157  ASSERT_TRUE(buffer.ReadUInt8(&message_type));
158  EXPECT_EQ(0x02, message_type);
159
160  EXPECT_TRUE(webrtc::ParseDataChannelOpenAckMessage(packet));
161}
162