tst_badblocks.c revision 57dca85467cf3fc61565e916a5f2e35db8020d88
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 Public
8 * License.
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#if EXT2_FLAT_INCLUDES
26#include "ext2_fs.h"
27#else
28#include <linux/ext2_fs.h>
29#endif
30
31#include "ext2fs.h"
32
33blk_t test1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0 };
34blk_t test2[] = { 11, 10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 1 };
35blk_t test3[] = { 3, 1, 4, 5, 9, 2, 7, 10, 5, 6, 10, 8, 0 };
36blk_t test4[] = { 20, 50, 12, 17, 13, 2, 66, 23, 56, 0 };
37blk_t test4a[] = {
38 	20, 1,
39	50, 1,
40	3, 0,
41	17, 1,
42	18, 0,
43	16, 0,
44	11, 0,
45	12, 1,
46	13, 1,
47	14, 0,
48	80, 0,
49	45, 0,
50	66, 1,
51	0 };
52
53static int test_fail = 0;
54
55static errcode_t create_test_list(blk_t *vec, badblocks_list *ret)
56{
57	errcode_t	retval;
58	badblocks_list	bb;
59	int		i;
60
61	retval = ext2fs_badblocks_list_create(&bb, 5);
62	if (retval) {
63		com_err("create_test_list", retval, "while creating list");
64		return retval;
65	}
66	for (i=0; vec[i]; i++) {
67		retval = ext2fs_badblocks_list_add(bb, vec[i]);
68		if (retval) {
69			com_err("create_test_list", retval,
70				"while adding test vector %d", i);
71			ext2fs_badblocks_list_free(bb);
72			return retval;
73		}
74	}
75	*ret = bb;
76	return 0;
77}
78
79static void print_list(badblocks_list bb, int verify)
80{
81	errcode_t	retval;
82	badblocks_iterate	iter;
83	blk_t			blk;
84	int			i, ok;
85
86	retval = ext2fs_badblocks_list_iterate_begin(bb, &iter);
87	if (retval) {
88		com_err("print_list", retval, "while setting up iterator");
89		return;
90	}
91	ok = i = 1;
92	while (ext2fs_badblocks_list_iterate(iter, &blk)) {
93		printf("%d ", blk);
94		if (i++ != blk)
95			ok = 0;
96	}
97	ext2fs_badblocks_list_iterate_end(iter);
98	if (verify) {
99		if (ok)
100			printf("--- OK");
101		else {
102			printf("--- NOT OK");
103			test_fail++;
104		}
105	}
106}
107
108static void validate_test_seq(badblocks_list bb, blk_t *vec)
109{
110	int	i, match, ok;
111
112	for (i = 0; vec[i]; i += 2) {
113		match = ext2fs_badblocks_list_test(bb, vec[i]);
114		if (match == vec[i+1])
115			ok = 1;
116		else {
117			ok = 0;
118			test_fail++;
119		}
120		printf("\tblock %d is %s --- %s\n", vec[i],
121		       match ? "present" : "absent",
122		       ok ? "OK" : "NOT OK");
123	}
124}
125
126int file_test(badblocks_list bb)
127{
128	char	tmp_filename[20] = "#testXXXXXX";
129	badblocks_list new_bb = 0;
130	errcode_t	retval;
131	FILE	*f;
132
133	mktemp(tmp_filename);
134
135	unlink(tmp_filename);
136	f = fopen(tmp_filename, "w");
137	if (!f) {
138		fprintf(stderr, "Error opening temp file %s: %s\n",
139			tmp_filename, error_message(errno));
140		return 1;
141	}
142	retval = ext2fs_write_bb_FILE(bb, 0, f);
143	if (retval) {
144		com_err("file_test", retval, "while writing bad blocks");
145		return 1;
146	}
147	fclose(f);
148
149	f = fopen(tmp_filename, "r");
150	if (!f) {
151		fprintf(stderr, "Error re-opening temp file %s: %s\n",
152			tmp_filename, error_message(errno));
153		return 1;
154	}
155	retval = ext2fs_read_bb_FILE2(0, f, &new_bb, 0, 0);
156	if (retval) {
157		com_err("file_test", retval, "while reading bad blocks");
158		return 1;
159	}
160	fclose(f);
161
162	if (ext2fs_badblocks_equal(bb, new_bb)) {
163		printf("Block bitmap matched after reading and writing.\n");
164	} else {
165		printf("Block bitmap NOT matched.\n");
166		test_fail++;
167	}
168
169}
170
171
172int main(int argc, char *argv)
173{
174	badblocks_list bb1, bb2, bb3, bb4;
175	int	equal;
176	errcode_t	retval;
177
178	bb1 = bb2 = bb3 = bb4 = 0;
179
180	printf("test1: ");
181	retval = create_test_list(test1, &bb1);
182	if (retval == 0)
183		print_list(bb1, 1);
184	printf("\n");
185
186	printf("test2: ");
187	retval = create_test_list(test2, &bb2);
188	if (retval == 0)
189		print_list(bb2, 1);
190	printf("\n");
191
192	printf("test3: ");
193	retval = create_test_list(test3, &bb3);
194	if (retval == 0)
195		print_list(bb3, 1);
196	printf("\n");
197
198	printf("test4: ");
199	retval = create_test_list(test4, &bb4);
200	if (retval == 0) {
201		print_list(bb4, 0);
202		printf("\n");
203		validate_test_seq(bb4, test4a);
204	}
205	printf("\n");
206
207	if (bb1 && bb2 && bb3 && bb4) {
208		printf("Comparison tests:\n");
209		equal = ext2fs_badblocks_equal(bb1, bb2);
210		printf("bb1 and bb2 are %sequal.\n", equal ? "" : "NOT ");
211		if (equal)
212			test_fail++;
213
214		equal = ext2fs_badblocks_equal(bb1, bb3);
215		printf("bb1 and bb3 are %sequal.\n", equal ? "" : "NOT ");
216		if (!equal)
217			test_fail++;
218
219		equal = ext2fs_badblocks_equal(bb1, bb4);
220		printf("bb1 and bb4 are %sequal.\n", equal ? "" : "NOT ");
221		if (equal)
222			test_fail++;
223		printf("\n");
224	}
225
226	if (test_fail == 0)
227		printf("ext2fs library badblocks tests checks out OK!\n");
228
229	file_test(bb4);
230
231	if (bb1)
232		ext2fs_badblocks_list_free(bb1);
233	if (bb2)
234		ext2fs_badblocks_list_free(bb2);
235	if (bb3)
236		ext2fs_badblocks_list_free(bb3);
237	if (bb4)
238		ext2fs_badblocks_list_free(bb4);
239
240	return test_fail;
241
242}
243