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/tlv_trait.h"
11
12 #include <vector>
13
14 #include "api/array_view.h"
15 #include "rtc_base/buffer.h"
16 #include "rtc_base/checks.h"
17 #include "rtc_base/gunit.h"
18 #include "test/gmock.h"
19
20 namespace dcsctp {
21 namespace {
22 using ::testing::ElementsAre;
23 using ::testing::SizeIs;
24
25 struct OneByteTypeConfig {
26 static constexpr int kTypeSizeInBytes = 1;
27 static constexpr int kType = 0x49;
28 static constexpr size_t kHeaderSize = 12;
29 static constexpr int kVariableLengthAlignment = 4;
30 };
31
32 class OneByteChunk : public TLVTrait<OneByteTypeConfig> {
33 public:
34 static constexpr size_t kVariableSize = 4;
35
SerializeTo(std::vector<uint8_t> & out)36 void SerializeTo(std::vector<uint8_t>& out) {
37 BoundedByteWriter<OneByteTypeConfig::kHeaderSize> writer =
38 AllocateTLV(out, kVariableSize);
39 writer.Store32<4>(0x01020304);
40 writer.Store16<8>(0x0506);
41 writer.Store16<10>(0x0708);
42
43 uint8_t variable_data[kVariableSize] = {0xDE, 0xAD, 0xBE, 0xEF};
44 writer.CopyToVariableData(rtc::ArrayView<const uint8_t>(variable_data));
45 }
46
47 static absl::optional<BoundedByteReader<OneByteTypeConfig::kHeaderSize>>
Parse(rtc::ArrayView<const uint8_t> data)48 Parse(rtc::ArrayView<const uint8_t> data) {
49 return ParseTLV(data);
50 }
51 };
52
TEST(TlvDataTest,CanWriteOneByteTypeTlvs)53 TEST(TlvDataTest, CanWriteOneByteTypeTlvs) {
54 std::vector<uint8_t> out;
55 OneByteChunk().SerializeTo(out);
56
57 EXPECT_THAT(out, SizeIs(OneByteTypeConfig::kHeaderSize +
58 OneByteChunk::kVariableSize));
59 EXPECT_THAT(out, ElementsAre(0x49, 0x00, 0x00, 0x10, 0x01, 0x02, 0x03, 0x04,
60 0x05, 0x06, 0x07, 0x08, 0xDE, 0xAD, 0xBE, 0xEF));
61 }
62
TEST(TlvDataTest,CanReadOneByteTypeTlvs)63 TEST(TlvDataTest, CanReadOneByteTypeTlvs) {
64 uint8_t data[] = {0x49, 0x00, 0x00, 0x10, 0x01, 0x02, 0x03, 0x04,
65 0x05, 0x06, 0x07, 0x08, 0xDE, 0xAD, 0xBE, 0xEF};
66
67 absl::optional<BoundedByteReader<OneByteTypeConfig::kHeaderSize>> reader =
68 OneByteChunk::Parse(data);
69 ASSERT_TRUE(reader.has_value());
70 EXPECT_EQ(reader->Load32<4>(), 0x01020304U);
71 EXPECT_EQ(reader->Load16<8>(), 0x0506U);
72 EXPECT_EQ(reader->Load16<10>(), 0x0708U);
73 EXPECT_THAT(reader->variable_data(), ElementsAre(0xDE, 0xAD, 0xBE, 0xEF));
74 }
75
76 struct TwoByteTypeConfig {
77 static constexpr int kTypeSizeInBytes = 2;
78 static constexpr int kType = 31337;
79 static constexpr size_t kHeaderSize = 8;
80 static constexpr int kVariableLengthAlignment = 2;
81 };
82
83 class TwoByteChunk : public TLVTrait<TwoByteTypeConfig> {
84 public:
85 static constexpr size_t kVariableSize = 8;
86
SerializeTo(std::vector<uint8_t> & out)87 void SerializeTo(std::vector<uint8_t>& out) {
88 BoundedByteWriter<TwoByteTypeConfig::kHeaderSize> writer =
89 AllocateTLV(out, kVariableSize);
90 writer.Store32<4>(0x01020304U);
91
92 uint8_t variable_data[] = {0x05, 0x06, 0x07, 0x08, 0xDE, 0xAD, 0xBE, 0xEF};
93 writer.CopyToVariableData(rtc::ArrayView<const uint8_t>(variable_data));
94 }
95
96 static absl::optional<BoundedByteReader<TwoByteTypeConfig::kHeaderSize>>
Parse(rtc::ArrayView<const uint8_t> data)97 Parse(rtc::ArrayView<const uint8_t> data) {
98 return ParseTLV(data);
99 }
100 };
101
TEST(TlvDataTest,CanWriteTwoByteTypeTlvs)102 TEST(TlvDataTest, CanWriteTwoByteTypeTlvs) {
103 std::vector<uint8_t> out;
104
105 TwoByteChunk().SerializeTo(out);
106
107 EXPECT_THAT(out, SizeIs(TwoByteTypeConfig::kHeaderSize +
108 TwoByteChunk::kVariableSize));
109 EXPECT_THAT(out, ElementsAre(0x7A, 0x69, 0x00, 0x10, 0x01, 0x02, 0x03, 0x04,
110 0x05, 0x06, 0x07, 0x08, 0xDE, 0xAD, 0xBE, 0xEF));
111 }
112
TEST(TlvDataTest,CanReadTwoByteTypeTlvs)113 TEST(TlvDataTest, CanReadTwoByteTypeTlvs) {
114 uint8_t data[] = {0x7A, 0x69, 0x00, 0x10, 0x01, 0x02, 0x03, 0x04,
115 0x05, 0x06, 0x07, 0x08, 0xDE, 0xAD, 0xBE, 0xEF};
116
117 absl::optional<BoundedByteReader<TwoByteTypeConfig::kHeaderSize>> reader =
118 TwoByteChunk::Parse(data);
119 EXPECT_TRUE(reader.has_value());
120 EXPECT_EQ(reader->Load32<4>(), 0x01020304U);
121 EXPECT_THAT(reader->variable_data(),
122 ElementsAre(0x05, 0x06, 0x07, 0x08, 0xDE, 0xAD, 0xBE, 0xEF));
123 }
124
TEST(TlvDataTest,CanHandleInvalidLengthSmallerThanFixedSize)125 TEST(TlvDataTest, CanHandleInvalidLengthSmallerThanFixedSize) {
126 // Has 'length=6', which is below the kHeaderSize of 8.
127 uint8_t data[] = {0x7A, 0x69, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04};
128
129 EXPECT_FALSE(TwoByteChunk::Parse(data).has_value());
130 }
131
132 } // namespace
133 } // namespace dcsctp
134