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