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 "config.h"
13#include <stdio.h>
14#include <string.h>
15#if HAVE_UNISTD_H
16#include <unistd.h>
17#endif
18#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
26#include "ext2_fs.h"
27#include "ext2fs.h"
28
29#define ADD_BLK	0x0001
30#define DEL_BLK	0x0002
31
32blk_t test1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0 };
33blk_t test2[] = { 11, 10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 1, 0 };
34blk_t test3[] = { 3, 1, 4, 5, 9, 2, 7, 10, 5, 6, 10, 8, 0 };
35blk_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,
46	14, 0,
47	80, 0,
48	45, 0,
49	66, 1,
50	0 };
51blk_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	};
64
65
66static int test_fail = 0;
67static int test_expected_fail = 0;
68
69static errcode_t create_test_list(blk_t *vec, badblocks_list *ret)
70{
71	errcode_t	retval;
72	badblocks_list	bb;
73	int		i;
74
75	retval = ext2fs_badblocks_list_create(&bb, 5);
76	if (retval) {
77		com_err("create_test_list", retval, "while creating list");
78		return retval;
79	}
80	for (i=0; vec[i]; i++) {
81		retval = ext2fs_badblocks_list_add(bb, vec[i]);
82		if (retval) {
83			com_err("create_test_list", retval,
84				"while adding test vector %d", i);
85			ext2fs_badblocks_list_free(bb);
86			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;
99
100	retval = ext2fs_badblocks_list_iterate_begin(bb, &iter);
101	if (retval) {
102		com_err("print_list", retval, "while setting up iterator");
103		return;
104	}
105	ok = i = 1;
106	while (ext2fs_badblocks_list_iterate(iter, &blk)) {
107		printf("%u ", blk);
108		if (i++ != blk)
109			ok = 0;
110	}
111	ext2fs_badblocks_list_iterate_end(iter);
112	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) {
127		match = ext2fs_badblocks_list_test(bb, vec[i]);
128		if (match == vec[i+1])
129			ok = 1;
130		else {
131			ok = 0;
132			test_fail++;
133		}
134		printf("\tblock %u is %s --- %s\n", vec[i],
135		       match ? "present" : "absent",
136		       ok ? "OK" : "NOT OK");
137	}
138}
139
140static 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]);
149			printf("Adding block %u --- now %s\n", vec[i],
150			       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]);
159			printf("Removing block %u --- now %s\n", vec[i],
160			       ext2fs_badblocks_list_test(bb, vec[i]) ?
161			       "present" : "absent");
162			if (match) {
163				printf("FAILURE!\n");
164				test_fail++;
165			}
166			break;
167		}
168	}
169}
170
171
172int file_test(badblocks_list bb)
173{
174	badblocks_list new_bb = 0;
175	errcode_t	retval;
176	FILE	*f;
177
178	f = tmpfile();
179	if (!f) {
180		fprintf(stderr, "Error opening temp file: %s\n",
181			error_message(errno));
182		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	}
189
190	rewind(f);
191	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	}
204	return 0;
205}
206
207static 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 {
213		printf("Invalid block #: %u\n", blk);
214		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;
231	ext2fs_blocks_count_set(fs->super, 100);
232
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	}
258
259
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}
268
269int main(int argc, char **argv)
270{
271	badblocks_list bb1, bb2, bb3, bb4, bb5;
272	int	equal;
273	errcode_t	retval;
274
275	add_error_table(&et_ext2_error_table);
276
277	bb1 = bb2 = bb3 = bb4 = bb5 = 0;
278
279	printf("test1: ");
280	retval = create_test_list(test1, &bb1);
281	if (retval == 0)
282		print_list(bb1, 1);
283	printf("\n");
284
285	printf("test2: ");
286	retval = create_test_list(test2, &bb2);
287	if (retval == 0)
288		print_list(bb2, 1);
289	printf("\n");
290
291	printf("test3: ");
292	retval = create_test_list(test3, &bb3);
293	if (retval == 0)
294		print_list(bb3, 1);
295	printf("\n");
296
297	printf("test4: ");
298	retval = create_test_list(test4, &bb4);
299	if (retval == 0) {
300		print_list(bb4, 0);
301		printf("\n");
302		validate_test_seq(bb4, test4a);
303	}
304	printf("\n");
305
306	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) {
319		printf("Comparison tests:\n");
320		equal = ext2fs_badblocks_equal(bb1, bb2);
321		printf("bb1 and bb2 are %sequal.\n", equal ? "" : "NOT ");
322		if (equal)
323			test_fail++;
324
325		equal = ext2fs_badblocks_equal(bb1, bb3);
326		printf("bb1 and bb3 are %sequal.\n", equal ? "" : "NOT ");
327		if (!equal)
328			test_fail++;
329
330		equal = ext2fs_badblocks_equal(bb1, bb4);
331		printf("bb1 and bb4 are %sequal.\n", equal ? "" : "NOT ");
332		if (equal)
333			test_fail++;
334
335		equal = ext2fs_badblocks_equal(bb4, bb5);
336		printf("bb4 and bb5 are %sequal.\n", equal ? "" : "NOT ");
337		if (!equal)
338			test_fail++;
339		printf("\n");
340	}
341
342	file_test(bb4);
343
344	file_test_invalid(bb4);
345
346	if (test_fail == 0)
347		printf("ext2fs library badblocks tests checks out OK!\n");
348
349	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
358	return test_fail;
359
360}
361