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 Public
20 #include <sys/types.h>
28 #define ADD_BLK 0x0001
29 #define DEL_BLK 0x0002
31 blk_t test1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0 };
32 blk_t test2[] = { 11, 10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 1 };
33 blk_t test3[] = { 3, 1, 4, 5, 9, 2, 7, 10, 5, 6, 10, 8, 0 };
34 blk_t test4[] = { 20, 50, 12, 17, 13, 2, 66, 23, 56, 0 };
50 blk_t test5[] = { 31, 20, 17, 51, 23, 1, 56, 57, 0 };
65 static int test_fail = 0;
67 static errcode_t create_test_list(blk_t *vec, badblocks_list *ret)
73 retval = ext2fs_badblocks_list_create(&bb, 5);
75 com_err("create_test_list", retval, "while creating list");
78 for (i=0; vec[i]; i++) {
79 retval = ext2fs_badblocks_list_add(bb, vec[i]);
81 com_err("create_test_list", retval,
82 "while adding test vector %d", i);
83 ext2fs_badblocks_list_free(bb);
91 static void print_list(badblocks_list bb, int verify)
94 badblocks_iterate iter;
98 retval = ext2fs_badblocks_list_iterate_begin(bb, &iter);
100 com_err("print_list", retval, "while setting up iterator");
104 while (ext2fs_badblocks_list_iterate(iter, &blk)) {
109 ext2fs_badblocks_list_iterate_end(iter);
114 printf("--- NOT OK");
120 static void validate_test_seq(badblocks_list bb, blk_t *vec)
124 for (i = 0; vec[i]; i += 2) {
125 match = ext2fs_badblocks_list_test(bb, vec[i]);
126 if (match == vec[i+1])
132 printf("\tblock %d is %s --- %s\n", vec[i],
133 match ? "present" : "absent",
134 ok ? "OK" : "NOT OK");
138 static void do_test_seq(badblocks_list bb, blk_t *vec)
142 for (i = 0; vec[i]; i += 2) {
145 ext2fs_badblocks_list_add(bb, vec[i]);
146 match = ext2fs_badblocks_list_test(bb, vec[i]);
147 printf("Adding block %d --- now %s\n", vec[i],
148 match ? "present" : "absent");
150 printf("FAILURE!\n");
155 ext2fs_badblocks_list_del(bb, vec[i]);
156 match = ext2fs_badblocks_list_test(bb, vec[i]);
157 printf("Removing block %d --- now %s\n", vec[i],
158 ext2fs_badblocks_list_test(bb, vec[i]) ?
159 "present" : "absent");
161 printf("FAILURE!\n");
170 int file_test(badblocks_list bb)
172 badblocks_list new_bb = 0;
178 fprintf(stderr, "Error opening temp file: %s\n",
179 error_message(errno));
182 retval = ext2fs_write_bb_FILE(bb, 0, f);
184 com_err("file_test", retval, "while writing bad blocks");
189 retval = ext2fs_read_bb_FILE2(0, f, &new_bb, 0, 0);
191 com_err("file_test", retval, "while reading bad blocks");
196 if (ext2fs_badblocks_equal(bb, new_bb)) {
197 printf("Block bitmap matched after reading and writing.\n");
199 printf("Block bitmap NOT matched.\n");
206 int main(int argc, char **argv)
208 badblocks_list bb1, bb2, bb3, bb4, bb5;
212 bb1 = bb2 = bb3 = bb4 = bb5 = 0;
215 retval = create_test_list(test1, &bb1);
221 retval = create_test_list(test2, &bb2);
227 retval = create_test_list(test3, &bb3);
233 retval = create_test_list(test4, &bb4);
237 validate_test_seq(bb4, test4a);
242 retval = create_test_list(test5, &bb5);
246 do_test_seq(bb5, test5a);
247 printf("After test5 sequence: ");
253 if (bb1 && bb2 && bb3 && bb4 && bb5) {
254 printf("Comparison tests:\n");
255 equal = ext2fs_badblocks_equal(bb1, bb2);
256 printf("bb1 and bb2 are %sequal.\n", equal ? "" : "NOT ");
260 equal = ext2fs_badblocks_equal(bb1, bb3);
261 printf("bb1 and bb3 are %sequal.\n", equal ? "" : "NOT ");
265 equal = ext2fs_badblocks_equal(bb1, bb4);
266 printf("bb1 and bb4 are %sequal.\n", equal ? "" : "NOT ");
270 equal = ext2fs_badblocks_equal(bb4, bb5);
271 printf("bb4 and bb5 are %sequal.\n", equal ? "" : "NOT ");
280 printf("ext2fs library badblocks tests checks out OK!\n");
283 ext2fs_badblocks_list_free(bb1);
285 ext2fs_badblocks_list_free(bb2);
287 ext2fs_badblocks_list_free(bb3);
289 ext2fs_badblocks_list_free(bb4);