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