xref: /spdk/test/unit/lib/util/crc32c.c/crc32c_ut.c (revision 927f1fd57bd004df581518466ec4c1b8083e5d23)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation 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 #include "spdk/stdinc.h"
35 
36 #include "spdk_cunit.h"
37 
38 #include "util/crc32.c"
39 #include "util/crc32c.c"
40 
41 static void
42 test_crc32c(void)
43 {
44 	uint32_t crc;
45 	char buf[1024], buf1[1024];
46 	struct iovec iov[2] = {};
47 
48 	/* Verify a string's CRC32-C value against the known correct result. */
49 	snprintf(buf, sizeof(buf), "%s", "Hello world!");
50 	crc = 0xFFFFFFFFu;
51 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
52 	crc ^= 0xFFFFFFFFu;
53 	CU_ASSERT(crc == 0x7b98e751);
54 
55 	crc = 0xFFFFFFFFu;
56 	iov[0].iov_base = buf;
57 	iov[0].iov_len = strlen(buf);
58 	crc = spdk_crc32c_iov_update(iov, 1, crc);
59 	crc ^= 0xFFFFFFFFu;
60 	CU_ASSERT(crc == 0x7b98e751);
61 
62 	crc = 0xFFFFFFFFu;
63 	snprintf(buf, sizeof(buf), "%s", "Hello");
64 	iov[0].iov_base = buf;
65 	iov[0].iov_len = strlen(buf);
66 
67 	snprintf(buf1, sizeof(buf1), "%s", " world!");
68 	iov[1].iov_base = buf1;
69 	iov[1].iov_len = strlen(buf1);
70 
71 	crc = spdk_crc32c_iov_update(iov, 2, crc);
72 	crc ^= 0xFFFFFFFFu;
73 	CU_ASSERT(crc == 0x7b98e751);
74 
75 	/*
76 	 * The main loop of the optimized CRC32-C implementation processes data in 8-byte blocks,
77 	 * followed by a loop to handle the 0-7 trailing bytes.
78 	 * Test all buffer sizes from 0 to 7 in order to hit all possible trailing byte counts.
79 	 */
80 
81 	/* 0-byte buffer should not modify CRC at all, so final result should be ~0 ^ ~0 == 0 */
82 	snprintf(buf, sizeof(buf), "%s", "");
83 	crc = 0xFFFFFFFFu;
84 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
85 	crc ^= 0xFFFFFFFFu;
86 	CU_ASSERT(crc == 0);
87 
88 	/* 1-byte buffer */
89 	snprintf(buf, sizeof(buf), "%s", "1");
90 	crc = 0xFFFFFFFFu;
91 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
92 	crc ^= 0xFFFFFFFFu;
93 	CU_ASSERT(crc == 0x90F599E3);
94 
95 	/* 2-byte buffer */
96 	snprintf(buf, sizeof(buf), "%s", "12");
97 	crc = 0xFFFFFFFFu;
98 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
99 	crc ^= 0xFFFFFFFFu;
100 	CU_ASSERT(crc == 0x7355C460);
101 
102 	/* 3-byte buffer */
103 	snprintf(buf, sizeof(buf), "%s", "123");
104 	crc = 0xFFFFFFFFu;
105 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
106 	crc ^= 0xFFFFFFFFu;
107 	CU_ASSERT(crc == 0x107B2FB2);
108 
109 	/* 4-byte buffer */
110 	snprintf(buf, sizeof(buf), "%s", "1234");
111 	crc = 0xFFFFFFFFu;
112 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
113 	crc ^= 0xFFFFFFFFu;
114 	CU_ASSERT(crc == 0xF63AF4EE);
115 
116 	/* 5-byte buffer */
117 	snprintf(buf, sizeof(buf), "%s", "12345");
118 	crc = 0xFFFFFFFFu;
119 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
120 	crc ^= 0xFFFFFFFFu;
121 	CU_ASSERT(crc == 0x18D12335);
122 
123 	/* 6-byte buffer */
124 	snprintf(buf, sizeof(buf), "%s", "123456");
125 	crc = 0xFFFFFFFFu;
126 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
127 	crc ^= 0xFFFFFFFFu;
128 	CU_ASSERT(crc == 0x41357186);
129 
130 	/* 7-byte buffer */
131 	snprintf(buf, sizeof(buf), "%s", "1234567");
132 	crc = 0xFFFFFFFFu;
133 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
134 	crc ^= 0xFFFFFFFFu;
135 	CU_ASSERT(crc == 0x124297EA);
136 
137 	/* Test a buffer of exactly 8 bytes (one block in the main CRC32-C loop). */
138 	snprintf(buf, sizeof(buf), "%s", "12345678");
139 	crc = 0xFFFFFFFFu;
140 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
141 	crc ^= 0xFFFFFFFFu;
142 	CU_ASSERT(crc == 0x6087809A);
143 }
144 
145 int
146 main(int argc, char **argv)
147 {
148 	CU_pSuite	suite = NULL;
149 	unsigned int	num_failures;
150 
151 	CU_set_error_action(CUEA_ABORT);
152 	CU_initialize_registry();
153 
154 	suite = CU_add_suite("crc32c", NULL, NULL);
155 
156 	CU_ADD_TEST(suite, test_crc32c);
157 
158 	CU_basic_set_mode(CU_BRM_VERBOSE);
159 
160 	CU_basic_run_tests();
161 
162 	num_failures = CU_get_number_of_failures();
163 	CU_cleanup_registry();
164 
165 	return num_failures;
166 }
167