From dce5997f2922e5ff80df1dda3a523483441595c5 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 5 Apr 2009 23:16:45 +0000 Subject: [PATCH] (trunk) fix another new bencode bug. add some more unit tests for benc. --- libtransmission/bencode-test.c | 43 ++++++++++++++++++++++++++++++++++ libtransmission/bencode.c | 2 +- libtransmission/json-test.c | 5 +++- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/libtransmission/bencode-test.c b/libtransmission/bencode-test.c index c0e4782f5..f627cf981 100644 --- a/libtransmission/bencode-test.c +++ b/libtransmission/bencode-test.c @@ -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 diff --git a/libtransmission/bencode.c b/libtransmission/bencode.c index de820a1fb..d15280cb6 100644 --- a/libtransmission/bencode.c +++ b/libtransmission/bencode.c @@ -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 ); } diff --git a/libtransmission/json-test.c b/libtransmission/json-test.c index 3e241825e..9aabe8114 100644 --- a/libtransmission/json-test.c +++ b/libtransmission/json-test.c @@ -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 ) ); -- 2.40.0