1 /*
2 * This testing program makes sure the badblocks implementation works.
3 *
4 * Copyright (C) 1996 by Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
9 * %End-Header%
10 */
11
12 #include "config.h"
13 #include <stdio.h>
14 #include <string.h>
15 #if HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #include <fcntl.h>
19 #include <time.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #if HAVE_ERRNO_H
23 #include <errno.h>
24 #endif
25
26 #include "ext2_fs.h"
27 #include "ext2fs.h"
28
29 #define ADD_BLK 0x0001
30 #define DEL_BLK 0x0002
31
32 blk_t test1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0 };
33 blk_t test2[] = { 11, 10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 1, 0 };
34 blk_t test3[] = { 3, 1, 4, 5, 9, 2, 7, 10, 5, 6, 10, 8, 0 };
35 blk_t test4[] = { 20, 50, 12, 17, 13, 2, 66, 23, 56, 0 };
36 blk_t test4a[] = {
37 20, 1,
38 50, 1,
39 3, 0,
40 17, 1,
41 18, 0,
42 16, 0,
43 11, 0,
44 12, 1,
45 13, 1,
46 14, 0,
47 80, 0,
48 45, 0,
49 66, 1,
50 0 };
51 blk_t test5[] = { 31, 20, 17, 51, 23, 1, 56, 57, 0 };
52 blk_t test5a[] = {
53 50, ADD_BLK,
54 51, DEL_BLK,
55 57, DEL_BLK,
56 66, ADD_BLK,
57 31, DEL_BLK,
58 12, ADD_BLK,
59 2, ADD_BLK,
60 13, ADD_BLK,
61 1, DEL_BLK,
62 0
63 };
64
65
66 static int test_fail = 0;
67 static int test_expected_fail = 0;
68
create_test_list(blk_t * vec,badblocks_list * ret)69 static errcode_t create_test_list(blk_t *vec, badblocks_list *ret)
70 {
71 errcode_t retval;
72 badblocks_list bb;
73 int i;
74
75 retval = ext2fs_badblocks_list_create(&bb, 5);
76 if (retval) {
77 com_err("create_test_list", retval, "while creating list");
78 return retval;
79 }
80 for (i=0; vec[i]; i++) {
81 retval = ext2fs_badblocks_list_add(bb, vec[i]);
82 if (retval) {
83 com_err("create_test_list", retval,
84 "while adding test vector %d", i);
85 ext2fs_badblocks_list_free(bb);
86 return retval;
87 }
88 }
89 *ret = bb;
90 return 0;
91 }
92
print_list(badblocks_list bb,int verify)93 static void print_list(badblocks_list bb, int verify)
94 {
95 errcode_t retval;
96 badblocks_iterate iter;
97 blk_t blk;
98 int i, ok;
99
100 retval = ext2fs_badblocks_list_iterate_begin(bb, &iter);
101 if (retval) {
102 com_err("print_list", retval, "while setting up iterator");
103 return;
104 }
105 ok = i = 1;
106 while (ext2fs_badblocks_list_iterate(iter, &blk)) {
107 printf("%u ", blk);
108 if (i++ != blk)
109 ok = 0;
110 }
111 ext2fs_badblocks_list_iterate_end(iter);
112 if (verify) {
113 if (ok)
114 printf("--- OK");
115 else {
116 printf("--- NOT OK");
117 test_fail++;
118 }
119 }
120 }
121
validate_test_seq(badblocks_list bb,blk_t * vec)122 static void validate_test_seq(badblocks_list bb, blk_t *vec)
123 {
124 int i, match, ok;
125
126 for (i = 0; vec[i]; i += 2) {
127 match = ext2fs_badblocks_list_test(bb, vec[i]);
128 if (match == vec[i+1])
129 ok = 1;
130 else {
131 ok = 0;
132 test_fail++;
133 }
134 printf("\tblock %u is %s --- %s\n", vec[i],
135 match ? "present" : "absent",
136 ok ? "OK" : "NOT OK");
137 }
138 }
139
do_test_seq(badblocks_list bb,blk_t * vec)140 static void do_test_seq(badblocks_list bb, blk_t *vec)
141 {
142 int i, match;
143
144 for (i = 0; vec[i]; i += 2) {
145 switch (vec[i+1]) {
146 case ADD_BLK:
147 ext2fs_badblocks_list_add(bb, vec[i]);
148 match = ext2fs_badblocks_list_test(bb, vec[i]);
149 printf("Adding block %u --- now %s\n", vec[i],
150 match ? "present" : "absent");
151 if (!match) {
152 printf("FAILURE!\n");
153 test_fail++;
154 }
155 break;
156 case DEL_BLK:
157 ext2fs_badblocks_list_del(bb, vec[i]);
158 match = ext2fs_badblocks_list_test(bb, vec[i]);
159 printf("Removing block %u --- now %s\n", vec[i],
160 ext2fs_badblocks_list_test(bb, vec[i]) ?
161 "present" : "absent");
162 if (match) {
163 printf("FAILURE!\n");
164 test_fail++;
165 }
166 break;
167 }
168 }
169 }
170
171
file_test(badblocks_list bb)172 int file_test(badblocks_list bb)
173 {
174 badblocks_list new_bb = 0;
175 errcode_t retval;
176 FILE *f;
177
178 f = tmpfile();
179 if (!f) {
180 fprintf(stderr, "Error opening temp file: %s\n",
181 error_message(errno));
182 return 1;
183 }
184 retval = ext2fs_write_bb_FILE(bb, 0, f);
185 if (retval) {
186 com_err("file_test", retval, "while writing bad blocks");
187 return 1;
188 }
189
190 rewind(f);
191 retval = ext2fs_read_bb_FILE2(0, f, &new_bb, 0, 0);
192 if (retval) {
193 com_err("file_test", retval, "while reading bad blocks");
194 return 1;
195 }
196 fclose(f);
197
198 if (ext2fs_badblocks_equal(bb, new_bb)) {
199 printf("Block bitmap matched after reading and writing.\n");
200 } else {
201 printf("Block bitmap NOT matched.\n");
202 test_fail++;
203 }
204 ext2fs_badblocks_list_free(new_bb);
205 return 0;
206 }
207
invalid_proc(ext2_filsys fs,blk_t blk)208 static void invalid_proc(ext2_filsys fs, blk_t blk)
209 {
210 if (blk == 34500) {
211 printf("Expected invalid block\n");
212 test_expected_fail++;
213 } else {
214 printf("Invalid block #: %u\n", blk);
215 test_fail++;
216 }
217 }
218
file_test_invalid(badblocks_list bb)219 void file_test_invalid(badblocks_list bb)
220 {
221 badblocks_list new_bb = 0;
222 errcode_t retval;
223 ext2_filsys fs;
224 FILE *f;
225
226 fs = malloc(sizeof(struct struct_ext2_filsys));
227 memset(fs, 0, sizeof(struct struct_ext2_filsys));
228 fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
229 fs->super = malloc(SUPERBLOCK_SIZE);
230 memset(fs->super, 0, SUPERBLOCK_SIZE);
231 fs->super->s_first_data_block = 1;
232 ext2fs_blocks_count_set(fs->super, 100);
233
234 f = tmpfile();
235 if (!f) {
236 fprintf(stderr, "Error opening temp file: %s\n",
237 error_message(errno));
238 test_fail++;
239 goto out;
240 }
241 retval = ext2fs_write_bb_FILE(bb, 0, f);
242 if (retval) {
243 com_err("file_test", retval, "while writing bad blocks");
244 test_fail++;
245 goto out;
246 }
247 fprintf(f, "34500\n");
248
249 rewind(f);
250 test_expected_fail = 0;
251 retval = ext2fs_read_bb_FILE(fs, f, &new_bb, invalid_proc);
252 if (retval) {
253 com_err("file_test", retval, "while reading bad blocks");
254 test_fail++;
255 goto out;
256 }
257 fclose(f);
258 if (!test_expected_fail) {
259 printf("Expected test failure didn't happen!\n");
260 test_fail++;
261 }
262
263
264 if (ext2fs_badblocks_equal(bb, new_bb)) {
265 printf("Block bitmap matched after reading and writing.\n");
266 } else {
267 printf("Block bitmap NOT matched.\n");
268 test_fail++;
269 }
270 ext2fs_badblocks_list_free(new_bb);
271 out:
272 free(fs->super);
273 free(fs);
274 }
275
main(int argc,char ** argv)276 int main(int argc, char **argv)
277 {
278 badblocks_list bb1, bb2, bb3, bb4, bb5;
279 int equal;
280 errcode_t retval;
281
282 add_error_table(&et_ext2_error_table);
283
284 bb1 = bb2 = bb3 = bb4 = bb5 = 0;
285
286 printf("test1: ");
287 retval = create_test_list(test1, &bb1);
288 if (retval == 0)
289 print_list(bb1, 1);
290 printf("\n");
291
292 printf("test2: ");
293 retval = create_test_list(test2, &bb2);
294 if (retval == 0)
295 print_list(bb2, 1);
296 printf("\n");
297
298 printf("test3: ");
299 retval = create_test_list(test3, &bb3);
300 if (retval == 0)
301 print_list(bb3, 1);
302 printf("\n");
303
304 printf("test4: ");
305 retval = create_test_list(test4, &bb4);
306 if (retval == 0) {
307 print_list(bb4, 0);
308 printf("\n");
309 validate_test_seq(bb4, test4a);
310 }
311 printf("\n");
312
313 printf("test5: ");
314 retval = create_test_list(test5, &bb5);
315 if (retval == 0) {
316 print_list(bb5, 0);
317 printf("\n");
318 do_test_seq(bb5, test5a);
319 printf("After test5 sequence: ");
320 print_list(bb5, 0);
321 printf("\n");
322 }
323 printf("\n");
324
325 if (bb1 && bb2 && bb3 && bb4 && bb5) {
326 printf("Comparison tests:\n");
327 equal = ext2fs_badblocks_equal(bb1, bb2);
328 printf("bb1 and bb2 are %sequal.\n", equal ? "" : "NOT ");
329 if (equal)
330 test_fail++;
331
332 equal = ext2fs_badblocks_equal(bb1, bb3);
333 printf("bb1 and bb3 are %sequal.\n", equal ? "" : "NOT ");
334 if (!equal)
335 test_fail++;
336
337 equal = ext2fs_badblocks_equal(bb1, bb4);
338 printf("bb1 and bb4 are %sequal.\n", equal ? "" : "NOT ");
339 if (equal)
340 test_fail++;
341
342 equal = ext2fs_badblocks_equal(bb4, bb5);
343 printf("bb4 and bb5 are %sequal.\n", equal ? "" : "NOT ");
344 if (!equal)
345 test_fail++;
346 printf("\n");
347 }
348
349 file_test(bb4);
350
351 file_test_invalid(bb4);
352
353 if (test_fail == 0)
354 printf("ext2fs library badblocks tests checks out OK!\n");
355
356 if (bb1)
357 ext2fs_badblocks_list_free(bb1);
358 if (bb2)
359 ext2fs_badblocks_list_free(bb2);
360 if (bb3)
361 ext2fs_badblocks_list_free(bb3);
362 if (bb4)
363 ext2fs_badblocks_list_free(bb4);
364 if (bb5)
365 ext2fs_badblocks_list_free(bb5);
366
367 return test_fail;
368
369 }
370