xref: /aosp_15_r20/system/nfc/tests/src/CrcChecksum_test.cc (revision 7eba2f3b06c51ae21384f6a4f14577b668a869b3)
1 //
2 // Copyright (C) 2024 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "CrcChecksum.h"
18 #include <gtest/gtest.h>
19 #include <fstream>
20 #include <iostream>
21 #include <string>
22 
writeFileWithChecksum(const std::string & filename,const std::string & data,uint16_t checksum)23 void writeFileWithChecksum(const std::string& filename, const std::string& data,
24                            uint16_t checksum)
25 {
26   std::ofstream file(filename, std::ios::binary);
27   if (file.is_open()) {
28     file.write(reinterpret_cast<const char*>(&checksum),
29                sizeof(checksum));
30     file.write(data.c_str(), data.size());
31     file.close();
32   }
33 }
34 class CrcChecksumTest : public ::testing::Test {
35  protected:
SetUp()36   void SetUp() override {}
TearDown()37   void TearDown() override {}
38 };
39 
TEST_F(CrcChecksumTest,EmptyBuffer)40 TEST_F(CrcChecksumTest, EmptyBuffer) {
41     unsigned char buffer[] = {};
42     uint16_t result = crcChecksumCompute(buffer, sizeof(buffer));
43     EXPECT_EQ(result, 0);
44 }
45 
TEST_F(CrcChecksumTest,SingleByteBuffer)46 TEST_F(CrcChecksumTest, SingleByteBuffer) {
47     unsigned char buffer[] = {0x01};
48     uint16_t result = crcChecksumCompute(buffer, sizeof(buffer));
49     EXPECT_EQ(result, 49345);
50 }
51 
TEST_F(CrcChecksumTest,MultipleByteBuffer)52 TEST_F(CrcChecksumTest, MultipleByteBuffer) {
53     unsigned char buffer[] = {0x01, 0x02, 0x03, 0x04, 0x05};
54     uint16_t result = crcChecksumCompute(buffer, sizeof(buffer));
55     EXPECT_EQ(result, 47886);
56 }
57 
TEST_F(CrcChecksumTest,AllZeroBuffer)58 TEST_F(CrcChecksumTest, AllZeroBuffer) {
59     unsigned char buffer[5] = {0};
60     uint16_t result = crcChecksumCompute(buffer, sizeof(buffer));
61     EXPECT_EQ(result, 0x0000);
62 }
63 
TEST_F(CrcChecksumTest,AllOneBuffer)64 TEST_F(CrcChecksumTest, AllOneBuffer) {
65     unsigned char buffer[] = {0xFF, 0xFF, 0xFF, 0xFF};
66     uint16_t result = crcChecksumCompute(buffer, sizeof(buffer));
67     EXPECT_EQ(result, 37889);
68 }
69 
TEST_F(CrcChecksumTest,AlternatingBytes)70 TEST_F(CrcChecksumTest, AlternatingBytes) {
71     unsigned char buffer[] = {0xAA, 0x55, 0xAA, 0x55};
72     uint16_t result = crcChecksumCompute(buffer, sizeof(buffer));
73     EXPECT_EQ(result, 22415);
74 }
75 
TEST_F(CrcChecksumTest,LargeBuffer)76 TEST_F(CrcChecksumTest, LargeBuffer) {
77     std::string largeData(10 * 1024 * 1024, 'A');
78     uint16_t expectedChecksum = crcChecksumCompute(
79             reinterpret_cast<const unsigned char*>(largeData.c_str()),
80             largeData.size());
81     std::string filename = "test_large_buffer.bin";
82     writeFileWithChecksum(filename, largeData, expectedChecksum);
83     bool result = crcChecksumVerifyIntegrity(filename.c_str());
84     EXPECT_TRUE(result);
85     remove(filename.c_str());
86 }
87 
88 class CrcChecksumFileTest : public ::testing::Test {
89  protected:
SetUp()90   void SetUp() override {}
TearDown()91   void TearDown() override {}
92 };
93 
TEST_F(CrcChecksumFileTest,VerifyFileIntegrity)94 TEST_F(CrcChecksumFileTest, VerifyFileIntegrity) {
95   // Define test data and compute the expected checksum
96   std::string data = "Hello, CRC!";
97   uint16_t expectedChecksum = crcChecksumCompute(
98           reinterpret_cast<const unsigned char*>(data.c_str()), data.size());
99   std::string filename = "test_file_with_crc.bin";
100   writeFileWithChecksum(filename, data, expectedChecksum);
101   bool result = crcChecksumVerifyIntegrity(filename.c_str());
102   EXPECT_TRUE(result);
103   remove(filename.c_str());
104 }
105 
106 
TEST_F(CrcChecksumFileTest,VerifyFileIntegrityWithCorruptedChecksum)107 TEST_F(CrcChecksumFileTest, VerifyFileIntegrityWithCorruptedChecksum)
108 {
109   std::string data = "Hello, CRC!";
110   uint16_t expectedChecksum = crcChecksumCompute(
111           reinterpret_cast<const unsigned char*>(data.c_str()), data.size());
112   uint16_t corruptedChecksum = expectedChecksum + 1;
113   std::string filename = "test_file_with_corrupted_crc.bin";
114   writeFileWithChecksum(filename, data, corruptedChecksum);
115   bool result = crcChecksumVerifyIntegrity(filename.c_str());
116   EXPECT_FALSE(result);
117   remove(filename.c_str());
118 }
119 
TEST_F(CrcChecksumFileTest,FileWithMissingChecksum)120 TEST_F(CrcChecksumFileTest, FileWithMissingChecksum) {
121     std::string data = "Hello, CRC!";
122     std::string filename = "test_missing_checksum.bin";
123     std::ofstream file(filename, std::ios::binary);
124     file.write(data.c_str(), data.size());
125     bool result = crcChecksumVerifyIntegrity(filename.c_str());
126     EXPECT_FALSE(result);
127     remove(filename.c_str());
128 }
129 
TEST_F(CrcChecksumFileTest,EmptyFile)130 TEST_F(CrcChecksumFileTest, EmptyFile) {
131     std::string filename = "test_empty_file.bin";
132     std::ofstream file(filename, std::ios::binary);
133     bool result = crcChecksumVerifyIntegrity(filename.c_str());
134     EXPECT_FALSE(result);
135     remove(filename.c_str());
136 }
137 
TEST_F(CrcChecksumFileTest,LargeFile)138 TEST_F(CrcChecksumFileTest, LargeFile) {
139     std::string data(10 * 1024 * 1024, 'A');
140     uint16_t checksum = crcChecksumCompute(
141             reinterpret_cast<const unsigned char*>(data.c_str()), data.size());
142     std::string filename = "test_large_file.bin";
143     writeFileWithChecksum(filename, data, checksum);
144     bool result = crcChecksumVerifyIntegrity(filename.c_str());
145     EXPECT_TRUE(result);
146     remove(filename.c_str());
147 }
148 
main(int argc,char ** argv)149 int main(int argc, char** argv) {
150     ::testing::InitGoogleTest(&argc, argv);
151     return RUN_ALL_TESTS();
152 }