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/cookie_echo_chunk.h"
11
12 #include <stdint.h>
13
14 #include <type_traits>
15 #include <vector>
16
17 #include "api/array_view.h"
18 #include "net/dcsctp/testing/testing_macros.h"
19 #include "rtc_base/gunit.h"
20 #include "test/gmock.h"
21
22 namespace dcsctp {
23 namespace {
24 using ::testing::ElementsAre;
25
TEST(CookieEchoChunkTest,FromCapture)26 TEST(CookieEchoChunkTest, FromCapture) {
27 /*
28 COOKIE_ECHO chunk (Cookie length: 256 bytes)
29 Chunk type: COOKIE_ECHO (10)
30 Chunk flags: 0x00
31 Chunk length: 260
32 Cookie: 12345678
33 */
34
35 uint8_t data[] = {0x0a, 0x00, 0x00, 0x08, 0x12, 0x34, 0x56, 0x78};
36
37 ASSERT_HAS_VALUE_AND_ASSIGN(CookieEchoChunk chunk,
38 CookieEchoChunk::Parse(data));
39
40 EXPECT_THAT(chunk.cookie(), ElementsAre(0x12, 0x34, 0x56, 0x78));
41 }
42
TEST(CookieEchoChunkTest,SerializeAndDeserialize)43 TEST(CookieEchoChunkTest, SerializeAndDeserialize) {
44 uint8_t cookie[] = {1, 2, 3, 4};
45 CookieEchoChunk chunk(cookie);
46
47 std::vector<uint8_t> serialized;
48 chunk.SerializeTo(serialized);
49
50 ASSERT_HAS_VALUE_AND_ASSIGN(CookieEchoChunk deserialized,
51 CookieEchoChunk::Parse(serialized));
52
53 EXPECT_THAT(deserialized.cookie(), ElementsAre(1, 2, 3, 4));
54 EXPECT_EQ(deserialized.ToString(), "COOKIE-ECHO");
55 }
56
57 } // namespace
58 } // namespace dcsctp
59