xref: /spdk/test/unit/lib/util/crc32c.c/crc32c_ut.c (revision 552e21cce6cccbf833ed9109827e08337377d7ce)
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];
46 
47 	/* Verify a string's CRC32-C value against the known correct result. */
48 	snprintf(buf, sizeof(buf), "%s", "Hello world!");
49 	crc = 0xFFFFFFFFu;
50 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
51 	crc ^= 0xFFFFFFFFu;
52 	CU_ASSERT(crc == 0x7b98e751);
53 
54 	/*
55 	 * The main loop of the optimized CRC32-C implementation processes data in 8-byte blocks,
56 	 * followed by a loop to handle the 0-7 trailing bytes.
57 	 * Test all buffer sizes from 0 to 7 in order to hit all possible trailing byte counts.
58 	 */
59 
60 	/* 0-byte buffer should not modify CRC at all, so final result should be ~0 ^ ~0 == 0 */
61 	snprintf(buf, sizeof(buf), "%s", "");
62 	crc = 0xFFFFFFFFu;
63 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
64 	crc ^= 0xFFFFFFFFu;
65 	CU_ASSERT(crc == 0);
66 
67 	/* 1-byte buffer */
68 	snprintf(buf, sizeof(buf), "%s", "1");
69 	crc = 0xFFFFFFFFu;
70 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
71 	crc ^= 0xFFFFFFFFu;
72 	CU_ASSERT(crc == 0x90F599E3);
73 
74 	/* 2-byte buffer */
75 	snprintf(buf, sizeof(buf), "%s", "12");
76 	crc = 0xFFFFFFFFu;
77 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
78 	crc ^= 0xFFFFFFFFu;
79 	CU_ASSERT(crc == 0x7355C460);
80 
81 	/* 3-byte buffer */
82 	snprintf(buf, sizeof(buf), "%s", "123");
83 	crc = 0xFFFFFFFFu;
84 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
85 	crc ^= 0xFFFFFFFFu;
86 	CU_ASSERT(crc == 0x107B2FB2);
87 
88 	/* 4-byte buffer */
89 	snprintf(buf, sizeof(buf), "%s", "1234");
90 	crc = 0xFFFFFFFFu;
91 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
92 	crc ^= 0xFFFFFFFFu;
93 	CU_ASSERT(crc == 0xF63AF4EE);
94 
95 	/* 5-byte buffer */
96 	snprintf(buf, sizeof(buf), "%s", "12345");
97 	crc = 0xFFFFFFFFu;
98 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
99 	crc ^= 0xFFFFFFFFu;
100 	CU_ASSERT(crc == 0x18D12335);
101 
102 	/* 6-byte buffer */
103 	snprintf(buf, sizeof(buf), "%s", "123456");
104 	crc = 0xFFFFFFFFu;
105 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
106 	crc ^= 0xFFFFFFFFu;
107 	CU_ASSERT(crc == 0x41357186);
108 
109 	/* 7-byte buffer */
110 	snprintf(buf, sizeof(buf), "%s", "1234567");
111 	crc = 0xFFFFFFFFu;
112 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
113 	crc ^= 0xFFFFFFFFu;
114 	CU_ASSERT(crc == 0x124297EA);
115 
116 	/* Test a buffer of exactly 8 bytes (one block in the main CRC32-C loop). */
117 	snprintf(buf, sizeof(buf), "%s", "12345678");
118 	crc = 0xFFFFFFFFu;
119 	crc = spdk_crc32c_update(buf, strlen(buf), crc);
120 	crc ^= 0xFFFFFFFFu;
121 	CU_ASSERT(crc == 0x6087809A);
122 }
123 
124 int
125 main(int argc, char **argv)
126 {
127 	CU_pSuite	suite = NULL;
128 	unsigned int	num_failures;
129 
130 	if (CU_initialize_registry() != CUE_SUCCESS) {
131 		return CU_get_error();
132 	}
133 
134 	suite = CU_add_suite("crc32c", NULL, NULL);
135 	if (suite == NULL) {
136 		CU_cleanup_registry();
137 		return CU_get_error();
138 	}
139 
140 	if (
141 		CU_add_test(suite, "test_crc32c", test_crc32c) == NULL) {
142 		CU_cleanup_registry();
143 		return CU_get_error();
144 	}
145 
146 	CU_basic_set_mode(CU_BRM_VERBOSE);
147 
148 	CU_basic_run_tests();
149 
150 	num_failures = CU_get_number_of_failures();
151 	CU_cleanup_registry();
152 
153 	return num_failures;
154 }
155