You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "pc/sctp_utils.h"
  11. #include <stdint.h>
  12. #include "rtc_base/byte_buffer.h"
  13. #include "rtc_base/copy_on_write_buffer.h"
  14. #include "test/gtest.h"
  15. class SctpUtilsTest : public ::testing::Test {
  16. public:
  17. void VerifyOpenMessageFormat(const rtc::CopyOnWriteBuffer& packet,
  18. const std::string& label,
  19. const webrtc::DataChannelInit& config) {
  20. uint8_t message_type;
  21. uint8_t channel_type;
  22. uint32_t reliability;
  23. uint16_t priority;
  24. uint16_t label_length;
  25. uint16_t protocol_length;
  26. rtc::ByteBufferReader buffer(packet.data<char>(), packet.size());
  27. ASSERT_TRUE(buffer.ReadUInt8(&message_type));
  28. EXPECT_EQ(0x03, message_type);
  29. ASSERT_TRUE(buffer.ReadUInt8(&channel_type));
  30. if (config.ordered) {
  31. EXPECT_EQ(
  32. config.maxRetransmits ? 0x01 : (config.maxRetransmitTime ? 0x02 : 0),
  33. channel_type);
  34. } else {
  35. EXPECT_EQ(config.maxRetransmits
  36. ? 0x81
  37. : (config.maxRetransmitTime ? 0x82 : 0x80),
  38. channel_type);
  39. }
  40. ASSERT_TRUE(buffer.ReadUInt16(&priority));
  41. ASSERT_TRUE(buffer.ReadUInt32(&reliability));
  42. if (config.maxRetransmits || config.maxRetransmitTime) {
  43. EXPECT_EQ(config.maxRetransmits ? *config.maxRetransmits
  44. : *config.maxRetransmitTime,
  45. static_cast<int>(reliability));
  46. }
  47. ASSERT_TRUE(buffer.ReadUInt16(&label_length));
  48. ASSERT_TRUE(buffer.ReadUInt16(&protocol_length));
  49. EXPECT_EQ(label.size(), label_length);
  50. EXPECT_EQ(config.protocol.size(), protocol_length);
  51. std::string label_output;
  52. ASSERT_TRUE(buffer.ReadString(&label_output, label_length));
  53. EXPECT_EQ(label, label_output);
  54. std::string protocol_output;
  55. ASSERT_TRUE(buffer.ReadString(&protocol_output, protocol_length));
  56. EXPECT_EQ(config.protocol, protocol_output);
  57. }
  58. };
  59. TEST_F(SctpUtilsTest, WriteParseOpenMessageWithOrderedReliable) {
  60. webrtc::DataChannelInit config;
  61. std::string label = "abc";
  62. config.protocol = "y";
  63. rtc::CopyOnWriteBuffer packet;
  64. ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
  65. VerifyOpenMessageFormat(packet, label, config);
  66. std::string output_label;
  67. webrtc::DataChannelInit output_config;
  68. ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
  69. &output_config));
  70. EXPECT_EQ(label, output_label);
  71. EXPECT_EQ(config.protocol, output_config.protocol);
  72. EXPECT_EQ(config.ordered, output_config.ordered);
  73. EXPECT_EQ(config.maxRetransmitTime, output_config.maxRetransmitTime);
  74. EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits);
  75. }
  76. TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmitTime) {
  77. webrtc::DataChannelInit config;
  78. std::string label = "abc";
  79. config.ordered = false;
  80. config.maxRetransmitTime = 10;
  81. config.protocol = "y";
  82. rtc::CopyOnWriteBuffer packet;
  83. ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
  84. VerifyOpenMessageFormat(packet, label, config);
  85. std::string output_label;
  86. webrtc::DataChannelInit output_config;
  87. ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
  88. &output_config));
  89. EXPECT_EQ(label, output_label);
  90. EXPECT_EQ(config.protocol, output_config.protocol);
  91. EXPECT_EQ(config.ordered, output_config.ordered);
  92. EXPECT_EQ(*config.maxRetransmitTime, *output_config.maxRetransmitTime);
  93. EXPECT_FALSE(output_config.maxRetransmits);
  94. }
  95. TEST_F(SctpUtilsTest, WriteParseOpenMessageWithMaxRetransmits) {
  96. webrtc::DataChannelInit config;
  97. std::string label = "abc";
  98. config.maxRetransmits = 10;
  99. config.protocol = "y";
  100. rtc::CopyOnWriteBuffer packet;
  101. ASSERT_TRUE(webrtc::WriteDataChannelOpenMessage(label, config, &packet));
  102. VerifyOpenMessageFormat(packet, label, config);
  103. std::string output_label;
  104. webrtc::DataChannelInit output_config;
  105. ASSERT_TRUE(webrtc::ParseDataChannelOpenMessage(packet, &output_label,
  106. &output_config));
  107. EXPECT_EQ(label, output_label);
  108. EXPECT_EQ(config.protocol, output_config.protocol);
  109. EXPECT_EQ(config.ordered, output_config.ordered);
  110. EXPECT_EQ(config.maxRetransmits, output_config.maxRetransmits);
  111. EXPECT_FALSE(output_config.maxRetransmitTime);
  112. }
  113. TEST_F(SctpUtilsTest, WriteParseAckMessage) {
  114. rtc::CopyOnWriteBuffer packet;
  115. webrtc::WriteDataChannelOpenAckMessage(&packet);
  116. uint8_t message_type;
  117. rtc::ByteBufferReader buffer(packet.data<char>(), packet.size());
  118. ASSERT_TRUE(buffer.ReadUInt8(&message_type));
  119. EXPECT_EQ(0x02, message_type);
  120. EXPECT_TRUE(webrtc::ParseDataChannelOpenAckMessage(packet));
  121. }
  122. TEST_F(SctpUtilsTest, TestIsOpenMessage) {
  123. rtc::CopyOnWriteBuffer open(1);
  124. open[0] = 0x03;
  125. EXPECT_TRUE(webrtc::IsOpenMessage(open));
  126. rtc::CopyOnWriteBuffer openAck(1);
  127. openAck[0] = 0x02;
  128. EXPECT_FALSE(webrtc::IsOpenMessage(openAck));
  129. rtc::CopyOnWriteBuffer invalid(1);
  130. invalid[0] = 0x01;
  131. EXPECT_FALSE(webrtc::IsOpenMessage(invalid));
  132. rtc::CopyOnWriteBuffer empty;
  133. EXPECT_FALSE(webrtc::IsOpenMessage(empty));
  134. }