tst_badblocks.c revision 9b9fe8ac19db2bbd202bffd305ceee767a45f57a
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[] = { 10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 1, 0 }; 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 main(int argc, char *argv) 127{ 128 badblocks_list bb; 129 errcode_t retval; 130 131 printf("test1: "); 132 retval = create_test_list(test1, &bb); 133 if (retval == 0) { 134 print_list(bb, 1); 135 ext2fs_badblocks_list_free(bb); 136 } 137 printf("\n"); 138 139 printf("test2: "); 140 retval = create_test_list(test2, &bb); 141 if (retval == 0) { 142 print_list(bb, 1); 143 ext2fs_badblocks_list_free(bb); 144 } 145 printf("\n"); 146 147 printf("test3: "); 148 retval = create_test_list(test3, &bb); 149 if (retval == 0) { 150 print_list(bb, 1); 151 ext2fs_badblocks_list_free(bb); 152 } 153 printf("\n"); 154 155 printf("test4: "); 156 retval = create_test_list(test4, &bb); 157 if (retval == 0) { 158 print_list(bb, 0); 159 printf("\n"); 160 validate_test_seq(bb, test4a); 161 ext2fs_badblocks_list_free(bb); 162 } 163 printf("\n"); 164 if (test_fail == 0) 165 printf("ext2fs library badblocks tests checks out OK!\n"); 166 return test_fail; 167} 168