1 /*
2 Copyright (c) 2018, MIPI Alliance, Inc.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
16
17 * Neither the name of the copyright holder nor the names of its
18 contributors may be used to endorse or promote products derived
19 from this software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Contributors:
36 * Norbert Schulz (Intel Corporation) - Initial API and implementation
37 */
38 #include "mipi_syst_gtest.h"
39
40 #ifdef MIPI_SYST_PCFG_ENABLE_CHECKSUM
41
42 #include "../../include/mipi_syst/crc32.h"
43 #include "../../src/mipi_syst_crc32.c"
44
45 /* Test input data for crc calculation tests. The last value shown
46 * in the array name is the reference crc32C.
47 */
48 static mipi_syst_u8 crc_input_u8_0xB5D83007[] = {
49 '0', '1', '2', '3', '4', '5', '6', '7',
50 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
51 };
52
53 static mipi_syst_u16 crc_input_u16_0xAA2741E5[] = {
54 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x10,
55 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20
56 };
57
58 static mipi_syst_u32 crc_input_u32_0xCDBDE657[] = {
59 0x00000000, 0xABCDEF12, 0x12345678, 0xFADEDBAD, 0xAA55AA55
60 };
61
62 static mipi_syst_u64 crc_input_u64_0x61A6DF0B[] = {
63 0x1122334455667788ull, 0x1122334455667788ull, 0xABCDEFAABBCCDDEEull,
64 0x0000000000000000ull, 0xFFFFFFFFFFFFFFFFull, 0x0101010101010101ull
65 };
66
67 class MipiSysTFixtureCrc32 : public testing::Test
68 {
69 public:
SetUp()70 void SetUp() {
71 crc = MIPI_SYST_CRC32_INIT(0);
72 }
73
TearDown()74 void TearDown(){
75 }
76
crc32_byte_array(mipi_syst_u8 * pData,mipi_syst_u32 bytes)77 mipi_syst_u32 crc32_byte_array( mipi_syst_u8 *pData, mipi_syst_u32 bytes)
78 {
79 while (bytes--) {
80 MIPI_SYST_CRC32_U8(crc, *pData++);
81 }
82 return MIPI_SYST_CRC32_GET(crc);
83 }
84
crc32_word_array(mipi_syst_u16 * pData,mipi_syst_u32 words)85 mipi_syst_u32 crc32_word_array( mipi_syst_u16 *pData, mipi_syst_u32 words)
86 {
87 while (words--) {
88 MIPI_SYST_CRC32_U16(crc, *pData++);
89 }
90 return MIPI_SYST_CRC32_GET(crc);
91 }
92
crc32_dword_array(mipi_syst_u32 * pData,mipi_syst_u32 dwords)93 mipi_syst_u32 crc32_dword_array( mipi_syst_u32 *pData, mipi_syst_u32 dwords)
94 {
95 while (dwords--) {
96 MIPI_SYST_CRC32_U32(crc, *pData++);
97 }
98 return MIPI_SYST_CRC32_GET(crc);
99 }
100
crc32_qword_array(mipi_syst_u64 * pData,mipi_syst_u32 qwords)101 mipi_syst_u32 crc32_qword_array( mipi_syst_u64 *pData, mipi_syst_u32 qwords)
102 {
103 while (qwords--) {
104 MIPI_SYST_CRC32_U64(crc, *pData++);
105 }
106 return MIPI_SYST_CRC32_GET(crc);
107 }
108 protected:
109 mipi_syst_u32 crc;
110 };
111
112 /* Test structure sizes used in binary output processing
113 */
TEST_F(MipiSysTFixtureCrc32,syst_crc32_bytes)114 TEST_F(MipiSysTFixtureCrc32, syst_crc32_bytes)
115 {
116 EXPECT_EQ(0xB5D83007 ,crc32_byte_array(crc_input_u8_0xB5D83007 ,
117 sizeof(crc_input_u8_0xB5D83007))) << "MIPI_SYST_CRC32_U8() test";
118 }
119
TEST_F(MipiSysTFixtureCrc32,syst_crc32_halfwords)120 TEST_F(MipiSysTFixtureCrc32, syst_crc32_halfwords)
121 {
122 EXPECT_EQ(0xAA2741E5,crc32_word_array((mipi_syst_u16*)crc_input_u16_0xAA2741E5,
123 sizeof(crc_input_u16_0xAA2741E5) / sizeof(mipi_syst_u16))) << "MIPI_SYST_CRC32_U16() test";
124 }
125
TEST_F(MipiSysTFixtureCrc32,syst_crc32_dwords)126 TEST_F(MipiSysTFixtureCrc32, syst_crc32_dwords)
127 {
128 EXPECT_EQ(0xCDBDE657,crc32_dword_array((mipi_syst_u32*)crc_input_u32_0xCDBDE657,
129 sizeof(crc_input_u32_0xCDBDE657) / sizeof(mipi_syst_u32))) << "MIPI_SYST_CRC32_U32() test";
130 }
131
TEST_F(MipiSysTFixtureCrc32,syst_crc32_quadwords)132 TEST_F(MipiSysTFixtureCrc32, syst_crc32_quadwords)
133 {
134 EXPECT_EQ(0x61A6DF0B,crc32_qword_array((mipi_syst_u64*)crc_input_u64_0x61A6DF0B,
135 sizeof(crc_input_u64_0x61A6DF0B) / sizeof(mipi_syst_u64))) << "MIPI_SYST_CRC32_U64() test";
136 }
137
138 #endif