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/abort_chunk.h"
11
12 #include <stdint.h>
13
14 #include <type_traits>
15 #include <vector>
16
17 #include "net/dcsctp/packet/error_cause/error_cause.h"
18 #include "net/dcsctp/packet/error_cause/user_initiated_abort_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
TEST(AbortChunkTest,FromCapture)26 TEST(AbortChunkTest, FromCapture) {
27 /*
28 ABORT chunk
29 Chunk type: ABORT (6)
30 Chunk flags: 0x00
31 Chunk length: 8
32 User initiated ABORT cause
33 */
34
35 uint8_t data[] = {0x06, 0x00, 0x00, 0x08, 0x00, 0x0c, 0x00, 0x04};
36
37 ASSERT_HAS_VALUE_AND_ASSIGN(AbortChunk chunk, AbortChunk::Parse(data));
38
39 ASSERT_HAS_VALUE_AND_ASSIGN(
40 UserInitiatedAbortCause cause,
41 chunk.error_causes().get<UserInitiatedAbortCause>());
42
43 EXPECT_EQ(cause.upper_layer_abort_reason(), "");
44 }
45
TEST(AbortChunkTest,SerializeAndDeserialize)46 TEST(AbortChunkTest, SerializeAndDeserialize) {
47 AbortChunk chunk(/*filled_in_verification_tag=*/true,
48 Parameters::Builder()
49 .Add(UserInitiatedAbortCause("Close called"))
50 .Build());
51
52 std::vector<uint8_t> serialized;
53 chunk.SerializeTo(serialized);
54
55 ASSERT_HAS_VALUE_AND_ASSIGN(AbortChunk deserialized,
56 AbortChunk::Parse(serialized));
57 ASSERT_HAS_VALUE_AND_ASSIGN(
58 UserInitiatedAbortCause cause,
59 deserialized.error_causes().get<UserInitiatedAbortCause>());
60
61 EXPECT_EQ(cause.upper_layer_abort_reason(), "Close called");
62 }
63
64 // Validates that AbortChunk doesn't make any alignment assumptions.
TEST(AbortChunkTest,SerializeAndDeserializeOneChar)65 TEST(AbortChunkTest, SerializeAndDeserializeOneChar) {
66 AbortChunk chunk(
67 /*filled_in_verification_tag=*/true,
68 Parameters::Builder().Add(UserInitiatedAbortCause("!")).Build());
69
70 std::vector<uint8_t> serialized;
71 chunk.SerializeTo(serialized);
72
73 ASSERT_HAS_VALUE_AND_ASSIGN(AbortChunk deserialized,
74 AbortChunk::Parse(serialized));
75 ASSERT_HAS_VALUE_AND_ASSIGN(
76 UserInitiatedAbortCause cause,
77 deserialized.error_causes().get<UserInitiatedAbortCause>());
78
79 EXPECT_EQ(cause.upper_layer_abort_reason(), "!");
80 }
81
82 } // namespace
83 } // namespace dcsctp
84