1 // Copyright (c) 2012 The Chromium 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.
5 // Uniitest for data encryption functions.
7 #include "base/logging.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 #include "rlz/lib/crc8.h"
13 TEST(Crc8Unittest, TestCrc8) {
16 // Externally calculated checksums use
17 // http://www.zorc.breitbandkatze.de/crc.html
18 // with the ATM HEC paramters:
19 // CRC-8, Polynomial 0x07, Initial value 0x00, Final XOR value 0x55
20 // (direct, don't reverse data byes, don't reverse CRC before final XOR)
21 unsigned char external_crc;
23 unsigned char corrupt_value;
25 {"Google", 0x01, 2, 0x53},
26 {"GOOGLE", 0xA6, 4, 0x11},
27 {"My CRC 8!", 0xDC, 0, 0x50},
34 for (size_t i = 0; i < sizeof(data) / sizeof(data[0]); ++i) {
35 bytes = reinterpret_cast<unsigned char*>(data[i].string);
38 length = strlen(data[i].string);
40 // Calculate CRC and compare against external value.
41 rlz_lib::Crc8::Generate(bytes, length, &crc);
42 EXPECT_TRUE(crc == data[i].external_crc);
43 rlz_lib::Crc8::Verify(bytes, length, crc, &matches);
46 // Corrupt string and see if CRC still matches.
47 data[i].string[data[i].random_byte] = data[i].corrupt_value;
48 rlz_lib::Crc8::Verify(bytes, length, crc, &matches);
49 EXPECT_FALSE(matches);