Whamcloud - gitweb
tst_badblocks.c (file_test): Use tmpfile() instead of mktemp().
[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         badblocks_list new_bb = 0;
124         errcode_t       retval;
125         FILE    *f;
126
127         f = tmpfile();
128         if (!f) {
129                 fprintf(stderr, "Error opening temp file: %s\n",
130                         error_message(errno));
131                 return 1;
132         }
133         retval = ext2fs_write_bb_FILE(bb, 0, f);
134         if (retval) {
135                 com_err("file_test", retval, "while writing bad blocks");
136                 return 1;
137         }
138
139         rewind(f);
140         retval = ext2fs_read_bb_FILE2(0, f, &new_bb, 0, 0);
141         if (retval) {
142                 com_err("file_test", retval, "while reading bad blocks");
143                 return 1;
144         }
145         fclose(f);
146
147         if (ext2fs_badblocks_equal(bb, new_bb)) {
148                 printf("Block bitmap matched after reading and writing.\n");
149         } else {
150                 printf("Block bitmap NOT matched.\n");
151                 test_fail++;
152         }
153         return 0;
154 }
155
156
157 int main(int argc, char **argv)
158 {
159         badblocks_list bb1, bb2, bb3, bb4;
160         int     equal;
161         errcode_t       retval;
162
163         bb1 = bb2 = bb3 = bb4 = 0;
164
165         printf("test1: ");
166         retval = create_test_list(test1, &bb1);
167         if (retval == 0)
168                 print_list(bb1, 1);
169         printf("\n");
170         
171         printf("test2: ");
172         retval = create_test_list(test2, &bb2);
173         if (retval == 0)
174                 print_list(bb2, 1);
175         printf("\n");
176
177         printf("test3: ");
178         retval = create_test_list(test3, &bb3);
179         if (retval == 0)
180                 print_list(bb3, 1);
181         printf("\n");
182         
183         printf("test4: ");
184         retval = create_test_list(test4, &bb4);
185         if (retval == 0) {
186                 print_list(bb4, 0);
187                 printf("\n");
188                 validate_test_seq(bb4, test4a);
189         }
190         printf("\n");
191
192         if (bb1 && bb2 && bb3 && bb4) {
193                 printf("Comparison tests:\n");
194                 equal = ext2fs_badblocks_equal(bb1, bb2);
195                 printf("bb1 and bb2 are %sequal.\n", equal ? "" : "NOT "); 
196                 if (equal)
197                         test_fail++;
198
199                 equal = ext2fs_badblocks_equal(bb1, bb3);
200                 printf("bb1 and bb3 are %sequal.\n", equal ? "" : "NOT "); 
201                 if (!equal)
202                         test_fail++;
203                 
204                 equal = ext2fs_badblocks_equal(bb1, bb4);
205                 printf("bb1 and bb4 are %sequal.\n", equal ? "" : "NOT "); 
206                 if (equal)
207                         test_fail++;
208                 printf("\n");
209         }
210         
211         if (test_fail == 0)
212                 printf("ext2fs library badblocks tests checks out OK!\n");
213
214         file_test(bb4);
215         
216         if (bb1)
217                 ext2fs_badblocks_list_free(bb1);
218         if (bb2)
219                 ext2fs_badblocks_list_free(bb2);
220         if (bb3)
221                 ext2fs_badblocks_list_free(bb3);
222         if (bb4)
223                 ext2fs_badblocks_list_free(bb4);
224
225         return test_fail;
226
227 }