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