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/heartbeat_request_chunk.h"
11
12 #include <stdint.h>
13
14 #include <utility>
15 #include <vector>
16
17 #include "api/array_view.h"
18 #include "net/dcsctp/packet/parameter/heartbeat_info_parameter.h"
19 #include "net/dcsctp/packet/parameter/parameter.h"
20 #include "net/dcsctp/testing/testing_macros.h"
21 #include "rtc_base/gunit.h"
22 #include "test/gmock.h"
23
24 namespace dcsctp {
25 namespace {
26 using ::testing::ElementsAre;
27
TEST(HeartbeatRequestChunkTest,FromCapture)28 TEST(HeartbeatRequestChunkTest, FromCapture) {
29 /*
30 HEARTBEAT chunk (Information: 40 bytes)
31 Chunk type: HEARTBEAT (4)
32 Chunk flags: 0x00
33 Chunk length: 44
34 Heartbeat info parameter (Information: 36 bytes)
35 Parameter type: Heartbeat info (0x0001)
36 Parameter length: 40
37 Heartbeat information: ad2436603726070000000000000000007b10000001…
38 */
39
40 uint8_t data[] = {0x04, 0x00, 0x00, 0x2c, 0x00, 0x01, 0x00, 0x28, 0xad,
41 0x24, 0x36, 0x60, 0x37, 0x26, 0x07, 0x00, 0x00, 0x00,
42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7b, 0x10, 0x00,
43 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
45
46 ASSERT_HAS_VALUE_AND_ASSIGN(HeartbeatRequestChunk chunk,
47 HeartbeatRequestChunk::Parse(data));
48
49 ASSERT_HAS_VALUE_AND_ASSIGN(HeartbeatInfoParameter info, chunk.info());
50
51 EXPECT_THAT(
52 info.info(),
53 ElementsAre(0xad, 0x24, 0x36, 0x60, 0x37, 0x26, 0x07, 0x00, 0x00, 0x00,
54 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7b, 0x10, 0x00, 0x00,
55 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
57 }
58
TEST(HeartbeatRequestChunkTest,SerializeAndDeserialize)59 TEST(HeartbeatRequestChunkTest, SerializeAndDeserialize) {
60 uint8_t info_data[] = {1, 2, 3, 4};
61 Parameters parameters =
62 Parameters::Builder().Add(HeartbeatInfoParameter(info_data)).Build();
63 HeartbeatRequestChunk chunk(std::move(parameters));
64
65 std::vector<uint8_t> serialized;
66 chunk.SerializeTo(serialized);
67
68 ASSERT_HAS_VALUE_AND_ASSIGN(HeartbeatRequestChunk deserialized,
69 HeartbeatRequestChunk::Parse(serialized));
70
71 ASSERT_HAS_VALUE_AND_ASSIGN(HeartbeatInfoParameter info, deserialized.info());
72
73 EXPECT_THAT(info.info(), ElementsAre(1, 2, 3, 4));
74
75 EXPECT_EQ(deserialized.ToString(), "HEARTBEAT");
76 }
77
78 } // namespace
79 } // namespace dcsctp
80