summaryrefslogtreecommitdiffstats
path: root/test/crc_unittest.cpp
blob: fb69cb4991a767057a846a82ccbb37754c647b6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "crc.hpp"

#include <string>
#include <vector>

#include <gtest/gtest.h>

namespace blobs
{

TEST(Crc16Test, VerifyCrcValue)
{
    // Verify the crc16 is producing the value we expect.

    // Origin: security/crypta/ipmi/portable/ipmi_utils_test.cc
    struct CrcTestVector
    {
        std::string input;
        uint16_t output;
    };

    std::string longString =
        "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
        "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
        "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
        "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
        "AAAAAAAAAAAAAAAA";

    std::vector<CrcTestVector> vectors({{"", 0x1D0F},
                                        {"A", 0x9479},
                                        {"123456789", 0xE5CC},
                                        {longString, 0xE938}});

    Crc16 crc;

    for (const CrcTestVector& testVector : vectors)
    {
        crc.clear();
        auto data = reinterpret_cast<const uint8_t*>(testVector.input.data());
        crc.compute(data, testVector.input.size());
        EXPECT_EQ(crc.get(), testVector.output);
    }
}
} // namespace blobs
OpenPOWER on IntegriCloud