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/error_chunk.h"
11
12 #include <cstdint>
13 #include <type_traits>
14 #include <vector>
15
16 #include "api/array_view.h"
17 #include "net/dcsctp/packet/error_cause/error_cause.h"
18 #include "net/dcsctp/packet/error_cause/unrecognized_chunk_type_cause.h"
19 #include "net/dcsctp/testing/testing_macros.h"
20 #include "rtc_base/gunit.h"
21 #include "test/gmock.h"
22
23 namespace dcsctp {
24 namespace {
25 using ::testing::ElementsAre;
26
TEST(ErrorChunkTest,FromCapture)27 TEST(ErrorChunkTest, FromCapture) {
28 /*
29 ERROR chunk
30 Chunk type: ERROR (9)
31 Chunk flags: 0x00
32 Chunk length: 12
33 Unrecognized chunk type cause (Type: 73 (unknown))
34 */
35
36 uint8_t data[] = {0x09, 0x00, 0x00, 0x0c, 0x00, 0x06,
37 0x00, 0x08, 0x49, 0x00, 0x00, 0x04};
38
39 ASSERT_HAS_VALUE_AND_ASSIGN(ErrorChunk chunk, ErrorChunk::Parse(data));
40
41 ASSERT_HAS_VALUE_AND_ASSIGN(
42 UnrecognizedChunkTypeCause cause,
43 chunk.error_causes().get<UnrecognizedChunkTypeCause>());
44
45 EXPECT_THAT(cause.unrecognized_chunk(), ElementsAre(0x49, 0x00, 0x00, 0x04));
46 }
47
TEST(ErrorChunkTest,SerializeAndDeserialize)48 TEST(ErrorChunkTest, SerializeAndDeserialize) {
49 ErrorChunk chunk(Parameters::Builder()
50 .Add(UnrecognizedChunkTypeCause({1, 2, 3, 4}))
51 .Build());
52
53 std::vector<uint8_t> serialized;
54 chunk.SerializeTo(serialized);
55
56 ASSERT_HAS_VALUE_AND_ASSIGN(ErrorChunk deserialized,
57 ErrorChunk::Parse(serialized));
58 ASSERT_HAS_VALUE_AND_ASSIGN(
59 UnrecognizedChunkTypeCause cause,
60 deserialized.error_causes().get<UnrecognizedChunkTypeCause>());
61
62 EXPECT_THAT(cause.unrecognized_chunk(), ElementsAre(1, 2, 3, 4));
63 }
64
65 } // namespace
66 } // namespace dcsctp
67