Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 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> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 14 | #if HAVE_UNISTD_H |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 16 | #endif |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 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 | |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 25 | #if EXT2_FLAT_INCLUDES |
| 26 | #include "ext2_fs.h" |
| 27 | #else |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 28 | #include <linux/ext2_fs.h> |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 29 | #endif |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 30 | |
| 31 | #include "ext2fs.h" |
| 32 | |
| 33 | blk_t test1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0 }; |
| 34 | blk_t test2[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; |
| 35 | blk_t test3[] = { 3, 1, 4, 5, 9, 2, 7, 10, 6, 8, 0 }; |
| 36 | blk_t test4[] = { 20, 50, 12, 17, 13, 2, 66, 23, 56, 0 }; |
| 37 | blk_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 | |
| 53 | static int test_fail = 0; |
| 54 | |
| 55 | static 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 = 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 = badblocks_list_add(bb, vec[i]); |
| 68 | if (retval) { |
| 69 | com_err("create_test_list", retval, |
| 70 | "while adding test vector %d", i); |
| 71 | badblocks_list_free(bb); |
| 72 | return retval; |
| 73 | } |
| 74 | } |
| 75 | *ret = bb; |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static 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 = 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 (badblocks_list_iterate(iter, &blk)) { |
| 93 | printf("%d ", blk); |
| 94 | if (i++ != blk) |
| 95 | ok = 0; |
| 96 | } |
| 97 | 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 | |
| 108 | static 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 = 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 | |
| 126 | int 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 | 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 | 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 | 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 | 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 | } |