1 // test the basename, dirname, basename_r and dirname_r 2 #include <libgen.h> 3 #include <errno.h> 4 #include <stdio.h> 5 #include <string.h> 6 7 static int fail = 0; 8 9 static void 10 test_basename(char* _input, const char* _expected, int _errno) 11 { 12 char temp[256], *input = _input; 13 char* ret; 14 #if HOST 15 /* GLibc does modify the input string. bummer */ 16 if (_input) { 17 strcpy(temp, _input); 18 input = temp; 19 } 20 #endif 21 errno = 0; 22 ret = basename(input); 23 if (_expected == NULL) { 24 if (ret != NULL) { 25 fprintf(stderr, 26 "KO: basename(\"%s\") returned \"%s\", NULL expected)\n", 27 _input, ret); 28 fail += 1; 29 } else if (errno != _errno) { 30 fprintf(stderr, 31 "KO: basename(\"%s\") returned NULL with error: %d (%d expected)\n", 32 _input, errno, _errno); 33 fail += 1; 34 } else { 35 printf( "OK: basename(\"%s\") returned NULL with error %d\n", 36 _input, _errno ); 37 } 38 } else { 39 if (ret == NULL) { 40 fprintf(stderr, "KO: basename(\"%s\") returned NULL with error %d\n", 41 _input, errno); 42 fail += 1; 43 } 44 else if (strcmp(ret, _expected)) { 45 fprintf(stderr, "KO: basename(\"%s\") returned \"%s\", instead of \"%s\"\n", 46 _input, ret, _expected); 47 } 48 else { 49 printf( "OK: basename(\"%s\") returned \"%s\"\n", 50 _input, ret ); 51 } 52 } 53 } 54 55 56 #if !HOST 57 static void 58 test_basename_r(char* _input, const char* _expected_content, int _expected, char* _buff, size_t _bufflen, int _errno) 59 { 60 int ret; 61 errno = 0; 62 ret = basename_r(_input, _buff, _bufflen ); 63 if (ret != _expected) { 64 fprintf(stderr, 65 "KO: basename_r(\"%s\", <buff>, %d) returned %d (expected %d)\n", 66 _input, _bufflen, ret, _expected); 67 fail += 1; 68 return; 69 } 70 if (ret == -1) { 71 if (errno != _errno) { 72 fprintf(stderr, 73 "KO: basename_r(\"%s\", <buff>, %d) returned -1 with errno=%d (expected %d)\n", 74 _input, _bufflen, errno, _errno); 75 fail += 1; 76 return; 77 } 78 } 79 else if (_buff != NULL && memcmp( _buff, _expected_content, ret ) ) { 80 fprintf(stderr, 81 "KO: basename_r(\"%s\", <buff>, %d) returned \"%s\", expected \"%s\"\n", 82 _input, _bufflen, _buff, _expected_content ); 83 fail += 1; 84 return; 85 } 86 printf("OK: basename_r(\"%s\", <buff>, %d) returned \"%s\"\n", 87 _input, _bufflen, _expected_content ); 88 } 89 90 static void 91 test_dirname_r(char* _input, const char* _expected_content, int _expected, char* _buff, size_t _bufflen, int _errno) 92 { 93 int ret; 94 errno = 0; 95 ret = dirname_r(_input, _buff, _bufflen ); 96 if (ret != _expected) { 97 fprintf(stderr, 98 "KO: dirname_r(\"%s\", <buff>, %d) returned %d (expected %d)\n", 99 _input, _bufflen, ret, _expected); 100 fail += 1; 101 return; 102 } 103 if (ret == -1) { 104 if (errno != _errno) { 105 fprintf(stderr, 106 "KO: dirname_r(\"%s\", <buff>, %d) returned -1 with errno=%d (expected %d)\n", 107 _input, _bufflen, errno, _errno); 108 fail += 1; 109 return; 110 } 111 } 112 else if (_buff != NULL && memcmp( _buff, _expected_content, ret ) ) { 113 fprintf(stderr, 114 "KO: dirname_r(\"%s\", <buff>, %d) returned \"%s\", expected \"%s\"\n", 115 _input, _bufflen, _buff, _expected_content ); 116 fail += 1; 117 return; 118 } 119 printf("OK: dirname_r(\"%s\", <buff>, %d) returned \"%s\"\n", 120 _input, _bufflen, _expected_content ); 121 } 122 #endif 123 124 125 static void 126 test_dirname(char* _input, const char* _expected, int _errno) 127 { 128 char temp[256], *input = _input; 129 char* ret; 130 #if HOST 131 /* GLibc does modify the input string. bummer */ 132 if (_input) { 133 strcpy(temp, _input); 134 input = temp; 135 } 136 #endif 137 errno = 0; 138 ret = dirname(input); 139 if (_expected == NULL) { 140 if (ret != NULL) { 141 fprintf(stderr, 142 "KO: dirname(\"%s\") returned \"%s\", NULL expected)\n", 143 _input, ret); 144 fail += 1; 145 } else if (errno != _errno) { 146 fprintf(stderr, 147 "KO: dirname(\"%s\") returned NULL with error: %d (%d expected)\n", 148 _input, errno, _errno); 149 fail += 1; 150 } else { 151 printf( "OK: dirname(\"%s\") returned NULL with error %d\n", 152 _input, _errno ); 153 } 154 } else { 155 if (ret == NULL) { 156 fprintf(stderr, "KO: dirname(\"%s\") returned NULL with error %d\n", 157 _input, errno); 158 fail += 1; 159 } 160 else if (strcmp(ret, _expected)) { 161 fprintf(stderr, "KO: dirname(\"%s\") returned \"%s\", instead of \"%s\"\n", 162 _input, ret, _expected); 163 } 164 else { 165 printf( "OK: dirname(\"%s\") returned \"%s\"\n", 166 _input, ret ); 167 } 168 } 169 } 170 171 172 173 174 int main( void ) 175 { 176 char buff[256]; 177 178 test_basename( "", ".", 0 ); 179 test_basename( "/usr/lib", "lib", 0 ); 180 test_basename( "/usr/", "usr", 0 ); 181 test_basename( "usr", "usr", 0 ); 182 test_basename( "/", "/", 0 ); 183 test_basename( ".", ".", 0 ); 184 test_basename( "..", "..", 0 ); 185 186 #if !HOST 187 test_basename_r( "", ".", 1, NULL, 0, 0 ); 188 test_basename_r( "", ".", -1, buff, 0, ERANGE ); 189 test_basename_r( "", ".", -1, buff, 1, ERANGE ); 190 test_basename_r( "", ".", 1, buff, 2, 0 ); 191 test_basename_r( "", ".", 1, buff, sizeof(buff), 0 ); 192 test_basename_r( "/usr/lib", "lib", 3, buff, sizeof(buff), 0 ); 193 test_basename_r( "/usr/", "usr", 3, buff, sizeof(buff), 0 ); 194 test_basename_r( "usr", "usr", 3, buff, sizeof(buff), 0 ); 195 test_basename_r( "/", "/", 1, buff, sizeof(buff), 0 ); 196 test_basename_r( ".", ".", 1, buff, sizeof(buff), 0 ); 197 test_basename_r( "..", "..", 2, buff, sizeof(buff), 0 ); 198 #endif 199 200 test_dirname( "", ".", 0 ); 201 test_dirname( "/usr/lib", "/usr", 0 ); 202 test_dirname( "/usr/", "/", 0 ); 203 test_dirname( "usr", ".", 0 ); 204 test_dirname( ".", ".", 0 ); 205 test_dirname( "..", ".", 0 ); 206 207 #if !HOST 208 test_dirname_r( "", ".", 1, NULL, 0, 0 ); 209 test_dirname_r( "", ".", -1, buff, 0, ERANGE ); 210 test_dirname_r( "", ".", -1, buff, 1, ERANGE ); 211 test_dirname_r( "", ".", 1, buff, 2, 0 ); 212 test_dirname_r( "/usr/lib", "/usr", 4, buff, sizeof(buff), 0 ); 213 test_dirname_r( "/usr/", "/", 1, buff, sizeof(buff), 0 ); 214 test_dirname_r( "usr", ".", 1, buff, sizeof(buff), 0 ); 215 test_dirname_r( ".", ".", 1, buff, sizeof(buff), 0 ); 216 test_dirname_r( "..", ".", 1, buff, sizeof(buff), 0 ); 217 #endif 218 219 return (fail > 0); 220 } 221 222