blob: fb69cb4991a767057a846a82ccbb37754c647b6e [file] [log] [blame]
Patrick Ventureef3aead2018-09-12 08:53:29 -07001#include "crc.hpp"
2
3#include <string>
4#include <vector>
5
6#include <gtest/gtest.h>
7
8namespace blobs
9{
10
11TEST(Crc16Test, VerifyCrcValue)
12{
13 // Verify the crc16 is producing the value we expect.
14
15 // Origin: security/crypta/ipmi/portable/ipmi_utils_test.cc
16 struct CrcTestVector
17 {
18 std::string input;
19 uint16_t output;
20 };
21
22 std::string longString =
23 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
24 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
25 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
26 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
27 "AAAAAAAAAAAAAAAA";
28
29 std::vector<CrcTestVector> vectors({{"", 0x1D0F},
30 {"A", 0x9479},
31 {"123456789", 0xE5CC},
32 {longString, 0xE938}});
33
34 Crc16 crc;
35
36 for (const CrcTestVector& testVector : vectors)
37 {
38 crc.clear();
39 auto data = reinterpret_cast<const uint8_t*>(testVector.input.data());
40 crc.compute(data, testVector.input.size());
41 EXPECT_EQ(crc.get(), testVector.output);
42 }
43}
44} // namespace blobs