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 blk_t test1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0 };
29 blk_t test2[] = { 11, 10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 1 };
30 blk_t test3[] = { 3, 1, 4, 5, 9, 2, 7, 10, 5, 6, 10, 8, 0 };
31 blk_t test4[] = { 20, 50, 12, 17, 13, 2, 66, 23, 56, 0 };
48 static int test_fail = 0;
50 static errcode_t create_test_list(blk_t *vec, badblocks_list *ret)
56 retval = ext2fs_badblocks_list_create(&bb, 5);
58 com_err("create_test_list", retval, "while creating list");
61 for (i=0; vec[i]; i++) {
62 retval = ext2fs_badblocks_list_add(bb, vec[i]);
64 com_err("create_test_list", retval,
65 "while adding test vector %d", i);
66 ext2fs_badblocks_list_free(bb);
74 static void print_list(badblocks_list bb, int verify)
77 badblocks_iterate iter;
81 retval = ext2fs_badblocks_list_iterate_begin(bb, &iter);
83 com_err("print_list", retval, "while setting up iterator");
87 while (ext2fs_badblocks_list_iterate(iter, &blk)) {
92 ext2fs_badblocks_list_iterate_end(iter);
103 static void validate_test_seq(badblocks_list bb, blk_t *vec)
107 for (i = 0; vec[i]; i += 2) {
108 match = ext2fs_badblocks_list_test(bb, vec[i]);
109 if (match == vec[i+1])
115 printf("\tblock %d is %s --- %s\n", vec[i],
116 match ? "present" : "absent",
117 ok ? "OK" : "NOT OK");
121 int file_test(badblocks_list bb)
123 char tmp_filename[20] = "#testXXXXXX";
124 badblocks_list new_bb = 0;
128 mktemp(tmp_filename);
130 unlink(tmp_filename);
131 f = fopen(tmp_filename, "w");
133 fprintf(stderr, "Error opening temp file %s: %s\n",
134 tmp_filename, error_message(errno));
137 retval = ext2fs_write_bb_FILE(bb, 0, f);
139 com_err("file_test", retval, "while writing bad blocks");
144 f = fopen(tmp_filename, "r");
146 fprintf(stderr, "Error re-opening temp file %s: %s\n",
147 tmp_filename, error_message(errno));
150 retval = ext2fs_read_bb_FILE2(0, f, &new_bb, 0, 0);
152 com_err("file_test", retval, "while reading bad blocks");
157 if (ext2fs_badblocks_equal(bb, new_bb)) {
158 printf("Block bitmap matched after reading and writing.\n");
160 printf("Block bitmap NOT matched.\n");
167 int main(int argc, char **argv)
169 badblocks_list bb1, bb2, bb3, bb4;
173 bb1 = bb2 = bb3 = bb4 = 0;
176 retval = create_test_list(test1, &bb1);
182 retval = create_test_list(test2, &bb2);
188 retval = create_test_list(test3, &bb3);
194 retval = create_test_list(test4, &bb4);
198 validate_test_seq(bb4, test4a);
202 if (bb1 && bb2 && bb3 && bb4) {
203 printf("Comparison tests:\n");
204 equal = ext2fs_badblocks_equal(bb1, bb2);
205 printf("bb1 and bb2 are %sequal.\n", equal ? "" : "NOT ");
209 equal = ext2fs_badblocks_equal(bb1, bb3);
210 printf("bb1 and bb3 are %sequal.\n", equal ? "" : "NOT ");
214 equal = ext2fs_badblocks_equal(bb1, bb4);
215 printf("bb1 and bb4 are %sequal.\n", equal ? "" : "NOT ");
222 printf("ext2fs library badblocks tests checks out OK!\n");
227 ext2fs_badblocks_list_free(bb1);
229 ext2fs_badblocks_list_free(bb2);
231 ext2fs_badblocks_list_free(bb3);
233 ext2fs_badblocks_list_free(bb4);