1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker * Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker *
4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker */
10*d9f75844SAndroid Build Coastguard Worker #include "net/dcsctp/packet/crc32c.h"
11*d9f75844SAndroid Build Coastguard Worker
12*d9f75844SAndroid Build Coastguard Worker #include "test/gmock.h"
13*d9f75844SAndroid Build Coastguard Worker
14*d9f75844SAndroid Build Coastguard Worker namespace dcsctp {
15*d9f75844SAndroid Build Coastguard Worker namespace {
16*d9f75844SAndroid Build Coastguard Worker
17*d9f75844SAndroid Build Coastguard Worker constexpr std::array<const uint8_t, 0> kEmpty = {};
18*d9f75844SAndroid Build Coastguard Worker constexpr std::array<const uint8_t, 1> kZero = {0};
19*d9f75844SAndroid Build Coastguard Worker constexpr std::array<const uint8_t, 4> kManyZeros = {0, 0, 0, 0};
20*d9f75844SAndroid Build Coastguard Worker constexpr std::array<const uint8_t, 4> kShort = {1, 2, 3, 4};
21*d9f75844SAndroid Build Coastguard Worker constexpr std::array<const uint8_t, 8> kLong = {1, 2, 3, 4, 5, 6, 7, 8};
22*d9f75844SAndroid Build Coastguard Worker // https://tools.ietf.org/html/rfc3720#appendix-B.4
23*d9f75844SAndroid Build Coastguard Worker constexpr std::array<const uint8_t, 32> k32Zeros = {
24*d9f75844SAndroid Build Coastguard Worker 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
25*d9f75844SAndroid Build Coastguard Worker 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
26*d9f75844SAndroid Build Coastguard Worker constexpr std::array<const uint8_t, 32> k32Ones = {
27*d9f75844SAndroid Build Coastguard Worker 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
28*d9f75844SAndroid Build Coastguard Worker 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
29*d9f75844SAndroid Build Coastguard Worker 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
30*d9f75844SAndroid Build Coastguard Worker constexpr std::array<const uint8_t, 32> k32Incrementing = {
31*d9f75844SAndroid Build Coastguard Worker 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
32*d9f75844SAndroid Build Coastguard Worker 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
33*d9f75844SAndroid Build Coastguard Worker constexpr std::array<const uint8_t, 32> k32Decrementing = {
34*d9f75844SAndroid Build Coastguard Worker 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
35*d9f75844SAndroid Build Coastguard Worker 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
36*d9f75844SAndroid Build Coastguard Worker constexpr std::array<const uint8_t, 48> kISCSICommandPDU = {
37*d9f75844SAndroid Build Coastguard Worker 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38*d9f75844SAndroid Build Coastguard Worker 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
39*d9f75844SAndroid Build Coastguard Worker 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x28, 0x00, 0x00, 0x00,
40*d9f75844SAndroid Build Coastguard Worker 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41*d9f75844SAndroid Build Coastguard Worker };
42*d9f75844SAndroid Build Coastguard Worker
TEST(Crc32Test,TestVectors)43*d9f75844SAndroid Build Coastguard Worker TEST(Crc32Test, TestVectors) {
44*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(GenerateCrc32C(kEmpty), 0U);
45*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(GenerateCrc32C(kZero), 0x51537d52U);
46*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(GenerateCrc32C(kManyZeros), 0xc74b6748U);
47*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(GenerateCrc32C(kShort), 0xf48c3029U);
48*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(GenerateCrc32C(kLong), 0x811f8946U);
49*d9f75844SAndroid Build Coastguard Worker // https://tools.ietf.org/html/rfc3720#appendix-B.4
50*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(GenerateCrc32C(k32Zeros), 0xaa36918aU);
51*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(GenerateCrc32C(k32Ones), 0x43aba862U);
52*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(GenerateCrc32C(k32Incrementing), 0x4e79dd46U);
53*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(GenerateCrc32C(k32Decrementing), 0x5cdb3f11U);
54*d9f75844SAndroid Build Coastguard Worker EXPECT_EQ(GenerateCrc32C(kISCSICommandPDU), 0x563a96d9U);
55*d9f75844SAndroid Build Coastguard Worker }
56*d9f75844SAndroid Build Coastguard Worker
57*d9f75844SAndroid Build Coastguard Worker } // namespace
58*d9f75844SAndroid Build Coastguard Worker } // namespace dcsctp
59