1 // Copyright 2017 The CRC32C Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
4
5 #ifndef CRC32C_CRC32C_EXTEND_UNITTESTS_H_
6 #define CRC32C_CRC32C_EXTEND_UNITTESTS_H_
7
8 #include <cstddef>
9 #include <cstdint>
10 #include <cstring>
11
12 #include "gtest/gtest.h"
13
14 // Common test cases for all implementations of CRC32C_Extend().
15
16 namespace crc32c {
17
18 template<typename TestTraits>
19 class ExtendTest : public testing::Test {};
20
21 TYPED_TEST_SUITE_P(ExtendTest);
22
TYPED_TEST_P(ExtendTest,StandardResults)23 TYPED_TEST_P(ExtendTest, StandardResults) {
24 // From rfc3720 section B.4.
25 uint8_t buf[32];
26
27 std::memset(buf, 0, sizeof(buf));
28 EXPECT_EQ(static_cast<uint32_t>(0x8a9136aa),
29 TypeParam::Extend(0, buf, sizeof(buf)));
30
31 std::memset(buf, 0xff, sizeof(buf));
32 EXPECT_EQ(static_cast<uint32_t>(0x62a8ab43),
33 TypeParam::Extend(0, buf, sizeof(buf)));
34
35 for (int i = 0; i < 32; ++i)
36 buf[i] = static_cast<uint8_t>(i);
37 EXPECT_EQ(static_cast<uint32_t>(0x46dd794e),
38 TypeParam::Extend(0, buf, sizeof(buf)));
39
40 for (int i = 0; i < 32; ++i)
41 buf[i] = static_cast<uint8_t>(31 - i);
42 EXPECT_EQ(static_cast<uint32_t>(0x113fdb5c),
43 TypeParam::Extend(0, buf, sizeof(buf)));
44
45 uint8_t data[48] = {
46 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
48 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x28, 0x00, 0x00, 0x00,
49 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50 };
51 EXPECT_EQ(static_cast<uint32_t>(0xd9963a56),
52 TypeParam::Extend(0, data, sizeof(data)));
53 }
54
TYPED_TEST_P(ExtendTest,HelloWorld)55 TYPED_TEST_P(ExtendTest, HelloWorld) {
56 const uint8_t* hello_space_world =
57 reinterpret_cast<const uint8_t*>("hello world");
58 const uint8_t* hello_space = reinterpret_cast<const uint8_t*>("hello ");
59 const uint8_t* world = reinterpret_cast<const uint8_t*>("world");
60
61 EXPECT_EQ(TypeParam::Extend(0, hello_space_world, 11),
62 TypeParam::Extend(TypeParam::Extend(0, hello_space, 6), world, 5));
63 }
64
TYPED_TEST_P(ExtendTest,BufferSlicing)65 TYPED_TEST_P(ExtendTest, BufferSlicing) {
66 uint8_t buffer[48] = {
67 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
69 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x28, 0x00, 0x00, 0x00,
70 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 };
72
73 for (size_t i = 0; i < 48; ++i) {
74 for (size_t j = i + 1; j <= 48; ++j) {
75 uint32_t crc = 0;
76
77 if (i > 0) crc = TypeParam::Extend(crc, buffer, i);
78 crc = TypeParam::Extend(crc, buffer + i, j - i);
79 if (j < 48) crc = TypeParam::Extend(crc, buffer + j, 48 - j);
80
81 EXPECT_EQ(static_cast<uint32_t>(0xd9963a56), crc);
82 }
83 }
84 }
85
TYPED_TEST_P(ExtendTest,LargeBufferSlicing)86 TYPED_TEST_P(ExtendTest, LargeBufferSlicing) {
87 uint8_t buffer[2048];
88 for (size_t i = 0; i < 2048; i++)
89 buffer[i] = static_cast<uint8_t>(3 * i * i + 7 * i + 11);
90
91 for (size_t i = 0; i < 2048; ++i) {
92 for (size_t j = i + 1; j <= 2048; ++j) {
93 uint32_t crc = 0;
94
95 if (i > 0) crc = TypeParam::Extend(crc, buffer, i);
96 crc = TypeParam::Extend(crc, buffer + i, j - i);
97 if (j < 2048) crc = TypeParam::Extend(crc, buffer + j, 2048 - j);
98
99 EXPECT_EQ(static_cast<uint32_t>(0x36dcc753), crc);
100 }
101 }
102 }
103
104 REGISTER_TYPED_TEST_SUITE_P(ExtendTest,
105 StandardResults,
106 HelloWorld,
107 BufferSlicing,
108 LargeBufferSlicing);
109
110 } // namespace crc32c
111
112 #endif // CRC32C_CRC32C_EXTEND_UNITTESTS_H_
113