2 * This testing program makes sure the badblocks implementation works.
4 * Copyright (C) 1996 by Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
21 #include <sys/types.h>
29 #define ADD_BLK 0x0001
30 #define DEL_BLK 0x0002
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 };
51 blk_t test5
[] = { 31, 20, 17, 51, 23, 1, 56, 57, 0 };
66 static int test_fail
= 0;
67 static int test_expected_fail
= 0;
69 static errcode_t
create_test_list(blk_t
*vec
, badblocks_list
*ret
)
75 retval
= ext2fs_badblocks_list_create(&bb
, 5);
77 com_err("create_test_list", retval
, "while creating list");
80 for (i
=0; vec
[i
]; i
++) {
81 retval
= ext2fs_badblocks_list_add(bb
, vec
[i
]);
83 com_err("create_test_list", retval
,
84 "while adding test vector %d", i
);
85 ext2fs_badblocks_list_free(bb
);
93 static void print_list(badblocks_list bb
, int verify
)
96 badblocks_iterate iter
;
100 retval
= ext2fs_badblocks_list_iterate_begin(bb
, &iter
);
102 com_err("print_list", retval
, "while setting up iterator");
106 while (ext2fs_badblocks_list_iterate(iter
, &blk
)) {
111 ext2fs_badblocks_list_iterate_end(iter
);
116 printf("--- NOT OK");
122 static void validate_test_seq(badblocks_list bb
, blk_t
*vec
)
126 for (i
= 0; vec
[i
]; i
+= 2) {
127 match
= ext2fs_badblocks_list_test(bb
, vec
[i
]);
128 if (match
== vec
[i
+1])
134 printf("\tblock %u is %s --- %s\n", vec
[i
],
135 match
? "present" : "absent",
136 ok
? "OK" : "NOT OK");
140 static void do_test_seq(badblocks_list bb
, blk_t
*vec
)
144 for (i
= 0; vec
[i
]; i
+= 2) {
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");
152 printf("FAILURE!\n");
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");
163 printf("FAILURE!\n");
172 int file_test(badblocks_list bb
)
174 badblocks_list new_bb
= 0;
180 fprintf(stderr
, "Error opening temp file: %s\n",
181 error_message(errno
));
184 retval
= ext2fs_write_bb_FILE(bb
, 0, f
);
186 com_err("file_test", retval
, "while writing bad blocks");
191 retval
= ext2fs_read_bb_FILE2(0, f
, &new_bb
, 0, 0);
193 com_err("file_test", retval
, "while reading bad blocks");
198 if (ext2fs_badblocks_equal(bb
, new_bb
)) {
199 printf("Block bitmap matched after reading and writing.\n");
201 printf("Block bitmap NOT matched.\n");
204 ext2fs_badblocks_list_free(new_bb
);
208 static void invalid_proc(ext2_filsys fs
, blk_t blk
)
211 printf("Expected invalid block\n");
212 test_expected_fail
++;
214 printf("Invalid block #: %u\n", blk
);
219 void file_test_invalid(badblocks_list bb
)
221 badblocks_list new_bb
= 0;
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);
236 fprintf(stderr
, "Error opening temp file: %s\n",
237 error_message(errno
));
241 retval
= ext2fs_write_bb_FILE(bb
, 0, f
);
243 com_err("file_test", retval
, "while writing bad blocks");
247 fprintf(f
, "34500\n");
250 test_expected_fail
= 0;
251 retval
= ext2fs_read_bb_FILE(fs
, f
, &new_bb
, invalid_proc
);
253 com_err("file_test", retval
, "while reading bad blocks");
258 if (!test_expected_fail
) {
259 printf("Expected test failure didn't happen!\n");
264 if (ext2fs_badblocks_equal(bb
, new_bb
)) {
265 printf("Block bitmap matched after reading and writing.\n");
267 printf("Block bitmap NOT matched.\n");
270 ext2fs_badblocks_list_free(new_bb
);
276 int main(int argc
, char **argv
)
278 badblocks_list bb1
, bb2
, bb3
, bb4
, bb5
;
282 add_error_table(&et_ext2_error_table
);
284 bb1
= bb2
= bb3
= bb4
= bb5
= 0;
287 retval
= create_test_list(test1
, &bb1
);
293 retval
= create_test_list(test2
, &bb2
);
299 retval
= create_test_list(test3
, &bb3
);
305 retval
= create_test_list(test4
, &bb4
);
309 validate_test_seq(bb4
, test4a
);
314 retval
= create_test_list(test5
, &bb5
);
318 do_test_seq(bb5
, test5a
);
319 printf("After test5 sequence: ");
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 ");
332 equal
= ext2fs_badblocks_equal(bb1
, bb3
);
333 printf("bb1 and bb3 are %sequal.\n", equal
? "" : "NOT ");
337 equal
= ext2fs_badblocks_equal(bb1
, bb4
);
338 printf("bb1 and bb4 are %sequal.\n", equal
? "" : "NOT ");
342 equal
= ext2fs_badblocks_equal(bb4
, bb5
);
343 printf("bb4 and bb5 are %sequal.\n", equal
? "" : "NOT ");
351 file_test_invalid(bb4
);
354 printf("ext2fs library badblocks tests checks out OK!\n");
357 ext2fs_badblocks_list_free(bb1
);
359 ext2fs_badblocks_list_free(bb2
);
361 ext2fs_badblocks_list_free(bb3
);
363 ext2fs_badblocks_list_free(bb4
);
365 ext2fs_badblocks_list_free(bb5
);