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_ack_chunk.h"
11
12 #include <stdint.h>
13
14 #include <utility>
15 #include <vector>
16
17 #include "api/array_view.h"
18 #include "net/dcsctp/packet/parameter/forward_tsn_supported_parameter.h"
19 #include "net/dcsctp/packet/parameter/parameter.h"
20 #include "net/dcsctp/packet/parameter/state_cookie_parameter.h"
21 #include "net/dcsctp/packet/parameter/supported_extensions_parameter.h"
22 #include "net/dcsctp/testing/testing_macros.h"
23 #include "rtc_base/gunit.h"
24 #include "test/gmock.h"
25
26 namespace dcsctp {
27 namespace {
28 using ::testing::ElementsAre;
29
TEST(InitAckChunkTest,FromCapture)30 TEST(InitAckChunkTest, FromCapture) {
31 /*
32 INIT_ACK chunk (Outbound streams: 1000, inbound streams: 2048)
33 Chunk type: INIT_ACK (2)
34 Chunk flags: 0x00
35 Chunk length: 292
36 Initiate tag: 0x579c2f98
37 Advertised receiver window credit (a_rwnd): 131072
38 Number of outbound streams: 1000
39 Number of inbound streams: 2048
40 Initial TSN: 1670811335
41 Forward TSN supported parameter
42 Parameter type: Forward TSN supported (0xc000)
43 Parameter length: 4
44 Supported Extensions parameter (Supported types: FORWARD_TSN, RE_CONFIG)
45 Parameter type: Supported Extensions (0x8008)
46 Parameter length: 6
47 Supported chunk type: FORWARD_TSN (192)
48 Supported chunk type: RE_CONFIG (130)
49 Parameter padding: 0000
50 State cookie parameter (Cookie length: 256 bytes)
51 Parameter type: State cookie (0x0007)
52 Parameter length: 260
53 State cookie: 4b414d452d42534420312e310000000096b8386000000000…
54 */
55
56 uint8_t data[] = {
57 0x02, 0x00, 0x01, 0x24, 0x57, 0x9c, 0x2f, 0x98, 0x00, 0x02, 0x00, 0x00,
58 0x03, 0xe8, 0x08, 0x00, 0x63, 0x96, 0x8e, 0xc7, 0xc0, 0x00, 0x00, 0x04,
59 0x80, 0x08, 0x00, 0x06, 0xc0, 0x82, 0x00, 0x00, 0x00, 0x07, 0x01, 0x04,
60 0x4b, 0x41, 0x4d, 0x45, 0x2d, 0x42, 0x53, 0x44, 0x20, 0x31, 0x2e, 0x31,
61 0x00, 0x00, 0x00, 0x00, 0x96, 0xb8, 0x38, 0x60, 0x00, 0x00, 0x00, 0x00,
62 0x52, 0x5a, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xea, 0x00, 0x00,
63 0xb5, 0xaa, 0x19, 0xea, 0x31, 0xef, 0xa4, 0x2b, 0x90, 0x16, 0x7a, 0xde,
64 0x57, 0x9c, 0x2f, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68 0x13, 0x88, 0x13, 0x88, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x00,
69 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x5a, 0xde, 0x7a, 0x16, 0x90,
70 0x00, 0x02, 0x00, 0x00, 0x03, 0xe8, 0x03, 0xe8, 0x25, 0x0d, 0x37, 0xe8,
71 0x80, 0x00, 0x00, 0x04, 0xc0, 0x00, 0x00, 0x04, 0x80, 0x08, 0x00, 0x09,
72 0xc0, 0x0f, 0xc1, 0x80, 0x82, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x24,
73 0xab, 0x31, 0x44, 0x62, 0x12, 0x1a, 0x15, 0x13, 0xfd, 0x5a, 0x5f, 0x69,
74 0xef, 0xaa, 0x06, 0xe9, 0xab, 0xd7, 0x48, 0xcc, 0x3b, 0xd1, 0x4b, 0x60,
75 0xed, 0x7f, 0xa6, 0x44, 0xce, 0x4d, 0xd2, 0xad, 0x80, 0x04, 0x00, 0x06,
76 0x00, 0x01, 0x00, 0x00, 0x80, 0x03, 0x00, 0x06, 0x80, 0xc1, 0x00, 0x00,
77 0x02, 0x00, 0x01, 0x24, 0x57, 0x9c, 0x2f, 0x98, 0x00, 0x02, 0x00, 0x00,
78 0x03, 0xe8, 0x08, 0x00, 0x63, 0x96, 0x8e, 0xc7, 0xc0, 0x00, 0x00, 0x04,
79 0x80, 0x08, 0x00, 0x06, 0xc0, 0x82, 0x00, 0x00, 0x51, 0x95, 0x01, 0x88,
80 0x0d, 0x80, 0x7b, 0x19, 0xe7, 0xf9, 0xc6, 0x18, 0x5c, 0x4a, 0xbf, 0x39,
81 0x32, 0xe5, 0x63, 0x8e};
82
83 ASSERT_HAS_VALUE_AND_ASSIGN(InitAckChunk chunk, InitAckChunk::Parse(data));
84
85 EXPECT_EQ(chunk.initiate_tag(), VerificationTag(0x579c2f98u));
86 EXPECT_EQ(chunk.a_rwnd(), 131072u);
87 EXPECT_EQ(chunk.nbr_outbound_streams(), 1000u);
88 EXPECT_EQ(chunk.nbr_inbound_streams(), 2048u);
89 EXPECT_EQ(chunk.initial_tsn(), TSN(1670811335u));
90 EXPECT_TRUE(
91 chunk.parameters().get<ForwardTsnSupportedParameter>().has_value());
92 EXPECT_TRUE(
93 chunk.parameters().get<SupportedExtensionsParameter>().has_value());
94 EXPECT_TRUE(chunk.parameters().get<StateCookieParameter>().has_value());
95 }
96
TEST(InitAckChunkTest,SerializeAndDeserialize)97 TEST(InitAckChunkTest, SerializeAndDeserialize) {
98 uint8_t state_cookie[] = {1, 2, 3, 4, 5};
99 Parameters parameters =
100 Parameters::Builder().Add(StateCookieParameter(state_cookie)).Build();
101 InitAckChunk chunk(VerificationTag(123), /*a_rwnd=*/456,
102 /*nbr_outbound_streams=*/65535,
103 /*nbr_inbound_streams=*/65534, /*initial_tsn=*/TSN(789),
104 /*parameters=*/std::move(parameters));
105
106 std::vector<uint8_t> serialized;
107 chunk.SerializeTo(serialized);
108
109 ASSERT_HAS_VALUE_AND_ASSIGN(InitAckChunk deserialized,
110 InitAckChunk::Parse(serialized));
111
112 EXPECT_EQ(chunk.initiate_tag(), VerificationTag(123u));
113 EXPECT_EQ(chunk.a_rwnd(), 456u);
114 EXPECT_EQ(chunk.nbr_outbound_streams(), 65535u);
115 EXPECT_EQ(chunk.nbr_inbound_streams(), 65534u);
116 EXPECT_EQ(chunk.initial_tsn(), TSN(789u));
117
118 ASSERT_HAS_VALUE_AND_ASSIGN(
119 StateCookieParameter cookie,
120 deserialized.parameters().get<StateCookieParameter>());
121 EXPECT_THAT(cookie.data(), ElementsAre(1, 2, 3, 4, 5));
122 EXPECT_EQ(deserialized.ToString(),
123 "INIT_ACK, initiate_tag=0x7b, initial_tsn=789");
124 }
125
126 } // namespace
127 } // namespace dcsctp
128