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/chunk/init_chunk.h"
11
12 #include <stdint.h>
13
14 #include <type_traits>
15 #include <vector>
16
17 #include "net/dcsctp/packet/parameter/forward_tsn_supported_parameter.h"
18 #include "net/dcsctp/packet/parameter/parameter.h"
19 #include "net/dcsctp/packet/parameter/supported_extensions_parameter.h"
20 #include "net/dcsctp/testing/testing_macros.h"
21 #include "rtc_base/gunit.h"
22 #include "test/gmock.h"
23
24 namespace dcsctp {
25 namespace {
26
TEST(InitChunkTest,FromCapture)27 TEST(InitChunkTest, FromCapture) {
28 /*
29 INIT chunk (Outbound streams: 1000, inbound streams: 1000)
30 Chunk type: INIT (1)
31 Chunk flags: 0x00
32 Chunk length: 90
33 Initiate tag: 0xde7a1690
34 Advertised receiver window credit (a_rwnd): 131072
35 Number of outbound streams: 1000
36 Number of inbound streams: 1000
37 Initial TSN: 621623272
38 ECN parameter
39 Parameter type: ECN (0x8000)
40 Parameter length: 4
41 Forward TSN supported parameter
42 Parameter type: Forward TSN supported (0xc000)
43 Parameter length: 4
44 Supported Extensions parameter (Supported types: FORWARD_TSN, AUTH,
45 ASCONF, ASCONF_ACK, RE_CONFIG) Parameter type: Supported Extensions (0x8008)
46 Parameter length: 9
47 Supported chunk type: FORWARD_TSN (192)
48 Supported chunk type: AUTH (15)
49 Supported chunk type: ASCONF (193)
50 Supported chunk type: ASCONF_ACK (128)
51 Supported chunk type: RE_CONFIG (130)
52 Parameter padding: 000000
53 Random parameter
54 Parameter type: Random (0x8002)
55 Parameter length: 36
56 Random number: ab314462121a1513fd5a5f69efaa06e9abd748cc3bd14b60…
57 Requested HMAC Algorithm parameter (Supported HMACs: SHA-1)
58 Parameter type: Requested HMAC Algorithm (0x8004)
59 Parameter length: 6
60 HMAC identifier: SHA-1 (1)
61 Parameter padding: 0000
62 Authenticated Chunk list parameter (Chunk types to be authenticated:
63 ASCONF_ACK, ASCONF) Parameter type: Authenticated Chunk list (0x8003)
64 Parameter length: 6
65 Chunk type: ASCONF_ACK (128)
66 Chunk type: ASCONF (193)
67 */
68
69 uint8_t data[] = {
70 0x01, 0x00, 0x00, 0x5a, 0xde, 0x7a, 0x16, 0x90, 0x00, 0x02, 0x00, 0x00,
71 0x03, 0xe8, 0x03, 0xe8, 0x25, 0x0d, 0x37, 0xe8, 0x80, 0x00, 0x00, 0x04,
72 0xc0, 0x00, 0x00, 0x04, 0x80, 0x08, 0x00, 0x09, 0xc0, 0x0f, 0xc1, 0x80,
73 0x82, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x24, 0xab, 0x31, 0x44, 0x62,
74 0x12, 0x1a, 0x15, 0x13, 0xfd, 0x5a, 0x5f, 0x69, 0xef, 0xaa, 0x06, 0xe9,
75 0xab, 0xd7, 0x48, 0xcc, 0x3b, 0xd1, 0x4b, 0x60, 0xed, 0x7f, 0xa6, 0x44,
76 0xce, 0x4d, 0xd2, 0xad, 0x80, 0x04, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00,
77 0x80, 0x03, 0x00, 0x06, 0x80, 0xc1, 0x00, 0x00};
78
79 ASSERT_HAS_VALUE_AND_ASSIGN(InitChunk chunk, InitChunk::Parse(data));
80
81 EXPECT_EQ(chunk.initiate_tag(), VerificationTag(0xde7a1690));
82 EXPECT_EQ(chunk.a_rwnd(), 131072u);
83 EXPECT_EQ(chunk.nbr_outbound_streams(), 1000u);
84 EXPECT_EQ(chunk.nbr_inbound_streams(), 1000u);
85 EXPECT_EQ(chunk.initial_tsn(), TSN(621623272u));
86 EXPECT_TRUE(
87 chunk.parameters().get<ForwardTsnSupportedParameter>().has_value());
88 EXPECT_TRUE(
89 chunk.parameters().get<SupportedExtensionsParameter>().has_value());
90 }
91
TEST(InitChunkTest,SerializeAndDeserialize)92 TEST(InitChunkTest, SerializeAndDeserialize) {
93 InitChunk chunk(VerificationTag(123), /*a_rwnd=*/456,
94 /*nbr_outbound_streams=*/65535,
95 /*nbr_inbound_streams=*/65534, /*initial_tsn=*/TSN(789),
96 /*parameters=*/Parameters::Builder().Build());
97
98 std::vector<uint8_t> serialized;
99 chunk.SerializeTo(serialized);
100
101 ASSERT_HAS_VALUE_AND_ASSIGN(InitChunk deserialized,
102 InitChunk::Parse(serialized));
103
104 EXPECT_EQ(deserialized.initiate_tag(), VerificationTag(123u));
105 EXPECT_EQ(deserialized.a_rwnd(), 456u);
106 EXPECT_EQ(deserialized.nbr_outbound_streams(), 65535u);
107 EXPECT_EQ(deserialized.nbr_inbound_streams(), 65534u);
108 EXPECT_EQ(deserialized.initial_tsn(), TSN(789u));
109 EXPECT_EQ(deserialized.ToString(),
110 "INIT, initiate_tag=0x7b, initial_tsn=789");
111 }
112 } // namespace
113 } // namespace dcsctp
114