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. 4 // 5 // Uniitest for data encryption functions. 6 7 #include "base/logging.h" 8 #include "testing/gmock/include/gmock/gmock.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 #include "rlz/lib/crc8.h" 12 13 TEST(Crc8Unittest, TestCrc8) { 14 struct Data { 15 char string[10]; 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; 22 int random_byte; 23 unsigned char corrupt_value; 24 } data[] = { 25 {"Google", 0x01, 2, 0x53}, 26 {"GOOGLE", 0xA6, 4, 0x11}, 27 {"My CRC 8!", 0xDC, 0, 0x50}, 28 }; 29 30 unsigned char* bytes; 31 unsigned char crc; 32 bool matches; 33 int length; 34 for (size_t i = 0; i < sizeof(data) / sizeof(data[0]); ++i) { 35 bytes = reinterpret_cast<unsigned char*>(data[i].string); 36 crc = 0; 37 matches = false; 38 length = strlen(data[i].string); 39 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); 44 EXPECT_TRUE(matches); 45 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); 50 } 51 } 52