xref: /isa-l/crc/crc16_t10dif_copy_test.c (revision 2ca781df19598766427435a011f263680d9e8faa)
1 /**********************************************************************
2   Copyright(c) 2011-2017 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 <string.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <assert.h>
35 #include "crc.h"
36 #include "crc_ref.h"
37 
38 #ifndef RANDOMS
39 # define RANDOMS   20
40 #endif
41 #ifndef TEST_SEED
42 # define TEST_SEED 0x1234
43 #endif
44 
45 #define MAX_BUF    2345
46 #define TEST_SIZE  217
47 #define TEST_LEN  (8 * 1024)
48 
49 typedef uint16_t u16;
50 typedef uint8_t u8;
51 
52 // bitwise crc version
53 uint16_t crc16_t10dif_copy_ref(uint16_t seed, uint8_t * dst, uint8_t * src, uint64_t len);
54 
55 void rand_buffer(unsigned char *buf, long buffer_size)
56 {
57 	long i;
58 	for (i = 0; i < buffer_size; i++)
59 		buf[i] = rand();
60 }
61 
62 int memtst(unsigned char *buf, unsigned char c, int len)
63 {
64 	int i;
65 	for (i = 0; i < len; i++)
66 		if (*buf++ != c)
67 			return 1;
68 
69 	return 0;
70 }
71 
72 int crc_copy_check(const char *description, u8 * dst, u8 * src, u8 dst_fill_val, int len,
73 		   int tot)
74 {
75 	u16 seed;
76 	int rem;
77 
78 	assert(tot >= len);
79 	seed = rand();
80 	rem = tot - len;
81 	memset(dst, dst_fill_val, tot);
82 
83 	// multi-binary crc version
84 	u16 crc_dut = crc16_t10dif_copy(seed, dst, src, len);
85 	u16 crc_ref = crc16_t10dif(seed, src, len);
86 	if (crc_dut != crc_ref) {
87 		printf("%s, crc gen fail: 0x%4x 0x%4x len=%d\n", description, crc_dut,
88 		       crc_ref, len);
89 		return 1;
90 	} else if (memcmp(dst, src, len)) {
91 		printf("%s, copy fail: len=%d\n", description, len);
92 		return 1;
93 	} else if (memtst(&dst[len], dst_fill_val, rem)) {
94 		printf("%s, writeover fail: len=%d\n", description, len);
95 		return 1;
96 	}
97 	// bitwise crc version
98 	crc_dut = crc16_t10dif_copy_ref(seed, dst, src, len);
99 	crc_ref = crc16_t10dif_ref(seed, src, len);
100 	if (crc_dut != crc_ref) {
101 		printf("%s, crc gen fail (table-driven): 0x%4x 0x%4x len=%d\n", description,
102 		       crc_dut, crc_ref, len);
103 		return 1;
104 	} else if (memcmp(dst, src, len)) {
105 		printf("%s, copy fail (table driven): len=%d\n", description, len);
106 		return 1;
107 	} else if (memtst(&dst[len], dst_fill_val, rem)) {
108 		printf("%s, writeover fail (table driven): len=%d\n", description, len);
109 		return 1;
110 	}
111 	return 0;
112 }
113 
114 int main(int argc, char *argv[])
115 {
116 	int r = 0;
117 	int i;
118 	int len, tot;
119 	u8 *src_raw, *dst_raw;
120 	u8 *src, *dst;
121 
122 	printf("Test crc16_t10dif_copy_test:\n");
123 	src_raw = (u8 *) malloc(TEST_LEN);
124 	dst_raw = (u8 *) malloc(TEST_LEN);
125 	if (NULL == src_raw || NULL == dst_raw) {
126 		printf("alloc error: Fail");
127 		return -1;
128 	}
129 	src = src_raw;
130 	dst = dst_raw;
131 
132 	srand(TEST_SEED);
133 
134 	// Test of all zeros
135 	memset(src, 0, TEST_LEN);
136 	r |= crc_copy_check("zero tst", dst, src, 0x5e, MAX_BUF, TEST_LEN);
137 
138 	// Another simple test pattern
139 	memset(src, 0xff, TEST_LEN);
140 	r |= crc_copy_check("simp tst", dst, src, 0x5e, MAX_BUF, TEST_LEN);
141 
142 	// Do a few short len random data tests
143 	rand_buffer(src, TEST_LEN);
144 	rand_buffer(dst, TEST_LEN);
145 	for (i = 0; i < MAX_BUF; i++) {
146 		r |= crc_copy_check("short len", dst, src, rand(), i, MAX_BUF);
147 	}
148 #ifdef TEST_VERBOSE
149 	printf(".");
150 #endif
151 
152 	// Do a few longer tests, random data
153 	for (i = TEST_LEN; i >= (TEST_LEN - TEST_SIZE); i--) {
154 		r |= crc_copy_check("long len", dst, src, rand(), i, TEST_LEN);
155 	}
156 #ifdef TEST_VERBOSE
157 	printf(".");
158 #endif
159 
160 	// Do random size, random data
161 	for (i = 0; i < RANDOMS; i++) {
162 		len = rand() % TEST_LEN;
163 		r |= crc_copy_check("rand len", dst, src, rand(), len, TEST_LEN);
164 	}
165 #ifdef TEST_VERBOSE
166 	printf(".");
167 #endif
168 
169 	// Run tests at end of buffer
170 	for (i = 0; i < RANDOMS; i++) {
171 		len = rand() % TEST_LEN;
172 		src = &src_raw[TEST_LEN - len - 1];
173 		dst = &dst_raw[TEST_LEN - len - 1];
174 		tot = len;
175 		r |= crc_copy_check("end of buffer", dst, src, rand(), len, tot);
176 	}
177 #ifdef TEST_VERBOSE
178 	printf(".");
179 #endif
180 
181 	printf("Test done: %s\n", r ? "Fail" : "Pass");
182 	return r;
183 }
184