2010-10-21 23:47:23 +00:00
|
|
|
|
#include <math.h>
|
2008-05-20 17:33:54 +00:00
|
|
|
|
#include <stdio.h> /* fprintf */
|
|
|
|
|
#include <string.h> /* strcmp */
|
2009-06-15 00:11:06 +00:00
|
|
|
|
|
2008-05-20 17:33:54 +00:00
|
|
|
|
#include "transmission.h"
|
2009-06-15 00:11:06 +00:00
|
|
|
|
#include "bitfield.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-09-06 02:09:53 +00:00
|
|
|
|
#include "crypto.h"
|
2009-12-10 10:44:06 +00:00
|
|
|
|
#include "utils.h"
|
|
|
|
|
#include "web.h"
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
2009-11-20 04:38:19 +00:00
|
|
|
|
/* #define VERBOSE */
|
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
|
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
|
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 */
|
|
|
|
|
bf = tr_bitfieldNew( bitCount );
|
|
|
|
|
for( i=0, n=tr_cryptoWeakRandInt(bitCount); i<n; ++i )
|
|
|
|
|
tr_bitfieldAdd( bf, tr_cryptoWeakRandInt(bitCount) );
|
|
|
|
|
|
|
|
|
|
begin = tr_cryptoWeakRandInt( bitCount );
|
|
|
|
|
do {
|
|
|
|
|
end = tr_cryptoWeakRandInt( bitCount );
|
|
|
|
|
} while( end == begin );
|
|
|
|
|
if( end < begin ) {
|
|
|
|
|
const int tmp = begin;
|
|
|
|
|
begin = end;
|
|
|
|
|
end = tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
count1 = 0;
|
|
|
|
|
for( i=begin; i<end; ++i )
|
|
|
|
|
if( tr_bitfieldHas( bf, i ) )
|
|
|
|
|
++count1;
|
|
|
|
|
count2 = tr_bitfieldCountRange( bf, begin, end );
|
|
|
|
|
check( count1 == count2 );
|
|
|
|
|
|
|
|
|
|
tr_bitfieldFree( bf );
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-25 01:13:35 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
in = "hello world";
|
2010-07-07 16:48:23 +00:00
|
|
|
|
out = tr_utf8clean( in, -1 );
|
2009-01-10 22:48:58 +00:00
|
|
|
|
check( out != NULL )
|
|
|
|
|
check( !strcmp( out, in ) )
|
|
|
|
|
tr_free( out );
|
|
|
|
|
|
|
|
|
|
in = "hello world";
|
2010-07-07 16:48:23 +00:00
|
|
|
|
out = tr_utf8clean( in, 5 );
|
2009-01-10 22:48:58 +00:00
|
|
|
|
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>";
|
2010-07-07 16:48:23 +00:00
|
|
|
|
out = tr_utf8clean( in, 17 );
|
2009-01-10 22:48:58 +00:00
|
|
|
|
check( out != NULL )
|
2010-07-07 17:08:38 +00:00
|
|
|
|
check( ( strlen( out ) == 17 ) || ( strlen( out ) == 32 ) )
|
2009-01-10 22:48:58 +00:00
|
|
|
|
check( tr_utf8_validate( out, -1, NULL ) )
|
|
|
|
|
tr_free( out );
|
|
|
|
|
|
|
|
|
|
/* same string, but utf-8 clean */
|
|
|
|
|
in = "Òðóäíî áûòü Áîãîì";
|
2010-07-07 16:48:23 +00:00
|
|
|
|
out = tr_utf8clean( in, -1 );
|
2009-01-10 22:48:58 +00:00
|
|
|
|
check( out != NULL )
|
|
|
|
|
check( tr_utf8_validate( out, -1, NULL ) )
|
|
|
|
|
check ( !strcmp( in, out ) )
|
|
|
|
|
tr_free( out );
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-09 17:25:48 +00:00
|
|
|
|
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<count; ++i )
|
|
|
|
|
check( numbers[i] == i+1 );
|
|
|
|
|
tr_free( numbers );
|
|
|
|
|
|
|
|
|
|
numbers = tr_parseNumberRange( "1-Hello", -1, &count );
|
|
|
|
|
check( count == 0 );
|
|
|
|
|
check( numbers == NULL );
|
|
|
|
|
|
|
|
|
|
numbers = tr_parseNumberRange( "1-", -1, &count );
|
|
|
|
|
check( count == 0 );
|
|
|
|
|
check( numbers == NULL );
|
|
|
|
|
|
|
|
|
|
numbers = tr_parseNumberRange( "Hello", -1, &count );
|
|
|
|
|
check( count == 0 );
|
|
|
|
|
check( numbers == NULL );
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-31 22:16:06 +00:00
|
|
|
|
static int
|
|
|
|
|
compareInts( const void * va, const void * vb )
|
|
|
|
|
{
|
|
|
|
|
const int a = *(const int *)va;
|
|
|
|
|
const int b = *(const int*)vb;
|
|
|
|
|
return a - b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
test_lowerbound( void )
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
const int A[] = { 1, 2, 3, 3, 3, 5, 8 };
|
|
|
|
|
const int expected_pos[] = { 0, 1, 2, 5, 5, 6, 6, 6, 7, 7 };
|
2011-03-22 15:19:54 +00:00
|
|
|
|
const int expected_exact[] = { true, true, true, false, true, false, false, true, false, false };
|
2009-10-31 22:16:06 +00:00
|
|
|
|
const int N = sizeof(A) / sizeof(A[0]);
|
|
|
|
|
|
|
|
|
|
for( i=1; i<=10; ++i )
|
|
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
|
bool exact;
|
2009-10-31 22:16:06 +00:00
|
|
|
|
const int pos = tr_lowerBound( &i, A, N, sizeof(int), compareInts, &exact );
|
|
|
|
|
|
|
|
|
|
#if 0
|
2010-12-27 19:18:17 +00:00
|
|
|
|
fprintf( stderr, "searching for %d. ", i );
|
2009-10-31 22:16:06 +00:00
|
|
|
|
fprintf( stderr, "result: index = %d, ", pos );
|
|
|
|
|
if( pos != N )
|
|
|
|
|
fprintf( stderr, "A[%d] == %d\n", pos, A[pos] );
|
|
|
|
|
else
|
|
|
|
|
fprintf( stderr, "which is off the end.\n" );
|
|
|
|
|
#endif
|
|
|
|
|
check( pos == expected_pos[i-1] )
|
|
|
|
|
check( exact == expected_exact[i-1] )
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-22 05:35:51 +00:00
|
|
|
|
static int
|
|
|
|
|
test_memmem( void )
|
|
|
|
|
{
|
|
|
|
|
char const haystack[12] = "abcabcabcabc";
|
|
|
|
|
char const needle[3] = "cab";
|
|
|
|
|
|
|
|
|
|
check( tr_memmem( haystack, sizeof haystack, haystack, sizeof haystack) == haystack )
|
|
|
|
|
check( tr_memmem( haystack, sizeof haystack, needle, sizeof needle) == haystack + 2 )
|
|
|
|
|
check( tr_memmem( needle, sizeof needle, haystack, sizeof haystack) == NULL )
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-20 04:38:19 +00:00
|
|
|
|
static int
|
|
|
|
|
test_hex( void )
|
|
|
|
|
{
|
|
|
|
|
char hex1[41];
|
|
|
|
|
char hex2[41];
|
|
|
|
|
uint8_t sha1[20];
|
|
|
|
|
/*uint8_t sha2[20];*/
|
|
|
|
|
|
|
|
|
|
memcpy( hex1, "fb5ef5507427b17e04b69cef31fa3379b456735a", 41 );
|
|
|
|
|
tr_hex_to_sha1( sha1, hex1 );
|
|
|
|
|
tr_sha1_to_hex( hex2, sha1 );
|
|
|
|
|
check( !strcmp( hex1, hex2 ) )
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-24 02:16:31 +00:00
|
|
|
|
static int
|
|
|
|
|
test_array( void )
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
|
|
|
|
int n = sizeof( array ) / sizeof( array[0] );
|
|
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
|
tr_removeElementFromArray( array, 5u, sizeof( int ), n-- );
|
2009-11-24 02:16:31 +00:00
|
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
|
check( array[i] == ( i<5 ? i : i+1 ) );
|
|
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
|
tr_removeElementFromArray( array, 0u, sizeof( int ), n-- );
|
2009-11-24 02:16:31 +00:00
|
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
|
check( array[i] == ( i<4 ? i+1 : i+2 ) );
|
|
|
|
|
|
|
|
|
|
tr_removeElementFromArray( array, n-1, sizeof( int ), n ); n--;
|
|
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
|
check( array[i] == ( i<4 ? i+1 : i+2 ) );
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-10 09:13:08 +00:00
|
|
|
|
static int
|
|
|
|
|
test_url( void )
|
|
|
|
|
{
|
|
|
|
|
int port;
|
2010-02-20 15:57:05 +00:00
|
|
|
|
char * scheme;
|
2009-12-10 09:13:08 +00:00
|
|
|
|
char * host;
|
|
|
|
|
char * path;
|
2009-12-10 10:44:06 +00:00
|
|
|
|
char * str;
|
2009-12-10 09:13:08 +00:00
|
|
|
|
const char * url;
|
|
|
|
|
|
|
|
|
|
url = "http://www.some-tracker.org/some/path";
|
2010-02-20 15:57:05 +00:00
|
|
|
|
check( !tr_urlParse( url, -1, &scheme, &host, &port, &path ) )
|
|
|
|
|
check( !strcmp( scheme, "http" ) )
|
2009-12-10 09:13:08 +00:00
|
|
|
|
check( !strcmp( host, "www.some-tracker.org" ) )
|
|
|
|
|
check( !strcmp( path, "/some/path" ) )
|
|
|
|
|
check( port == 80 )
|
2010-02-20 15:57:05 +00:00
|
|
|
|
tr_free( scheme );
|
2009-12-10 10:44:06 +00:00
|
|
|
|
tr_free( path );
|
|
|
|
|
tr_free( host );
|
2009-12-10 09:13:08 +00:00
|
|
|
|
|
|
|
|
|
url = "http://www.some-tracker.org:80/some/path";
|
2010-02-20 15:57:05 +00:00
|
|
|
|
check( !tr_urlParse( url, -1, &scheme, &host, &port, &path ) )
|
|
|
|
|
check( !strcmp( scheme, "http" ) )
|
2009-12-10 09:13:08 +00:00
|
|
|
|
check( !strcmp( host, "www.some-tracker.org" ) )
|
|
|
|
|
check( !strcmp( path, "/some/path" ) )
|
|
|
|
|
check( port == 80 )
|
2010-02-20 15:57:05 +00:00
|
|
|
|
tr_free( scheme );
|
2009-12-10 10:44:06 +00:00
|
|
|
|
tr_free( path );
|
|
|
|
|
tr_free( host );
|
2009-12-10 09:13:08 +00:00
|
|
|
|
|
2009-12-10 10:44:06 +00:00
|
|
|
|
url = "http%3A%2F%2Fwww.example.com%2F~user%2F%3Ftest%3D1%26test1%3D2";
|
|
|
|
|
str = tr_http_unescape( url, strlen( url ) );
|
|
|
|
|
check( !strcmp( str, "http://www.example.com/~user/?test=1&test1=2" ) )
|
|
|
|
|
tr_free( str );
|
2009-12-10 09:13:08 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-24 17:09:39 +00:00
|
|
|
|
static int
|
|
|
|
|
test_truncd( void )
|
2008-05-20 17:33:54 +00:00
|
|
|
|
{
|
2009-07-14 20:35:48 +00:00
|
|
|
|
char buf[32];
|
2010-10-21 23:47:23 +00:00
|
|
|
|
const double nan = sqrt( -1 );
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
2009-07-14 20:35:48 +00:00
|
|
|
|
tr_snprintf( buf, sizeof( buf ), "%.2f%%", 99.999 );
|
|
|
|
|
check( !strcmp( buf, "100.00%" ) );
|
2010-07-24 17:09:39 +00:00
|
|
|
|
|
2009-07-14 20:35:48 +00:00
|
|
|
|
tr_snprintf( buf, sizeof( buf ), "%.2f%%", tr_truncd( 99.999, 2 ) );
|
|
|
|
|
check( !strcmp( buf, "99.99%" ) );
|
|
|
|
|
|
2010-07-24 17:09:39 +00:00
|
|
|
|
tr_snprintf( buf, sizeof( buf ), "%.4f", tr_truncd( 403650.656250, 4 ) );
|
|
|
|
|
check( !strcmp( buf, "403650.6562" ) );
|
|
|
|
|
|
2010-09-22 16:09:36 +00:00
|
|
|
|
tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( 2.15, 2 ) );
|
|
|
|
|
check( !strcmp( buf, "2.15" ) );
|
|
|
|
|
|
2010-10-17 18:29:36 +00:00
|
|
|
|
tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( 2.05, 2 ) );
|
|
|
|
|
check( !strcmp( buf, "2.05" ) );
|
|
|
|
|
|
2010-10-21 23:47:23 +00:00
|
|
|
|
tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( 3.3333, 2 ) );
|
|
|
|
|
check( !strcmp( buf, "3.33" ) );
|
|
|
|
|
|
|
|
|
|
tr_snprintf( buf, sizeof( buf ), "%.0f", tr_truncd( 3.3333, 0 ) );
|
|
|
|
|
check( !strcmp( buf, "3" ) );
|
|
|
|
|
|
|
|
|
|
tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( nan, 2 ) );
|
2011-01-02 07:49:30 +00:00
|
|
|
|
check( strstr( buf, "nan" ) != NULL );
|
2010-10-21 23:47:23 +00:00
|
|
|
|
|
2010-07-24 17:09:39 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
|
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 */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2010-07-24 17:09:39 +00:00
|
|
|
|
int
|
|
|
|
|
main( void )
|
|
|
|
|
{
|
|
|
|
|
char *in, *out;
|
|
|
|
|
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 );
|
2011-01-31 22:47:07 +00:00
|
|
|
|
check( !strcmp( out, "WU9ZTyE=" ) );
|
|
|
|
|
check( len == 8 );
|
2008-05-20 17:33:54 +00:00
|
|
|
|
in = tr_base64_decode( out, -1, &len );
|
|
|
|
|
check( in );
|
|
|
|
|
check( !strcmp( in, "YOYO!" ) );
|
|
|
|
|
check( len == 5 );
|
|
|
|
|
tr_free( in );
|
|
|
|
|
tr_free( out );
|
2009-12-09 12:44:23 +00:00
|
|
|
|
out = tr_base64_encode( NULL, 0, &len );
|
|
|
|
|
check( out == NULL );
|
|
|
|
|
check( len == 0 );
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
2009-11-20 04:38:19 +00:00
|
|
|
|
if( ( i = test_hex( ) ) )
|
|
|
|
|
return i;
|
2009-10-31 22:16:06 +00:00
|
|
|
|
if( ( i = test_lowerbound( ) ) )
|
|
|
|
|
return i;
|
2010-06-25 01:13:35 +00:00
|
|
|
|
if( ( i = test_strip_positional_args( ) ) )
|
|
|
|
|
return i;
|
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;
|
2009-02-09 17:25:48 +00:00
|
|
|
|
if( ( i = test_numbers( ) ) )
|
|
|
|
|
return i;
|
2009-05-22 05:35:51 +00:00
|
|
|
|
if( ( i = test_memmem( ) ) )
|
|
|
|
|
return i;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
if( ( i = test_array( ) ) )
|
|
|
|
|
return i;
|
2009-12-10 09:13:08 +00:00
|
|
|
|
if( ( i = test_url( ) ) )
|
|
|
|
|
return i;
|
2010-07-24 17:09:39 +00:00
|
|
|
|
if( ( i = test_truncd( ) ) )
|
|
|
|
|
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;
|
|
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
|
/* bitfield count range */
|
|
|
|
|
for( l=0; l<1000000; ++l )
|
|
|
|
|
if(( i = test_bitfield_count_range( )))
|
|
|
|
|
return i;
|
|
|
|
|
|
2008-05-20 17:33:54 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|