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 | |
Paul Bakker | b34fef2 | 2013-08-20 12:06:33 +0200 | [diff] [blame^] | 91 | SUITE_PRE_DEP |
| 92 | #define TEST_SUITE_ACTIVE |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 93 | FUNCTION_CODE |
Paul Bakker | b34fef2 | 2013-08-20 12:06:33 +0200 | [diff] [blame^] | 94 | SUITE_POST_DEP |
| 95 | |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 96 | int dispatch_test(int cnt, char *params[50]) |
| 97 | { |
| 98 | int ret; |
Paul Bakker | b34fef2 | 2013-08-20 12:06:33 +0200 | [diff] [blame^] | 99 | ((void) cnt); |
| 100 | ((void) params); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 101 | |
Paul Bakker | b34fef2 | 2013-08-20 12:06:33 +0200 | [diff] [blame^] | 102 | #if defined(TEST_SUITE_ACTIVE) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 103 | DISPATCH_FUNCTION |
| 104 | { |
| 105 | fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] ); |
| 106 | fflush( stdout ); |
| 107 | return( 1 ); |
| 108 | } |
Paul Bakker | b34fef2 | 2013-08-20 12:06:33 +0200 | [diff] [blame^] | 109 | #else |
| 110 | return( 3 ); |
| 111 | #endif |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 112 | return( ret ); |
| 113 | } |
| 114 | |
| 115 | int get_line( FILE *f, char *buf, size_t len ) |
| 116 | { |
| 117 | char *ret; |
| 118 | |
| 119 | ret = fgets( buf, len, f ); |
| 120 | if( ret == NULL ) |
| 121 | return( -1 ); |
| 122 | |
| 123 | if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' ) |
| 124 | buf[strlen(buf) - 1] = '\0'; |
| 125 | if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' ) |
| 126 | buf[strlen(buf) - 1] = '\0'; |
| 127 | |
| 128 | return( 0 ); |
| 129 | } |
| 130 | |
| 131 | int parse_arguments( char *buf, size_t len, char *params[50] ) |
| 132 | { |
| 133 | int cnt = 0, i; |
| 134 | char *cur = buf; |
| 135 | char *p = buf, *q; |
| 136 | |
| 137 | params[cnt++] = cur; |
| 138 | |
| 139 | while( *p != '\0' && p < buf + len ) |
| 140 | { |
| 141 | if( *p == '\\' ) |
| 142 | { |
| 143 | *p++; |
| 144 | *p++; |
| 145 | continue; |
| 146 | } |
| 147 | if( *p == ':' ) |
| 148 | { |
| 149 | if( p + 1 < buf + len ) |
| 150 | { |
| 151 | cur = p + 1; |
| 152 | params[cnt++] = cur; |
| 153 | } |
| 154 | *p = '\0'; |
| 155 | } |
| 156 | |
| 157 | *p++; |
| 158 | } |
| 159 | |
| 160 | // Replace newlines, question marks and colons in strings |
| 161 | for( i = 0; i < cnt; i++ ) |
| 162 | { |
| 163 | p = params[i]; |
| 164 | q = params[i]; |
| 165 | |
| 166 | while( *p != '\0' ) |
| 167 | { |
| 168 | if( *p == '\\' && *(p + 1) == 'n' ) |
| 169 | { |
| 170 | p += 2; |
| 171 | *(q++) = '\n'; |
| 172 | } |
| 173 | else if( *p == '\\' && *(p + 1) == ':' ) |
| 174 | { |
| 175 | p += 2; |
| 176 | *(q++) = ':'; |
| 177 | } |
| 178 | else if( *p == '\\' && *(p + 1) == '?' ) |
| 179 | { |
| 180 | p += 2; |
| 181 | *(q++) = '?'; |
| 182 | } |
| 183 | else |
| 184 | *(q++) = *(p++); |
| 185 | } |
| 186 | *q = '\0'; |
| 187 | } |
| 188 | |
| 189 | return( cnt ); |
| 190 | } |
| 191 | |
| 192 | int main() |
| 193 | { |
| 194 | int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0; |
| 195 | const char *filename = "TEST_FILENAME"; |
| 196 | FILE *file; |
| 197 | char buf[5000]; |
| 198 | char *params[50]; |
| 199 | |
| 200 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
| 201 | unsigned char buf[1000000]; |
| 202 | memory_buffer_alloc_init( buf, sizeof(buf) ); |
| 203 | #endif |
| 204 | |
| 205 | file = fopen( filename, "r" ); |
| 206 | if( file == NULL ) |
| 207 | { |
| 208 | fprintf( stderr, "Failed to open\n" ); |
| 209 | return( 1 ); |
| 210 | } |
| 211 | |
| 212 | while( !feof( file ) ) |
| 213 | { |
| 214 | int skip = 0; |
| 215 | |
| 216 | if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) |
| 217 | break; |
Paul Bakker | 55a7e90 | 2013-08-19 14:02:10 +0200 | [diff] [blame] | 218 | fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 219 | fprintf( stdout, " " ); |
| 220 | for( i = strlen( buf ) + 1; i < 67; i++ ) |
| 221 | fprintf( stdout, "." ); |
| 222 | fprintf( stdout, " " ); |
| 223 | fflush( stdout ); |
| 224 | |
| 225 | total_tests++; |
| 226 | |
| 227 | if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) |
| 228 | break; |
| 229 | cnt = parse_arguments( buf, strlen(buf), params ); |
| 230 | |
| 231 | if( strcmp( params[0], "depends_on" ) == 0 ) |
| 232 | { |
| 233 | for( i = 1; i < cnt; i++ ) |
| 234 | if( dep_check( params[i] ) != 0 ) |
| 235 | skip = 1; |
| 236 | |
| 237 | if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) |
| 238 | break; |
| 239 | cnt = parse_arguments( buf, strlen(buf), params ); |
| 240 | } |
| 241 | |
| 242 | if( skip == 0 ) |
| 243 | { |
| 244 | test_errors = 0; |
| 245 | ret = dispatch_test( cnt, params ); |
| 246 | } |
| 247 | |
| 248 | if( skip == 1 || ret == 3 ) |
| 249 | { |
| 250 | total_skipped++; |
Paul Bakker | b34fef2 | 2013-08-20 12:06:33 +0200 | [diff] [blame^] | 251 | fprintf( stdout, "----\n" ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 252 | fflush( stdout ); |
| 253 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 254 | else if( ret == 0 && test_errors == 0 ) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 255 | { |
| 256 | fprintf( stdout, "PASS\n" ); |
| 257 | fflush( stdout ); |
| 258 | } |
| 259 | else if( ret == 2 ) |
| 260 | { |
| 261 | fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" ); |
| 262 | fclose(file); |
| 263 | exit( 2 ); |
| 264 | } |
| 265 | else |
| 266 | total_errors++; |
| 267 | |
| 268 | if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 ) |
| 269 | break; |
| 270 | if( strlen(buf) != 0 ) |
| 271 | { |
Paul Bakker | 55a7e90 | 2013-08-19 14:02:10 +0200 | [diff] [blame] | 272 | fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) ); |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 273 | return( 1 ); |
| 274 | } |
| 275 | } |
| 276 | fclose(file); |
| 277 | |
| 278 | fprintf( stdout, "\n----------------------------------------------------------------------------\n\n"); |
| 279 | if( total_errors == 0 ) |
| 280 | fprintf( stdout, "PASSED" ); |
| 281 | else |
| 282 | fprintf( stdout, "FAILED" ); |
| 283 | |
| 284 | fprintf( stdout, " (%d / %d tests (%d skipped))\n", |
| 285 | total_tests - total_errors, total_tests, total_skipped ); |
| 286 | |
| 287 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && defined(POLARSSL_MEMORY_DEBUG) |
| 288 | memory_buffer_alloc_status(); |
| 289 | #endif |
| 290 | |
| 291 | return( total_errors != 0 ); |
| 292 | } |
| 293 | |