1 /**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
12 distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include <inttypes.h>
35
36 #include "crc64.h"
37 #include "crc64_ref.h"
38 #include "test.h"
39
40 #ifndef TEST_SEED
41 #define TEST_SEED 0x1234
42 #endif
43
44 #define MAX_BUF 4096
45 #define TEST_SIZE 32
46
47 typedef uint64_t u64;
48 typedef uint32_t u32;
49 typedef uint16_t u16;
50 typedef uint8_t u8;
51
52 typedef uint64_t (*crc64_func_t)(uint64_t, const uint8_t *, uint64_t);
53
54 typedef struct func_case {
55 char *note;
56 crc64_func_t crc64_func_call;
57 crc64_func_t crc64_base_call;
58 crc64_func_t crc64_ref_call;
59 } func_case_t;
60
61 func_case_t test_funcs[] = {
62 { "crc64_ecma_norm", crc64_ecma_norm, crc64_ecma_norm_base, crc64_ecma_norm_ref },
63 { "crc64_ecma_refl", crc64_ecma_refl, crc64_ecma_refl_base, crc64_ecma_refl_ref },
64 { "crc64_iso_norm", crc64_iso_norm, crc64_iso_norm_base, crc64_iso_norm_ref },
65 { "crc64_iso_refl", crc64_iso_refl, crc64_iso_refl_base, crc64_iso_refl_ref },
66 { "crc64_jones_norm", crc64_jones_norm, crc64_jones_norm_base, crc64_jones_norm_ref },
67 { "crc64_jones_refl", crc64_jones_refl, crc64_jones_refl_base, crc64_jones_refl_ref },
68 { "crc64_rocksoft_norm", crc64_rocksoft_norm, crc64_rocksoft_norm_base,
69 crc64_rocksoft_norm_ref },
70 { "crc64_rocksoft_refl", crc64_rocksoft_refl, crc64_rocksoft_refl_base,
71 crc64_rocksoft_refl_ref }
72 };
73
74 // Generates pseudo-random data
75
76 void
rand_buffer(unsigned char * buf,long buffer_size)77 rand_buffer(unsigned char *buf, long buffer_size)
78 {
79 long i;
80 for (i = 0; i < buffer_size; i++)
81 buf[i] = rand();
82 }
83
84 // Test cases
85 int
86 zeros_test(func_case_t *test_func);
87
88 int
89 simple_pattern_test(func_case_t *test_func);
90
91 int
92 seeds_sizes_test(func_case_t *test_func);
93
94 int
95 eob_test(func_case_t *test_func);
96
97 int
98 update_test(func_case_t *test_func);
99
100 void *buf_alloc = NULL;
101
102 int
main(int argc,char * argv[])103 main(int argc, char *argv[])
104 {
105 int fail = 0, fail_case;
106 int i, ret;
107 func_case_t *test_func;
108
109 // Align to 32B boundary
110 ret = posix_memalign(&buf_alloc, TEST_SIZE, MAX_BUF * TEST_SIZE);
111 if (ret) {
112 printf("alloc error: Fail");
113 return -1;
114 }
115 srand(TEST_SEED);
116 printf("CRC64 Tests\n");
117
118 for (i = 0; i < sizeof(test_funcs) / sizeof(test_funcs[0]); i++) {
119 fail_case = 0;
120 test_func = &test_funcs[i];
121
122 printf("Test %s\t", test_func->note);
123 fail_case += zeros_test(test_func);
124 fail_case += simple_pattern_test(test_func);
125 fail_case += seeds_sizes_test(test_func);
126 fail_case += eob_test(test_func);
127 fail_case += update_test(test_func);
128 printf(" done: %s\n", fail_case ? "Fail" : "Pass");
129
130 if (fail_case) {
131 printf("\n%s Failed %d tests\n", test_func->note, fail_case);
132 fail++;
133 }
134 }
135
136 printf("CRC64 Tests all done: %s\n", fail ? "Fail" : "Pass");
137
138 aligned_free(buf_alloc);
139
140 return fail;
141 }
142
143 // Test of all zeros
144 int
zeros_test(func_case_t * test_func)145 zeros_test(func_case_t *test_func)
146 {
147 uint64_t crc_ref, crc_base, crc;
148 int fail = 0;
149 unsigned char *buf = NULL;
150
151 buf = (unsigned char *) buf_alloc;
152 memset(buf, 0, MAX_BUF * 10);
153 crc_ref = test_func->crc64_ref_call(TEST_SEED, buf, MAX_BUF * 10);
154 crc_base = test_func->crc64_base_call(TEST_SEED, buf, MAX_BUF * 10);
155 crc = test_func->crc64_func_call(TEST_SEED, buf, MAX_BUF * 10);
156
157 if ((crc_base != crc_ref) || (crc != crc_ref)) {
158 fail++;
159 printf("\n opt ref\n");
160 printf(" ------ ------\n");
161 printf("fail crc zero = 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n",
162 crc_ref, crc_base, crc);
163 }
164 #ifdef TEST_VERBOSE
165 else
166 printf(".");
167 #endif
168
169 return fail;
170 }
171
172 // Another simple test pattern
173 int
simple_pattern_test(func_case_t * test_func)174 simple_pattern_test(func_case_t *test_func)
175 {
176 uint64_t crc_ref, crc_base, crc;
177 int fail = 0;
178 unsigned char *buf = NULL;
179
180 buf = (unsigned char *) buf_alloc;
181 memset(buf, 0x8a, MAX_BUF);
182 crc_ref = test_func->crc64_ref_call(TEST_SEED, buf, MAX_BUF);
183 crc_base = test_func->crc64_base_call(TEST_SEED, buf, MAX_BUF);
184 crc = test_func->crc64_func_call(TEST_SEED, buf, MAX_BUF);
185
186 if ((crc_base != crc_ref) || (crc != crc_ref)) {
187 fail++;
188 printf("fail crc all 8a = 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n", crc_ref,
189 crc_base, crc);
190 }
191 #ifdef TEST_VERBOSE
192 else
193 printf(".");
194 #endif
195 return fail;
196 }
197
198 int
seeds_sizes_test(func_case_t * test_func)199 seeds_sizes_test(func_case_t *test_func)
200 {
201 uint64_t crc_ref, crc_base, crc;
202 int fail = 0;
203 int i;
204 uint64_t r, s;
205 unsigned char *buf = NULL;
206
207 // Do a few random tests
208 buf = (unsigned char *) buf_alloc; // reset buf
209 r = rand();
210 rand_buffer(buf, MAX_BUF * TEST_SIZE);
211
212 for (i = 0; i < TEST_SIZE; i++) {
213 crc_ref = test_func->crc64_ref_call(r, buf, MAX_BUF);
214 crc_base = test_func->crc64_base_call(r, buf, MAX_BUF);
215 crc = test_func->crc64_func_call(r, buf, MAX_BUF);
216
217 if ((crc_base != crc_ref) || (crc != crc_ref)) {
218 fail++;
219 printf("fail crc rand%3d = 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n", i,
220 crc_ref, crc_base, crc);
221 }
222 #ifdef TEST_VERBOSE
223 else if (i % (TEST_SIZE / 8) == 0)
224 printf(".");
225 #endif
226 buf += MAX_BUF;
227 }
228
229 // Do a few random sizes
230 buf = (unsigned char *) buf_alloc; // reset buf
231 r = rand();
232
233 for (i = MAX_BUF; i >= 0; i--) {
234 crc_ref = test_func->crc64_ref_call(r, buf, i);
235 crc_base = test_func->crc64_base_call(r, buf, i);
236 crc = test_func->crc64_func_call(r, buf, i);
237
238 if ((crc_base != crc_ref) || (crc != crc_ref)) {
239 fail++;
240 printf("fail random size %d 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n", i,
241 crc_ref, crc_base, crc);
242 }
243 #ifdef TEST_VERBOSE
244 else if (i % (MAX_BUF / 8) == 0)
245 printf(".");
246 #endif
247 }
248
249 // Try different seeds
250 for (s = 0; s < 20; s++) {
251 buf = (unsigned char *) buf_alloc; // reset buf
252
253 r = rand(); // just to get a new seed
254 rand_buffer(buf, MAX_BUF * TEST_SIZE); // new pseudo-rand data
255
256 #ifdef TEST_VERBOSE
257 printf("seed = 0x%lx\n", r);
258 #endif
259
260 for (i = 0; i < TEST_SIZE; i++) {
261 crc_ref = test_func->crc64_ref_call(r, buf, MAX_BUF);
262 crc_base = test_func->crc64_base_call(r, buf, MAX_BUF);
263 crc = test_func->crc64_func_call(r, buf, MAX_BUF);
264
265 if ((crc_base != crc_ref) || (crc != crc_ref)) {
266 fail++;
267 printf("fail crc rand%3d = 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64
268 "\n",
269 i, crc_ref, crc_base, crc);
270 }
271 #ifdef TEST_VERBOSE
272 else if (i % (TEST_SIZE * 20 / 8) == 0)
273 printf(".");
274 #endif
275 buf += MAX_BUF;
276 }
277 }
278
279 return fail;
280 }
281
282 // Run tests at end of buffer
283 int
eob_test(func_case_t * test_func)284 eob_test(func_case_t *test_func)
285 {
286 uint64_t crc_ref, crc_base, crc;
287 int fail = 0;
288 int i;
289 unsigned char *buf = NULL;
290
291 // Null test
292 if (0 != test_func->crc64_func_call(0, NULL, 0)) {
293 fail++;
294 printf("crc null test fail\n");
295 }
296
297 buf = (unsigned char *) buf_alloc; // reset buf
298 buf = buf + ((MAX_BUF - 1) * TEST_SIZE); // Line up TEST_SIZE from end
299 for (i = 0; i <= TEST_SIZE; i++) {
300 crc_ref = test_func->crc64_ref_call(TEST_SEED, buf + i, TEST_SIZE - i);
301 crc_base = test_func->crc64_base_call(TEST_SEED, buf + i, TEST_SIZE - i);
302 crc = test_func->crc64_func_call(TEST_SEED, buf + i, TEST_SIZE - i);
303
304 if ((crc_base != crc_ref) || (crc != crc_ref)) {
305 fail++;
306 printf("fail crc eob rand%3d = 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n",
307 i, crc_ref, crc_base, crc);
308 }
309 #ifdef TEST_VERBOSE
310 else if (i % (TEST_SIZE / 8) == 0)
311 printf(".");
312 #endif
313 }
314
315 return fail;
316 }
317
318 int
update_test(func_case_t * test_func)319 update_test(func_case_t *test_func)
320 {
321 uint64_t crc_ref, crc_base, crc;
322 int fail = 0;
323 int i;
324 uint64_t r;
325 unsigned char *buf = NULL;
326
327 buf = (unsigned char *) buf_alloc; // reset buf
328 r = rand();
329 // Process the whole buf with reference func single call.
330 crc_ref = test_func->crc64_ref_call(r, buf, MAX_BUF * TEST_SIZE);
331 crc_base = test_func->crc64_base_call(r, buf, MAX_BUF * TEST_SIZE);
332 // Process buf with update method.
333 for (i = 0; i < TEST_SIZE; i++) {
334 crc = test_func->crc64_func_call(r, buf, MAX_BUF);
335 // Update crc seeds and buf pointer.
336 r = crc;
337 buf += MAX_BUF;
338 }
339
340 if ((crc_base != crc_ref) || (crc != crc_ref)) {
341 fail++;
342 printf("fail crc rand%3d = 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n", i, crc_ref,
343 crc_base, crc);
344 }
345 #ifdef TEST_VERBOSE
346 else
347 printf(".");
348 #endif
349
350 return fail;
351 }
352