Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | |
| 4 | static int test_errors = 0; |
| 5 | |
| 6 | static int test_assert( int correct, char *test ) |
| 7 | { |
| 8 | if( correct ) |
| 9 | return( 0 ); |
| 10 | |
| 11 | test_errors++; |
| 12 | // if( test_errors == 1 ) |
| 13 | printf( "FAILED\n %s\n\n", test ); |
| 14 | |
| 15 | return( 1 ); |
| 16 | } |
| 17 | |
| 18 | #define TEST_ASSERT( TEST ) (test_assert((TEST) ? 1 : 0, #TEST)) |
| 19 | |
| 20 | int verify_string( char **str ) |
| 21 | { |
| 22 | if( (*str)[0] != '"' || |
| 23 | (*str)[strlen( *str ) - 1] != '"' ) |
| 24 | { |
| 25 | printf( "Expected string (with \"\") for parameter and got: %s\n", *str ); |
| 26 | return( -1 ); |
| 27 | } |
| 28 | |
| 29 | (*str)++; |
| 30 | (*str)[strlen( *str ) - 1] = '\0'; |
| 31 | |
| 32 | return( 0 ); |
| 33 | } |
| 34 | |
| 35 | int verify_int( char *str, int *value ) |
| 36 | { |
| 37 | size_t i; |
| 38 | int minus = 0; |
| 39 | int digits = 1; |
| 40 | int hex = 0; |
| 41 | |
| 42 | for( i = 0; i < strlen( str ); i++ ) |
| 43 | { |
| 44 | if( i == 0 && str[i] == '-' ) |
| 45 | { |
| 46 | minus = 1; |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) && |
| 51 | str[i - 1] == '0' && str[i] == 'x' ) |
| 52 | { |
| 53 | hex = 1; |
| 54 | continue; |
| 55 | } |
| 56 | |
| 57 | if( str[i] < '0' || str[i] > '9' ) |
| 58 | { |
| 59 | digits = 0; |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if( digits ) |
| 65 | { |
| 66 | if( hex ) |
| 67 | *value = strtol( str, NULL, 16 ); |
| 68 | else |
| 69 | *value = strtol( str, NULL, 10 ); |
| 70 | |
| 71 | return( 0 ); |
| 72 | } |
| 73 | |
| 74 | MAPPING_CODE |
| 75 | |
| 76 | printf( "Expected integer for parameter and got: %s\n", str ); |
| 77 | return( -1 ); |
| 78 | } |
| 79 | |
| 80 | int dep_check( char *str ) |
| 81 | { |
| 82 | if( str == NULL ) |
| 83 | return( 1 ); |
| 84 | |
| 85 | DEP_CHECK_CODE |
| 86 | |
| 87 | return( 1 ); |
| 88 | } |
| 89 | |
| 90 | FUNCTION_CODE |
| 91 | int dispatch_test(int cnt, char *params[50]) |
| 92 | { |
| 93 | int ret; |
| 94 | |
| 95 | DISPATCH_FUNCTION |
| 96 | { |
| 97 | fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] ); |
| 98 | fflush( stdout ); |
| 99 | return( 1 ); |
| 100 | } |
| 101 | |
| 102 | return( ret ); |
| 103 | } |
| 104 | |
| 105 | int get_line( FILE *f, char *buf, size_t len ) |
| 106 | { |
| 107 | char *ret; |
| 108 | |
| 109 | ret = fgets( buf, len, f ); |
| 110 | if( ret == NULL ) |
| 111 | return( -1 ); |
| 112 | |
| 113 | if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' ) |
| 114 | buf[strlen(buf) - 1] = '\0'; |
| 115 | if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' ) |
| 116 | buf[strlen(buf) - 1] = '\0'; |
| 117 | |
| 118 | return( 0 ); |
| 119 | } |
| 120 | |
| 121 | int parse_arguments( char *buf, size_t len, char *params[50] ) |
| 122 | { |
| 123 | int cnt = 0, i; |
| 124 | char *cur = buf; |
| 125 | char *p = buf, *q; |
| 126 | |
| 127 | params[cnt++] = cur; |
| 128 | |
| 129 | while( *p != '\0' && p < buf + len ) |
| 130 | { |
| 131 | if( *p == '\\' ) |
| 132 | { |
| 133 | *p++; |
| 134 | *p++; |
| 135 | continue; |
| 136 | } |
| 137 | if( *p == ':' ) |
| 138 | { |
| 139 | if( p + 1 < buf + len ) |
| 140 | { |
| 141 | cur = p + 1; |
| 142 | params[cnt++] = cur; |
| 143 | } |
| 144 | *p = '\0'; |
| 145 | } |
| 146 | |
| 147 | *p++; |
| 148 | } |
| 149 | |
| 150 | // Replace newlines, question marks and colons in strings |
| 151 | for( i = 0; i < cnt; i++ ) |
| 152 | { |
| 153 | p = params[i]; |
| 154 | q = params[i]; |
| 155 | |
| 156 | while( *p != '\0' ) |
| 157 | { |
| 158 | if( *p == '\\' && *(p + 1) == 'n' ) |
| 159 | { |
| 160 | p += 2; |
| 161 | *(q++) = '\n'; |
| 162 | } |
| 163 | else if( *p == '\\' && *(p + 1) == ':' ) |
| 164 | { |
| 165 | p += 2; |
| 166 | *(q++) = ':'; |
| 167 | } |
| 168 | else if( *p == '\\' && *(p + 1) == '?' ) |
| 169 | { |
| 170 | p += 2; |
| 171 | *(q++) = '?'; |
| 172 | } |
| 173 | else |
| 174 | *(q++) = *(p++); |
| 175 | } |
| 176 | *q = '\0'; |
| 177 | } |
| 178 | |
| 179 | return( cnt ); |
| 180 | } |
| 181 | |
| 182 | int main() |
| 183 | { |
| 184 | int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0; |
| 185 | const char *filename = "TEST_FILENAME"; |
| 186 | FILE *file; |
| 187 | char buf[5000]; |
| 188 | char *params[50]; |
| 189 | |
| 190 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
| 191 | unsigned char buf[1000000]; |
| 192 | memory_buffer_alloc_init( buf, sizeof(buf) ); |
| 193 | #endif |
| 194 | |
| 195 | file = fopen( filename, "r" ); |
| 196 | if( file == NULL ) |
| 197 | { |
| 198 | fprintf( stderr, "Failed to open\n" ); |
| 199 | return( 1 ); |
| 200 | } |
| 201 | |
| 202 | while( !feof( file ) ) |
| 203 | { |
| 204 | int skip = 0; |
| 205 | |
| 206 | if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) |
| 207 | break; |
| 208 | fprintf( stdout, "%.66s", buf ); |
| 209 | fprintf( stdout, " " ); |
| 210 | for( i = strlen( buf ) + 1; i < 67; i++ ) |
| 211 | fprintf( stdout, "." ); |
| 212 | fprintf( stdout, " " ); |
| 213 | fflush( stdout ); |
| 214 | |
| 215 | total_tests++; |
| 216 | |
| 217 | if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) |
| 218 | break; |
| 219 | cnt = parse_arguments( buf, strlen(buf), params ); |
| 220 | |
| 221 | if( strcmp( params[0], "depends_on" ) == 0 ) |
| 222 | { |
| 223 | for( i = 1; i < cnt; i++ ) |
| 224 | if( dep_check( params[i] ) != 0 ) |
| 225 | skip = 1; |
| 226 | |
| 227 | if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) |
| 228 | break; |
| 229 | cnt = parse_arguments( buf, strlen(buf), params ); |
| 230 | } |
| 231 | |
| 232 | if( skip == 0 ) |
| 233 | { |
| 234 | test_errors = 0; |
| 235 | ret = dispatch_test( cnt, params ); |
| 236 | } |
| 237 | |
| 238 | if( skip == 1 || ret == 3 ) |
| 239 | { |
| 240 | total_skipped++; |
| 241 | fprintf( stdout, "-------\n" ); |
| 242 | fflush( stdout ); |
| 243 | } |
| 244 | else if( ret == 0 && test_errors == 0 ) |
| 245 | { |
| 246 | fprintf( stdout, "PASS\n" ); |
| 247 | fflush( stdout ); |
| 248 | } |
| 249 | else if( ret == 2 ) |
| 250 | { |
| 251 | fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" ); |
| 252 | fclose(file); |
| 253 | exit( 2 ); |
| 254 | } |
| 255 | else |
| 256 | total_errors++; |
| 257 | |
| 258 | if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) |
| 259 | break; |
| 260 | if( strlen(buf) != 0 ) |
| 261 | { |
| 262 | fprintf( stderr, "Should be empty %d\n", strlen(buf) ); |
| 263 | return( 1 ); |
| 264 | } |
| 265 | } |
| 266 | fclose(file); |
| 267 | |
| 268 | fprintf( stdout, "\n----------------------------------------------------------------------------\n\n"); |
| 269 | if( total_errors == 0 ) |
| 270 | fprintf( stdout, "PASSED" ); |
| 271 | else |
| 272 | fprintf( stdout, "FAILED" ); |
| 273 | |
| 274 | fprintf( stdout, " (%d / %d tests (%d skipped))\n", |
| 275 | total_tests - total_errors, total_tests, total_skipped ); |
| 276 | |
| 277 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && defined(POLARSSL_MEMORY_DEBUG) |
| 278 | memory_buffer_alloc_status(); |
| 279 | #endif |
| 280 | |
| 281 | return( total_errors != 0 ); |
| 282 | } |
| 283 | |