xref: /aosp_15_r20/external/webrtc/net/dcsctp/packet/chunk/sack_chunk_test.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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/sack_chunk.h"
11 
12 #include <cstdint>
13 #include <type_traits>
14 #include <vector>
15 
16 #include "api/array_view.h"
17 #include "net/dcsctp/testing/testing_macros.h"
18 #include "rtc_base/gunit.h"
19 #include "test/gmock.h"
20 
21 namespace dcsctp {
22 namespace {
23 using ::testing::ElementsAre;
24 
TEST(SackChunkTest,FromCapture)25 TEST(SackChunkTest, FromCapture) {
26   /*
27   SACK chunk (Cumulative TSN: 916312075, a_rwnd: 126323,
28     gaps: 2, duplicate TSNs: 1)
29       Chunk type: SACK (3)
30       Chunk flags: 0x00
31       Chunk length: 28
32       Cumulative TSN ACK: 916312075
33       Advertised receiver window credit (a_rwnd): 126323
34       Number of gap acknowledgement blocks: 2
35       Number of duplicated TSNs: 1
36       Gap Acknowledgement for TSN 916312077 to 916312081
37       Gap Acknowledgement for TSN 916312083 to 916312083
38       [Number of TSNs in gap acknowledgement blocks: 6]
39       Duplicate TSN: 916312081
40 
41   */
42 
43   uint8_t data[] = {0x03, 0x00, 0x00, 0x1c, 0x36, 0x9d, 0xd0, 0x0b, 0x00, 0x01,
44                     0xed, 0x73, 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0x00, 0x06,
45                     0x00, 0x08, 0x00, 0x08, 0x36, 0x9d, 0xd0, 0x11};
46 
47   ASSERT_HAS_VALUE_AND_ASSIGN(SackChunk chunk, SackChunk::Parse(data));
48 
49   TSN cum_ack_tsn(916312075);
50   EXPECT_EQ(chunk.cumulative_tsn_ack(), cum_ack_tsn);
51   EXPECT_EQ(chunk.a_rwnd(), 126323u);
52   EXPECT_THAT(
53       chunk.gap_ack_blocks(),
54       ElementsAre(SackChunk::GapAckBlock(
55                       static_cast<uint16_t>(916312077 - *cum_ack_tsn),
56                       static_cast<uint16_t>(916312081 - *cum_ack_tsn)),
57                   SackChunk::GapAckBlock(
58                       static_cast<uint16_t>(916312083 - *cum_ack_tsn),
59                       static_cast<uint16_t>(916312083 - *cum_ack_tsn))));
60   EXPECT_THAT(chunk.duplicate_tsns(), ElementsAre(TSN(916312081)));
61 }
62 
TEST(SackChunkTest,SerializeAndDeserialize)63 TEST(SackChunkTest, SerializeAndDeserialize) {
64   SackChunk chunk(TSN(123), /*a_rwnd=*/456, {SackChunk::GapAckBlock(2, 3)},
65                   {TSN(1), TSN(2), TSN(3)});
66   std::vector<uint8_t> serialized;
67   chunk.SerializeTo(serialized);
68 
69   ASSERT_HAS_VALUE_AND_ASSIGN(SackChunk deserialized,
70                               SackChunk::Parse(serialized));
71 
72   EXPECT_EQ(*deserialized.cumulative_tsn_ack(), 123u);
73   EXPECT_EQ(deserialized.a_rwnd(), 456u);
74   EXPECT_THAT(deserialized.gap_ack_blocks(),
75               ElementsAre(SackChunk::GapAckBlock(2, 3)));
76   EXPECT_THAT(deserialized.duplicate_tsns(),
77               ElementsAre(TSN(1), TSN(2), TSN(3)));
78 
79   EXPECT_EQ(deserialized.ToString(),
80             "SACK, cum_ack_tsn=123, a_rwnd=456, gap=125--126, dup_tsns=1,2,3");
81 }
82 
83 }  // namespace
84 }  // namespace dcsctp
85