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.
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;
66 static int test_expected_fail = 0;
68 static errcode_t create_test_list(blk_t *vec, badblocks_list *ret)
74 retval = ext2fs_badblocks_list_create(&bb, 5);
76 com_err("create_test_list", retval, "while creating list");
79 for (i=0; vec[i]; i++) {
80 retval = ext2fs_badblocks_list_add(bb, vec[i]);
82 com_err("create_test_list", retval,
83 "while adding test vector %d", i);
84 ext2fs_badblocks_list_free(bb);
92 static void print_list(badblocks_list bb, int verify)
95 badblocks_iterate iter;
99 retval = ext2fs_badblocks_list_iterate_begin(bb, &iter);
101 com_err("print_list", retval, "while setting up iterator");
105 while (ext2fs_badblocks_list_iterate(iter, &blk)) {
110 ext2fs_badblocks_list_iterate_end(iter);
115 printf("--- NOT OK");
121 static void validate_test_seq(badblocks_list bb, blk_t *vec)
125 for (i = 0; vec[i]; i += 2) {
126 match = ext2fs_badblocks_list_test(bb, vec[i]);
127 if (match == vec[i+1])
133 printf("\tblock %u is %s --- %s\n", vec[i],
134 match ? "present" : "absent",
135 ok ? "OK" : "NOT OK");
139 static void do_test_seq(badblocks_list bb, blk_t *vec)
143 for (i = 0; vec[i]; i += 2) {
146 ext2fs_badblocks_list_add(bb, vec[i]);
147 match = ext2fs_badblocks_list_test(bb, vec[i]);
148 printf("Adding block %u --- now %s\n", vec[i],
149 match ? "present" : "absent");
151 printf("FAILURE!\n");
156 ext2fs_badblocks_list_del(bb, vec[i]);
157 match = ext2fs_badblocks_list_test(bb, vec[i]);
158 printf("Removing block %u --- now %s\n", vec[i],
159 ext2fs_badblocks_list_test(bb, vec[i]) ?
160 "present" : "absent");
162 printf("FAILURE!\n");
171 int file_test(badblocks_list bb)
173 badblocks_list new_bb = 0;
179 fprintf(stderr, "Error opening temp file: %s\n",
180 error_message(errno));
183 retval = ext2fs_write_bb_FILE(bb, 0, f);
185 com_err("file_test", retval, "while writing bad blocks");
190 retval = ext2fs_read_bb_FILE2(0, f, &new_bb, 0, 0);
192 com_err("file_test", retval, "while reading bad blocks");
197 if (ext2fs_badblocks_equal(bb, new_bb)) {
198 printf("Block bitmap matched after reading and writing.\n");
200 printf("Block bitmap NOT matched.\n");
206 static void invalid_proc(ext2_filsys fs, blk_t blk)
209 printf("Expected invalid block\n");
210 test_expected_fail++;
212 printf("Invalid block #: %u\n", blk);
217 int file_test_invalid(badblocks_list bb)
219 badblocks_list new_bb = 0;
224 fs = malloc(sizeof(struct struct_ext2_filsys));
225 memset(fs, 0, sizeof(struct struct_ext2_filsys));
226 fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
227 fs->super = malloc(SUPERBLOCK_SIZE);
228 memset(fs->super, 0, SUPERBLOCK_SIZE);
229 fs->super->s_first_data_block = 1;
230 ext2fs_blocks_count_set(fs->super, 100);
234 fprintf(stderr, "Error opening temp file: %s\n",
235 error_message(errno));
238 retval = ext2fs_write_bb_FILE(bb, 0, f);
240 com_err("file_test", retval, "while writing bad blocks");
243 fprintf(f, "34500\n");
246 test_expected_fail = 0;
247 retval = ext2fs_read_bb_FILE(fs, f, &new_bb, invalid_proc);
249 com_err("file_test", retval, "while reading bad blocks");
253 if (!test_expected_fail) {
254 printf("Expected test failure didn't happen!\n");
259 if (ext2fs_badblocks_equal(bb, new_bb)) {
260 printf("Block bitmap matched after reading and writing.\n");
262 printf("Block bitmap NOT matched.\n");
268 int main(int argc, char **argv)
270 badblocks_list bb1, bb2, bb3, bb4, bb5;
274 add_error_table(&et_ext2_error_table);
276 bb1 = bb2 = bb3 = bb4 = bb5 = 0;
279 retval = create_test_list(test1, &bb1);
285 retval = create_test_list(test2, &bb2);
291 retval = create_test_list(test3, &bb3);
297 retval = create_test_list(test4, &bb4);
301 validate_test_seq(bb4, test4a);
306 retval = create_test_list(test5, &bb5);
310 do_test_seq(bb5, test5a);
311 printf("After test5 sequence: ");
317 if (bb1 && bb2 && bb3 && bb4 && bb5) {
318 printf("Comparison tests:\n");
319 equal = ext2fs_badblocks_equal(bb1, bb2);
320 printf("bb1 and bb2 are %sequal.\n", equal ? "" : "NOT ");
324 equal = ext2fs_badblocks_equal(bb1, bb3);
325 printf("bb1 and bb3 are %sequal.\n", equal ? "" : "NOT ");
329 equal = ext2fs_badblocks_equal(bb1, bb4);
330 printf("bb1 and bb4 are %sequal.\n", equal ? "" : "NOT ");
334 equal = ext2fs_badblocks_equal(bb4, bb5);
335 printf("bb4 and bb5 are %sequal.\n", equal ? "" : "NOT ");
343 file_test_invalid(bb4);
346 printf("ext2fs library badblocks tests checks out OK!\n");
349 ext2fs_badblocks_list_free(bb1);
351 ext2fs_badblocks_list_free(bb2);
353 ext2fs_badblocks_list_free(bb3);
355 ext2fs_badblocks_list_free(bb4);