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