1/*
2 * This testing program makes sure the badblocks implementation works.
3 *
4 * Copyright (C) 1996 by Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
9 * %End-Header%
10 */
11
12#include <stdio.h>
13#include <string.h>
14#if HAVE_UNISTD_H
15#include <unistd.h>
16#endif
17#include <fcntl.h>
18#include <time.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#if HAVE_ERRNO_H
22#include <errno.h>
23#endif
24
25#include "ext2_fs.h"
26#include "ext2fs.h"
27
28#define ADD_BLK	0x0001
29#define DEL_BLK	0x0002
30
31blk_t test1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0 };
32blk_t test2[] = { 11, 10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 1 };
33blk_t test3[] = { 3, 1, 4, 5, 9, 2, 7, 10, 5, 6, 10, 8, 0 };
34blk_t test4[] = { 20, 50, 12, 17, 13, 2, 66, 23, 56, 0 };
35blk_t test4a[] = {
36 	20, 1,
37	50, 1,
38	3, 0,
39	17, 1,
40	18, 0,
41	16, 0,
42	11, 0,
43	12, 1,
44	13, 1,
45	14, 0,
46	80, 0,
47	45, 0,
48	66, 1,
49	0 };
50blk_t test5[] = { 31, 20, 17, 51, 23, 1, 56, 57, 0 };
51blk_t test5a[] = {
52	50, ADD_BLK,
53	51, DEL_BLK,
54	57, DEL_BLK,
55	66, ADD_BLK,
56	31, DEL_BLK,
57	12, ADD_BLK,
58	2, ADD_BLK,
59	13, ADD_BLK,
60	1, DEL_BLK,
61	0
62	};
63
64
65static int test_fail = 0;
66static int test_expected_fail = 0;
67
68static errcode_t create_test_list(blk_t *vec, badblocks_list *ret)
69{
70	errcode_t	retval;
71	badblocks_list	bb;
72	int		i;
73
74	retval = ext2fs_badblocks_list_create(&bb, 5);
75	if (retval) {
76		com_err("create_test_list", retval, "while creating list");
77		return retval;
78	}
79	for (i=0; vec[i]; i++) {
80		retval = ext2fs_badblocks_list_add(bb, vec[i]);
81		if (retval) {
82			com_err("create_test_list", retval,
83				"while adding test vector %d", i);
84			ext2fs_badblocks_list_free(bb);
85			return retval;
86		}
87	}
88	*ret = bb;
89	return 0;
90}
91
92static void print_list(badblocks_list bb, int verify)
93{
94	errcode_t	retval;
95	badblocks_iterate	iter;
96	blk_t			blk;
97	int			i, ok;
98
99	retval = ext2fs_badblocks_list_iterate_begin(bb, &iter);
100	if (retval) {
101		com_err("print_list", retval, "while setting up iterator");
102		return;
103	}
104	ok = i = 1;
105	while (ext2fs_badblocks_list_iterate(iter, &blk)) {
106		printf("%u ", blk);
107		if (i++ != blk)
108			ok = 0;
109	}
110	ext2fs_badblocks_list_iterate_end(iter);
111	if (verify) {
112		if (ok)
113			printf("--- OK");
114		else {
115			printf("--- NOT OK");
116			test_fail++;
117		}
118	}
119}
120
121static void validate_test_seq(badblocks_list bb, blk_t *vec)
122{
123	int	i, match, ok;
124
125	for (i = 0; vec[i]; i += 2) {
126		match = ext2fs_badblocks_list_test(bb, vec[i]);
127		if (match == vec[i+1])
128			ok = 1;
129		else {
130			ok = 0;
131			test_fail++;
132		}
133		printf("\tblock %u is %s --- %s\n", vec[i],
134		       match ? "present" : "absent",
135		       ok ? "OK" : "NOT OK");
136	}
137}
138
139static void do_test_seq(badblocks_list bb, blk_t *vec)
140{
141	int	i, match;
142
143	for (i = 0; vec[i]; i += 2) {
144		switch (vec[i+1]) {
145		case ADD_BLK:
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");
150			if (!match) {
151				printf("FAILURE!\n");
152				test_fail++;
153			}
154			break;
155		case DEL_BLK:
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");
161			if (match) {
162				printf("FAILURE!\n");
163				test_fail++;
164			}
165			break;
166		}
167	}
168}
169
170
171int file_test(badblocks_list bb)
172{
173	badblocks_list new_bb = 0;
174	errcode_t	retval;
175	FILE	*f;
176
177	f = tmpfile();
178	if (!f) {
179		fprintf(stderr, "Error opening temp file: %s\n",
180			error_message(errno));
181		return 1;
182	}
183	retval = ext2fs_write_bb_FILE(bb, 0, f);
184	if (retval) {
185		com_err("file_test", retval, "while writing bad blocks");
186		return 1;
187	}
188
189	rewind(f);
190	retval = ext2fs_read_bb_FILE2(0, f, &new_bb, 0, 0);
191	if (retval) {
192		com_err("file_test", retval, "while reading bad blocks");
193		return 1;
194	}
195	fclose(f);
196
197	if (ext2fs_badblocks_equal(bb, new_bb)) {
198		printf("Block bitmap matched after reading and writing.\n");
199	} else {
200		printf("Block bitmap NOT matched.\n");
201		test_fail++;
202	}
203	return 0;
204}
205
206static void invalid_proc(ext2_filsys fs, blk_t blk)
207{
208	if (blk == 34500) {
209		printf("Expected invalid block\n");
210		test_expected_fail++;
211	} else {
212		printf("Invalid block #: %u\n", blk);
213		test_fail++;
214	}
215}
216
217int file_test_invalid(badblocks_list bb)
218{
219	badblocks_list new_bb = 0;
220	errcode_t	retval;
221	ext2_filsys 	fs;
222	FILE	*f;
223
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);
231
232	f = tmpfile();
233	if (!f) {
234		fprintf(stderr, "Error opening temp file: %s\n",
235			error_message(errno));
236		return 1;
237	}
238	retval = ext2fs_write_bb_FILE(bb, 0, f);
239	if (retval) {
240		com_err("file_test", retval, "while writing bad blocks");
241		return 1;
242	}
243	fprintf(f, "34500\n");
244
245	rewind(f);
246	test_expected_fail = 0;
247	retval = ext2fs_read_bb_FILE(fs, f, &new_bb, invalid_proc);
248	if (retval) {
249		com_err("file_test", retval, "while reading bad blocks");
250		return 1;
251	}
252	fclose(f);
253	if (!test_expected_fail) {
254		printf("Expected test failure didn't happen!\n");
255		test_fail++;
256	}
257
258
259	if (ext2fs_badblocks_equal(bb, new_bb)) {
260		printf("Block bitmap matched after reading and writing.\n");
261	} else {
262		printf("Block bitmap NOT matched.\n");
263		test_fail++;
264	}
265	return 0;
266}
267
268int main(int argc, char **argv)
269{
270	badblocks_list bb1, bb2, bb3, bb4, bb5;
271	int	equal;
272	errcode_t	retval;
273
274	add_error_table(&et_ext2_error_table);
275
276	bb1 = bb2 = bb3 = bb4 = bb5 = 0;
277
278	printf("test1: ");
279	retval = create_test_list(test1, &bb1);
280	if (retval == 0)
281		print_list(bb1, 1);
282	printf("\n");
283
284	printf("test2: ");
285	retval = create_test_list(test2, &bb2);
286	if (retval == 0)
287		print_list(bb2, 1);
288	printf("\n");
289
290	printf("test3: ");
291	retval = create_test_list(test3, &bb3);
292	if (retval == 0)
293		print_list(bb3, 1);
294	printf("\n");
295
296	printf("test4: ");
297	retval = create_test_list(test4, &bb4);
298	if (retval == 0) {
299		print_list(bb4, 0);
300		printf("\n");
301		validate_test_seq(bb4, test4a);
302	}
303	printf("\n");
304
305	printf("test5: ");
306	retval = create_test_list(test5, &bb5);
307	if (retval == 0) {
308		print_list(bb5, 0);
309		printf("\n");
310		do_test_seq(bb5, test5a);
311		printf("After test5 sequence: ");
312		print_list(bb5, 0);
313		printf("\n");
314	}
315	printf("\n");
316
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 ");
321		if (equal)
322			test_fail++;
323
324		equal = ext2fs_badblocks_equal(bb1, bb3);
325		printf("bb1 and bb3 are %sequal.\n", equal ? "" : "NOT ");
326		if (!equal)
327			test_fail++;
328
329		equal = ext2fs_badblocks_equal(bb1, bb4);
330		printf("bb1 and bb4 are %sequal.\n", equal ? "" : "NOT ");
331		if (equal)
332			test_fail++;
333
334		equal = ext2fs_badblocks_equal(bb4, bb5);
335		printf("bb4 and bb5 are %sequal.\n", equal ? "" : "NOT ");
336		if (!equal)
337			test_fail++;
338		printf("\n");
339	}
340
341	file_test(bb4);
342
343	file_test_invalid(bb4);
344
345	if (test_fail == 0)
346		printf("ext2fs library badblocks tests checks out OK!\n");
347
348	if (bb1)
349		ext2fs_badblocks_list_free(bb1);
350	if (bb2)
351		ext2fs_badblocks_list_free(bb2);
352	if (bb3)
353		ext2fs_badblocks_list_free(bb3);
354	if (bb4)
355		ext2fs_badblocks_list_free(bb4);
356
357	return test_fail;
358
359}
360