1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-25 01:03:01 +00:00

(trunk) fix another new bencode bug. add some more unit tests for benc.

This commit is contained in:
Charles Kerr 2009-04-05 23:16:45 +00:00
parent 8f256a4648
commit dce5997f29
3 changed files with 48 additions and 2 deletions

View file

@ -485,6 +485,46 @@ testBool( void )
return 0;
}
static int
testParse2( void )
{
tr_benc top;
tr_benc top2;
int64_t intVal;
const char * strVal;
double realVal;
tr_bool boolVal;
int len;
char * benc;
const uint8_t * end;
tr_bencInitDict( &top, 0 );
tr_bencDictAddBool( &top, "this-is-a-bool", TRUE );
tr_bencDictAddInt( &top, "this-is-an-int", 1234 );
tr_bencDictAddReal( &top, "this-is-a-real", 0.5 );
tr_bencDictAddStr( &top, "this-is-a-string", "this-is-a-string" );
benc = tr_bencSave( &top, &len );
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" ) )
check( !tr_bencParse( benc, benc+len, &top2, &end ) )
check( (char*)end == benc + len )
check( tr_bencIsDict( &top2 ) )
check( tr_bencDictFindInt( &top, "this-is-an-int", &intVal ) )
check( intVal == 1234 )
check( tr_bencDictFindBool( &top, "this-is-a-bool", &boolVal ) )
check( boolVal == TRUE )
check( tr_bencDictFindStr( &top, "this-is-a-string", &strVal ) )
check( !strcmp( strVal, "this-is-a-string" ) )
check( tr_bencDictFindReal( &top, "this-is-a-real", &realVal ) )
check( (int)(realVal*100) == 50 )
tr_bencFree( &top2 );
tr_free( benc );
tr_bencFree( &top );
return 0;
}
int
main( void )
{
@ -508,6 +548,9 @@ main( void )
if(( i = testBool( )))
return i;
if(( i = testParse2( )))
return i;
#ifndef WIN32
i = testStackSmash( 1000000 );
#else

View file

@ -1120,7 +1120,7 @@ saveRealFunc( const tr_benc * val, void * evbuf )
tr_free( locale );
len = strlen( buf );
evbuffer_add_printf( evbuf, "%lu:", (unsigned long)buf );
evbuffer_add_printf( evbuf, "%lu:", (unsigned long)len );
evbuffer_add( evbuf, buf, len );
}

View file

@ -144,7 +144,10 @@ test2( void )
{
tr_benc top;
const char * in = " ";
const int err = tr_jsonParse( in, strlen( in ), &top, NULL );
int err;
top.type = 0;
err = tr_jsonParse( in, strlen( in ), &top, NULL );
check( err );
check( !tr_bencIsDict( &top ) );