Victor Boivie | b87746b | 2021-03-30 17:50:17 | [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/chunk/idata_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 | |
| 25 | TEST(IDataChunkTest, AtBeginningFromCapture) { |
| 26 | /* |
| 27 | I_DATA chunk(ordered, first segment, TSN: 2487901653, SID: 1, MID: 0, |
| 28 | payload length: 1180 bytes) |
| 29 | Chunk type: I_DATA (64) |
| 30 | Chunk flags: 0x02 |
| 31 | Chunk length: 1200 |
| 32 | Transmission sequence number: 2487901653 |
| 33 | Stream identifier: 0x0001 |
| 34 | Reserved: 0 |
| 35 | Message identifier: 0 |
| 36 | Payload protocol identifier: WebRTC Binary (53) |
| 37 | Reassembled Message in frame: 39 |
| 38 | */ |
| 39 | |
| 40 | uint8_t data[] = {0x40, 0x02, 0x00, 0x15, 0x94, 0x4a, 0x5d, 0xd5, |
| 41 | 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 42 | 0x00, 0x00, 0x00, 0x35, 0x01, 0x00, 0x00, 0x00}; |
| 43 | |
| 44 | ASSERT_HAS_VALUE_AND_ASSIGN(IDataChunk chunk, IDataChunk::Parse(data)); |
| 45 | EXPECT_EQ(*chunk.tsn(), 2487901653); |
| 46 | EXPECT_EQ(*chunk.stream_id(), 1); |
Victor Boivie | ee0270b | 2023-10-04 12:25:26 | [diff] [blame] | 47 | EXPECT_EQ(*chunk.mid(), 0u); |
Victor Boivie | b87746b | 2021-03-30 17:50:17 | [diff] [blame] | 48 | EXPECT_EQ(*chunk.ppid(), 53u); |
| 49 | EXPECT_EQ(*chunk.fsn(), 0u); // Not provided (so set to zero) |
| 50 | } |
| 51 | |
| 52 | TEST(IDataChunkTest, AtBeginningSerializeAndDeserialize) { |
| 53 | IDataChunk::Options options; |
| 54 | options.is_beginning = Data::IsBeginning(true); |
Victor Boivie | 4b7024b | 2021-12-01 18:57:22 | [diff] [blame] | 55 | IDataChunk chunk(TSN(123), StreamID(456), MID(789), PPID(53), FSN(0), {1}, |
| 56 | options); |
Victor Boivie | b87746b | 2021-03-30 17:50:17 | [diff] [blame] | 57 | |
| 58 | std::vector<uint8_t> serialized; |
| 59 | chunk.SerializeTo(serialized); |
| 60 | |
| 61 | ASSERT_HAS_VALUE_AND_ASSIGN(IDataChunk deserialized, |
| 62 | IDataChunk::Parse(serialized)); |
| 63 | EXPECT_EQ(*deserialized.tsn(), 123u); |
| 64 | EXPECT_EQ(*deserialized.stream_id(), 456u); |
Victor Boivie | ee0270b | 2023-10-04 12:25:26 | [diff] [blame] | 65 | EXPECT_EQ(*deserialized.mid(), 789u); |
Victor Boivie | b87746b | 2021-03-30 17:50:17 | [diff] [blame] | 66 | EXPECT_EQ(*deserialized.ppid(), 53u); |
| 67 | EXPECT_EQ(*deserialized.fsn(), 0u); |
| 68 | |
| 69 | EXPECT_EQ(deserialized.ToString(), |
| 70 | "I-DATA, type=ordered::first, tsn=123, stream_id=456, " |
Victor Boivie | ee0270b | 2023-10-04 12:25:26 | [diff] [blame] | 71 | "mid=789, ppid=53, length=1"); |
Victor Boivie | b87746b | 2021-03-30 17:50:17 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | TEST(IDataChunkTest, InMiddleFromCapture) { |
| 75 | /* |
| 76 | I_DATA chunk(ordered, last segment, TSN: 2487901706, SID: 3, MID: 1, |
| 77 | FSN: 8, payload length: 560 bytes) |
| 78 | Chunk type: I_DATA (64) |
| 79 | Chunk flags: 0x01 |
| 80 | Chunk length: 580 |
| 81 | Transmission sequence number: 2487901706 |
| 82 | Stream identifier: 0x0003 |
| 83 | Reserved: 0 |
| 84 | Message identifier: 1 |
| 85 | Fragment sequence number: 8 |
| 86 | Reassembled SCTP Fragments (10000 bytes, 9 fragments): |
| 87 | */ |
| 88 | |
| 89 | uint8_t data[] = {0x40, 0x01, 0x00, 0x15, 0x94, 0x4a, 0x5e, 0x0a, |
| 90 | 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
| 91 | 0x00, 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00}; |
| 92 | |
| 93 | ASSERT_HAS_VALUE_AND_ASSIGN(IDataChunk chunk, IDataChunk::Parse(data)); |
| 94 | EXPECT_EQ(*chunk.tsn(), 2487901706); |
| 95 | EXPECT_EQ(*chunk.stream_id(), 3u); |
Victor Boivie | ee0270b | 2023-10-04 12:25:26 | [diff] [blame] | 96 | EXPECT_EQ(*chunk.mid(), 1u); |
Victor Boivie | b87746b | 2021-03-30 17:50:17 | [diff] [blame] | 97 | EXPECT_EQ(*chunk.ppid(), 0u); // Not provided (so set to zero) |
| 98 | EXPECT_EQ(*chunk.fsn(), 8u); |
| 99 | } |
| 100 | |
| 101 | TEST(IDataChunkTest, InMiddleSerializeAndDeserialize) { |
| 102 | IDataChunk chunk(TSN(123), StreamID(456), MID(789), PPID(0), FSN(101112), |
Victor Boivie | 4b7024b | 2021-12-01 18:57:22 | [diff] [blame] | 103 | {1, 2, 3}, /*options=*/{}); |
Victor Boivie | b87746b | 2021-03-30 17:50:17 | [diff] [blame] | 104 | |
| 105 | std::vector<uint8_t> serialized; |
| 106 | chunk.SerializeTo(serialized); |
| 107 | |
| 108 | ASSERT_HAS_VALUE_AND_ASSIGN(IDataChunk deserialized, |
| 109 | IDataChunk::Parse(serialized)); |
| 110 | EXPECT_EQ(*deserialized.tsn(), 123u); |
| 111 | EXPECT_EQ(*deserialized.stream_id(), 456u); |
Victor Boivie | ee0270b | 2023-10-04 12:25:26 | [diff] [blame] | 112 | EXPECT_EQ(*deserialized.mid(), 789u); |
Victor Boivie | b87746b | 2021-03-30 17:50:17 | [diff] [blame] | 113 | EXPECT_EQ(*deserialized.ppid(), 0u); |
| 114 | EXPECT_EQ(*deserialized.fsn(), 101112u); |
| 115 | EXPECT_THAT(deserialized.payload(), ElementsAre(1, 2, 3)); |
| 116 | |
| 117 | EXPECT_EQ(deserialized.ToString(), |
| 118 | "I-DATA, type=ordered::middle, tsn=123, stream_id=456, " |
Victor Boivie | ee0270b | 2023-10-04 12:25:26 | [diff] [blame] | 119 | "mid=789, fsn=101112, length=3"); |
Victor Boivie | b87746b | 2021-03-30 17:50:17 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | } // namespace |
| 123 | } // namespace dcsctp |