]>
git.proxmox.com Git - ceph.git/blob - ceph/src/isa-l/raid/xor_check_test.c
dfb571a60ac9cfeccae9dd8273e556b03f8d68ae
1 /**********************************************************************
2 Copyright(c) 2011-2015 Intel Corporation All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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
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.
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 **********************************************************************/
37 #define TEST_SOURCES 16
39 #define TEST_MEM ((TEST_SOURCES + 1)*(TEST_LEN))
41 # define TEST_SEED 0x1234
44 // Generates pseudo-random data
46 void rand_buffer(unsigned char *buf
, long buffer_size
)
49 for (i
= 0; i
< buffer_size
; i
++)
53 int main(int argc
, char *argv
[])
55 int i
, j
, k
, ret
, fail
= 0;
56 void *buffs
[TEST_SOURCES
+ 1];
59 char *tmp_buf
[TEST_SOURCES
+ 1];
61 printf("Test xor_check_test %d sources X %d bytes\n", TEST_SOURCES
, TEST_LEN
);
65 // Allocate the arrays
66 for (i
= 0; i
< TEST_SOURCES
+ 1; i
++) {
68 if (posix_memalign(&buf
, 16, TEST_LEN
)) {
69 printf("alloc error: Fail");
76 for (i
= 0; i
< TEST_SOURCES
+ 1; i
++)
77 memset(buffs
[i
], 0, TEST_LEN
);
79 xor_gen_base(TEST_SOURCES
+ 1, TEST_LEN
, buffs
);
80 ret
= xor_check(TEST_SOURCES
+ 1, TEST_LEN
, buffs
);
83 printf("\nfail zero test %d\n", ret
);
86 ((char *)(buffs
[0]))[TEST_LEN
- 2] = 0x7; // corrupt buffer
87 ret
= xor_check(TEST_SOURCES
+ 1, TEST_LEN
, buffs
);
90 printf("\nfail corrupt buffer test %d\n", ret
);
92 ((char *)(buffs
[0]))[TEST_LEN
- 2] = 0; // un-corrupt buffer
94 // Test corrupted buffer any location on all sources
95 for (j
= 0; j
< TEST_SOURCES
+ 1; j
++) {
96 for (i
= TEST_LEN
- 1; i
>= 0; i
--) {
97 ((char *)buffs
[j
])[i
] = 0x5; // corrupt buffer
98 ret
= xor_check(TEST_SOURCES
+ 1, TEST_LEN
, buffs
);
101 printf("\nfail corrupt buffer test j=%d, i=%d\n", j
, i
);
104 ((char *)buffs
[j
])[i
] = 0; // un-corrupt buffer
110 for (i
= 0; i
< TEST_SOURCES
+ 1; i
++)
111 rand_buffer(buffs
[i
], TEST_LEN
);
113 xor_gen_base(TEST_SOURCES
+ 1, TEST_LEN
, buffs
);
114 ret
= xor_check(TEST_SOURCES
+ 1, TEST_LEN
, buffs
);
117 printf("fail first rand test %d\n", ret
);
120 c
= ((char *)(buffs
[0]))[TEST_LEN
- 2];
121 ((char *)(buffs
[0]))[TEST_LEN
- 2] = c
^ 0x1;
122 ret
= xor_check(TEST_SOURCES
+ 1, TEST_LEN
, buffs
);
125 printf("\nFail corrupt buffer test, passed when should have failed\n");
127 ((char *)(buffs
[0]))[TEST_LEN
- 2] = c
; // un-corrupt buffer
129 // Test corrupted buffer any location on all sources w/ random data
130 for (j
= 0; j
< TEST_SOURCES
+ 1; j
++) {
131 for (i
= TEST_LEN
- 1; i
>= 0; i
--) {
132 // Check it still passes
133 ret
= xor_check(TEST_SOURCES
+ 1, TEST_LEN
, buffs
);
134 if (ret
!= 0) { // should pass
137 ("\nFail rand test with un-corrupted buffer j=%d, i=%d\n",
141 c
= ((char *)buffs
[j
])[i
];
142 ((char *)buffs
[j
])[i
] = c
^ 1; // corrupt buffer
143 ret
= xor_check(TEST_SOURCES
+ 1, TEST_LEN
, buffs
);
144 if (ret
== 0) { // Check it now fails
146 printf("\nfail corrupt buffer test j=%d, i=%d\n", j
, i
);
149 ((char *)buffs
[j
])[i
] = c
; // un-corrupt buffer
154 // Test various number of sources, full length
155 for (j
= 3; j
<= TEST_SOURCES
+ 1; j
++) {
157 for (i
= 0; i
< j
; i
++)
158 rand_buffer(buffs
[i
], TEST_LEN
);
160 // Generate xor parity for this number of sources
161 xor_gen_base(j
, TEST_LEN
, buffs
);
163 // Set errors up in each source and len position
164 for (i
= 0; i
< j
; i
++) {
165 for (k
= 0; k
< TEST_LEN
; k
++) {
166 // See if it still passes
167 ret
= xor_check(j
, TEST_LEN
, buffs
);
168 if (ret
!= 0) { // Should pass
169 printf("\nfail rand test %d sources\n", j
);
174 c
= ((char *)buffs
[i
])[k
];
175 ((char *)buffs
[i
])[k
] = c
^ 1; // corrupt buffer
177 ret
= xor_check(j
, TEST_LEN
, buffs
);
178 if (ret
== 0) { // Should fail
180 ("\nfail rand test corrupted buffer %d sources\n",
185 ((char *)buffs
[i
])[k
] = c
; // un-corrupt buffer
193 // Test various number of sources and len
195 while (k
<= TEST_LEN
) {
196 for (j
= 3; j
<= TEST_SOURCES
+ 1; j
++) {
197 for (i
= 0; i
< j
; i
++)
198 rand_buffer(buffs
[i
], k
);
200 // Generate xor parity for this number of sources
201 xor_gen_base(j
, k
, buffs
);
203 // Inject errors at various source and len positions
204 for (lerr
= 0; lerr
< k
; lerr
+= 10) {
205 for (serr
= 0; serr
< j
; serr
++) {
207 // See if it still passes
208 ret
= xor_check(j
, k
, buffs
);
209 if (ret
!= 0) { // Should pass
210 printf("\nfail rand test %d sources\n", j
);
215 c
= ((char *)buffs
[serr
])[lerr
];
216 ((char *)buffs
[serr
])[lerr
] = c
^ 1; // corrupt buffer
218 ret
= xor_check(j
, k
, buffs
);
219 if (ret
== 0) { // Should fail
220 printf("\nfail rand test corrupted buffer "
221 "%d sources, len=%d, ret=%d\n", j
, k
,
226 ((char *)buffs
[serr
])[lerr
] = c
; // un-corrupt buffer
235 // Test at the end of buffer
236 for (i
= 0; i
< TEST_LEN
; i
+= 32) {
237 for (j
= 0; j
< TEST_SOURCES
+ 1; j
++) {
238 rand_buffer(buffs
[j
], TEST_LEN
- i
);
239 tmp_buf
[j
] = (char *)buffs
[j
] + i
;
242 xor_gen_base(TEST_SOURCES
+ 1, TEST_LEN
- i
, (void *)tmp_buf
);
245 ret
= xor_check(TEST_SOURCES
+ 1, TEST_LEN
- i
, (void *)tmp_buf
);
247 printf("fail end test - offset: %d, len: %d\n", i
, TEST_LEN
- i
);
252 for (serr
= 0; serr
< TEST_SOURCES
+ 1; serr
++) {
253 for (lerr
= 0; lerr
< (TEST_LEN
- i
); lerr
++) {
254 c
= tmp_buf
[serr
][lerr
];
255 tmp_buf
[serr
][lerr
] = c
^ 1;
258 xor_check(TEST_SOURCES
+ 1, TEST_LEN
- i
, (void *)tmp_buf
);
260 printf("fail end test corrupted buffer - "
261 "offset: %d, len: %d, ret: %d\n", i
,
267 tmp_buf
[serr
][lerr
] = c
;