xref: /aosp_15_r20/external/webrtc/pc/sctp_utils_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2013 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "pc/sctp_utils.h"
12 
13 #include <stdint.h>
14 
15 #include "absl/types/optional.h"
16 #include "api/priority.h"
17 #include "rtc_base/byte_buffer.h"
18 #include "rtc_base/copy_on_write_buffer.h"
19 #include "test/gtest.h"
20 
21 class SctpUtilsTest : public ::testing::Test {
22  public:
VerifyOpenMessageFormat(const rtc::CopyOnWriteBuffer & packet,const std::string & label,const webrtc::DataChannelInit & config)23   void VerifyOpenMessageFormat(const rtc::CopyOnWriteBuffer& packet,
24                                const std::string& label,
25                                const webrtc::DataChannelInit& config) {
26     uint8_t message_type;
27     uint8_t channel_type;
28     uint32_t reliability;
29     uint16_t priority;
30     uint16_t label_length;
31     uint16_t protocol_length;
32 
33     rtc::ByteBufferReader buffer(packet.data<char>(), packet.size());
34     ASSERT_TRUE(buffer.ReadUInt8(&message_type));
35     EXPECT_EQ(0x03, message_type);
36 
37     ASSERT_TRUE(buffer.ReadUInt8(&channel_type));
38     if (config.ordered) {
39       EXPECT_EQ(
40           config.maxRetransmits ? 0x01 : (config.maxRetransmitTime ? 0x02 : 0),
41           channel_type);
42     } else {
43       EXPECT_EQ(config.maxRetransmits
44                     ? 0x81
45                     : (config.maxRetransmitTime ? 0x82 : 0x80),
46                 channel_type);
47     }
48 
49     ASSERT_TRUE(buffer.ReadUInt16(&priority));
50     if (config.priority) {
51       // Exact values are checked by round-trip conversion, but
52       // all values defined are greater than zero.
53       EXPECT_GT(priority, 0);
54     } else {
55       EXPECT_EQ(priority, 0);
56     }
57 
58     ASSERT_TRUE(buffer.ReadUInt32(&reliability));
59     if (config.maxRetransmits || config.maxRetransmitTime) {
60       EXPECT_EQ(config.maxRetransmits ? *config.maxRetransmits
61                                       : *config.maxRetransmitTime,
62                 static_cast<int>(reliability));
63     }
64 
65     ASSERT_TRUE(buffer.ReadUInt16(&label_length));
66     ASSERT_TRUE(buffer.ReadUInt16(&protocol_length));
67     EXPECT_EQ(label.size(), label_length);
68     EXPECT_EQ(config.protocol.size(), protocol_length);
69 
70     std::string label_output;
71     ASSERT_TRUE(buffer.ReadString(&label_output, label_length));
72     EXPECT_EQ(label, label_output);
73     std::string protocol_output;
74     ASSERT_TRUE(buffer.ReadString(&protocol_output, protocol_length));
75     EXPECT_EQ(config.protocol, protocol_output);
76   }
77 };
78 
TEST_F(SctpUtilsTest,WriteParseOpenMessageWithOrderedReliable)79 TEST_F(SctpUtilsTest, WriteParseOpenMessageWithOrderedReliable) {
80   webrtc::DataChannelInit config;
81   std::string label = "abc";
82   config.protocol = "y";
83 
84   rtc::CopyOnWriteBuffer packet;
85   ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
86 
87   VerifyOpenMessageFormat(packet, label, config);
88 
89   std::string output_label;
90   webrtc::DataChannelInit output_config;
91   ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
92                                                   &output_config));
93 
94   EXPECT_EQ(label, output_label);
95   EXPECT_EQ(config.protocol, output_config.protocol);
96   EXPECT_EQ(config.ordered, output_config.ordered);
97   EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime);
98   EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits);
99 }
100 
TEST_F(SctpUtilsTest,WriteParseOpenMessageWithMaxRetransmitTime)101 TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmitTime) {
102   webrtc::DataChannelInit config;
103   std::string label = "abc";
104   config.ordered = false;
105   config.maxRetransmitTime = 10;
106   config.protocol = "y";
107 
108   rtc::CopyOnWriteBuffer packet;
109   ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
110 
111   VerifyOpenMessageFormat(packet, label, config);
112 
113   std::string output_label;
114   webrtc::DataChannelInit output_config;
115   ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
116                                                   &output_config));
117 
118   EXPECT_EQ(label, output_label);
119   EXPECT_EQ(config.protocol, output_config.protocol);
120   EXPECT_EQ(config.ordered, output_config.ordered);
121   EXPECT_EQ(*config.maxRetransmitTime, *output_config.maxRetransmitTime);
122   EXPECT_FALSE(output_config.maxRetransmits);
123 }
124 
TEST_F(SctpUtilsTest,WriteParseOpenMessageWithMaxRetransmits)125 TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmits) {
126   webrtc::DataChannelInit config;
127   std::string label = "abc";
128   config.maxRetransmits = 10;
129   config.protocol = "y";
130 
131   rtc::CopyOnWriteBuffer packet;
132   ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
133 
134   VerifyOpenMessageFormat(packet, label, config);
135 
136   std::string output_label;
137   webrtc::DataChannelInit output_config;
138   ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
139                                                   &output_config));
140 
141   EXPECT_EQ(label, output_label);
142   EXPECT_EQ(config.protocol, output_config.protocol);
143   EXPECT_EQ(config.ordered, output_config.ordered);
144   EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits);
145   EXPECT_FALSE(output_config.maxRetransmitTime);
146 }
147 
TEST_F(SctpUtilsTest,WriteParseOpenMessageWithPriority)148 TEST_F(SctpUtilsTest, WriteParseOpenMessageWithPriority) {
149   webrtc::DataChannelInit config;
150   std::string label = "abc";
151   config.protocol = "y";
152   config.priority = webrtc::Priority::kVeryLow;
153 
154   rtc::CopyOnWriteBuffer packet;
155   ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
156 
157   VerifyOpenMessageFormat(packet, label, config);
158 
159   std::string output_label;
160   webrtc::DataChannelInit output_config;
161   ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
162                                                   &output_config));
163 
164   EXPECT_EQ(label, output_label);
165   ASSERT_TRUE(output_config.priority);
166   EXPECT_EQ(*config.priority, *output_config.priority);
167 }
168 
TEST_F(SctpUtilsTest,WriteParseAckMessage)169 TEST_F(SctpUtilsTest, WriteParseAckMessage) {
170   rtc::CopyOnWriteBuffer packet;
171   webrtc::WriteDataChannelOpenAckMessage(&packet);
172 
173   uint8_t message_type;
174   rtc::ByteBufferReader buffer(packet.data<char>(), packet.size());
175   ASSERT_TRUE(buffer.ReadUInt8(&message_type));
176   EXPECT_EQ(0x02, message_type);
177 
178   EXPECT_TRUE(webrtc::ParseDataChannelOpenAckMessage(packet));
179 }
180 
TEST_F(SctpUtilsTest,TestIsOpenMessage)181 TEST_F(SctpUtilsTest, TestIsOpenMessage) {
182   rtc::CopyOnWriteBuffer open(1);
183   open.MutableData()[0] = 0x03;
184   EXPECT_TRUE(webrtc::IsOpenMessage(open));
185 
186   rtc::CopyOnWriteBuffer openAck(1);
187   openAck.MutableData()[0] = 0x02;
188   EXPECT_FALSE(webrtc::IsOpenMessage(openAck));
189 
190   rtc::CopyOnWriteBuffer invalid(1);
191   invalid.MutableData()[0] = 0x01;
192   EXPECT_FALSE(webrtc::IsOpenMessage(invalid));
193 
194   rtc::CopyOnWriteBuffer empty;
195   EXPECT_FALSE(webrtc::IsOpenMessage(empty));
196 }
197