2013-10-16 18:12:02 +00:00
|
|
|
/*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Copyright 2013 The WebRTC project authors. All Rights Reserved.
|
2013-10-16 18:12:02 +00:00
|
|
|
*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2013-10-16 18:12:02 +00:00
|
|
|
*/
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "pc/sctp_utils.h"
|
2018-11-28 16:47:49 +01:00
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2022-02-23 13:44:59 +00:00
|
|
|
#include "absl/types/optional.h"
|
|
|
|
|
#include "api/priority.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/byte_buffer.h"
|
|
|
|
|
#include "rtc_base/copy_on_write_buffer.h"
|
2018-11-28 16:47:49 +01:00
|
|
|
#include "test/gtest.h"
|
2013-10-16 18:12:02 +00:00
|
|
|
|
2019-04-09 15:11:12 +02:00
|
|
|
class SctpUtilsTest : public ::testing::Test {
|
2013-10-16 18:12:02 +00:00
|
|
|
public:
|
2016-11-08 02:05:32 -08:00
|
|
|
void VerifyOpenMessageFormat(const rtc::CopyOnWriteBuffer& packet,
|
2013-10-16 18:12:02 +00:00
|
|
|
const std::string& label,
|
|
|
|
|
const webrtc::DataChannelInit& config) {
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint8_t message_type;
|
|
|
|
|
uint8_t channel_type;
|
|
|
|
|
uint32_t reliability;
|
|
|
|
|
uint16_t priority;
|
|
|
|
|
uint16_t label_length;
|
|
|
|
|
uint16_t protocol_length;
|
2013-10-16 18:12:02 +00:00
|
|
|
|
2016-11-08 02:05:32 -08:00
|
|
|
rtc::ByteBufferReader buffer(packet.data<char>(), packet.size());
|
2013-10-16 18:12:02 +00:00
|
|
|
ASSERT_TRUE(buffer.ReadUInt8(&message_type));
|
|
|
|
|
EXPECT_EQ(0x03, message_type);
|
|
|
|
|
|
|
|
|
|
ASSERT_TRUE(buffer.ReadUInt8(&channel_type));
|
|
|
|
|
if (config.ordered) {
|
2019-04-08 13:09:30 +02:00
|
|
|
EXPECT_EQ(
|
|
|
|
|
config.maxRetransmits ? 0x01 : (config.maxRetransmitTime ? 0x02 : 0),
|
|
|
|
|
channel_type);
|
2013-10-16 18:12:02 +00:00
|
|
|
} else {
|
2019-04-08 13:09:30 +02:00
|
|
|
EXPECT_EQ(config.maxRetransmits
|
2013-10-16 18:12:02 +00:00
|
|
|
? 0x81
|
2019-04-08 13:09:30 +02:00
|
|
|
: (config.maxRetransmitTime ? 0x82 : 0x80),
|
2013-10-16 18:12:02 +00:00
|
|
|
channel_type);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-25 21:18:33 +00:00
|
|
|
ASSERT_TRUE(buffer.ReadUInt16(&priority));
|
2020-05-16 08:37:49 +02:00
|
|
|
if (config.priority) {
|
|
|
|
|
// Exact values are checked by round-trip conversion, but
|
|
|
|
|
// all values defined are greater than zero.
|
|
|
|
|
EXPECT_GT(priority, 0);
|
|
|
|
|
} else {
|
|
|
|
|
EXPECT_EQ(priority, 0);
|
|
|
|
|
}
|
2013-10-25 21:18:33 +00:00
|
|
|
|
|
|
|
|
ASSERT_TRUE(buffer.ReadUInt32(&reliability));
|
2019-04-08 13:09:30 +02:00
|
|
|
if (config.maxRetransmits || config.maxRetransmitTime) {
|
|
|
|
|
EXPECT_EQ(config.maxRetransmits ? *config.maxRetransmits
|
|
|
|
|
: *config.maxRetransmitTime,
|
2013-10-25 21:18:33 +00:00
|
|
|
static_cast<int>(reliability));
|
2013-10-16 18:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASSERT_TRUE(buffer.ReadUInt16(&label_length));
|
|
|
|
|
ASSERT_TRUE(buffer.ReadUInt16(&protocol_length));
|
|
|
|
|
EXPECT_EQ(label.size(), label_length);
|
|
|
|
|
EXPECT_EQ(config.protocol.size(), protocol_length);
|
|
|
|
|
|
|
|
|
|
std::string label_output;
|
|
|
|
|
ASSERT_TRUE(buffer.ReadString(&label_output, label_length));
|
|
|
|
|
EXPECT_EQ(label, label_output);
|
|
|
|
|
std::string protocol_output;
|
|
|
|
|
ASSERT_TRUE(buffer.ReadString(&protocol_output, protocol_length));
|
|
|
|
|
EXPECT_EQ(config.protocol, protocol_output);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-01-14 10:00:58 +00:00
|
|
|
TEST_F(SctpUtilsTest, WriteParseOpenMessageWithOrderedReliable) {
|
2013-10-16 18:12:02 +00:00
|
|
|
webrtc::DataChannelInit config;
|
2014-01-14 10:00:58 +00:00
|
|
|
std::string label = "abc";
|
2013-10-16 18:12:02 +00:00
|
|
|
config.protocol = "y";
|
|
|
|
|
|
2016-11-08 02:05:32 -08:00
|
|
|
rtc::CopyOnWriteBuffer packet;
|
2014-01-14 10:00:58 +00:00
|
|
|
ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
|
2013-10-16 18:12:02 +00:00
|
|
|
|
2014-01-14 10:00:58 +00:00
|
|
|
VerifyOpenMessageFormat(packet, label, config);
|
2013-10-16 18:12:02 +00:00
|
|
|
|
|
|
|
|
std::string output_label;
|
|
|
|
|
webrtc::DataChannelInit output_config;
|
2014-01-14 10:00:58 +00:00
|
|
|
ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
|
2013-10-16 18:12:02 +00:00
|
|
|
&output_config));
|
|
|
|
|
|
2014-01-14 10:00:58 +00:00
|
|
|
EXPECT_EQ(label, output_label);
|
2013-10-16 18:12:02 +00:00
|
|
|
EXPECT_EQ(config.protocol, output_config.protocol);
|
|
|
|
|
EXPECT_EQ(config.ordered, output_config.ordered);
|
|
|
|
|
EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime);
|
|
|
|
|
EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmitTime) {
|
|
|
|
|
webrtc::DataChannelInit config;
|
2014-01-14 10:00:58 +00:00
|
|
|
std::string label = "abc";
|
2013-10-16 18:12:02 +00:00
|
|
|
config.ordered = false;
|
|
|
|
|
config.maxRetransmitTime = 10;
|
|
|
|
|
config.protocol = "y";
|
|
|
|
|
|
2016-11-08 02:05:32 -08:00
|
|
|
rtc::CopyOnWriteBuffer packet;
|
2014-01-14 10:00:58 +00:00
|
|
|
ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
|
2013-10-16 18:12:02 +00:00
|
|
|
|
2014-01-14 10:00:58 +00:00
|
|
|
VerifyOpenMessageFormat(packet, label, config);
|
2013-10-16 18:12:02 +00:00
|
|
|
|
|
|
|
|
std::string output_label;
|
|
|
|
|
webrtc::DataChannelInit output_config;
|
2014-01-14 10:00:58 +00:00
|
|
|
ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
|
2013-10-16 18:12:02 +00:00
|
|
|
&output_config));
|
|
|
|
|
|
2014-01-14 10:00:58 +00:00
|
|
|
EXPECT_EQ(label, output_label);
|
2013-10-16 18:12:02 +00:00
|
|
|
EXPECT_EQ(config.protocol, output_config.protocol);
|
|
|
|
|
EXPECT_EQ(config.ordered, output_config.ordered);
|
2019-04-08 13:09:30 +02:00
|
|
|
EXPECT_EQ(*config.maxRetransmitTime, *output_config.maxRetransmitTime);
|
|
|
|
|
EXPECT_FALSE(output_config.maxRetransmits);
|
2013-10-16 18:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmits) {
|
|
|
|
|
webrtc::DataChannelInit config;
|
2014-01-14 10:00:58 +00:00
|
|
|
std::string label = "abc";
|
2013-10-16 18:12:02 +00:00
|
|
|
config.maxRetransmits = 10;
|
|
|
|
|
config.protocol = "y";
|
|
|
|
|
|
2016-11-08 02:05:32 -08:00
|
|
|
rtc::CopyOnWriteBuffer packet;
|
2014-01-14 10:00:58 +00:00
|
|
|
ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
|
2013-10-16 18:12:02 +00:00
|
|
|
|
2014-01-14 10:00:58 +00:00
|
|
|
VerifyOpenMessageFormat(packet, label, config);
|
2013-10-16 18:12:02 +00:00
|
|
|
|
|
|
|
|
std::string output_label;
|
|
|
|
|
webrtc::DataChannelInit output_config;
|
2014-01-14 10:00:58 +00:00
|
|
|
ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
|
2013-10-16 18:12:02 +00:00
|
|
|
&output_config));
|
|
|
|
|
|
2014-01-14 10:00:58 +00:00
|
|
|
EXPECT_EQ(label, output_label);
|
2013-10-16 18:12:02 +00:00
|
|
|
EXPECT_EQ(config.protocol, output_config.protocol);
|
|
|
|
|
EXPECT_EQ(config.ordered, output_config.ordered);
|
|
|
|
|
EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits);
|
2019-04-08 13:09:30 +02:00
|
|
|
EXPECT_FALSE(output_config.maxRetransmitTime);
|
2013-10-16 18:12:02 +00:00
|
|
|
}
|
2014-01-14 10:00:58 +00:00
|
|
|
|
2020-05-16 08:37:49 +02:00
|
|
|
TEST_F(SctpUtilsTest, WriteParseOpenMessageWithPriority) {
|
|
|
|
|
webrtc::DataChannelInit config;
|
|
|
|
|
std::string label = "abc";
|
|
|
|
|
config.protocol = "y";
|
|
|
|
|
config.priority = webrtc::Priority::kVeryLow;
|
|
|
|
|
|
|
|
|
|
rtc::CopyOnWriteBuffer packet;
|
|
|
|
|
ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
|
|
|
|
|
|
|
|
|
|
VerifyOpenMessageFormat(packet, label, config);
|
|
|
|
|
|
|
|
|
|
std::string output_label;
|
|
|
|
|
webrtc::DataChannelInit output_config;
|
|
|
|
|
ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
|
|
|
|
|
&output_config));
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(label, output_label);
|
|
|
|
|
ASSERT_TRUE(output_config.priority);
|
|
|
|
|
EXPECT_EQ(*config.priority, *output_config.priority);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-14 10:00:58 +00:00
|
|
|
TEST_F(SctpUtilsTest, WriteParseAckMessage) {
|
2016-11-08 02:05:32 -08:00
|
|
|
rtc::CopyOnWriteBuffer packet;
|
2014-01-14 10:00:58 +00:00
|
|
|
webrtc::WriteDataChannelOpenAckMessage(&packet);
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint8_t message_type;
|
2016-11-08 02:05:32 -08:00
|
|
|
rtc::ByteBufferReader buffer(packet.data<char>(), packet.size());
|
2014-01-14 10:00:58 +00:00
|
|
|
ASSERT_TRUE(buffer.ReadUInt8(&message_type));
|
|
|
|
|
EXPECT_EQ(0x02, message_type);
|
|
|
|
|
|
|
|
|
|
EXPECT_TRUE(webrtc::ParseDataChannelOpenAckMessage(packet));
|
|
|
|
|
}
|
2015-10-14 11:33:11 -07:00
|
|
|
|
|
|
|
|
TEST_F(SctpUtilsTest, TestIsOpenMessage) {
|
2016-11-08 02:05:32 -08:00
|
|
|
rtc::CopyOnWriteBuffer open(1);
|
2021-01-07 15:24:05 +01:00
|
|
|
open.MutableData()[0] = 0x03;
|
2015-10-14 11:33:11 -07:00
|
|
|
EXPECT_TRUE(webrtc::IsOpenMessage(open));
|
|
|
|
|
|
2016-11-08 02:05:32 -08:00
|
|
|
rtc::CopyOnWriteBuffer openAck(1);
|
2021-01-07 15:24:05 +01:00
|
|
|
openAck.MutableData()[0] = 0x02;
|
2016-11-08 02:05:32 -08:00
|
|
|
EXPECT_FALSE(webrtc::IsOpenMessage(openAck));
|
2015-10-14 11:33:11 -07:00
|
|
|
|
2016-11-08 02:05:32 -08:00
|
|
|
rtc::CopyOnWriteBuffer invalid(1);
|
2021-01-07 15:24:05 +01:00
|
|
|
invalid.MutableData()[0] = 0x01;
|
2015-10-14 11:33:11 -07:00
|
|
|
EXPECT_FALSE(webrtc::IsOpenMessage(invalid));
|
|
|
|
|
|
2016-11-08 02:05:32 -08:00
|
|
|
rtc::CopyOnWriteBuffer empty;
|
2015-10-14 11:33:11 -07:00
|
|
|
EXPECT_FALSE(webrtc::IsOpenMessage(empty));
|
|
|
|
|
}
|