#include #include /* fprintf */ #include /* strcmp */ #include "transmission.h" #include "bitfield.h" #include "ConvertUTF.h" /* tr_utf8_validate*/ #include "platform.h" #include "crypto.h" #include "utils.h" #include "web.h" /* #define VERBOSE */ #undef VERBOSE #define NUM_LOOPS 1 #define SPEED_TEST 0 #if SPEED_TEST #define VERBOSE #undef NUM_LOOPS #define NUM_LOOPS 200 #endif #include "libtransmission-test.h" static int test_base64( void ) { char *in, *out; int len; /* base64 */ out = tr_base64_encode( "YOYO!", -1, &len ); check( out ); check( !strcmp( out, "WU9ZTyE=" ) ); check( len == 8 ); in = tr_base64_decode( out, -1, &len ); check( in ); check( !strcmp( in, "YOYO!" ) ); check( len == 5 ); tr_free( in ); tr_free( out ); out = tr_base64_encode( NULL, 0, &len ); check( out == NULL ); check( len == 0 ); return 0; } static int test_bitfield_count_range( void ) { int i; int n; int begin; int end; int count1; int count2; const int bitCount = 100 + tr_cryptoWeakRandInt( 1000 ); tr_bitfield bf; /* generate a random bitfield */ tr_bitfieldConstruct( &bf, bitCount ); for( i=0, n=tr_cryptoWeakRandInt(bitCount); i= 21 ) ) ); /* test tr_bitfieldRemRange on the boundaries */ tr_bitfieldAddRange( &field, 0, 64 ); tr_bitfieldRemRange( &field, 8, 24 ); for( i = 0; i < 64; ++i ) check( tr_bitfieldHas( &field, i ) == ( ( i < 8 ) || ( i >= 24 ) ) ); /* test tr_bitfieldRemRange when begin & end is on the same word */ tr_bitfieldAddRange( &field, 0, 64 ); tr_bitfieldRemRange( &field, 4, 5 ); for( i = 0; i < 64; ++i ) check( tr_bitfieldHas( &field, i ) == ( ( i < 4 ) || ( i >= 5 ) ) ); /* test tr_bitfieldAddRange */ tr_bitfieldRemRange( &field, 0, 64 ); tr_bitfieldAddRange( &field, 4, 21 ); for( i = 0; i < 64; ++i ) check( tr_bitfieldHas( &field, i ) == ( ( 4 <= i ) && ( i < 21 ) ) ); /* test tr_bitfieldAddRange on the boundaries */ tr_bitfieldRemRange( &field, 0, 64 ); tr_bitfieldAddRange( &field, 8, 24 ); for( i = 0; i < 64; ++i ) check( tr_bitfieldHas( &field, i ) == ( ( 8 <= i ) && ( i < 24 ) ) ); /* test tr_bitfieldAddRange when begin & end is on the same word */ tr_bitfieldRemRange( &field, 0, 64 ); tr_bitfieldAddRange( &field, 4, 5 ); for( i = 0; i < 64; ++i ) check( tr_bitfieldHas( &field, i ) == ( ( 4 <= i ) && ( i < 5 ) ) ); tr_bitfieldDestruct( &field ); return 0; } static int test_strip_positional_args( void ) { const char * in; const char * out; const char * expected; in = "Hello %1$s foo %2$.*f"; expected = "Hello %s foo %.*f"; out = tr_strip_positional_args( in ); check( out != NULL ); check( !strcmp( out, expected ) ); in = "Hello %1$'d foo %2$'f"; expected = "Hello %d foo %f"; out = tr_strip_positional_args( in ); check( out != NULL ); check( !strcmp( out, expected ) ); return 0; } static int test_strstrip( void ) { char *in, *out; /* strstrip */ in = tr_strdup( " test " ); out = tr_strstrip( in ); check( in == out ); check( !strcmp( in, "test" ) ); tr_free( in ); /* strstrip */ in = tr_strdup( " test test " ); out = tr_strstrip( in ); check( in == out ); check( !strcmp( in, "test test" ) ); tr_free( in ); /* strstrip */ in = tr_strdup( "test" ); out = tr_strstrip( in ); check( in == out ); check( !strcmp( in, "test" ) ); tr_free( in ); return 0; } static int test_buildpath( void ) { char * out; out = tr_buildPath( "foo", "bar", NULL ); check( !strcmp( out, "foo" TR_PATH_DELIMITER_STR "bar" ) ); tr_free( out ); out = tr_buildPath( "", "foo", "bar", NULL ); check( !strcmp( out, TR_PATH_DELIMITER_STR "foo" TR_PATH_DELIMITER_STR "bar" ) ); tr_free( out ); return 0; } static int test_utf8( void ) { const char * in; char * out; in = "hello world"; out = tr_utf8clean( in, -1 ); check( out != NULL ); check( !strcmp( out, in ) ); tr_free( out ); in = "hello world"; out = tr_utf8clean( in, 5 ); check( out != NULL ); check( !strcmp( out, "hello" ) ); tr_free( out ); /* this version is not utf-8 */ in = "Òðóäíî áûòü Áîãîì"; out = tr_utf8clean( in, 17 ); check( out != NULL ); check( ( strlen( out ) == 17 ) || ( strlen( out ) == 32 ) ); check( tr_utf8_validate( out, -1, NULL ) ); tr_free( out ); /* same string, but utf-8 clean */ in = "Òðóäíî áûòü Ãîãîì"; out = tr_utf8clean( in, -1 ); check( out != NULL ); check( tr_utf8_validate( out, -1, NULL ) ); check ( !strcmp( in, out ) ); tr_free( out ); return 0; } static int test_numbers( void ) { int i; int count; int * numbers; numbers = tr_parseNumberRange( "1-10,13,16-19", -1, &count ); check( count == 15 ); check( numbers != NULL ); check( numbers[0] == 1 ); check( numbers[5] == 6 ); check( numbers[9] == 10 ); check( numbers[10] == 13 ); check( numbers[11] == 16 ); check( numbers[14] == 19 ); tr_free( numbers ); numbers = tr_parseNumberRange( "1-5,3-7,2-6", -1, &count ); check( count == 7 ); check( numbers != NULL ); for( i=0; i= 0 ); check( val < 100 ); } return 0; } struct blah { uint8_t hash[SHA_DIGEST_LENGTH]; /* pieces hash */ int8_t priority; /* TR_PRI_HIGH, _NORMAL, or _LOW */ int8_t dnd; /* "do not download" flag */ time_t timeChecked; /* the last time we tested this piece */ }; int main( void ) { const testFunc tests[] = { test_base64, test_hex, test_lowerbound, test_strip_positional_args, test_strstrip, test_buildpath, test_utf8, test_numbers, test_memmem, test_array, test_url, test_truncd, test_cryptoRand, }; int ret; int l; if( (ret = runTests(tests, NUM_TESTS(tests))) ) return ret; /* simple bitfield tests */ for( l = 0; l < NUM_LOOPS; ++l ) if( ( ret = test_bitfields( ) ) ) return ret; /* bitfield count range */ for( l=0; l<10000; ++l ) if(( ret = test_bitfield_count_range( ))) return ret; return 0; }