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/data_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(DataChunkTest,FromCapture)25 TEST(DataChunkTest, FromCapture) {
26 /*
27 DATA chunk(ordered, complete segment, TSN: 1426601532, SID: 2, SSN: 1,
28 PPID: 53, payload length: 4 bytes)
29 Chunk type: DATA (0)
30 Chunk flags: 0x03
31 Chunk length: 20
32 Transmission sequence number: 1426601532
33 Stream identifier: 0x0002
34 Stream sequence number: 1
35 Payload protocol identifier: WebRTC Binary (53)
36 */
37
38 uint8_t data[] = {0x00, 0x03, 0x00, 0x14, 0x55, 0x08, 0x36, 0x3c, 0x00, 0x02,
39 0x00, 0x01, 0x00, 0x00, 0x00, 0x35, 0x00, 0x01, 0x02, 0x03};
40
41 ASSERT_HAS_VALUE_AND_ASSIGN(DataChunk chunk, DataChunk::Parse(data));
42 EXPECT_EQ(*chunk.tsn(), 1426601532u);
43 EXPECT_EQ(*chunk.stream_id(), 2u);
44 EXPECT_EQ(*chunk.ssn(), 1u);
45 EXPECT_EQ(*chunk.ppid(), 53u);
46 EXPECT_TRUE(*chunk.options().is_beginning);
47 EXPECT_TRUE(*chunk.options().is_end);
48 EXPECT_FALSE(*chunk.options().is_unordered);
49 EXPECT_FALSE(*chunk.options().immediate_ack);
50 EXPECT_THAT(chunk.payload(), ElementsAre(0x0, 0x1, 0x2, 0x3));
51 }
52
TEST(DataChunkTest,SerializeAndDeserialize)53 TEST(DataChunkTest, SerializeAndDeserialize) {
54 DataChunk chunk(TSN(123), StreamID(456), SSN(789), PPID(9090),
55 /*payload=*/{1, 2, 3, 4, 5},
56 /*options=*/{});
57
58 std::vector<uint8_t> serialized;
59 chunk.SerializeTo(serialized);
60
61 ASSERT_HAS_VALUE_AND_ASSIGN(DataChunk deserialized,
62 DataChunk::Parse(serialized));
63 EXPECT_EQ(*chunk.tsn(), 123u);
64 EXPECT_EQ(*chunk.stream_id(), 456u);
65 EXPECT_EQ(*chunk.ssn(), 789u);
66 EXPECT_EQ(*chunk.ppid(), 9090u);
67 EXPECT_THAT(chunk.payload(), ElementsAre(1, 2, 3, 4, 5));
68
69 EXPECT_EQ(deserialized.ToString(),
70 "DATA, type=ordered::middle, tsn=123, sid=456, ssn=789, ppid=9090, "
71 "length=5");
72 }
73 } // namespace
74 } // namespace dcsctp
75