blob: 3b39ef134a744cb74379fb8bb7afaa0c5e5cdfbd [file] [log] [blame]
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001/*
2 * This testing program makes sure the badblocks implementation works.
3 *
4 * Copyright (C) 1996 by Theodore Ts'o.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04005 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00006 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * %End-Header%
10 */
11
Theodore Ts'od1154eb2011-09-18 17:34:37 -040012#include "config.h"
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000013#include <stdio.h>
14#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000015#if HAVE_UNISTD_H
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000016#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000017#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000018#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
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000026#include "ext2_fs.h"
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000027#include "ext2fs.h"
28
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -040029#define ADD_BLK 0x0001
30#define DEL_BLK 0x0002
31
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000032blk_t test1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0 };
Theodore Ts'o57dca852000-07-04 19:20:25 +000033blk_t test2[] = { 11, 10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 1 };
Theodore Ts'o9b9fe8a1999-11-08 22:05:04 +000034blk_t test3[] = { 3, 1, 4, 5, 9, 2, 7, 10, 5, 6, 10, 8, 0 };
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000035blk_t test4[] = { 20, 50, 12, 17, 13, 2, 66, 23, 56, 0 };
36blk_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,
Theodore Ts'oefc6f622008-08-27 23:07:54 -040046 14, 0,
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000047 80, 0,
48 45, 0,
49 66, 1,
50 0 };
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -040051blk_t test5[] = { 31, 20, 17, 51, 23, 1, 56, 57, 0 };
52blk_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 };
Theodore Ts'oefc6f622008-08-27 23:07:54 -040064
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000065
66static int test_fail = 0;
Theodore Ts'obc479522005-12-10 14:37:10 -050067static int test_expected_fail = 0;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000068
69static errcode_t create_test_list(blk_t *vec, badblocks_list *ret)
70{
71 errcode_t retval;
72 badblocks_list bb;
73 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040074
Theodore Ts'o9b9fe8a1999-11-08 22:05:04 +000075 retval = ext2fs_badblocks_list_create(&bb, 5);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000076 if (retval) {
77 com_err("create_test_list", retval, "while creating list");
78 return retval;
79 }
80 for (i=0; vec[i]; i++) {
Theodore Ts'o9b9fe8a1999-11-08 22:05:04 +000081 retval = ext2fs_badblocks_list_add(bb, vec[i]);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000082 if (retval) {
83 com_err("create_test_list", retval,
84 "while adding test vector %d", i);
Theodore Ts'o9b9fe8a1999-11-08 22:05:04 +000085 ext2fs_badblocks_list_free(bb);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000086 return retval;
87 }
88 }
89 *ret = bb;
90 return 0;
91}
92
93static 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;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040099
Theodore Ts'o9b9fe8a1999-11-08 22:05:04 +0000100 retval = ext2fs_badblocks_list_iterate_begin(bb, &iter);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000101 if (retval) {
102 com_err("print_list", retval, "while setting up iterator");
103 return;
104 }
105 ok = i = 1;
Theodore Ts'o9b9fe8a1999-11-08 22:05:04 +0000106 while (ext2fs_badblocks_list_iterate(iter, &blk)) {
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400107 printf("%u ", blk);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000108 if (i++ != blk)
109 ok = 0;
110 }
Theodore Ts'o9b9fe8a1999-11-08 22:05:04 +0000111 ext2fs_badblocks_list_iterate_end(iter);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000112 if (verify) {
113 if (ok)
114 printf("--- OK");
115 else {
116 printf("--- NOT OK");
117 test_fail++;
118 }
119 }
120}
121
122static 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) {
Theodore Ts'o9b9fe8a1999-11-08 22:05:04 +0000127 match = ext2fs_badblocks_list_test(bb, vec[i]);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000128 if (match == vec[i+1])
129 ok = 1;
130 else {
131 ok = 0;
132 test_fail++;
133 }
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400134 printf("\tblock %u is %s --- %s\n", vec[i],
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000135 match ? "present" : "absent",
136 ok ? "OK" : "NOT OK");
137 }
138}
139
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400140static 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]);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400149 printf("Adding block %u --- now %s\n", vec[i],
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400150 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]);
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400159 printf("Removing block %u --- now %s\n", vec[i],
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400160 ext2fs_badblocks_list_test(bb, vec[i]) ?
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400161 "present" : "absent");
162 if (match) {
163 printf("FAILURE!\n");
164 test_fail++;
165 }
166 break;
167 }
168 }
169}
170
171
Theodore Ts'o57dca852000-07-04 19:20:25 +0000172int file_test(badblocks_list bb)
173{
Theodore Ts'o57dca852000-07-04 19:20:25 +0000174 badblocks_list new_bb = 0;
175 errcode_t retval;
176 FILE *f;
177
Theodore Ts'o68dbdc22003-05-03 16:39:50 -0400178 f = tmpfile();
Theodore Ts'o57dca852000-07-04 19:20:25 +0000179 if (!f) {
Theodore Ts'o68dbdc22003-05-03 16:39:50 -0400180 fprintf(stderr, "Error opening temp file: %s\n",
181 error_message(errno));
Theodore Ts'o57dca852000-07-04 19:20:25 +0000182 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 }
Theodore Ts'o57dca852000-07-04 19:20:25 +0000189
Theodore Ts'o68dbdc22003-05-03 16:39:50 -0400190 rewind(f);
Theodore Ts'o57dca852000-07-04 19:20:25 +0000191 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 }
Theodore Ts'o546a1ff2002-03-07 23:52:56 -0500204 return 0;
Theodore Ts'o57dca852000-07-04 19:20:25 +0000205}
206
Theodore Ts'obc479522005-12-10 14:37:10 -0500207static void invalid_proc(ext2_filsys fs, blk_t blk)
208{
209 if (blk == 34500) {
210 printf("Expected invalid block\n");
211 test_expected_fail++;
212 } else {
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400213 printf("Invalid block #: %u\n", blk);
Theodore Ts'obc479522005-12-10 14:37:10 -0500214 test_fail++;
215 }
216}
217
218int file_test_invalid(badblocks_list bb)
219{
220 badblocks_list new_bb = 0;
221 errcode_t retval;
222 ext2_filsys fs;
223 FILE *f;
224
225 fs = malloc(sizeof(struct struct_ext2_filsys));
226 memset(fs, 0, sizeof(struct struct_ext2_filsys));
227 fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
228 fs->super = malloc(SUPERBLOCK_SIZE);
229 memset(fs->super, 0, SUPERBLOCK_SIZE);
230 fs->super->s_first_data_block = 1;
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400231 ext2fs_blocks_count_set(fs->super, 100);
Theodore Ts'obc479522005-12-10 14:37:10 -0500232
233 f = tmpfile();
234 if (!f) {
235 fprintf(stderr, "Error opening temp file: %s\n",
236 error_message(errno));
237 return 1;
238 }
239 retval = ext2fs_write_bb_FILE(bb, 0, f);
240 if (retval) {
241 com_err("file_test", retval, "while writing bad blocks");
242 return 1;
243 }
244 fprintf(f, "34500\n");
245
246 rewind(f);
247 test_expected_fail = 0;
248 retval = ext2fs_read_bb_FILE(fs, f, &new_bb, invalid_proc);
249 if (retval) {
250 com_err("file_test", retval, "while reading bad blocks");
251 return 1;
252 }
253 fclose(f);
254 if (!test_expected_fail) {
255 printf("Expected test failure didn't happen!\n");
256 test_fail++;
257 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400258
Theodore Ts'obc479522005-12-10 14:37:10 -0500259
260 if (ext2fs_badblocks_equal(bb, new_bb)) {
261 printf("Block bitmap matched after reading and writing.\n");
262 } else {
263 printf("Block bitmap NOT matched.\n");
264 test_fail++;
265 }
266 return 0;
267}
Theodore Ts'o57dca852000-07-04 19:20:25 +0000268
Theodore Ts'o546a1ff2002-03-07 23:52:56 -0500269int main(int argc, char **argv)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000270{
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400271 badblocks_list bb1, bb2, bb3, bb4, bb5;
Theodore Ts'o57dca852000-07-04 19:20:25 +0000272 int equal;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000273 errcode_t retval;
274
Theodore Ts'o04f13d62009-09-08 21:33:03 -0400275 add_error_table(&et_ext2_error_table);
276
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400277 bb1 = bb2 = bb3 = bb4 = bb5 = 0;
Theodore Ts'o57dca852000-07-04 19:20:25 +0000278
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000279 printf("test1: ");
Theodore Ts'o57dca852000-07-04 19:20:25 +0000280 retval = create_test_list(test1, &bb1);
281 if (retval == 0)
282 print_list(bb1, 1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000283 printf("\n");
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400284
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000285 printf("test2: ");
Theodore Ts'o57dca852000-07-04 19:20:25 +0000286 retval = create_test_list(test2, &bb2);
287 if (retval == 0)
288 print_list(bb2, 1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000289 printf("\n");
290
291 printf("test3: ");
Theodore Ts'o57dca852000-07-04 19:20:25 +0000292 retval = create_test_list(test3, &bb3);
293 if (retval == 0)
294 print_list(bb3, 1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000295 printf("\n");
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400296
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000297 printf("test4: ");
Theodore Ts'o57dca852000-07-04 19:20:25 +0000298 retval = create_test_list(test4, &bb4);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000299 if (retval == 0) {
Theodore Ts'o57dca852000-07-04 19:20:25 +0000300 print_list(bb4, 0);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000301 printf("\n");
Theodore Ts'o57dca852000-07-04 19:20:25 +0000302 validate_test_seq(bb4, test4a);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000303 }
304 printf("\n");
Theodore Ts'o57dca852000-07-04 19:20:25 +0000305
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400306 printf("test5: ");
307 retval = create_test_list(test5, &bb5);
308 if (retval == 0) {
309 print_list(bb5, 0);
310 printf("\n");
311 do_test_seq(bb5, test5a);
312 printf("After test5 sequence: ");
313 print_list(bb5, 0);
314 printf("\n");
315 }
316 printf("\n");
317
318 if (bb1 && bb2 && bb3 && bb4 && bb5) {
Theodore Ts'o57dca852000-07-04 19:20:25 +0000319 printf("Comparison tests:\n");
320 equal = ext2fs_badblocks_equal(bb1, bb2);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400321 printf("bb1 and bb2 are %sequal.\n", equal ? "" : "NOT ");
Theodore Ts'o57dca852000-07-04 19:20:25 +0000322 if (equal)
323 test_fail++;
324
325 equal = ext2fs_badblocks_equal(bb1, bb3);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400326 printf("bb1 and bb3 are %sequal.\n", equal ? "" : "NOT ");
Theodore Ts'o57dca852000-07-04 19:20:25 +0000327 if (!equal)
328 test_fail++;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400329
Theodore Ts'o57dca852000-07-04 19:20:25 +0000330 equal = ext2fs_badblocks_equal(bb1, bb4);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400331 printf("bb1 and bb4 are %sequal.\n", equal ? "" : "NOT ");
Theodore Ts'o57dca852000-07-04 19:20:25 +0000332 if (equal)
333 test_fail++;
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400334
335 equal = ext2fs_badblocks_equal(bb4, bb5);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400336 printf("bb4 and bb5 are %sequal.\n", equal ? "" : "NOT ");
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400337 if (!equal)
338 test_fail++;
Theodore Ts'o57dca852000-07-04 19:20:25 +0000339 printf("\n");
340 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400341
Theodore Ts'o7d7bdd52003-06-24 17:34:02 -0400342 file_test(bb4);
Theodore Ts'obc479522005-12-10 14:37:10 -0500343
344 file_test_invalid(bb4);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400345
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000346 if (test_fail == 0)
347 printf("ext2fs library badblocks tests checks out OK!\n");
Theodore Ts'o57dca852000-07-04 19:20:25 +0000348
Theodore Ts'o57dca852000-07-04 19:20:25 +0000349 if (bb1)
350 ext2fs_badblocks_list_free(bb1);
351 if (bb2)
352 ext2fs_badblocks_list_free(bb2);
353 if (bb3)
354 ext2fs_badblocks_list_free(bb3);
355 if (bb4)
356 ext2fs_badblocks_list_free(bb4);
357
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000358 return test_fail;
Theodore Ts'o57dca852000-07-04 19:20:25 +0000359
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000360}