]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /********************************************************************** |
2 | Copyright(c) 2011-2015 Intel Corporation All rights reserved. | |
3 | ||
4 | Redistribution and use in source and binary forms, with or without | |
f91f0fd5 | 5 | modification, are permitted provided that the following conditions |
7c673cae FG |
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<stdint.h> | |
32 | #include<string.h> | |
33 | #include<stdlib.h> | |
34 | #include "raid.h" | |
35 | #include "types.h" | |
36 | ||
37 | #define TEST_SOURCES 16 | |
38 | #define TEST_LEN 1024 | |
39 | #define TEST_MEM ((TEST_SOURCES + 2)*(TEST_LEN)) | |
40 | #ifndef TEST_SEED | |
41 | # define TEST_SEED 0x1234 | |
42 | #endif | |
43 | ||
44 | int ref_multi_pq(int vects, int len, void **array) | |
45 | { | |
46 | int i, j; | |
47 | unsigned char p, q, s; | |
48 | unsigned char **src = (unsigned char **)array; | |
49 | ||
50 | for (i = 0; i < len; i++) { | |
51 | q = p = src[vects - 3][i]; | |
52 | ||
53 | for (j = vects - 4; j >= 0; j--) { | |
54 | p ^= s = src[j][i]; | |
55 | q = s ^ ((q << 1) ^ ((q & 0x80) ? 0x1d : 0)); // mult by GF{2} | |
56 | } | |
57 | ||
58 | src[vects - 2][i] = p; // second to last pointer is p | |
59 | src[vects - 1][i] = q; // last pointer is q | |
60 | } | |
61 | return 0; | |
62 | } | |
63 | ||
64 | // Generates pseudo-random data | |
65 | ||
66 | void rand_buffer(unsigned char *buf, long buffer_size) | |
67 | { | |
68 | long i; | |
69 | for (i = 0; i < buffer_size; i++) | |
70 | buf[i] = rand(); | |
71 | } | |
72 | ||
73 | int main(int argc, char *argv[]) | |
74 | { | |
75 | int i, j, k, ret, fail = 0; | |
76 | void *buffs[TEST_SOURCES + 2]; | |
77 | char c; | |
78 | char *tmp_buf[TEST_SOURCES + 2]; | |
79 | int serr, lerr; | |
80 | ||
81 | printf("Test pq_check_test %d sources X %d bytes\n", TEST_SOURCES, TEST_LEN); | |
82 | ||
83 | srand(TEST_SEED); | |
84 | ||
85 | // Allocate the arrays | |
86 | for (i = 0; i < TEST_SOURCES + 2; i++) { | |
87 | void *buf; | |
88 | if (posix_memalign(&buf, 16, TEST_LEN)) { | |
89 | printf("alloc error: Fail"); | |
90 | return 1; | |
91 | } | |
92 | buffs[i] = buf; | |
93 | } | |
94 | ||
95 | // Test of all zeros | |
96 | for (i = 0; i < TEST_SOURCES + 2; i++) | |
97 | memset(buffs[i], 0, TEST_LEN); | |
98 | ||
99 | ref_multi_pq(TEST_SOURCES + 2, TEST_LEN, buffs); | |
100 | ret = pq_check(TEST_SOURCES + 2, TEST_LEN, buffs); | |
101 | if (ret != 0) { | |
102 | fail++; | |
103 | printf("\nfail zero test %d\n", ret); | |
104 | } | |
105 | ||
106 | ((char *)(buffs[0]))[TEST_LEN - 2] = 0x7; // corrupt buffer | |
107 | ret = pq_check(TEST_SOURCES + 2, TEST_LEN, buffs); | |
108 | if (ret == 0) { | |
109 | fail++; | |
110 | printf("\nfail corrupt buffer test %d\n", ret); | |
111 | } | |
112 | ((char *)(buffs[0]))[TEST_LEN - 2] = 0; // un-corrupt buffer | |
113 | ||
114 | // Test corrupted buffer any location on all sources | |
115 | for (j = 0; j < TEST_SOURCES + 2; j++) { | |
116 | for (i = TEST_LEN - 1; i >= 0; i--) { | |
117 | ((char *)buffs[j])[i] = 0x5; // corrupt buffer | |
118 | ret = pq_check(TEST_SOURCES + 2, TEST_LEN, buffs); | |
119 | if (ret == 0) { | |
120 | fail++; | |
121 | printf("\nfail corrupt zero buffer test j=%d, i=%d\n", j, i); | |
122 | return 1; | |
123 | } | |
124 | ((char *)buffs[j])[i] = 0; // un-corrupt buffer | |
125 | } | |
126 | putchar('.'); | |
127 | } | |
128 | ||
129 | // Test rand1 | |
130 | for (i = 0; i < TEST_SOURCES + 2; i++) | |
131 | rand_buffer(buffs[i], TEST_LEN); | |
132 | ||
133 | ref_multi_pq(TEST_SOURCES + 2, TEST_LEN, buffs); | |
134 | ret = pq_check(TEST_SOURCES + 2, TEST_LEN, buffs); | |
135 | if (ret != 0) { | |
136 | fail++; | |
137 | printf("fail first rand test %d\n", ret); | |
138 | } | |
139 | ||
140 | c = ((char *)(buffs[0]))[TEST_LEN - 2]; | |
141 | ((char *)(buffs[0]))[TEST_LEN - 2] = c ^ 0x1; | |
142 | ret = pq_check(TEST_SOURCES + 2, TEST_LEN, buffs); | |
143 | if (ret == 0) { | |
144 | fail++; | |
145 | printf("\nFail corrupt buffer test, passed when should have failed\n"); | |
146 | } | |
147 | ((char *)(buffs[0]))[TEST_LEN - 2] = c; // un-corrupt buffer | |
148 | ||
149 | // Test corrupted buffer any location on all sources w/ random data | |
150 | for (j = 0; j < TEST_SOURCES + 2; j++) { | |
151 | for (i = TEST_LEN - 1; i >= 0; i--) { | |
152 | // Check it still passes | |
153 | ret = pq_check(TEST_SOURCES + 2, TEST_LEN, buffs); | |
154 | if (ret != 0) { // should pass | |
155 | fail++; | |
156 | printf | |
157 | ("\nFail rand test with un-corrupted buffer j=%d, i=%d\n", | |
158 | j, i); | |
159 | return 1; | |
160 | } | |
161 | c = ((char *)buffs[j])[i]; | |
162 | ((char *)buffs[j])[i] = c ^ 1; // corrupt buffer | |
163 | ret = pq_check(TEST_SOURCES + 2, TEST_LEN, buffs); | |
164 | if (ret == 0) { // Check it now fails | |
165 | fail++; | |
166 | printf("\nfail corrupt buffer test j=%d, i=%d\n", j, i); | |
167 | return 1; | |
168 | } | |
169 | ((char *)buffs[j])[i] = c; // un-corrupt buffer | |
170 | } | |
171 | putchar('.'); | |
172 | } | |
173 | ||
174 | // Test various number of sources, full length | |
175 | for (j = 4; j <= TEST_SOURCES + 2; j++) { | |
176 | // New random data | |
177 | for (i = 0; i < j; i++) | |
178 | rand_buffer(buffs[i], TEST_LEN); | |
179 | ||
180 | // Generate p,q parity for this number of sources | |
181 | ref_multi_pq(j, TEST_LEN, buffs); | |
182 | ||
183 | // Set errors up in each source and len position | |
184 | for (i = 0; i < j; i++) { | |
185 | for (k = 0; k < TEST_LEN; k++) { | |
186 | // See if it still passes | |
187 | ret = pq_check(j, TEST_LEN, buffs); | |
188 | if (ret != 0) { // Should pass | |
189 | printf("\nfail rand fixed len test %d sources\n", j); | |
190 | fail++; | |
191 | return 1; | |
192 | } | |
193 | ||
194 | c = ((char *)buffs[i])[k]; | |
195 | ((char *)buffs[i])[k] = c ^ 1; // corrupt buffer | |
196 | ||
197 | ret = pq_check(j, TEST_LEN, buffs); | |
198 | if (ret == 0) { // Should fail | |
199 | printf | |
200 | ("\nfail rand fixed len test corrupted buffer %d sources\n", | |
201 | j); | |
202 | fail++; | |
203 | return 1; | |
204 | } | |
205 | ((char *)buffs[i])[k] = c; // un-corrupt buffer | |
206 | } | |
207 | } | |
208 | putchar('.'); | |
209 | } | |
210 | ||
211 | fflush(0); | |
212 | ||
213 | // Test various number of sources and len | |
214 | k = 16; | |
215 | while (k <= TEST_LEN) { | |
216 | char *tmp; | |
217 | for (j = 4; j <= TEST_SOURCES + 2; j++) { | |
218 | for (i = 0; i < j; i++) | |
219 | rand_buffer(buffs[i], k); | |
220 | ||
221 | // Generate p,q parity for this number of sources | |
222 | ref_multi_pq(j, k, buffs); | |
223 | ||
224 | // Inject errors at various source and len positions | |
225 | for (lerr = 0; lerr < k; lerr++) { | |
226 | for (serr = 0; serr < j; serr++) { | |
227 | // See if it still passes | |
228 | ret = pq_check(j, k, buffs); | |
229 | if (ret != 0) { // Should pass | |
230 | printf | |
231 | ("\nfail rand var src, len test %d sources, len=%d\n", | |
232 | j, k); | |
233 | fail++; | |
234 | return 1; | |
235 | } | |
236 | ||
237 | tmp = (char *)buffs[serr]; | |
238 | c = tmp[lerr]; | |
239 | ((char *)buffs[serr])[lerr] = c ^ 1; // corrupt buffer | |
240 | ||
241 | ret = pq_check(j, k, buffs); | |
242 | if (ret == 0) { // Should fail | |
243 | printf | |
244 | ("\nfail rand var src, len test corrupted buffer " | |
245 | "%d sources, len=%d, ret=%d\n", j, k, | |
246 | ret); | |
247 | fail++; | |
248 | return 1; | |
249 | } | |
250 | ((char *)buffs[serr])[lerr] = c; // un-corrupt buffer | |
251 | } | |
252 | } | |
253 | putchar('.'); | |
254 | fflush(0); | |
255 | } | |
256 | k += 16; | |
257 | } | |
258 | ||
259 | // Test at the end of buffer | |
260 | for (i = 0; i < TEST_LEN; i += 16) { | |
261 | for (j = 0; j < TEST_SOURCES + 2; j++) { | |
262 | rand_buffer(buffs[j], TEST_LEN - i); | |
263 | tmp_buf[j] = (char *)buffs[j] + i; | |
264 | } | |
265 | ||
266 | pq_gen_base(TEST_SOURCES + 2, TEST_LEN - i, (void *)tmp_buf); | |
267 | ||
268 | // Test good data | |
269 | ret = pq_check(TEST_SOURCES + 2, TEST_LEN - i, (void *)tmp_buf); | |
270 | if (ret != 0) { | |
271 | printf("fail end test - offset: %d, len: %d\n", i, TEST_LEN - i); | |
272 | fail++; | |
273 | return 1; | |
274 | } | |
275 | // Test bad data | |
276 | for (serr = 0; serr < TEST_SOURCES + 2; serr++) { | |
277 | for (lerr = 0; lerr < (TEST_LEN - i); lerr++) { | |
278 | c = tmp_buf[serr][lerr]; | |
279 | tmp_buf[serr][lerr] = c ^ 1; | |
280 | ||
281 | ret = | |
282 | pq_check(TEST_SOURCES + 2, TEST_LEN - i, (void *)tmp_buf); | |
283 | if (ret == 0) { | |
284 | printf("fail end test corrupted buffer - " | |
285 | "offset: %d, len: %d, ret: %d\n", i, | |
286 | TEST_LEN - i, ret); | |
287 | fail++; | |
288 | return 1; | |
289 | } | |
290 | ||
291 | tmp_buf[serr][lerr] = c; | |
292 | } | |
293 | } | |
294 | ||
295 | putchar('.'); | |
296 | fflush(0); | |
297 | } | |
298 | ||
299 | if (fail == 0) | |
300 | printf("Pass\n"); | |
301 | ||
302 | return fail; | |
303 | ||
304 | } |