Ticket #4934: 4934-txTestHeader.patch
File 4934-txTestHeader.patch, 29.1 KB (added by gvdl, 9 years ago) |
---|
-
libtransmission/Makefile.am
diff --git a/libtransmission/Makefile.am b/libtransmission/Makefile.am index 515e912..be6b1d7 100644
a b magnet_test_SOURCES = magnet-test.c 174 174 magnet_test_LDADD = ${apps_ldadd} 175 175 magnet_test_LDFLAGS = ${apps_ldflags} 176 176 177 peer_msgs_test_SOURCES = peer-msgs-test.c 178 peer_msgs_test_LDADD = ${apps_ldadd} 179 peer_msgs_test_LDFLAGS = ${apps_ldflags} 180 177 181 rpc_test_SOURCES = rpc-test.c 178 182 rpc_test_LDADD = ${apps_ldadd} 179 183 rpc_test_LDFLAGS = ${apps_ldflags} … … test_peer_id_SOURCES = test-peer-id.c 182 186 test_peer_id_LDADD = ${apps_ldadd} 183 187 test_peer_id_LDFLAGS = ${apps_ldflags} 184 188 185 peer_msgs_test_SOURCES = peer-msgs-test.c186 peer_msgs_test_LDADD = ${apps_ldadd}187 peer_msgs_test_LDFLAGS = ${apps_ldflags}188 189 189 utils_test_SOURCES = utils-test.c 190 190 utils_test_LDADD = ${apps_ldadd} 191 191 utils_test_LDFLAGS = ${apps_ldflags} -
libtransmission/bencode-test.c
diff --git a/libtransmission/bencode-test.c b/libtransmission/bencode-test.c index f7acdad..5e906bd 100644
a b 9 9 #include "bencode.h" 10 10 #include "utils.h" /* tr_free */ 11 11 12 /* #define VERBOSE */ 13 14 static int test = 0; 15 16 #ifdef VERBOSE 17 #define check( A ) \ 18 { \ 19 ++test; \ 20 if( A ){ \ 21 fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 22 } else { \ 23 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 24 return test; \ 25 } \ 26 } 12 // #define VERBOSE 13 #include "libtransmission-test.h" 14 15 #ifndef WIN32 16 #define STACK_SMASH_DEPTH (1 * 1000 * 1000) 27 17 #else 28 #define check( A ) \ 29 { \ 30 ++test; \ 31 if( !( A ) ){ \ 32 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 33 return test; \ 34 } \ 35 } 18 #define STACK_SMASH_DEPTH ( 100 * 1000) 36 19 #endif 37 20 38 21 static int … … testMerge( void ) 423 406 } 424 407 425 408 static int 426 testStackSmash( int depth)409 testStackSmash( void ) 427 410 { 428 411 int i; 429 412 int len; … … testStackSmash( int depth ) 432 415 const uint8_t * end; 433 416 tr_benc val; 434 417 char * saved; 418 const int depth = STACK_SMASH_DEPTH; 435 419 436 420 in = tr_new( uint8_t, depth * 2 + 1 ); 437 421 for( i = 0; i < depth; ++i ) … … testBool( void ) 465 449 tr_bencDictAddBool( &top, "key2", 0 ); 466 450 tr_bencDictAddInt ( &top, "key3", true ); 467 451 tr_bencDictAddInt ( &top, "key4", 1 ); 468 check( tr_bencDictFindBool( &top, "key1", &boolVal ) ) 469 check( !boolVal ) 470 check( tr_bencDictFindBool( &top, "key2", &boolVal ) ) 471 check( !boolVal ) 472 check( tr_bencDictFindBool( &top, "key3", &boolVal ) ) 473 check( boolVal ) 474 check( tr_bencDictFindBool( &top, "key4", &boolVal ) ) 475 check( boolVal ) 476 check( tr_bencDictFindInt( &top, "key1", &intVal ) ) 477 check( !intVal) 478 check( tr_bencDictFindInt( &top, "key2", &intVal ) ) 479 check( !intVal ) 480 check( tr_bencDictFindInt( &top, "key3", &intVal ) ) 481 check( intVal ) 482 check( tr_bencDictFindInt( &top, "key4", &intVal ) ) 483 check( intVal ) 452 check( tr_bencDictFindBool( &top, "key1", &boolVal ) ); 453 check( !boolVal ); 454 check( tr_bencDictFindBool( &top, "key2", &boolVal ) ); 455 check( !boolVal ); 456 check( tr_bencDictFindBool( &top, "key3", &boolVal ) ); 457 check( boolVal ); 458 check( tr_bencDictFindBool( &top, "key4", &boolVal ) ); 459 check( boolVal ); 460 check( tr_bencDictFindInt( &top, "key1", &intVal ) ); 461 check( !intVal); 462 check( tr_bencDictFindInt( &top, "key2", &intVal ) ); 463 check( !intVal ); 464 check( tr_bencDictFindInt( &top, "key3", &intVal ) ); 465 check( intVal ); 466 check( tr_bencDictFindInt( &top, "key4", &intVal ) ); 467 check( intVal ); 484 468 485 469 tr_bencFree( &top ); 486 470 return 0; … … testParse2( void ) 506 490 tr_bencDictAddStr( &top, "this-is-a-string", "this-is-a-string" ); 507 491 508 492 benc = tr_bencToStr( &top, TR_FMT_BENC, &len ); 509 check( !strcmp( benc, "d14:this-is-a-booli1e14:this-is-a-real8:0.50000016:this-is-a-string16:this-is-a-string14:this-is-an-inti1234ee" ) ) 510 check( !tr_bencParse( benc, benc+len, &top2, &end ) ) 511 check( (char*)end == benc + len ) 512 check( tr_bencIsDict( &top2 ) ) 513 check( tr_bencDictFindInt( &top, "this-is-an-int", &intVal ) ) 514 check( intVal == 1234 ) 515 check( tr_bencDictFindBool( &top, "this-is-a-bool", &boolVal ) ) 516 check( boolVal == true ) 517 check( tr_bencDictFindStr( &top, "this-is-a-string", &strVal ) ) 518 check( !strcmp( strVal, "this-is-a-string" ) ) 519 check( tr_bencDictFindReal( &top, "this-is-a-real", &realVal ) ) 520 check( (int)(realVal*100) == 50 ) 493 check( !strcmp( benc, "d14:this-is-a-booli1e14:this-is-a-real8:0.50000016:this-is-a-string16:this-is-a-string14:this-is-an-inti1234ee" ) ); 494 check( !tr_bencParse( benc, benc+len, &top2, &end ) ); 495 check( (char*)end == benc + len ); 496 check( tr_bencIsDict( &top2 ) ); 497 check( tr_bencDictFindInt( &top, "this-is-an-int", &intVal ) ); 498 check( intVal == 1234 ); 499 check( tr_bencDictFindBool( &top, "this-is-a-bool", &boolVal ) ); 500 check( boolVal == true ); 501 check( tr_bencDictFindStr( &top, "this-is-a-string", &strVal ) ); 502 check( !strcmp( strVal, "this-is-a-string" ) ); 503 check( tr_bencDictFindReal( &top, "this-is-a-real", &realVal ) ); 504 check( (int)(realVal*100) == 50 ); 521 505 522 506 tr_bencFree( &top2 ); 523 507 tr_free( benc ); … … testParse2( void ) 529 513 int 530 514 main( void ) 531 515 { 532 int i; 533 534 if(( i = testInt( ))) 535 return i; 536 537 if(( i = testStr( ))) 538 return i; 539 540 if(( i = testParse( ))) 541 return i; 542 543 if(( i = testJSON( ))) 544 return i; 545 546 if(( i = testMerge( ))) 547 return i; 516 static const testFunc tests[] = { 517 testInt, testStr, testParse, testJSON, testMerge, testBool, 518 testParse2, testStackSmash, 519 }; 548 520 549 if(( i = testBool( ))) 550 return i; 551 552 if(( i = testParse2( ))) 553 return i; 554 555 #ifndef WIN32 556 i = testStackSmash( 1000000 ); 557 #else 558 i = testStackSmash( 100000 ); 559 #endif 560 if( i ) 561 return i; 562 563 return 0; 521 return runTests(tests, NUM_TESTS(tests)); 564 522 } 565 -
libtransmission/blocklist-test.c
diff --git a/libtransmission/blocklist-test.c b/libtransmission/blocklist-test.c index ce859c1..e1cb328 100644
a b 5 5 #include "utils.h" 6 6 7 7 #undef VERBOSE 8 #include "libtransmission-test.h" 8 9 9 #ifdef VERBOSE 10 #define check( A ) \ 11 { \ 12 ++test; \ 13 if( A ){ \ 14 fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 15 } else { \ 16 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 17 return test; \ 18 } \ 19 } 10 #ifndef WIN32 11 #define TEMPDIR_PREFIX "/tmp/" 20 12 #else 21 #define check( A )\ 22 { \ 23 ++test; \ 24 if( !( A ) ){ \ 25 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 26 return test; \ 27 } \ 28 } 13 #define TEMPDIR_PREFIX 29 14 #endif 30 15 16 #define TEMPFILE_TXT TEMPDIR_PREFIX "transmission-blocklist-test.txt" 17 #define TEMPFILE_BIN TEMPDIR_PREFIX "transmission-blocklist-test.bin" 18 31 19 static void 32 20 createTestBlocklist( const char * tmpfile ) 33 21 { … … createTestBlocklist( const char * tmpfile ) 46 34 fclose( out ); 47 35 } 48 36 49 int50 main( void )37 static int 38 testBlockList( void ) 51 39 { 52 #ifndef WIN32 53 const char * tmpfile_txt = "/tmp/transmission-blocklist-test.txt"; 54 const char * tmpfile_bin = "/tmp/transmission-blocklist-test.bin"; 55 #else 56 const char * tmpfile_txt = "transmission-blocklist-test.txt"; 57 const char * tmpfile_bin = "transmission-blocklist-test.bin"; 58 #endif 40 const char * tmpfile_txt = TEMPFILE_TXT; 41 const char * tmpfile_bin = TEMPFILE_BIN; 59 42 struct tr_address addr; 60 int test = 0;61 43 tr_blocklist * b; 62 44 63 45 remove( tmpfile_txt ); … … main( void ) 101 83 return 0; 102 84 } 103 85 86 MAIN_SINGLE_TEST(testBlockList) 87 -
libtransmission/clients-test.c
diff --git a/libtransmission/clients-test.c b/libtransmission/clients-test.c index db33ebe..d582931 100644
a b 4 4 #include "clients.h" 5 5 6 6 #undef VERBOSE 7 #include "libtransmission-test.h" 7 8 8 #ifdef VERBOSE9 #define check( A ) \10 { \11 ++test; \12 if( A ){ \13 fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \14 } else { \15 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \16 return test; \17 } \18 }19 #else20 #define check( A ) \21 { \22 ++test; \23 if( !( A ) ){ \24 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \25 return test; \26 } \27 }28 #endif29 9 30 10 #define TEST_CLIENT( A, B ) \ 31 11 tr_clientForId( buf, sizeof( buf ), A ); \ … … 34 14 int 35 15 main( void ) 36 16 { 37 int test = 0;38 17 char buf[128]; 39 18 40 19 TEST_CLIENT( "-FC1013-", "FileCroc 1.0.1.3" ); -
libtransmission/history-test.c
diff --git a/libtransmission/history-test.c b/libtransmission/history-test.c index d606f13..b524937 100644
a b 5 5 #include "history.h" 6 6 7 7 #undef VERBOSE 8 9 static int test = 0; 10 11 #ifdef VERBOSE 12 #define check( A ) \ 13 { \ 14 ++test; \ 15 if( A ){ \ 16 fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 17 } else { \ 18 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 19 return test; \ 20 } \ 21 } 22 #else 23 #define check( A ) \ 24 { \ 25 ++test; \ 26 if( !( A ) ){ \ 27 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 28 return test; \ 29 } \ 30 } 31 #endif 8 #include "libtransmission-test.h" 32 9 33 10 static int 34 11 test1( void ) … … test1( void ) 38 15 memset( &h, 0, sizeof( tr_recentHistory ) ); 39 16 40 17 tr_historyAdd( &h, 10000, 1 ); 41 check( (int)tr_historyGet( &h, 12000, 1000 ) == 0 ) 42 check( (int)tr_historyGet( &h, 12000, 3000 ) == 1 ) 43 check( (int)tr_historyGet( &h, 12000, 5000 ) == 1 ) 18 check( (int)tr_historyGet( &h, 12000, 1000 ) == 0 ); 19 check( (int)tr_historyGet( &h, 12000, 3000 ) == 1 ); 20 check( (int)tr_historyGet( &h, 12000, 5000 ) == 1 ); 44 21 tr_historyAdd( &h, 20000, 1 ); 45 check( (int)tr_historyGet( &h, 22000, 1000 ) == 0 ) 46 check( (int)tr_historyGet( &h, 22000, 3000 ) == 1 ) 47 check( (int)tr_historyGet( &h, 22000, 15000 ) == 2 ) 48 check( (int)tr_historyGet( &h, 22000, 20000 ) == 2 ) 49 50 return 0; 51 } 52 53 int 54 main( void ) 55 { 56 int i; 57 58 if( ( i = test1( ) ) ) 59 return i; 22 check( (int)tr_historyGet( &h, 22000, 1000 ) == 0 ); 23 check( (int)tr_historyGet( &h, 22000, 3000 ) == 1 ); 24 check( (int)tr_historyGet( &h, 22000, 15000 ) == 2 ); 25 check( (int)tr_historyGet( &h, 22000, 20000 ) == 2 ); 60 26 61 27 return 0; 62 28 } 63 29 30 MAIN_SINGLE_TEST(test1) -
libtransmission/json-test.c
diff --git a/libtransmission/json-test.c b/libtransmission/json-test.c index ab7b16e..7d8c987 100644
a b 6 6 #include "utils.h" /* tr_free */ 7 7 8 8 #undef VERBOSE 9 10 static int test = 0; 11 12 #ifdef VERBOSE 13 #define check( A ) \ 14 { \ 15 ++test; \ 16 if( A ){ \ 17 fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 18 } else { \ 19 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 20 return test; \ 21 } \ 22 } 23 #else 24 #define check( A ) \ 25 { \ 26 ++test; \ 27 if( !( A ) ){ \ 28 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 29 return test; \ 30 } \ 31 } 32 #endif 9 #include "libtransmission-test.h" 33 10 34 11 #include "ConvertUTF.h" 35 12 … … test3( void ) 176 153 int 177 154 main( void ) 178 155 { 179 int i; 180 181 if( ( i = test_utf8( ) ) ) 182 return i; 183 184 if( ( i = test1( ) ) ) 185 return i; 186 187 if( ( i = test2( ) ) ) 188 return i; 189 190 if( ( i = test3( ) ) ) 191 return i; 192 193 return 0; 156 const testFunc tests[] = { test_utf8, test1, test2, test3, }; 157 return runTests(tests, NUM_TESTS(tests)); 194 158 } 195 159 -
new file libtransmission/libtransmission-test.h
diff --git a/libtransmission/libtransmission-test.h b/libtransmission/libtransmission-test.h new file mode 100644 index 0000000..7a4021c
- + 1 /* Note VERBOSE needs to be (un)defined before including this file */ 2 3 #ifndef LIBTRANSMISSION_TEST_H 4 #define LIBTRANSMISSION_TEST_H 1 5 6 #include <stdio.h> 7 #include <string.h> 8 9 static int test = 0; 10 11 #define REPORT_TEST(test, res) \ 12 fprintf( stderr, "%s test #%d (%s, %d)\n", res, test, __FILE__, __LINE__ ) 13 14 #ifdef VERBOSE 15 #define check( A ) \ 16 do { \ 17 ++test; \ 18 if( A ) \ 19 REPORT_TEST(test, "PASS"); \ 20 else { \ 21 REPORT_TEST(test, "FAIL"); \ 22 return test; \ 23 } \ 24 } while(0) 25 #else 26 #define check( A ) \ 27 do { \ 28 ++test; \ 29 if( !( A ) ){ \ 30 REPORT_TEST(test, "FAIL"); \ 31 return test; \ 32 } \ 33 } while(0) 34 #endif 35 36 37 typedef int (*testFunc)( void ); 38 #define NUM_TESTS(tarray) ((int) (sizeof(tarray)/sizeof(tarray[0]))) 39 40 static inline int 41 runTests( const testFunc * const tests, int numTests ) 42 { 43 int ret, i; 44 45 (void) test; /* Use test even if we don't have any tests to run */ 46 47 for( i = 0; i < numTests; i++ ) 48 if( (ret = (*tests[i])()) ) 49 return ret; 50 51 return 0; /* All tests passed */ 52 } 53 54 #define MAIN_SINGLE_TEST(test) \ 55 int main( void ) \ 56 { \ 57 const testFunc tests[] = { test }; \ 58 \ 59 return runTests(tests, 1); \ 60 } 61 62 #endif /* !LIBTRANSMISSION_TEST_H */ -
libtransmission/magnet-test.c
diff --git a/libtransmission/magnet-test.c b/libtransmission/magnet-test.c index 6aabe17..e018279 100644
a b 6 6 7 7 /* #define VERBOSE */ 8 8 #undef VERBOSE 9 10 static int test = 0; 11 12 #ifdef VERBOSE 13 #define check( A ) \ 14 { \ 15 ++test; \ 16 if( A ){ \ 17 fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 18 } else { \ 19 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 20 return test; \ 21 } \ 22 } 23 #else 24 #define check( A ) \ 25 { \ 26 ++test; \ 27 if( !( A ) ){ \ 28 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 29 return test; \ 30 } \ 31 } 32 #endif 9 #include "libtransmission-test.h" 33 10 34 11 static int 35 12 test1( void ) … … test1( void ) 47 24 "&tr=http%3A%2F%2Ftracker.opentracker.org%2Fannounce" 48 25 "&ws=http%3A%2F%2Fserver.webseed.org%2Fpath%2Fto%2Ffile"; 49 26 info = tr_magnetParse( uri ); 50 check( info != NULL ) 27 check( info != NULL ); 51 28 check( info->trackerCount == 2 ); 52 check( !strcmp( info->trackers[0], "http://tracker.openbittorrent.com/announce" ) ) 53 check( !strcmp( info->trackers[1], "http://tracker.opentracker.org/announce" ) ) 29 check( !strcmp( info->trackers[0], "http://tracker.openbittorrent.com/announce" ) ); 30 check( !strcmp( info->trackers[1], "http://tracker.opentracker.org/announce" ) ); 54 31 check( info->webseedCount == 1 ); 55 check( !strcmp( info->webseeds[0], "http://server.webseed.org/path/to/file" ) ) 56 check( !strcmp( info->displayName, "Display Name" ) ) 57 for( i=0; i<20; ++i ) 32 check( !strcmp( info->webseeds[0], "http://server.webseed.org/path/to/file" ) ); 33 check( !strcmp( info->displayName, "Display Name" ) ); 34 for( i=0; i<20; ++i ); 58 35 check( info->hash[i] == dec[i] ); 59 36 tr_magnetFree( info ); 60 37 info = NULL; … … test1( void ) 67 44 "&ws=http%3A%2F%2Fserver.webseed.org%2Fpath%2Fto%2Ffile" 68 45 "&tr=http%3A%2F%2Ftracker.opentracker.org%2Fannounce"; 69 46 info = tr_magnetParse( uri ); 70 check( info != NULL ) 47 check( info != NULL ); 71 48 check( info->trackerCount == 2 ); 72 check( !strcmp( info->trackers[0], "http://tracker.openbittorrent.com/announce" ) ) 73 check( !strcmp( info->trackers[1], "http://tracker.opentracker.org/announce" ) ) 49 check( !strcmp( info->trackers[0], "http://tracker.openbittorrent.com/announce" ) ); 50 check( !strcmp( info->trackers[1], "http://tracker.opentracker.org/announce" ) ); 74 51 check( info->webseedCount == 1 ); 75 check( !strcmp( info->webseeds[0], "http://server.webseed.org/path/to/file" ) ) 76 check( !strcmp( info->displayName, "Display Name" ) ) 52 check( !strcmp( info->webseeds[0], "http://server.webseed.org/path/to/file" ) ); 53 check( !strcmp( info->displayName, "Display Name" ) ); 77 54 for( i=0; i<20; ++i ) 78 55 check( info->hash[i] == dec[i] ); 79 56 tr_magnetFree( info ); … … test1( void ) 85 62 int 86 63 main( void ) 87 64 { 88 int i; 65 const testFunc tests[] = { test1 }; 66 int ret; 89 67 90 if( ( i = test1( )) )91 return i;68 if( (ret = runTests(tests, 1)) ) 69 return ret; 92 70 93 71 #ifdef VERBOSE 94 72 fprintf( stderr, "magnet-test passed\n" ); 95 73 #endif 74 96 75 return 0; 97 76 } 98 77 -
libtransmission/peer-msgs-test.c
diff --git a/libtransmission/peer-msgs-test.c b/libtransmission/peer-msgs-test.c index a29c4f3..eef8ce8 100644
a b 4 4 #include "utils.h" 5 5 6 6 #undef VERBOSE 7 8 #ifdef VERBOSE 9 #define check( A ) \ 10 { \ 11 ++test; \ 12 if( A ){ \ 13 fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 14 } else { \ 15 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 16 return test; \ 17 } \ 18 } 19 #else 20 #define check( A ) \ 21 { \ 22 ++test; \ 23 if( !( A ) ){ \ 24 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 25 return test; \ 26 } \ 27 } 28 #endif 7 #include "libtransmission-test.h" 29 8 30 9 int 31 10 main( void ) 32 11 { 33 12 #if 0 34 13 uint32_t i; 35 int test = 0;36 14 uint8_t infohash[SHA_DIGEST_LENGTH]; 37 15 struct tr_address addr; 38 16 tr_piece_index_t pieceCount = 1313; -
libtransmission/rpc-test.c
diff --git a/libtransmission/rpc-test.c b/libtransmission/rpc-test.c index 180aef8..f7ffd65 100644
a b 6 6 #include "utils.h" 7 7 8 8 #undef VERBOSE 9 10 static int test = 0; 11 12 #ifdef VERBOSE 13 #define check( A ) \ 14 { \ 15 ++test; \ 16 if( A ){ \ 17 fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 18 } else { \ 19 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 20 return test; \ 21 } \ 22 } 23 #else 24 #define check( A ) \ 25 { \ 26 ++test; \ 27 if( !( A ) ){ \ 28 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 29 return test; \ 30 } \ 31 } 32 #endif 9 #include "libtransmission-test.h" 33 10 34 11 static int 35 12 test_list( void ) … … test_list( void ) 81 58 return 0; 82 59 } 83 60 84 int 85 main( void ) 86 { 87 int i; 88 89 if( ( i = test_list( ) ) ) 90 return i; 91 92 return 0; 93 } 94 61 MAIN_SINGLE_TEST(test_list) -
libtransmission/test-peer-id.c
diff --git a/libtransmission/test-peer-id.c b/libtransmission/test-peer-id.c index b0f46bc..096aea8 100644
a b 7 7 #include "version.h" 8 8 9 9 #undef VERBOSE 10 #include "libtransmission-test.h" 10 11 11 #ifdef VERBOSE 12 #define check( A ) \ 13 { \ 14 ++test; \ 15 if( A ){ \ 16 fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 17 } else { \ 18 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 19 return test; \ 20 } \ 21 } 22 #else 23 #define check( A ) \ 24 { \ 25 ++test; \ 26 if( !( A ) ){ \ 27 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 28 return test; \ 29 } \ 30 } 31 #endif 32 33 int 34 main( void ) 12 static int 13 testPeerId( void ) 35 14 { 36 15 int i; 37 16 int test = 0; … … main( void ) 59 38 return 0; 60 39 } 61 40 41 MAIN_SINGLE_TEST(testPeerId) -
libtransmission/utils-test.c
diff --git a/libtransmission/utils-test.c b/libtransmission/utils-test.c index f163ce2..07a213d 100644
a b 21 21 #define NUM_LOOPS 200 22 22 #endif 23 23 24 static int test = 0; 25 26 #ifdef VERBOSE 27 #define check( A ) \ 28 { \ 29 ++test; \ 30 if( A ){ \ 31 fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 32 } else { \ 33 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 34 return test; \ 35 } \ 36 } 37 #else 38 #define check( A ) \ 39 { \ 40 ++test; \ 41 if( !( A ) ){ \ 42 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ 43 return test; \ 44 } \ 45 } 46 #endif 24 #include "libtransmission-test.h" 25 26 static int 27 test_base64( void ) 28 { 29 char *in, *out; 30 int len; 31 32 /* base64 */ 33 out = tr_base64_encode( "YOYO!", -1, &len ); 34 check( out ); 35 check( !strcmp( out, "WU9ZTyE=" ) ); 36 check( len == 8 ); 37 in = tr_base64_decode( out, -1, &len ); 38 check( in ); 39 check( !strcmp( in, "YOYO!" ) ); 40 check( len == 5 ); 41 tr_free( in ); 42 tr_free( out ); 43 out = tr_base64_encode( NULL, 0, &len ); 44 check( out == NULL ); 45 check( len == 0 ); 46 47 return 0; 48 } 47 49 48 50 static int 49 51 test_bitfield_count_range( void ) … … test_strip_positional_args( void ) 153 155 in = "Hello %1$s foo %2$.*f"; 154 156 expected = "Hello %s foo %.*f"; 155 157 out = tr_strip_positional_args( in ); 156 check( out != NULL ) 157 check( !strcmp( out, expected ) ) 158 check( out != NULL ); 159 check( !strcmp( out, expected ) ); 158 160 159 161 in = "Hello %1$'d foo %2$'f"; 160 162 expected = "Hello %d foo %f"; 161 163 out = tr_strip_positional_args( in ); 162 check( out != NULL ) 163 check( !strcmp( out, expected ) ) 164 check( out != NULL ); 165 check( !strcmp( out, expected ) ); 164 166 165 167 return 0; 166 168 } … … test_utf8( void ) 218 220 219 221 in = "hello world"; 220 222 out = tr_utf8clean( in, -1 ); 221 check( out != NULL ) 222 check( !strcmp( out, in ) ) 223 check( out != NULL ); 224 check( !strcmp( out, in ) ); 223 225 tr_free( out ); 224 226 225 227 in = "hello world"; 226 228 out = tr_utf8clean( in, 5 ); 227 check( out != NULL ) 228 check( !strcmp( out, "hello" ) ) 229 check( out != NULL ); 230 check( !strcmp( out, "hello" ) ); 229 231 tr_free( out ); 230 232 231 233 /* this version is not utf-8 */ 232 234 in = "Òðóäíî áûòü Áîãîì"; 233 235 out = tr_utf8clean( in, 17 ); 234 check( out != NULL ) 235 check( ( strlen( out ) == 17 ) || ( strlen( out ) == 32 ) ) 236 check( tr_utf8_validate( out, -1, NULL ) ) 236 check( out != NULL ); 237 check( ( strlen( out ) == 17 ) || ( strlen( out ) == 32 ) ); 238 check( tr_utf8_validate( out, -1, NULL ) ); 237 239 tr_free( out ); 238 240 239 241 /* same string, but utf-8 clean */ 240 242 in = "ÃðóÀÃî áûòÌ Ãîãîì"; 241 243 out = tr_utf8clean( in, -1 ); 242 check( out != NULL ) 243 check( tr_utf8_validate( out, -1, NULL ) ) 244 check ( !strcmp( in, out ) ) 244 check( out != NULL ); 245 check( tr_utf8_validate( out, -1, NULL ) ); 246 check ( !strcmp( in, out ) ); 245 247 tr_free( out ); 246 248 247 249 return 0; … … test_lowerbound( void ) 317 319 else 318 320 fprintf( stderr, "which is off the end.\n" ); 319 321 #endif 320 check( pos == expected_pos[i-1] ) 321 check( exact == expected_exact[i-1] ) 322 check( pos == expected_pos[i-1] ); 323 check( exact == expected_exact[i-1] ); 322 324 } 323 325 324 326 return 0; … … test_memmem( void ) 330 332 char const haystack[12] = "abcabcabcabc"; 331 333 char const needle[3] = "cab"; 332 334 333 check( tr_memmem( haystack, sizeof haystack, haystack, sizeof haystack) == haystack ) 334 check( tr_memmem( haystack, sizeof haystack, needle, sizeof needle) == haystack + 2 ) 335 check( tr_memmem( needle, sizeof needle, haystack, sizeof haystack) == NULL ) 335 check( tr_memmem( haystack, sizeof haystack, haystack, sizeof haystack) == haystack ); 336 check( tr_memmem( haystack, sizeof haystack, needle, sizeof needle) == haystack + 2 ); 337 check( tr_memmem( needle, sizeof needle, haystack, sizeof haystack) == NULL ); 336 338 337 339 return 0; 338 340 } … … test_hex( void ) 348 350 memcpy( hex1, "fb5ef5507427b17e04b69cef31fa3379b456735a", 41 ); 349 351 tr_hex_to_sha1( sha1, hex1 ); 350 352 tr_sha1_to_hex( hex2, sha1 ); 351 check( !strcmp( hex1, hex2 ) ) 353 check( !strcmp( hex1, hex2 ) ); 352 354 353 355 return 0; 354 356 } … … test_url( void ) 387 389 388 390 url = "http://1"; 389 391 check( !tr_urlParse( url, -1, &scheme, &host, &port, &path ) ); 390 check( !strcmp( scheme, "http" ) ) 391 check( !strcmp( host, "1" ) ) 392 check( !strcmp( path, "/" ) ) 393 check( port == 80 ) 392 check( !strcmp( scheme, "http" ) ); 393 check( !strcmp( host, "1" ) ); 394 check( !strcmp( path, "/" ) ); 395 check( port == 80 ); 394 396 tr_free( scheme ); 395 397 tr_free( path ); 396 398 tr_free( host ); 397 399 398 400 url = "http://www.some-tracker.org/some/path"; 399 check( !tr_urlParse( url, -1, &scheme, &host, &port, &path ) ) 400 check( !strcmp( scheme, "http" ) ) 401 check( !strcmp( host, "www.some-tracker.org" ) ) 402 check( !strcmp( path, "/some/path" ) ) 403 check( port == 80 ) 401 check( !tr_urlParse( url, -1, &scheme, &host, &port, &path ) ); 402 check( !strcmp( scheme, "http" ) ); 403 check( !strcmp( host, "www.some-tracker.org" ) ); 404 check( !strcmp( path, "/some/path" ) ); 405 check( port == 80 ); 404 406 tr_free( scheme ); 405 407 tr_free( path ); 406 408 tr_free( host ); 407 409 408 410 url = "http://www.some-tracker.org:80/some/path"; 409 check( !tr_urlParse( url, -1, &scheme, &host, &port, &path ) ) 410 check( !strcmp( scheme, "http" ) ) 411 check( !strcmp( host, "www.some-tracker.org" ) ) 412 check( !strcmp( path, "/some/path" ) ) 413 check( port == 80 ) 411 check( !tr_urlParse( url, -1, &scheme, &host, &port, &path ) ); 412 check( !strcmp( scheme, "http" ) ); 413 check( !strcmp( host, "www.some-tracker.org" ) ); 414 check( !strcmp( path, "/some/path" ) ); 415 check( port == 80 ); 414 416 tr_free( scheme ); 415 417 tr_free( path ); 416 418 tr_free( host ); 417 419 418 420 url = "http%3A%2F%2Fwww.example.com%2F~user%2F%3Ftest%3D1%26test1%3D2"; 419 421 str = tr_http_unescape( url, strlen( url ) ); 420 check( !strcmp( str, "http://www.example.com/~user/?test=1&test1=2" ) ) 422 check( !strcmp( str, "http://www.example.com/~user/?test=1&test1=2" ) ); 421 423 tr_free( str ); 422 424 423 425 return 0; … … test_truncd( void ) 456 458 return 0; 457 459 } 458 460 461 static int 462 test_cryptoRand( void ) 463 { 464 int i; 465 466 /* test that tr_cryptoRandInt() stays in-bounds */ 467 for( i = 0; i < 100000; ++i ) 468 { 469 const int val = tr_cryptoRandInt( 100 ); 470 check( val >= 0 ); 471 check( val < 100 ); 472 } 473 474 return 0; 475 } 476 459 477 struct blah 460 478 { 461 479 uint8_t hash[SHA_DIGEST_LENGTH]; /* pieces hash */ … … struct blah 464 482 time_t timeChecked; /* the last time we tested this piece */ 465 483 }; 466 484 467 468 485 int 469 486 main( void ) 470 487 { 471 char *in, *out; 472 int len; 473 int i; 488 const testFunc tests[] = { 489 test_base64, test_hex, test_lowerbound, test_strip_positional_args, 490 test_strstrip, test_buildpath, test_utf8, test_numbers, test_memmem, 491 test_array, test_url, test_truncd, test_cryptoRand, 492 }; 493 int ret; 474 494 int l; 475 495 476 /* base64 */ 477 out = tr_base64_encode( "YOYO!", -1, &len ); 478 check( out ); 479 check( !strcmp( out, "WU9ZTyE=" ) ); 480 check( len == 8 ); 481 in = tr_base64_decode( out, -1, &len ); 482 check( in ); 483 check( !strcmp( in, "YOYO!" ) ); 484 check( len == 5 ); 485 tr_free( in ); 486 tr_free( out ); 487 out = tr_base64_encode( NULL, 0, &len ); 488 check( out == NULL ); 489 check( len == 0 ); 490 491 if( ( i = test_hex( ) ) ) 492 return i; 493 if( ( i = test_lowerbound( ) ) ) 494 return i; 495 if( ( i = test_strip_positional_args( ) ) ) 496 return i; 497 if( ( i = test_strstrip( ) ) ) 498 return i; 499 if( ( i = test_buildpath( ) ) ) 500 return i; 501 if( ( i = test_utf8( ) ) ) 502 return i; 503 if( ( i = test_numbers( ) ) ) 504 return i; 505 if( ( i = test_memmem( ) ) ) 506 return i; 507 if( ( i = test_array( ) ) ) 508 return i; 509 if( ( i = test_url( ) ) ) 510 return i; 511 if( ( i = test_truncd( ) ) ) 512 return i; 513 514 /* test that tr_cryptoRandInt() stays in-bounds */ 515 for( i = 0; i < 100000; ++i ) 516 { 517 const int val = tr_cryptoRandInt( 100 ); 518 check( val >= 0 ); 519 check( val < 100 ); 520 } 496 if( (ret = runTests(tests, NUM_TESTS(tests))) ) 497 return ret; 521 498 522 499 /* simple bitfield tests */ 523 500 for( l = 0; l < NUM_LOOPS; ++l ) 524 if( ( i= test_bitfields( ) ) )525 return i;501 if( ( ret = test_bitfields( ) ) ) 502 return ret; 526 503 527 504 /* bitfield count range */ 528 505 for( l=0; l<10000; ++l ) 529 if(( i= test_bitfield_count_range( )))530 return i;506 if(( ret = test_bitfield_count_range( ))) 507 return ret; 531 508 532 509 return 0; 533 510 }