2008-05-20 17:33:54 +00:00
|
|
|
|
#include <stdio.h> /* fprintf */
|
|
|
|
|
#include <string.h> /* strcmp */
|
|
|
|
|
#include "transmission.h"
|
2009-01-10 22:48:58 +00:00
|
|
|
|
#include "ConvertUTF.h" /* tr_utf8_validate*/
|
2008-10-30 19:47:00 +00:00
|
|
|
|
#include "platform.h"
|
2008-05-20 17:33:54 +00:00
|
|
|
|
#include "utils.h"
|
2008-09-06 02:09:53 +00:00
|
|
|
|
#include "crypto.h"
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
2009-01-23 18:44:15 +00:00
|
|
|
|
#undef VERBOSE
|
2008-06-02 15:52:16 +00:00
|
|
|
|
#define NUM_LOOPS 1
|
|
|
|
|
#define SPEED_TEST 0
|
|
|
|
|
|
|
|
|
|
#if SPEED_TEST
|
2009-01-23 18:44:15 +00:00
|
|
|
|
#define VERBOSE
|
2008-09-23 19:11:04 +00:00
|
|
|
|
#undef NUM_LOOPS
|
|
|
|
|
#define NUM_LOOPS 200
|
2008-06-02 15:52:16 +00:00
|
|
|
|
#endif
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
2008-08-11 19:05:02 +00:00
|
|
|
|
static int test = 0;
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
2009-01-23 18:44:15 +00:00
|
|
|
|
#ifdef VERBOSE
|
|
|
|
|
#define check( A ) \
|
2008-09-23 19:11:04 +00:00
|
|
|
|
{ \
|
|
|
|
|
++test; \
|
|
|
|
|
if( A ){ \
|
2009-01-23 18:44:15 +00:00
|
|
|
|
fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
|
2008-09-23 19:11:04 +00:00
|
|
|
|
} else { \
|
2009-01-23 18:44:15 +00:00
|
|
|
|
fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
|
2008-09-23 19:11:04 +00:00
|
|
|
|
return test; \
|
|
|
|
|
} \
|
|
|
|
|
}
|
2009-01-23 18:44:15 +00:00
|
|
|
|
#else
|
|
|
|
|
#define check( A ) \
|
|
|
|
|
{ \
|
|
|
|
|
++test; \
|
|
|
|
|
if( !( A ) ){ \
|
|
|
|
|
fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
|
|
|
|
|
return test; \
|
|
|
|
|
} \
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
2008-06-02 15:52:16 +00:00
|
|
|
|
static int
|
|
|
|
|
test_bitfields( void )
|
2008-09-23 19:11:04 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned int i;
|
|
|
|
|
unsigned int bitcount = 5000000;
|
2008-06-02 15:52:16 +00:00
|
|
|
|
tr_bitfield * field = tr_bitfieldNew( bitcount );
|
|
|
|
|
|
2008-08-13 18:55:27 +00:00
|
|
|
|
/* test tr_bitfieldAdd */
|
2008-09-23 19:11:04 +00:00
|
|
|
|
for( i = 0; i < bitcount; ++i )
|
2008-06-02 15:52:16 +00:00
|
|
|
|
if( !( i % 7 ) )
|
|
|
|
|
tr_bitfieldAdd( field, i );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
for( i = 0; i < bitcount; ++i )
|
|
|
|
|
check( tr_bitfieldHas( field, i ) == ( !( i % 7 ) ) );
|
2008-06-02 15:52:16 +00:00
|
|
|
|
|
2008-08-13 18:55:27 +00:00
|
|
|
|
/* test tr_bitfieldAddRange */
|
|
|
|
|
tr_bitfieldAddRange( field, 0, bitcount );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
for( i = 0; i < bitcount; ++i )
|
2008-08-13 18:55:27 +00:00
|
|
|
|
check( tr_bitfieldHas( field, i ) );
|
|
|
|
|
|
|
|
|
|
/* test tr_bitfieldRemRange in the middle of a boundary */
|
|
|
|
|
tr_bitfieldRemRange( field, 4, 21 );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
for( i = 0; i < 64; ++i )
|
2008-08-13 18:55:27 +00:00
|
|
|
|
check( tr_bitfieldHas( field, i ) == ( ( i < 4 ) || ( i >= 21 ) ) );
|
|
|
|
|
|
|
|
|
|
/* test tr_bitfieldRemRange on the boundaries */
|
|
|
|
|
tr_bitfieldAddRange( field, 0, 64 );
|
|
|
|
|
tr_bitfieldRemRange( field, 8, 24 );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
for( i = 0; i < 64; ++i )
|
2008-08-13 18:55:27 +00:00
|
|
|
|
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 );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
for( i = 0; i < 64; ++i )
|
2008-08-13 18:55:27 +00:00
|
|
|
|
check( tr_bitfieldHas( field, i ) == ( ( i < 4 ) || ( i >= 5 ) ) );
|
2008-06-07 14:41:31 +00:00
|
|
|
|
|
2008-08-14 14:38:13 +00:00
|
|
|
|
/* test tr_bitfieldAddRange */
|
|
|
|
|
tr_bitfieldRemRange( field, 0, 64 );
|
|
|
|
|
tr_bitfieldAddRange( field, 4, 21 );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
for( i = 0; i < 64; ++i )
|
2008-08-14 14:38:13 +00:00
|
|
|
|
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 );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
for( i = 0; i < 64; ++i )
|
2008-08-14 14:38:13 +00:00
|
|
|
|
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 );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
for( i = 0; i < 64; ++i )
|
2008-08-14 14:38:13 +00:00
|
|
|
|
check( tr_bitfieldHas( field, i ) == ( ( 4 <= i ) && ( i < 5 ) ) );
|
|
|
|
|
|
2008-06-02 15:52:16 +00:00
|
|
|
|
tr_bitfieldFree( field );
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-05 19:11:30 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-30 19:47:00 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-10 22:48:58 +00:00
|
|
|
|
static int
|
|
|
|
|
test_utf8( void )
|
|
|
|
|
{
|
|
|
|
|
const char * in;
|
|
|
|
|
char * out;
|
|
|
|
|
tr_bool err;
|
|
|
|
|
|
|
|
|
|
in = "hello world";
|
|
|
|
|
out = tr_utf8clean( in, -1, &err );
|
|
|
|
|
check( err == FALSE )
|
|
|
|
|
check( out != NULL )
|
|
|
|
|
check( !strcmp( out, in ) )
|
|
|
|
|
tr_free( out );
|
|
|
|
|
|
|
|
|
|
in = "hello world";
|
|
|
|
|
out = tr_utf8clean( in, 5, &err );
|
|
|
|
|
check( err == FALSE )
|
|
|
|
|
check( out != NULL )
|
|
|
|
|
check( !strcmp( out, "hello" ) )
|
|
|
|
|
tr_free( out );
|
|
|
|
|
|
|
|
|
|
/* this version is not utf-8 */
|
|
|
|
|
in = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>";
|
|
|
|
|
out = tr_utf8clean( in, 17, &err );
|
|
|
|
|
check( out != NULL )
|
|
|
|
|
check( err != 0 )
|
|
|
|
|
check( strlen( out ) == 17 )
|
|
|
|
|
check( tr_utf8_validate( out, -1, NULL ) )
|
|
|
|
|
tr_free( out );
|
|
|
|
|
|
|
|
|
|
/* same string, but utf-8 clean */
|
|
|
|
|
in = "Òðóäíî áûòü Áîãîì";
|
|
|
|
|
out = tr_utf8clean( in, -1, &err );
|
|
|
|
|
check( out != NULL )
|
|
|
|
|
check( !err );
|
|
|
|
|
check( tr_utf8_validate( out, -1, NULL ) )
|
|
|
|
|
check ( !strcmp( in, out ) )
|
|
|
|
|
tr_free( out );
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-20 17:33:54 +00:00
|
|
|
|
int
|
|
|
|
|
main( void )
|
|
|
|
|
{
|
|
|
|
|
char *in, *out;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
int len;
|
|
|
|
|
int i;
|
|
|
|
|
int l;
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
|
|
|
|
/* base64 */
|
2009-01-23 18:44:15 +00:00
|
|
|
|
out = tr_base64_encode( "YOYO!", -1, &len );
|
2008-05-20 17:33:54 +00:00
|
|
|
|
check( out );
|
|
|
|
|
check( !strcmp( out, "WU9ZTyE=\n" ) );
|
|
|
|
|
check( len == 9 );
|
|
|
|
|
in = tr_base64_decode( out, -1, &len );
|
|
|
|
|
check( in );
|
|
|
|
|
check( !strcmp( in, "YOYO!" ) );
|
|
|
|
|
check( len == 5 );
|
|
|
|
|
tr_free( in );
|
|
|
|
|
tr_free( out );
|
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
|
if( ( i = test_strstrip( ) ) )
|
2008-09-05 19:11:30 +00:00
|
|
|
|
return i;
|
2008-10-30 19:47:00 +00:00
|
|
|
|
if( ( i = test_buildpath( ) ) )
|
|
|
|
|
return i;
|
2009-01-10 22:48:58 +00:00
|
|
|
|
if( ( i = test_utf8( ) ) )
|
|
|
|
|
return i;
|
2008-09-05 19:11:30 +00:00
|
|
|
|
|
2008-09-06 02:04:37 +00:00
|
|
|
|
/* test that tr_cryptoRandInt() stays in-bounds */
|
2008-09-23 19:11:04 +00:00
|
|
|
|
for( i = 0; i < 100000; ++i )
|
|
|
|
|
{
|
2008-09-06 02:04:37 +00:00
|
|
|
|
const int val = tr_cryptoRandInt( 100 );
|
2008-09-06 02:08:43 +00:00
|
|
|
|
check( val >= 0 );
|
|
|
|
|
check( val < 100 );
|
2008-09-06 02:04:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-06-02 15:52:16 +00:00
|
|
|
|
/* simple bitfield tests */
|
2008-09-23 19:11:04 +00:00
|
|
|
|
for( l = 0; l < NUM_LOOPS; ++l )
|
|
|
|
|
if( ( i = test_bitfields( ) ) )
|
2008-06-02 15:52:16 +00:00
|
|
|
|
return i;
|
|
|
|
|
|
2008-05-20 17:33:54 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|