| 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 <limits> |
| 16 |
| 17 | #include "absl/types/optional.h" |
| 18 | #include "api/priority.h" |
| 19 | #include "media/sctp/sctp_transport_internal.h" |
| 20 | #include "rtc_base/byte_buffer.h" |
| 21 | #include "rtc_base/copy_on_write_buffer.h" |
| 22 | #include "test/gtest.h" |
| 23 |
| 24 | using webrtc::StreamId; |
| 25 |
| 26 | class SctpUtilsTest : public ::testing::Test { |
| 27 | public: |
| 28 | void VerifyOpenMessageFormat(const rtc::CopyOnWriteBuffer& packet, |
| 29 | const std::string& label, |
| 30 | const webrtc::DataChannelInit& config) { |
| 31 | uint8_t message_type; |
| 32 | uint8_t channel_type; |
| 33 | uint32_t reliability; |
| 34 | uint16_t priority; |
| 35 | uint16_t label_length; |
| 36 | uint16_t protocol_length; |
| 37 |
| 38 | rtc::ByteBufferReader buffer(packet); |
| 39 | ASSERT_TRUE(buffer.ReadUInt8(&message_type)); |
| 40 | EXPECT_EQ(0x03, message_type); |
| 41 |
| 42 | ASSERT_TRUE(buffer.ReadUInt8(&channel_type)); |
| 43 | if (config.ordered) { |
| 44 | EXPECT_EQ( |
| 45 | config.maxRetransmits ? 0x01 : (config.maxRetransmitTime ? 0x02 : 0), |
| 46 | channel_type); |
| 47 | } else { |
| 48 | EXPECT_EQ(config.maxRetransmits |
| 49 | ? 0x81 |
| 50 | : (config.maxRetransmitTime ? 0x82 : 0x80), |
| 51 | channel_type); |
| 52 | } |
| 53 |
| 54 | ASSERT_TRUE(buffer.ReadUInt16(&priority)); |
| 55 | if (config.priority) { |
| 56 | // Exact values are checked by round-trip conversion, but |
| 57 | // all values defined are greater than zero. |
| 58 | EXPECT_GT(priority, 0); |
| 59 | } else { |
| 60 | EXPECT_EQ(priority, 0); |
| 61 | } |
| 62 |
| 63 | ASSERT_TRUE(buffer.ReadUInt32(&reliability)); |
| 64 | if (config.maxRetransmits || config.maxRetransmitTime) { |
| 65 | EXPECT_EQ(config.maxRetransmits ? *config.maxRetransmits |
| 66 | : *config.maxRetransmitTime, |
| 67 | static_cast<int>(reliability)); |
| 68 | } |
| 69 |
| 70 | ASSERT_TRUE(buffer.ReadUInt16(&label_length)); |
| 71 | ASSERT_TRUE(buffer.ReadUInt16(&protocol_length)); |
| 72 | EXPECT_EQ(label.size(), label_length); |
| 73 | EXPECT_EQ(config.protocol.size(), protocol_length); |
| 74 |
| 75 | absl::string_view label_output; |
| 76 | ASSERT_TRUE(buffer.ReadStringView(&label_output, label_length)); |
| 77 | EXPECT_EQ(label, label_output); |
| 78 | absl::string_view protocol_output; |
| 79 | ASSERT_TRUE(buffer.ReadStringView(&protocol_output, protocol_length)); |
| 80 | EXPECT_EQ(config.protocol, protocol_output); |
| 81 | } |
| 82 | }; |
| 83 |
| 84 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithOrderedReliable) { |
| 85 | webrtc::DataChannelInit config; |
| 86 | std::string label = "abc"; |
| 87 | config.protocol = "y"; |
| 88 |
| 89 | rtc::CopyOnWriteBuffer packet; |
| 90 | ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet)); |
| 91 |
| 92 | VerifyOpenMessageFormat(packet, label, config); |
| 93 |
| 94 | std::string output_label; |
| 95 | webrtc::DataChannelInit output_config; |
| 96 | ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label, |
| 97 | &output_config)); |
| 98 |
| 99 | EXPECT_EQ(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 | webrtc::DataChannelInit config; |
| 108 | std::string label = "abc"; |
| 109 | config.ordered = false; |
| 110 | config.maxRetransmitTime = 10; |
| 111 | config.protocol = "y"; |
| 112 |
| 113 | rtc::CopyOnWriteBuffer packet; |
| 114 | ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet)); |
| 115 |
| 116 | VerifyOpenMessageFormat(packet, label, config); |
| 117 |
| 118 | std::string output_label; |
| 119 | webrtc::DataChannelInit output_config; |
| 120 | ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label, |
| 121 | &output_config)); |
| 122 |
| 123 | EXPECT_EQ(label, output_label); |
| 124 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 125 | EXPECT_EQ(config.ordered, output_config.ordered); |
| 126 | EXPECT_EQ(*config.maxRetransmitTime, *output_config.maxRetransmitTime); |
| 127 | EXPECT_FALSE(output_config.maxRetransmits); |
| 128 | } |
| 129 |
| 130 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmits) { |
| 131 | webrtc::DataChannelInit config; |
| 132 | std::string label = "abc"; |
| 133 | config.maxRetransmits = 10; |
| 134 | config.protocol = "y"; |
| 135 |
| 136 | rtc::CopyOnWriteBuffer packet; |
| 137 | ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet)); |
| 138 |
| 139 | VerifyOpenMessageFormat(packet, label, config); |
| 140 |
| 141 | std::string output_label; |
| 142 | webrtc::DataChannelInit output_config; |
| 143 | ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label, |
| 144 | &output_config)); |
| 145 |
| 146 | EXPECT_EQ(label, output_label); |
| 147 | EXPECT_EQ(config.protocol, output_config.protocol); |
| 148 | EXPECT_EQ(config.ordered, output_config.ordered); |
| 149 | EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits); |
| 150 | EXPECT_FALSE(output_config.maxRetransmitTime); |
| 151 | } |
| 152 |
| 153 | TEST_F(SctpUtilsTest, WriteParseOpenMessageWithPriority) { |
| 154 | webrtc::DataChannelInit config; |
| 155 | std::string label = "abc"; |
| 156 | config.protocol = "y"; |
| 157 | config.priority = webrtc::Priority::kVeryLow; |
| 158 |
| 159 | rtc::CopyOnWriteBuffer packet; |
| 160 | ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet)); |
| 161 |
| 162 | VerifyOpenMessageFormat(packet, label, config); |
| 163 |
| 164 | std::string output_label; |
| 165 | webrtc::DataChannelInit output_config; |
| 166 | ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label, |
| 167 | &output_config)); |
| 168 |
| 169 | EXPECT_EQ(label, output_label); |
| 170 | ASSERT_TRUE(output_config.priority); |
| 171 | EXPECT_EQ(*config.priority, *output_config.priority); |
| 172 | } |
| 173 |
| 174 | TEST_F(SctpUtilsTest, WriteParseAckMessage) { |
| 175 | rtc::CopyOnWriteBuffer packet; |
| 176 | webrtc::WriteDataChannelOpenAckMessage(&packet); |
| 177 |
| 178 | uint8_t message_type; |
| 179 | rtc::ByteBufferReader buffer(packet); |
| 180 | ASSERT_TRUE(buffer.ReadUInt8(&message_type)); |
| 181 | EXPECT_EQ(0x02, message_type); |
| 182 |
| 183 | EXPECT_TRUE(webrtc::ParseDataChannelOpenAckMessage(packet)); |
| 184 | } |
| 185 |
| 186 | TEST_F(SctpUtilsTest, TestIsOpenMessage) { |
| 187 | rtc::CopyOnWriteBuffer open(1); |
| 188 | open.MutableData()[0] = 0x03; |
| 189 | EXPECT_TRUE(webrtc::IsOpenMessage(open)); |
| 190 |
| 191 | rtc::CopyOnWriteBuffer openAck(1); |
| 192 | openAck.MutableData()[0] = 0x02; |
| 193 | EXPECT_FALSE(webrtc::IsOpenMessage(openAck)); |
| 194 |
| 195 | rtc::CopyOnWriteBuffer invalid(1); |
| 196 | invalid.MutableData()[0] = 0x01; |
| 197 | EXPECT_FALSE(webrtc::IsOpenMessage(invalid)); |
| 198 |
| 199 | rtc::CopyOnWriteBuffer empty; |
| 200 | EXPECT_FALSE(webrtc::IsOpenMessage(empty)); |
| 201 | } |
| 202 |
| 203 | TEST(SctpSidTest, Basics) { |
| 204 | // These static asserts are mostly here to aid with readability (i.e. knowing |
| 205 | // what these constants represent). |
| 206 | static_assert(cricket::kMinSctpSid == 0, "Min stream id should be 0"); |
| 207 | static_assert(cricket::kMaxSctpSid <= cricket::kSpecMaxSctpSid, ""); |
| 208 | static_assert( |
| 209 | cricket::kSpecMaxSctpSid == std::numeric_limits<uint16_t>::max(), |
| 210 | "Max legal sctp stream value should be 0xffff"); |
| 211 |
| 212 | // cricket::kMaxSctpSid is a chosen value in the webrtc implementation, |
| 213 | // the highest generated `sid` value chosen for resource reservation reasons. |
| 214 | // It's one less than kMaxSctpStreams (1024) or 1023 since sid values are |
| 215 | // zero based. |
| 216 |
| 217 | EXPECT_TRUE(!StreamId(-1).HasValue()); |
| 218 | EXPECT_TRUE(!StreamId(-2).HasValue()); |
| 219 | EXPECT_TRUE(StreamId(cricket::kMinSctpSid).HasValue()); |
| 220 | EXPECT_TRUE(StreamId(cricket::kMinSctpSid + 1).HasValue()); |
| 221 | EXPECT_TRUE(StreamId(cricket::kSpecMaxSctpSid).HasValue()); |
| 222 | EXPECT_TRUE(StreamId(cricket::kMaxSctpSid).HasValue()); |
| 223 |
| 224 | // Two illegal values are equal (both not valid). |
| 225 | EXPECT_EQ(StreamId(-1), StreamId(-2)); |
| 226 | // Two different, but legal, values, are not equal. |
| 227 | EXPECT_NE(StreamId(1), StreamId(2)); |
| 228 | // Test operator<() for container compatibility. |
| 229 | EXPECT_LT(StreamId(1), StreamId(2)); |
| 230 |
| 231 | // Test assignment, value() and reset(). |
| 232 | StreamId sid1; |
| 233 | StreamId sid2(cricket::kMaxSctpSid); |
| 234 | EXPECT_NE(sid1, sid2); |
| 235 | sid1 = sid2; |
| 236 | EXPECT_EQ(sid1, sid2); |
| 237 |
| 238 | EXPECT_EQ(sid1.stream_id_int(), cricket::kMaxSctpSid); |
| 239 | EXPECT_TRUE(sid1.HasValue()); |
| 240 | sid1.reset(); |
| 241 | EXPECT_FALSE(sid1.HasValue()); |
| 242 | } |
| 243 |