]> granicus.if.org Git - transmission/commitdiff
(trunk) fix another new bencode bug. add some more unit tests for benc.
authorCharles Kerr <charles@transmissionbt.com>
Sun, 5 Apr 2009 23:16:45 +0000 (23:16 +0000)
committerCharles Kerr <charles@transmissionbt.com>
Sun, 5 Apr 2009 23:16:45 +0000 (23:16 +0000)
libtransmission/bencode-test.c
libtransmission/bencode.c
libtransmission/json-test.c

index c0e4782f55d2447035e981b0e5767e972248ac07..f627cf981c81f978dfcf243f87b4a0e5840ede2e 100644 (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
index de820a1fb2117fcbf2aa1957740268137fb13b8b..d15280cb69a68fbae1a00902056eaf60d3b09698 100644 (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 );
 }
 
index 3e241825e916828f5b2b1982f915cb31ce3d6e26..9aabe8114e6aecc7552f1a844967a329c58e751c 100644 (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 ) );