Victor Boivie | 471fc8c | 2021-03-30 20:05:21 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 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 "net/dcsctp/packet/sctp_packet.h" |
| 11 | |
| 12 | #include <cstdint> |
| 13 | #include <utility> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "api/array_view.h" |
| 17 | #include "net/dcsctp/common/internal_types.h" |
Victor Boivie | 78aa5cd | 2021-04-13 21:42:39 | [diff] [blame] | 18 | #include "net/dcsctp/common/math.h" |
Victor Boivie | 471fc8c | 2021-03-30 20:05:21 | [diff] [blame] | 19 | #include "net/dcsctp/packet/chunk/abort_chunk.h" |
| 20 | #include "net/dcsctp/packet/chunk/cookie_ack_chunk.h" |
| 21 | #include "net/dcsctp/packet/chunk/data_chunk.h" |
| 22 | #include "net/dcsctp/packet/chunk/init_chunk.h" |
| 23 | #include "net/dcsctp/packet/chunk/sack_chunk.h" |
| 24 | #include "net/dcsctp/packet/error_cause/error_cause.h" |
| 25 | #include "net/dcsctp/packet/error_cause/user_initiated_abort_cause.h" |
| 26 | #include "net/dcsctp/packet/parameter/parameter.h" |
| 27 | #include "net/dcsctp/packet/tlv_trait.h" |
Victor Boivie | 78aa5cd | 2021-04-13 21:42:39 | [diff] [blame] | 28 | #include "net/dcsctp/public/dcsctp_options.h" |
Victor Boivie | 471fc8c | 2021-03-30 20:05:21 | [diff] [blame] | 29 | #include "net/dcsctp/testing/testing_macros.h" |
| 30 | #include "rtc_base/gunit.h" |
| 31 | #include "test/gmock.h" |
| 32 | |
| 33 | namespace dcsctp { |
| 34 | namespace { |
Victor Boivie | 3c6b46f | 2023-04-20 15:44:59 | [diff] [blame] | 35 | using ::testing::ElementsAre; |
Victor Boivie | 471fc8c | 2021-03-30 20:05:21 | [diff] [blame] | 36 | using ::testing::SizeIs; |
| 37 | |
| 38 | constexpr VerificationTag kVerificationTag = VerificationTag(0x12345678); |
Victor Boivie | 3c6b46f | 2023-04-20 15:44:59 | [diff] [blame] | 39 | constexpr DcSctpOptions kVerifyChecksumOptions = |
| 40 | DcSctpOptions{.disable_checksum_verification = false, |
Victor Boivie | 2bfb5db | 2024-01-26 14:19:41 | [diff] [blame] | 41 | .zero_checksum_alternate_error_detection_method = |
| 42 | ZeroChecksumAlternateErrorDetectionMethod::None()}; |
Victor Boivie | 471fc8c | 2021-03-30 20:05:21 | [diff] [blame] | 43 | |
| 44 | TEST(SctpPacketTest, DeserializeSimplePacketFromCapture) { |
| 45 | /* |
| 46 | Stream Control Transmission Protocol, Src Port: 5000 (5000), Dst Port: 5000 |
| 47 | (5000) Source port: 5000 Destination port: 5000 Verification tag: 0x00000000 |
| 48 | [Association index: 1] |
| 49 | Checksum: 0xaa019d33 [unverified] |
| 50 | [Checksum Status: Unverified] |
| 51 | INIT chunk (Outbound streams: 1000, inbound streams: 1000) |
| 52 | Chunk type: INIT (1) |
| 53 | Chunk flags: 0x00 |
| 54 | Chunk length: 90 |
| 55 | Initiate tag: 0x0eddca08 |
| 56 | Advertised receiver window credit (a_rwnd): 131072 |
| 57 | Number of outbound streams: 1000 |
| 58 | Number of inbound streams: 1000 |
| 59 | Initial TSN: 1426601527 |
| 60 | ECN parameter |
| 61 | Parameter type: ECN (0x8000) |
| 62 | Parameter length: 4 |
| 63 | Forward TSN supported parameter |
| 64 | Parameter type: Forward TSN supported (0xc000) |
| 65 | Parameter length: 4 |
| 66 | Supported Extensions parameter (Supported types: FORWARD_TSN, AUTH, |
| 67 | ASCONF, ASCONF_ACK, RE_CONFIG) Parameter type: Supported Extensions |
| 68 | (0x8008) Parameter length: 9 Supported chunk type: FORWARD_TSN (192) Supported |
| 69 | chunk type: AUTH (15) Supported chunk type: ASCONF (193) Supported chunk type: |
| 70 | ASCONF_ACK (128) Supported chunk type: RE_CONFIG (130) Parameter padding: |
| 71 | 000000 Random parameter Parameter type: Random (0x8002) Parameter length: 36 |
| 72 | Random number: c5a86155090e6f420050634cc8d6b908dfd53e17c99cb143… |
| 73 | Requested HMAC Algorithm parameter (Supported HMACs: SHA-1) |
| 74 | Parameter type: Requested HMAC Algorithm (0x8004) |
| 75 | Parameter length: 6 |
| 76 | HMAC identifier: SHA-1 (1) |
| 77 | Parameter padding: 0000 |
| 78 | Authenticated Chunk list parameter (Chunk types to be authenticated: |
| 79 | ASCONF_ACK, ASCONF) Parameter type: Authenticated Chunk list |
| 80 | (0x8003) Parameter length: 6 Chunk type: ASCONF_ACK (128) Chunk type: ASCONF |
| 81 | (193) Chunk padding: 0000 |
| 82 | */ |
| 83 | |
| 84 | uint8_t data[] = { |
| 85 | 0x13, 0x88, 0x13, 0x88, 0x00, 0x00, 0x00, 0x00, 0xaa, 0x01, 0x9d, 0x33, |
| 86 | 0x01, 0x00, 0x00, 0x5a, 0x0e, 0xdd, 0xca, 0x08, 0x00, 0x02, 0x00, 0x00, |
| 87 | 0x03, 0xe8, 0x03, 0xe8, 0x55, 0x08, 0x36, 0x37, 0x80, 0x00, 0x00, 0x04, |
| 88 | 0xc0, 0x00, 0x00, 0x04, 0x80, 0x08, 0x00, 0x09, 0xc0, 0x0f, 0xc1, 0x80, |
| 89 | 0x82, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x24, 0xc5, 0xa8, 0x61, 0x55, |
| 90 | 0x09, 0x0e, 0x6f, 0x42, 0x00, 0x50, 0x63, 0x4c, 0xc8, 0xd6, 0xb9, 0x08, |
| 91 | 0xdf, 0xd5, 0x3e, 0x17, 0xc9, 0x9c, 0xb1, 0x43, 0x28, 0x4e, 0xaf, 0x64, |
| 92 | 0x68, 0x2a, 0xc2, 0x97, 0x80, 0x04, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, |
| 93 | 0x80, 0x03, 0x00, 0x06, 0x80, 0xc1, 0x00, 0x00}; |
| 94 | |
Victor Boivie | 3c6b46f | 2023-04-20 15:44:59 | [diff] [blame] | 95 | ASSERT_HAS_VALUE_AND_ASSIGN(SctpPacket packet, |
| 96 | SctpPacket::Parse(data, kVerifyChecksumOptions)); |
Victor Boivie | 471fc8c | 2021-03-30 20:05:21 | [diff] [blame] | 97 | EXPECT_EQ(packet.common_header().source_port, 5000); |
| 98 | EXPECT_EQ(packet.common_header().destination_port, 5000); |
| 99 | EXPECT_EQ(packet.common_header().verification_tag, VerificationTag(0)); |
| 100 | EXPECT_EQ(packet.common_header().checksum, 0xaa019d33); |
| 101 | |
| 102 | EXPECT_THAT(packet.descriptors(), SizeIs(1)); |
| 103 | EXPECT_EQ(packet.descriptors()[0].type, InitChunk::kType); |
| 104 | ASSERT_HAS_VALUE_AND_ASSIGN(InitChunk init, |
| 105 | InitChunk::Parse(packet.descriptors()[0].data)); |
| 106 | EXPECT_EQ(init.initial_tsn(), TSN(1426601527)); |
| 107 | } |
| 108 | |
| 109 | TEST(SctpPacketTest, DeserializePacketWithTwoChunks) { |
| 110 | /* |
| 111 | Stream Control Transmission Protocol, Src Port: 1234 (1234), |
| 112 | Dst Port: 4321 (4321) |
| 113 | Source port: 1234 |
| 114 | Destination port: 4321 |
| 115 | Verification tag: 0x697e3a4e |
| 116 | [Association index: 3] |
| 117 | Checksum: 0xc06e8b36 [unverified] |
| 118 | [Checksum Status: Unverified] |
| 119 | COOKIE_ACK chunk |
| 120 | Chunk type: COOKIE_ACK (11) |
| 121 | Chunk flags: 0x00 |
| 122 | Chunk length: 4 |
| 123 | SACK chunk (Cumulative TSN: 2930332242, a_rwnd: 131072, |
| 124 | gaps: 0, duplicate TSNs: 0) |
| 125 | Chunk type: SACK (3) |
| 126 | Chunk flags: 0x00 |
| 127 | Chunk length: 16 |
| 128 | Cumulative TSN ACK: 2930332242 |
| 129 | Advertised receiver window credit (a_rwnd): 131072 |
| 130 | Number of gap acknowledgement blocks: 0 |
| 131 | Number of duplicated TSNs: 0 |
| 132 | */ |
| 133 | |
| 134 | uint8_t data[] = {0x04, 0xd2, 0x10, 0xe1, 0x69, 0x7e, 0x3a, 0x4e, |
| 135 | 0xc0, 0x6e, 0x8b, 0x36, 0x0b, 0x00, 0x00, 0x04, |
| 136 | 0x03, 0x00, 0x00, 0x10, 0xae, 0xa9, 0x52, 0x52, |
| 137 | 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 138 | |
Victor Boivie | 3c6b46f | 2023-04-20 15:44:59 | [diff] [blame] | 139 | ASSERT_HAS_VALUE_AND_ASSIGN(SctpPacket packet, |
| 140 | SctpPacket::Parse(data, kVerifyChecksumOptions)); |
Victor Boivie | 471fc8c | 2021-03-30 20:05:21 | [diff] [blame] | 141 | EXPECT_EQ(packet.common_header().source_port, 1234); |
| 142 | EXPECT_EQ(packet.common_header().destination_port, 4321); |
| 143 | EXPECT_EQ(packet.common_header().verification_tag, |
| 144 | VerificationTag(0x697e3a4eu)); |
| 145 | EXPECT_EQ(packet.common_header().checksum, 0xc06e8b36u); |
| 146 | |
| 147 | EXPECT_THAT(packet.descriptors(), SizeIs(2)); |
| 148 | EXPECT_EQ(packet.descriptors()[0].type, CookieAckChunk::kType); |
| 149 | EXPECT_EQ(packet.descriptors()[1].type, SackChunk::kType); |
| 150 | ASSERT_HAS_VALUE_AND_ASSIGN( |
| 151 | CookieAckChunk cookie_ack, |
| 152 | CookieAckChunk::Parse(packet.descriptors()[0].data)); |
| 153 | ASSERT_HAS_VALUE_AND_ASSIGN(SackChunk sack, |
| 154 | SackChunk::Parse(packet.descriptors()[1].data)); |
| 155 | } |
| 156 | |
| 157 | TEST(SctpPacketTest, DeserializePacketWithWrongChecksum) { |
| 158 | /* |
| 159 | Stream Control Transmission Protocol, Src Port: 5000 (5000), |
| 160 | Dst Port: 5000 (5000) |
| 161 | Source port: 5000 |
| 162 | Destination port: 5000 |
| 163 | Verification tag: 0x0eddca08 |
| 164 | [Association index: 1] |
| 165 | Checksum: 0x2a81f531 [unverified] |
| 166 | [Checksum Status: Unverified] |
| 167 | SACK chunk (Cumulative TSN: 1426601536, a_rwnd: 131072, |
| 168 | gaps: 0, duplicate TSNs: 0) |
| 169 | Chunk type: SACK (3) |
| 170 | Chunk flags: 0x00 |
| 171 | Chunk length: 16 |
| 172 | Cumulative TSN ACK: 1426601536 |
| 173 | Advertised receiver window credit (a_rwnd): 131072 |
| 174 | Number of gap acknowledgement blocks: 0 |
| 175 | Number of duplicated TSNs: 0 |
| 176 | */ |
| 177 | |
| 178 | uint8_t data[] = {0x13, 0x88, 0x13, 0x88, 0x0e, 0xdd, 0xca, 0x08, 0x2a, 0x81, |
| 179 | 0xf5, 0x31, 0x03, 0x00, 0x00, 0x10, 0x55, 0x08, 0x36, 0x40, |
| 180 | 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 181 | |
Victor Boivie | 3c6b46f | 2023-04-20 15:44:59 | [diff] [blame] | 182 | EXPECT_FALSE(SctpPacket::Parse(data, kVerifyChecksumOptions).has_value()); |
Victor Boivie | 471fc8c | 2021-03-30 20:05:21 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | TEST(SctpPacketTest, DeserializePacketDontValidateChecksum) { |
| 186 | /* |
| 187 | Stream Control Transmission Protocol, Src Port: 5000 (5000), |
| 188 | Dst Port: 5000 (5000) |
| 189 | Source port: 5000 |
| 190 | Destination port: 5000 |
| 191 | Verification tag: 0x0eddca08 |
| 192 | [Association index: 1] |
| 193 | Checksum: 0x2a81f531 [unverified] |
| 194 | [Checksum Status: Unverified] |
| 195 | SACK chunk (Cumulative TSN: 1426601536, a_rwnd: 131072, |
| 196 | gaps: 0, duplicate TSNs: 0) |
| 197 | Chunk type: SACK (3) |
| 198 | Chunk flags: 0x00 |
| 199 | Chunk length: 16 |
| 200 | Cumulative TSN ACK: 1426601536 |
| 201 | Advertised receiver window credit (a_rwnd): 131072 |
| 202 | Number of gap acknowledgement blocks: 0 |
| 203 | Number of duplicated TSNs: 0 |
| 204 | */ |
| 205 | |
| 206 | uint8_t data[] = {0x13, 0x88, 0x13, 0x88, 0x0e, 0xdd, 0xca, 0x08, 0x2a, 0x81, |
| 207 | 0xf5, 0x31, 0x03, 0x00, 0x00, 0x10, 0x55, 0x08, 0x36, 0x40, |
| 208 | 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 209 | |
| 210 | ASSERT_HAS_VALUE_AND_ASSIGN( |
| 211 | SctpPacket packet, |
Victor Boivie | 2bfb5db | 2024-01-26 14:19:41 | [diff] [blame] | 212 | SctpPacket::Parse( |
| 213 | data, {.disable_checksum_verification = true, |
| 214 | .zero_checksum_alternate_error_detection_method = |
| 215 | ZeroChecksumAlternateErrorDetectionMethod::None()})); |
Victor Boivie | 471fc8c | 2021-03-30 20:05:21 | [diff] [blame] | 216 | EXPECT_EQ(packet.common_header().source_port, 5000); |
| 217 | EXPECT_EQ(packet.common_header().destination_port, 5000); |
| 218 | EXPECT_EQ(packet.common_header().verification_tag, |
| 219 | VerificationTag(0x0eddca08u)); |
| 220 | EXPECT_EQ(packet.common_header().checksum, 0x2a81f531u); |
| 221 | } |
| 222 | |
| 223 | TEST(SctpPacketTest, SerializeAndDeserializeSingleChunk) { |
| 224 | SctpPacket::Builder b(kVerificationTag, {}); |
| 225 | InitChunk init(/*initiate_tag=*/VerificationTag(123), /*a_rwnd=*/456, |
| 226 | /*nbr_outbound_streams=*/65535, |
| 227 | /*nbr_inbound_streams=*/65534, /*initial_tsn=*/TSN(789), |
| 228 | /*parameters=*/Parameters()); |
| 229 | |
| 230 | b.Add(init); |
| 231 | std::vector<uint8_t> serialized = b.Build(); |
| 232 | |
Victor Boivie | 3c6b46f | 2023-04-20 15:44:59 | [diff] [blame] | 233 | ASSERT_HAS_VALUE_AND_ASSIGN( |
| 234 | SctpPacket packet, SctpPacket::Parse(serialized, kVerifyChecksumOptions)); |
Victor Boivie | 471fc8c | 2021-03-30 20:05:21 | [diff] [blame] | 235 | |
| 236 | EXPECT_EQ(packet.common_header().verification_tag, kVerificationTag); |
| 237 | |
| 238 | ASSERT_THAT(packet.descriptors(), SizeIs(1)); |
| 239 | EXPECT_EQ(packet.descriptors()[0].type, InitChunk::kType); |
| 240 | |
| 241 | ASSERT_HAS_VALUE_AND_ASSIGN(InitChunk deserialized, |
| 242 | InitChunk::Parse(packet.descriptors()[0].data)); |
| 243 | EXPECT_EQ(deserialized.initiate_tag(), VerificationTag(123)); |
| 244 | EXPECT_EQ(deserialized.a_rwnd(), 456u); |
| 245 | EXPECT_EQ(deserialized.nbr_outbound_streams(), 65535u); |
| 246 | EXPECT_EQ(deserialized.nbr_inbound_streams(), 65534u); |
| 247 | EXPECT_EQ(deserialized.initial_tsn(), TSN(789)); |
| 248 | } |
| 249 | |
| 250 | TEST(SctpPacketTest, SerializeAndDeserializeThreeChunks) { |
| 251 | SctpPacket::Builder b(kVerificationTag, {}); |
| 252 | b.Add(SackChunk(/*cumulative_tsn_ack=*/TSN(999), /*a_rwnd=*/456, |
| 253 | {SackChunk::GapAckBlock(2, 3)}, |
| 254 | /*duplicate_tsns=*/{TSN(1), TSN(2), TSN(3)})); |
| 255 | b.Add(DataChunk(TSN(123), StreamID(456), SSN(789), PPID(9090), |
Victor Boivie | 4b7024b | 2021-12-01 18:57:22 | [diff] [blame] | 256 | /*payload=*/{1, 2, 3, 4, 5}, |
Victor Boivie | 471fc8c | 2021-03-30 20:05:21 | [diff] [blame] | 257 | /*options=*/{})); |
| 258 | b.Add(DataChunk(TSN(124), StreamID(654), SSN(987), PPID(909), |
Victor Boivie | 4b7024b | 2021-12-01 18:57:22 | [diff] [blame] | 259 | /*payload=*/{5, 4, 3, 3, 1}, |
Victor Boivie | 471fc8c | 2021-03-30 20:05:21 | [diff] [blame] | 260 | /*options=*/{})); |
| 261 | |
| 262 | std::vector<uint8_t> serialized = b.Build(); |
| 263 | |
Victor Boivie | 3c6b46f | 2023-04-20 15:44:59 | [diff] [blame] | 264 | ASSERT_HAS_VALUE_AND_ASSIGN( |
| 265 | SctpPacket packet, SctpPacket::Parse(serialized, kVerifyChecksumOptions)); |
Victor Boivie | 471fc8c | 2021-03-30 20:05:21 | [diff] [blame] | 266 | |
| 267 | EXPECT_EQ(packet.common_header().verification_tag, kVerificationTag); |
| 268 | |
| 269 | ASSERT_THAT(packet.descriptors(), SizeIs(3)); |
| 270 | EXPECT_EQ(packet.descriptors()[0].type, SackChunk::kType); |
| 271 | EXPECT_EQ(packet.descriptors()[1].type, DataChunk::kType); |
| 272 | EXPECT_EQ(packet.descriptors()[2].type, DataChunk::kType); |
| 273 | |
| 274 | ASSERT_HAS_VALUE_AND_ASSIGN(SackChunk sack, |
| 275 | SackChunk::Parse(packet.descriptors()[0].data)); |
| 276 | EXPECT_EQ(sack.cumulative_tsn_ack(), TSN(999)); |
| 277 | EXPECT_EQ(sack.a_rwnd(), 456u); |
| 278 | |
| 279 | ASSERT_HAS_VALUE_AND_ASSIGN(DataChunk data1, |
| 280 | DataChunk::Parse(packet.descriptors()[1].data)); |
| 281 | EXPECT_EQ(data1.tsn(), TSN(123)); |
| 282 | |
| 283 | ASSERT_HAS_VALUE_AND_ASSIGN(DataChunk data2, |
| 284 | DataChunk::Parse(packet.descriptors()[2].data)); |
| 285 | EXPECT_EQ(data2.tsn(), TSN(124)); |
| 286 | } |
| 287 | |
| 288 | TEST(SctpPacketTest, ParseAbortWithEmptyCause) { |
| 289 | SctpPacket::Builder b(kVerificationTag, {}); |
| 290 | b.Add(AbortChunk( |
| 291 | /*filled_in_verification_tag=*/true, |
| 292 | Parameters::Builder().Add(UserInitiatedAbortCause("")).Build())); |
| 293 | |
Victor Boivie | 3c6b46f | 2023-04-20 15:44:59 | [diff] [blame] | 294 | ASSERT_HAS_VALUE_AND_ASSIGN( |
| 295 | SctpPacket packet, SctpPacket::Parse(b.Build(), kVerifyChecksumOptions)); |
Victor Boivie | 471fc8c | 2021-03-30 20:05:21 | [diff] [blame] | 296 | |
| 297 | EXPECT_EQ(packet.common_header().verification_tag, kVerificationTag); |
| 298 | |
| 299 | ASSERT_THAT(packet.descriptors(), SizeIs(1)); |
| 300 | EXPECT_EQ(packet.descriptors()[0].type, AbortChunk::kType); |
| 301 | |
| 302 | ASSERT_HAS_VALUE_AND_ASSIGN(AbortChunk abort, |
| 303 | AbortChunk::Parse(packet.descriptors()[0].data)); |
| 304 | ASSERT_HAS_VALUE_AND_ASSIGN( |
| 305 | UserInitiatedAbortCause cause, |
| 306 | abort.error_causes().get<UserInitiatedAbortCause>()); |
| 307 | EXPECT_EQ(cause.upper_layer_abort_reason(), ""); |
| 308 | } |
| 309 | |
Victor Boivie | ca7412d | 2021-04-08 14:25:42 | [diff] [blame] | 310 | TEST(SctpPacketTest, DetectPacketWithZeroSizeChunk) { |
| 311 | uint8_t data[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0x0a, 0x0a, 0x5c, |
| 312 | 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x00, 0x00, 0x00}; |
| 313 | |
Victor Boivie | 3c6b46f | 2023-04-20 15:44:59 | [diff] [blame] | 314 | EXPECT_FALSE(SctpPacket::Parse(data, kVerifyChecksumOptions).has_value()); |
Victor Boivie | ca7412d | 2021-04-08 14:25:42 | [diff] [blame] | 315 | } |
Victor Boivie | 78aa5cd | 2021-04-13 21:42:39 | [diff] [blame] | 316 | |
| 317 | TEST(SctpPacketTest, ReturnsCorrectSpaceAvailableToStayWithinMTU) { |
| 318 | DcSctpOptions options; |
| 319 | options.mtu = 1191; |
| 320 | |
| 321 | SctpPacket::Builder builder(VerificationTag(123), options); |
| 322 | |
| 323 | // Chunks will be padded to an even 4 bytes, so the maximum packet size should |
| 324 | // be rounded down. |
| 325 | const size_t kMaxPacketSize = RoundDownTo4(options.mtu); |
| 326 | EXPECT_EQ(kMaxPacketSize, 1188u); |
| 327 | |
| 328 | const size_t kSctpHeaderSize = 12; |
| 329 | EXPECT_EQ(builder.bytes_remaining(), kMaxPacketSize - kSctpHeaderSize); |
| 330 | EXPECT_EQ(builder.bytes_remaining(), 1176u); |
| 331 | |
| 332 | // Add a smaller packet first. |
| 333 | DataChunk::Options data_options; |
| 334 | |
Victor Boivie | 4b7024b | 2021-12-01 18:57:22 | [diff] [blame] | 335 | std::vector<uint8_t> payload1(183); |
Victor Boivie | 78aa5cd | 2021-04-13 21:42:39 | [diff] [blame] | 336 | builder.Add( |
| 337 | DataChunk(TSN(1), StreamID(1), SSN(0), PPID(53), payload1, data_options)); |
| 338 | |
| 339 | size_t chunk1_size = RoundUpTo4(DataChunk::kHeaderSize + payload1.size()); |
| 340 | EXPECT_EQ(builder.bytes_remaining(), |
| 341 | kMaxPacketSize - kSctpHeaderSize - chunk1_size); |
| 342 | EXPECT_EQ(builder.bytes_remaining(), 976u); // Hand-calculated. |
| 343 | |
Victor Boivie | 4b7024b | 2021-12-01 18:57:22 | [diff] [blame] | 344 | std::vector<uint8_t> payload2(957); |
Victor Boivie | 78aa5cd | 2021-04-13 21:42:39 | [diff] [blame] | 345 | builder.Add( |
| 346 | DataChunk(TSN(1), StreamID(1), SSN(0), PPID(53), payload2, data_options)); |
| 347 | |
| 348 | size_t chunk2_size = RoundUpTo4(DataChunk::kHeaderSize + payload2.size()); |
| 349 | EXPECT_EQ(builder.bytes_remaining(), |
| 350 | kMaxPacketSize - kSctpHeaderSize - chunk1_size - chunk2_size); |
| 351 | EXPECT_EQ(builder.bytes_remaining(), 0u); // Hand-calculated. |
| 352 | } |
| 353 | |
Victor Boivie | 3c6b46f | 2023-04-20 15:44:59 | [diff] [blame] | 354 | TEST(SctpPacketTest, AcceptsZeroSetZeroChecksum) { |
| 355 | /* |
| 356 | Stream Control Transmission Protocol, Src Port: 5000 (5000), |
| 357 | Dst Port: 5000 (5000) |
| 358 | Source port: 5000 |
| 359 | Destination port: 5000 |
| 360 | Verification tag: 0x0eddca08 |
| 361 | [Association index: 1] |
| 362 | Checksum: 0x00000000 [unverified] |
| 363 | [Checksum Status: Unverified] |
| 364 | SACK chunk (Cumulative TSN: 1426601536, a_rwnd: 131072, |
| 365 | gaps: 0, duplicate TSNs: 0) |
| 366 | Chunk type: SACK (3) |
| 367 | Chunk flags: 0x00 |
| 368 | Chunk length: 16 |
| 369 | Cumulative TSN ACK: 1426601536 |
| 370 | Advertised receiver window credit (a_rwnd): 131072 |
| 371 | Number of gap acknowledgement blocks: 0 |
| 372 | Number of duplicated TSNs: 0 |
| 373 | */ |
| 374 | |
| 375 | uint8_t data[] = {0x13, 0x88, 0x13, 0x88, 0x0e, 0xdd, 0xca, 0x08, 0x00, 0x00, |
| 376 | 0x00, 0x00, 0x03, 0x00, 0x00, 0x10, 0x55, 0x08, 0x36, 0x40, |
| 377 | 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 378 | |
| 379 | ASSERT_HAS_VALUE_AND_ASSIGN( |
| 380 | SctpPacket packet, |
Victor Boivie | 2bfb5db | 2024-01-26 14:19:41 | [diff] [blame] | 381 | SctpPacket::Parse( |
| 382 | data, |
| 383 | {.disable_checksum_verification = false, |
| 384 | .zero_checksum_alternate_error_detection_method = |
| 385 | ZeroChecksumAlternateErrorDetectionMethod::LowerLayerDtls()})); |
Victor Boivie | 3c6b46f | 2023-04-20 15:44:59 | [diff] [blame] | 386 | EXPECT_EQ(packet.common_header().source_port, 5000); |
| 387 | EXPECT_EQ(packet.common_header().destination_port, 5000); |
| 388 | EXPECT_EQ(packet.common_header().verification_tag, |
| 389 | VerificationTag(0x0eddca08u)); |
| 390 | EXPECT_EQ(packet.common_header().checksum, 0x00000000u); |
| 391 | } |
| 392 | |
| 393 | TEST(SctpPacketTest, RejectsNonZeroIncorrectChecksumWhenZeroChecksumIsActive) { |
| 394 | /* |
| 395 | Stream Control Transmission Protocol, Src Port: 5000 (5000), |
| 396 | Dst Port: 5000 (5000) |
| 397 | Source port: 5000 |
| 398 | Destination port: 5000 |
| 399 | Verification tag: 0x0eddca08 |
| 400 | [Association index: 1] |
| 401 | Checksum: 0x00000001 [unverified] |
| 402 | [Checksum Status: Unverified] |
| 403 | SACK chunk (Cumulative TSN: 1426601536, a_rwnd: 131072, |
| 404 | gaps: 0, duplicate TSNs: 0) |
| 405 | Chunk type: SACK (3) |
| 406 | Chunk flags: 0x00 |
| 407 | Chunk length: 16 |
| 408 | Cumulative TSN ACK: 1426601536 |
| 409 | Advertised receiver window credit (a_rwnd): 131072 |
| 410 | Number of gap acknowledgement blocks: 0 |
| 411 | Number of duplicated TSNs: 0 |
| 412 | */ |
| 413 | |
| 414 | uint8_t data[] = {0x13, 0x88, 0x13, 0x88, 0x0e, 0xdd, 0xca, 0x08, 0x01, 0x00, |
| 415 | 0x00, 0x00, 0x03, 0x00, 0x00, 0x10, 0x55, 0x08, 0x36, 0x40, |
| 416 | 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 417 | |
Victor Boivie | 2bfb5db | 2024-01-26 14:19:41 | [diff] [blame] | 418 | EXPECT_FALSE( |
| 419 | SctpPacket::Parse( |
| 420 | data, |
| 421 | {.disable_checksum_verification = false, |
| 422 | .zero_checksum_alternate_error_detection_method = |
| 423 | ZeroChecksumAlternateErrorDetectionMethod::LowerLayerDtls()}) |
| 424 | .has_value()); |
Victor Boivie | 3c6b46f | 2023-04-20 15:44:59 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | TEST(SctpPacketTest, WritePacketWithCalculatedChecksum) { |
| 428 | SctpPacket::Builder b(kVerificationTag, {}); |
| 429 | b.Add(SackChunk(/*cumulative_tsn_ack=*/TSN(999), /*a_rwnd=*/456, |
| 430 | /*gap_ack_blocks=*/{}, |
| 431 | /*duplicate_tsns=*/{})); |
| 432 | EXPECT_THAT(b.Build(), |
| 433 | ElementsAre(0x13, 0x88, 0x13, 0x88, 0x12, 0x34, 0x56, 0x78, // |
| 434 | 0x07, 0xe8, 0x38, 0x77, // checksum |
| 435 | 0x03, 0x00, 0x00, 0x10, 0x00, 0x00, 0x03, 0xe7, 0x00, |
| 436 | 0x00, 0x01, 0xc8, 0x00, 0x00, 0x00, 0x00)); |
| 437 | } |
| 438 | |
| 439 | TEST(SctpPacketTest, WritePacketWithZeroChecksum) { |
| 440 | SctpPacket::Builder b(kVerificationTag, {}); |
| 441 | b.Add(SackChunk(/*cumulative_tsn_ack=*/TSN(999), /*a_rwnd=*/456, |
| 442 | /*gap_ack_blocks=*/{}, |
| 443 | /*duplicate_tsns=*/{})); |
| 444 | EXPECT_THAT(b.Build(/*write_checksum=*/false), |
| 445 | ElementsAre(0x13, 0x88, 0x13, 0x88, 0x12, 0x34, 0x56, 0x78, // |
| 446 | 0x00, 0x00, 0x00, 0x00, // checksum |
| 447 | 0x03, 0x00, 0x00, 0x10, 0x00, 0x00, 0x03, 0xe7, 0x00, |
| 448 | 0x00, 0x01, 0xc8, 0x00, 0x00, 0x00, 0x00)); |
| 449 | } |
| 450 | |
Victor Boivie | 471fc8c | 2021-03-30 20:05:21 | [diff] [blame] | 451 | } // namespace |
| 452 | } // namespace dcsctp |