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