1 /**********************************************************************
2 Copyright(c) 2011-2018 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 "igzip_checksums.h"
35 #include "checksum_test_ref.h"
36 #include "test.h"
37
38 #ifndef TEST_SEED
39 #define TEST_SEED 0x1234
40 #endif
41
42 #define MAX_BUF 512
43 #define TEST_SIZE 20
44
45 typedef uint32_t (*checksum32_func_t)(uint32_t, const unsigned char *, uint64_t);
46
47 typedef struct func_case {
48 char *note;
49 checksum32_func_t checksum32_func_call;
50 checksum32_func_t checksum32_ref_call;
51 } func_case_t;
52
53 func_case_t test_funcs[] = {
54 { "checksum32_adler", isal_adler32, adler_ref },
55 };
56
57 // Generates pseudo-random data
58
59 void
rand_buffer(unsigned char * buf,long buffer_size)60 rand_buffer(unsigned char *buf, long buffer_size)
61 {
62 long i;
63 for (i = 0; i < buffer_size; i++)
64 buf[i] = rand();
65 }
66
67 // Test cases
68 int
69 zeros_test(func_case_t *test_func);
70 int
71 simple_pattern_test(func_case_t *test_func);
72 int
73 seeds_sizes_test(func_case_t *test_func);
74 int
75 eob_test(func_case_t *test_func);
76 int
77 update_test(func_case_t *test_func);
78 int
79 update_over_mod_test(func_case_t *test_func);
80
81 void *buf_alloc = NULL;
82
83 int
main(int argc,char * argv[])84 main(int argc, char *argv[])
85 {
86 int fail = 0, fail_case;
87 int i, ret;
88 func_case_t *test_func;
89
90 // Align to MAX_BUF boundary
91 ret = posix_memalign(&buf_alloc, MAX_BUF, MAX_BUF * TEST_SIZE);
92 if (ret) {
93 printf("alloc error: Fail");
94 return -1;
95 }
96 srand(TEST_SEED);
97 printf("CHECKSUM32 Tests seed=0x%x\n", TEST_SEED);
98
99 for (i = 0; i < sizeof(test_funcs) / sizeof(test_funcs[0]); i++) {
100 fail_case = 0;
101 test_func = &test_funcs[i];
102
103 printf("Test %s ", test_func->note);
104 fail_case += zeros_test(test_func);
105 fail_case += simple_pattern_test(test_func);
106 fail_case += seeds_sizes_test(test_func);
107 fail_case += eob_test(test_func);
108 fail_case += update_test(test_func);
109 fail_case += update_over_mod_test(test_func);
110 printf("Test %s done: %s\n", test_func->note, fail_case ? "Fail" : "Pass");
111
112 if (fail_case) {
113 printf("\n%s Failed %d tests\n", test_func->note, fail_case);
114 fail++;
115 }
116 }
117
118 printf("CHECKSUM32 Tests all done: %s\n", fail ? "Fail" : "Pass");
119
120 aligned_free(buf_alloc);
121
122 return fail;
123 }
124
125 // Test of all zeros
126 int
zeros_test(func_case_t * test_func)127 zeros_test(func_case_t *test_func)
128 {
129 uint32_t c_dut, c_ref;
130 int fail = 0;
131 unsigned char *buf = NULL;
132
133 buf = (unsigned char *) buf_alloc;
134 memset(buf, 0, MAX_BUF * 10);
135 c_dut = test_func->checksum32_func_call(TEST_SEED, buf, MAX_BUF * 10);
136 c_ref = test_func->checksum32_ref_call(TEST_SEED, buf, MAX_BUF * 10);
137
138 if (c_dut != c_ref) {
139 fail++;
140 printf("\n opt ref\n");
141 printf(" ------ ------\n");
142 printf("checksum zero = 0x%8x 0x%8x \n", c_dut, c_ref);
143 }
144 #ifdef TEST_VERBOSE
145 else
146 printf(".");
147 #endif
148
149 return fail;
150 }
151
152 // Another simple test pattern
153 int
simple_pattern_test(func_case_t * test_func)154 simple_pattern_test(func_case_t *test_func)
155 {
156 uint32_t c_dut, c_ref;
157 int fail = 0;
158 unsigned char *buf = NULL;
159
160 buf = (unsigned char *) buf_alloc;
161 memset(buf, 0x8a, MAX_BUF);
162 c_dut = test_func->checksum32_func_call(TEST_SEED, buf, MAX_BUF);
163 c_ref = test_func->checksum32_ref_call(TEST_SEED, buf, MAX_BUF);
164 if (c_dut != c_ref) {
165 fail++;
166 printf("fail checksum all 8a = 0x%8x 0x%8x\n", c_dut, c_ref);
167 }
168 #ifdef TEST_VERBOSE
169 else
170 printf(".");
171 #endif
172
173 return fail;
174 }
175
176 int
seeds_sizes_test(func_case_t * test_func)177 seeds_sizes_test(func_case_t *test_func)
178 {
179 uint32_t c_dut, c_ref;
180 int fail = 0;
181 int i;
182 uint32_t r, s;
183 unsigned char *buf = NULL;
184
185 // Do a few random tests
186 buf = (unsigned char *) buf_alloc; // reset buf
187 r = rand();
188 rand_buffer(buf, MAX_BUF * TEST_SIZE);
189
190 for (i = 0; i < TEST_SIZE; i++) {
191 c_dut = test_func->checksum32_func_call(r, buf, MAX_BUF);
192 c_ref = test_func->checksum32_ref_call(r, buf, MAX_BUF);
193 if (c_dut != c_ref) {
194 fail++;
195 printf("fail checksum rand%3d = 0x%8x 0x%8x\n", i, c_dut, c_ref);
196 }
197 #ifdef TEST_VERBOSE
198 else
199 printf(".");
200 #endif
201 buf += MAX_BUF;
202 }
203
204 // Do a few random sizes
205 buf = (unsigned char *) buf_alloc; // reset buf
206 r = rand();
207
208 for (i = MAX_BUF; i >= 0; i--) {
209 c_dut = test_func->checksum32_func_call(r, buf, i);
210 c_ref = test_func->checksum32_ref_call(r, buf, i);
211 if (c_dut != c_ref) {
212 fail++;
213 printf("fail random size%i 0x%8x 0x%8x\n", i, c_dut, c_ref);
214 }
215 #ifdef TEST_VERBOSE
216 else
217 printf(".");
218 #endif
219 }
220
221 // Try different seeds
222 for (s = 0; s < 20; s++) {
223 buf = (unsigned char *) buf_alloc; // reset buf
224
225 r = rand(); // just to get a new seed
226 rand_buffer(buf, MAX_BUF * TEST_SIZE); // new pseudo-rand data
227
228 #ifdef TEST_VERBOSE
229 printf("seed = 0x%x\n", r);
230 #endif
231
232 for (i = 0; i < TEST_SIZE; i++) {
233 c_dut = test_func->checksum32_func_call(r, buf, MAX_BUF);
234 c_ref = test_func->checksum32_ref_call(r, buf, MAX_BUF);
235 if (c_dut != c_ref) {
236 fail++;
237 printf("fail checksum rand%3d = 0x%8x 0x%8x\n", i, c_dut, c_ref);
238 }
239 #ifdef TEST_VERBOSE
240 else
241 printf(".");
242 #endif
243 buf += MAX_BUF;
244 }
245 }
246
247 return fail;
248 }
249
250 // Run tests at end of buffer
251 int
eob_test(func_case_t * test_func)252 eob_test(func_case_t *test_func)
253 {
254 uint32_t c_dut, c_ref;
255 int fail = 0;
256 int i;
257 unsigned char *buf = NULL;
258
259 buf = (unsigned char *) buf_alloc; // reset buf
260 buf = buf + ((MAX_BUF - 1) * TEST_SIZE); // Line up TEST_SIZE from end
261 for (i = 0; i < TEST_SIZE; i++) {
262 c_dut = test_func->checksum32_func_call(TEST_SEED, buf + i, TEST_SIZE - i);
263 c_ref = test_func->checksum32_ref_call(TEST_SEED, buf + i, TEST_SIZE - i);
264 if (c_dut != c_ref) {
265 fail++;
266 printf("fail checksum eob rand%3d = 0x%8x 0x%8x\n", i, c_dut, c_ref);
267 }
268 #ifdef TEST_VERBOSE
269 else
270 printf(".");
271 #endif
272 }
273
274 return fail;
275 }
276
277 int
update_test(func_case_t * test_func)278 update_test(func_case_t *test_func)
279 {
280 uint32_t c_dut, c_ref;
281 int fail = 0;
282 int i;
283 uint32_t r;
284 unsigned char *buf = NULL;
285
286 buf = (unsigned char *) buf_alloc; // reset buf
287 r = rand();
288 // Process the whole buf with reference func single call.
289 c_ref = test_func->checksum32_ref_call(r, buf, MAX_BUF * TEST_SIZE);
290 // Process buf with update method.
291 for (i = 0; i < TEST_SIZE; i++) {
292 c_dut = test_func->checksum32_func_call(r, buf, MAX_BUF);
293 // Update checksum seeds and buf pointer.
294 r = c_dut;
295 buf += MAX_BUF;
296 }
297
298 if (c_dut != c_ref) {
299 fail++;
300 printf("checksum rand%3d = 0x%8x 0x%8x\n", i, c_dut, c_ref);
301 }
302 #ifdef TEST_VERBOSE
303 else
304 printf(".");
305 #endif
306
307 return fail;
308 }
309
310 int
update_over_mod_test(func_case_t * test_func)311 update_over_mod_test(func_case_t *test_func)
312 {
313 uint32_t c_dut, c_ref;
314 int fail = 0;
315 int i;
316 unsigned char *buf = NULL;
317
318 buf = malloc(ADLER_MOD);
319 if (buf == NULL)
320 return 1;
321 memset(buf, 0xff, ADLER_MOD);
322
323 c_ref = c_dut = rand();
324
325 // Process buf with update method.
326 for (i = 0; i < 20; i++) {
327 c_ref = test_func->checksum32_ref_call(c_ref, buf, ADLER_MOD - 64);
328 c_dut = test_func->checksum32_func_call(c_dut, buf, ADLER_MOD - 64);
329 }
330
331 if (c_dut != c_ref) {
332 fail++;
333 printf("checksum rand%3d = 0x%8x 0x%8x\n", i, c_dut, c_ref);
334 }
335 #ifdef TEST_VERBOSE
336 else
337 printf(".");
338 #endif
339
340 free(buf);
341 return fail;
342 }
343