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