2008-05-22 16:41:16 +00:00
|
|
|
#include <ctype.h>
|
2008-10-03 04:49:06 +00:00
|
|
|
#include <errno.h>
|
2008-01-30 15:39:41 +00:00
|
|
|
#include <stdio.h>
|
2008-08-21 14:57:59 +00:00
|
|
|
#include <string.h>
|
2008-12-30 22:07:39 +00:00
|
|
|
|
|
|
|
#include "event.h"
|
|
|
|
|
2008-01-30 15:39:41 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "bencode.h"
|
2008-05-13 12:52:58 +00:00
|
|
|
#include "json.h"
|
2008-01-30 15:39:41 +00:00
|
|
|
#include "utils.h" /* tr_free */
|
|
|
|
|
2009-01-23 18:44:15 +00:00
|
|
|
#undef VERBOSE
|
2008-01-30 15:39:41 +00:00
|
|
|
|
2008-08-11 19:05:02 +00:00
|
|
|
static int test = 0;
|
2008-01-30 15:39:41 +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-01-30 15:39:41 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
testInt( void )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint8_t buf[128];
|
|
|
|
int64_t val;
|
|
|
|
int err;
|
2008-01-30 15:39:41 +00:00
|
|
|
const uint8_t * end;
|
|
|
|
|
|
|
|
/* good int string */
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( (char*)buf, sizeof( buf ), "i64e" );
|
2008-09-23 19:11:04 +00:00
|
|
|
err = tr_bencParseInt( buf, buf + 4, &end, &val );
|
2008-01-30 15:39:41 +00:00
|
|
|
check( err == 0 );
|
|
|
|
check( val == 64 );
|
|
|
|
check( end == buf + 4 );
|
|
|
|
|
|
|
|
/* missing 'e' */
|
|
|
|
end = NULL;
|
|
|
|
val = 888;
|
2008-09-23 19:11:04 +00:00
|
|
|
err = tr_bencParseInt( buf, buf + 3, &end, &val );
|
2008-10-03 04:49:06 +00:00
|
|
|
check( err == EILSEQ );
|
2008-01-30 15:39:41 +00:00
|
|
|
check( val == 888 );
|
|
|
|
check( end == NULL );
|
|
|
|
|
|
|
|
/* empty buffer */
|
2008-09-23 19:11:04 +00:00
|
|
|
err = tr_bencParseInt( buf, buf + 0, &end, &val );
|
2008-10-03 04:49:06 +00:00
|
|
|
check( err == EILSEQ );
|
2008-01-30 15:39:41 +00:00
|
|
|
check( val == 888 );
|
|
|
|
check( end == NULL );
|
|
|
|
|
|
|
|
/* bad number */
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( (char*)buf, sizeof( buf ), "i6z4e" );
|
2008-09-23 19:11:04 +00:00
|
|
|
err = tr_bencParseInt( buf, buf + 5, &end, &val );
|
2008-10-03 04:49:06 +00:00
|
|
|
check( err == EILSEQ );
|
2008-01-30 15:39:41 +00:00
|
|
|
check( val == 888 );
|
|
|
|
check( end == NULL );
|
|
|
|
|
|
|
|
/* negative number */
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( (char*)buf, sizeof( buf ), "i-3e" );
|
2008-09-23 19:11:04 +00:00
|
|
|
err = tr_bencParseInt( buf, buf + 4, &end, &val );
|
2008-10-03 04:49:06 +00:00
|
|
|
check( err == 0 );
|
2008-01-30 15:39:41 +00:00
|
|
|
check( val == -3 );
|
|
|
|
check( end == buf + 4 );
|
|
|
|
|
|
|
|
/* zero */
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( (char*)buf, sizeof( buf ), "i0e" );
|
2008-09-23 19:11:04 +00:00
|
|
|
err = tr_bencParseInt( buf, buf + 4, &end, &val );
|
2008-10-03 04:49:06 +00:00
|
|
|
check( err == 0 );
|
2008-01-30 15:39:41 +00:00
|
|
|
check( val == 0 );
|
|
|
|
check( end == buf + 3 );
|
|
|
|
|
|
|
|
/* no leading zeroes allowed */
|
|
|
|
val = 0;
|
|
|
|
end = NULL;
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( (char*)buf, sizeof( buf ), "i04e" );
|
2008-09-23 19:11:04 +00:00
|
|
|
err = tr_bencParseInt( buf, buf + 4, &end, &val );
|
2008-10-03 04:49:06 +00:00
|
|
|
check( err == EILSEQ );
|
2008-01-30 15:39:41 +00:00
|
|
|
check( val == 0 );
|
|
|
|
check( end == NULL );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
testStr( void )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint8_t buf[128];
|
|
|
|
int err;
|
2008-01-30 15:39:41 +00:00
|
|
|
const uint8_t * end;
|
2008-08-21 18:40:40 +00:00
|
|
|
const uint8_t * str;
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t len;
|
2008-01-30 15:39:41 +00:00
|
|
|
|
|
|
|
/* good string */
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( (char*)buf, sizeof( buf ), "4:boat" );
|
2008-09-23 19:11:04 +00:00
|
|
|
err = tr_bencParseStr( buf, buf + 6, &end, &str, &len );
|
2008-10-03 04:49:06 +00:00
|
|
|
check( err == 0 );
|
2008-08-21 18:40:40 +00:00
|
|
|
check( !strncmp( (char*)str, "boat", len ) );
|
2008-01-30 15:39:41 +00:00
|
|
|
check( len == 4 );
|
|
|
|
check( end == buf + 6 );
|
|
|
|
str = NULL;
|
|
|
|
end = NULL;
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
/* string goes past end of buffer */
|
2008-09-23 19:11:04 +00:00
|
|
|
err = tr_bencParseStr( buf, buf + 5, &end, &str, &len );
|
2008-10-03 04:49:06 +00:00
|
|
|
check( err == EILSEQ );
|
2008-01-30 15:39:41 +00:00
|
|
|
check( str == NULL );
|
|
|
|
check( end == NULL );
|
|
|
|
check( !len );
|
|
|
|
|
|
|
|
/* empty string */
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( (char*)buf, sizeof( buf ), "0:" );
|
2008-09-23 19:11:04 +00:00
|
|
|
err = tr_bencParseStr( buf, buf + 2, &end, &str, &len );
|
2008-10-03 04:49:06 +00:00
|
|
|
check( err == 0 );
|
2008-01-30 15:39:41 +00:00
|
|
|
check( !*str );
|
|
|
|
check( !len );
|
|
|
|
check( end == buf + 2 );
|
|
|
|
str = NULL;
|
|
|
|
end = NULL;
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
/* short string */
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( (char*)buf, sizeof( buf ), "3:boat" );
|
2008-09-23 19:11:04 +00:00
|
|
|
err = tr_bencParseStr( buf, buf + 6, &end, &str, &len );
|
2008-10-03 04:49:06 +00:00
|
|
|
check( err == 0 );
|
2008-08-21 18:40:40 +00:00
|
|
|
check( !strncmp( (char*)str, "boa", len ) );
|
2008-01-30 15:39:41 +00:00
|
|
|
check( len == 3 );
|
|
|
|
check( end == buf + 5 );
|
|
|
|
str = NULL;
|
|
|
|
end = NULL;
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-01-31 02:24:43 +00:00
|
|
|
|
2008-02-01 15:50:17 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
testString( const char * str,
|
|
|
|
int isGood )
|
2008-02-01 15:50:17 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc val;
|
2008-02-01 15:50:17 +00:00
|
|
|
const uint8_t * end = NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
char * saved;
|
|
|
|
const size_t len = strlen( str );
|
|
|
|
int savedLen;
|
|
|
|
int err = tr_bencParse( str, str + len, &val, &end );
|
|
|
|
|
|
|
|
if( !isGood )
|
|
|
|
{
|
2008-02-01 15:50:17 +00:00
|
|
|
check( err );
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-02-01 15:50:17 +00:00
|
|
|
check( !err );
|
2008-05-22 16:41:16 +00:00
|
|
|
#if 0
|
|
|
|
fprintf( stderr, "in: [%s]\n", str );
|
2008-09-23 19:11:04 +00:00
|
|
|
fprintf( stderr, "out:\n%s", tr_bencSaveAsJSON( &val, NULL ) );
|
2008-05-22 16:41:16 +00:00
|
|
|
#endif
|
2008-02-01 15:50:17 +00:00
|
|
|
check( end == (const uint8_t*)str + len );
|
2009-06-02 01:48:48 +00:00
|
|
|
saved = tr_bencToStr( &val, TR_FMT_BENC, &savedLen );
|
2008-02-01 15:50:17 +00:00
|
|
|
check( !strcmp( saved, str ) );
|
|
|
|
check( len == (size_t)savedLen );
|
|
|
|
tr_free( saved );
|
|
|
|
tr_bencFree( &val );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
static int
|
|
|
|
testParse( void )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc val;
|
|
|
|
tr_benc * child;
|
|
|
|
tr_benc * child2;
|
|
|
|
uint8_t buf[512];
|
2008-01-31 02:24:43 +00:00
|
|
|
const uint8_t * end;
|
2008-09-23 19:11:04 +00:00
|
|
|
int err;
|
|
|
|
int len;
|
|
|
|
int64_t i;
|
|
|
|
char * saved;
|
2008-01-31 02:24:43 +00:00
|
|
|
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( (char*)buf, sizeof( buf ), "i64e" );
|
2008-01-31 02:24:43 +00:00
|
|
|
err = tr_bencParse( buf, buf + sizeof( buf ), &val, &end );
|
|
|
|
check( !err );
|
2008-04-13 22:31:07 +00:00
|
|
|
check( tr_bencGetInt( &val, &i ) );
|
|
|
|
check( i == 64 );
|
2008-01-31 02:24:43 +00:00
|
|
|
check( end == buf + 4 );
|
|
|
|
tr_bencFree( &val );
|
|
|
|
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( (char*)buf, sizeof( buf ), "li64ei32ei16ee" );
|
2008-01-31 02:24:43 +00:00
|
|
|
err = tr_bencParse( buf, buf + sizeof( buf ), &val, &end );
|
|
|
|
check( !err );
|
|
|
|
check( end == buf + strlen( (char*)buf ) );
|
|
|
|
check( val.val.l.count == 3 );
|
2008-04-13 22:31:07 +00:00
|
|
|
check( tr_bencGetInt( &val.val.l.vals[0], &i ) );
|
|
|
|
check( i == 64 );
|
|
|
|
check( tr_bencGetInt( &val.val.l.vals[1], &i ) );
|
|
|
|
check( i == 32 );
|
|
|
|
check( tr_bencGetInt( &val.val.l.vals[2], &i ) );
|
|
|
|
check( i == 16 );
|
2009-06-02 01:48:48 +00:00
|
|
|
saved = tr_bencToStr( &val, TR_FMT_BENC, &len );
|
2008-01-31 02:24:43 +00:00
|
|
|
check( !strcmp( saved, (char*)buf ) );
|
|
|
|
tr_free( saved );
|
|
|
|
tr_bencFree( &val );
|
|
|
|
|
|
|
|
end = NULL;
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( (char*)buf, sizeof( buf ), "lllee" );
|
2008-09-23 19:11:04 +00:00
|
|
|
err = tr_bencParse( buf, buf + strlen( (char*)buf ), &val, &end );
|
2008-01-31 02:24:43 +00:00
|
|
|
check( err );
|
|
|
|
check( end == NULL );
|
|
|
|
|
|
|
|
end = NULL;
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( (char*)buf, sizeof( buf ), "le" );
|
2008-09-23 19:11:04 +00:00
|
|
|
err = tr_bencParse( buf, buf + sizeof( buf ), &val, &end );
|
2008-01-31 02:24:43 +00:00
|
|
|
check( !err );
|
|
|
|
check( end == buf + 2 );
|
2009-06-02 01:48:48 +00:00
|
|
|
saved = tr_bencToStr( &val, TR_FMT_BENC, &len );
|
2008-01-31 02:24:43 +00:00
|
|
|
check( !strcmp( saved, "le" ) );
|
|
|
|
tr_free( saved );
|
|
|
|
tr_bencFree( &val );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( err = testString( "llleee", TRUE ) ) )
|
2008-02-01 15:50:17 +00:00
|
|
|
return err;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( err = testString( "d3:cow3:moo4:spam4:eggse", TRUE ) ) )
|
2008-02-01 15:50:17 +00:00
|
|
|
return err;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( err = testString( "d4:spaml1:a1:bee", TRUE ) ) )
|
2008-02-01 15:50:17 +00:00
|
|
|
return err;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( err =
|
|
|
|
testString( "d5:greenli1ei2ei3ee4:spamd1:ai123e3:keyi214eee",
|
|
|
|
TRUE ) ) )
|
2008-05-22 16:41:16 +00:00
|
|
|
return err;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( err =
|
|
|
|
testString(
|
|
|
|
"d9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee",
|
|
|
|
TRUE ) ) )
|
2008-02-01 15:50:17 +00:00
|
|
|
return err;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( err =
|
|
|
|
testString(
|
|
|
|
"d8:completei1e8:intervali1800e12:min intervali1800e5:peers0:e",
|
|
|
|
TRUE ) ) )
|
2008-02-01 15:50:17 +00:00
|
|
|
return err;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( err = testString( "d1:ai0e1:be", FALSE ) ) ) /* odd number of children
|
|
|
|
*/
|
2008-02-01 15:50:17 +00:00
|
|
|
return err;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( err = testString( "", FALSE ) ) )
|
2008-02-01 15:50:17 +00:00
|
|
|
return err;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( err = testString( " ", FALSE ) ) )
|
2008-02-01 17:33:49 +00:00
|
|
|
return err;
|
2008-01-31 02:24:43 +00:00
|
|
|
|
|
|
|
/* nested containers
|
|
|
|
* parse an unsorted dict
|
|
|
|
* save as a sorted dict */
|
|
|
|
end = NULL;
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( (char*)buf, sizeof( buf ), "lld1:bi32e1:ai64eeee" );
|
2008-01-31 02:24:43 +00:00
|
|
|
err = tr_bencParse( buf, buf + sizeof( buf ), &val, &end );
|
|
|
|
check( !err );
|
|
|
|
check( end == buf + strlen( (const char*)buf ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
check( ( child = tr_bencListChild( &val, 0 ) ) );
|
|
|
|
check( ( child2 = tr_bencListChild( child, 0 ) ) );
|
2009-06-02 01:48:48 +00:00
|
|
|
saved = tr_bencToStr( &val, TR_FMT_BENC, &len );
|
2008-01-31 02:24:43 +00:00
|
|
|
check( !strcmp( saved, "lld1:ai64e1:bi32eeee" ) );
|
|
|
|
tr_free( saved );
|
|
|
|
tr_bencFree( &val );
|
|
|
|
|
2008-02-01 21:53:01 +00:00
|
|
|
/* too many endings */
|
|
|
|
end = NULL;
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( (char*)buf, sizeof( buf ), "leee" );
|
2008-02-01 21:53:01 +00:00
|
|
|
err = tr_bencParse( buf, buf + sizeof( buf ), &val, &end );
|
|
|
|
check( !err );
|
|
|
|
check( end == buf + 2 );
|
2009-06-02 01:48:48 +00:00
|
|
|
saved = tr_bencToStr( &val, TR_FMT_BENC, &len );
|
2008-02-01 21:53:01 +00:00
|
|
|
check( !strcmp( saved, "le" ) );
|
|
|
|
tr_free( saved );
|
|
|
|
tr_bencFree( &val );
|
|
|
|
|
|
|
|
/* no ending */
|
|
|
|
end = NULL;
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( (char*)buf, sizeof( buf ), "l1:a1:b1:c" );
|
2008-02-01 21:53:01 +00:00
|
|
|
err = tr_bencParse( buf, buf + strlen( (char*)buf ), &val, &end );
|
|
|
|
check( err );
|
|
|
|
|
|
|
|
/* incomplete string */
|
|
|
|
end = NULL;
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( (char*)buf, sizeof( buf ), "1:" );
|
2008-02-01 21:53:01 +00:00
|
|
|
err = tr_bencParse( buf, buf + strlen( (char*)buf ), &val, &end );
|
|
|
|
check( err );
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-22 16:41:16 +00:00
|
|
|
static void
|
|
|
|
stripWhitespace( char * in )
|
|
|
|
{
|
|
|
|
char * out;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
for( out = in; *in; ++in )
|
2008-05-22 16:41:16 +00:00
|
|
|
if( !isspace( *in ) )
|
|
|
|
*out++ = *in;
|
|
|
|
*out = '\0';
|
|
|
|
}
|
|
|
|
|
2008-03-04 19:29:51 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
testJSONSnippet( const char * benc_str,
|
|
|
|
const char * expected )
|
2008-03-04 19:29:51 +00:00
|
|
|
{
|
|
|
|
tr_benc top;
|
2009-06-14 01:00:36 +00:00
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
2008-12-30 22:07:39 +00:00
|
|
|
char * serialized;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-03-04 19:29:51 +00:00
|
|
|
tr_bencLoad( benc_str, strlen( benc_str ), &top, NULL );
|
2009-06-02 01:48:48 +00:00
|
|
|
tr_bencToBuf( &top, TR_FMT_JSON, buf );
|
|
|
|
serialized = (char*) EVBUFFER_DATA( buf );
|
2008-05-22 16:41:16 +00:00
|
|
|
stripWhitespace( serialized );
|
2008-05-13 12:52:58 +00:00
|
|
|
#if 0
|
2008-09-23 19:11:04 +00:00
|
|
|
fprintf( stderr, "benc: %s\n", benc_str );
|
|
|
|
fprintf( stderr, "json: %s\n", serialized );
|
|
|
|
fprintf( stderr, "want: %s\n", expected );
|
2008-05-13 12:52:58 +00:00
|
|
|
#endif
|
2008-03-04 19:29:51 +00:00
|
|
|
check( !strcmp( serialized, expected ) );
|
|
|
|
tr_bencFree( &top );
|
2009-06-14 01:00:36 +00:00
|
|
|
evbuffer_free( buf );
|
2008-03-04 19:29:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-05-12 17:54:57 +00:00
|
|
|
testJSON( void )
|
2008-03-04 19:29:51 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int val;
|
2008-03-04 19:29:51 +00:00
|
|
|
const char * benc_str;
|
|
|
|
const char * expected;
|
|
|
|
|
|
|
|
benc_str = "i6e";
|
2008-05-12 17:54:57 +00:00
|
|
|
expected = "6";
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( val = testJSONSnippet( benc_str, expected ) ) )
|
2008-03-04 19:29:51 +00:00
|
|
|
return val;
|
|
|
|
|
2008-05-12 17:54:57 +00:00
|
|
|
benc_str = "d5:helloi1e5:worldi2ee";
|
2008-09-23 19:11:04 +00:00
|
|
|
expected = "{\"hello\":1,\"world\":2}";
|
|
|
|
if( ( val = testJSONSnippet( benc_str, expected ) ) )
|
2008-03-04 19:29:51 +00:00
|
|
|
return val;
|
|
|
|
|
2008-08-11 17:00:23 +00:00
|
|
|
benc_str = "d5:helloi1e5:worldi2e3:fooli1ei2ei3eee";
|
2008-05-22 16:41:16 +00:00
|
|
|
expected = "{\"foo\":[1,2,3],\"hello\":1,\"world\":2}";
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( val = testJSONSnippet( benc_str, expected ) ) )
|
2008-05-12 17:54:57 +00:00
|
|
|
return val;
|
|
|
|
|
2008-08-11 17:00:23 +00:00
|
|
|
benc_str = "d5:helloi1e5:worldi2e3:fooli1ei2ei3ed1:ai0eeee";
|
2008-05-22 16:41:16 +00:00
|
|
|
expected = "{\"foo\":[1,2,3,{\"a\":0}],\"hello\":1,\"world\":2}";
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( val = testJSONSnippet( benc_str, expected ) ) )
|
2008-03-04 19:29:51 +00:00
|
|
|
return val;
|
|
|
|
|
2008-05-22 16:41:16 +00:00
|
|
|
benc_str = "d4:argsd6:statusle7:status2lee6:result7:successe";
|
2008-09-23 19:11:04 +00:00
|
|
|
expected =
|
|
|
|
"{\"args\":{\"status\":[],\"status2\":[]},\"result\":\"success\"}";
|
|
|
|
if( ( val = testJSONSnippet( benc_str, expected ) ) )
|
2008-05-13 12:52:58 +00:00
|
|
|
return val;
|
|
|
|
|
2008-03-04 19:29:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-12 02:12:17 +00:00
|
|
|
static int
|
|
|
|
testMerge( void )
|
|
|
|
{
|
|
|
|
tr_benc dest, src;
|
|
|
|
int64_t i;
|
|
|
|
const char * s;
|
|
|
|
|
|
|
|
/* initial dictionary (default values) */
|
|
|
|
tr_bencInitDict( &dest, 10 );
|
|
|
|
tr_bencDictAddInt( &dest, "i1", 1 );
|
|
|
|
tr_bencDictAddInt( &dest, "i2", 2 );
|
|
|
|
tr_bencDictAddInt( &dest, "i4", -35 ); /* remains untouched */
|
|
|
|
tr_bencDictAddStr( &dest, "s5", "abc" );
|
|
|
|
tr_bencDictAddStr( &dest, "s6", "def" );
|
|
|
|
tr_bencDictAddStr( &dest, "s7", "127.0.0.1" ); /* remains untouched */
|
|
|
|
|
|
|
|
/* new dictionary, will overwrite items in dest */
|
|
|
|
tr_bencInitDict( &src, 10 );
|
|
|
|
tr_bencDictAddInt( &src, "i1", 1 ); /* same value */
|
|
|
|
tr_bencDictAddInt( &src, "i2", 4 ); /* new value */
|
|
|
|
tr_bencDictAddInt( &src, "i3", 3 ); /* new key:value */
|
|
|
|
tr_bencDictAddStr( &src, "s5", "abc" ); /* same value */
|
|
|
|
tr_bencDictAddStr( &src, "s6", "xyz" ); /* new value */
|
|
|
|
tr_bencDictAddStr( &src, "s8", "ghi" ); /* new key:value */
|
|
|
|
|
|
|
|
tr_bencMergeDicts( &dest, /*const*/ &src );
|
|
|
|
|
|
|
|
check( tr_bencDictFindInt( &dest, "i1", &i ));
|
|
|
|
check( i == 1);
|
|
|
|
check( tr_bencDictFindInt( &dest, "i2", &i ));
|
|
|
|
check( i == 4);
|
|
|
|
check( tr_bencDictFindInt( &dest, "i3", &i ));
|
|
|
|
check( i == 3);
|
|
|
|
check( tr_bencDictFindInt( &dest, "i4", &i ));
|
|
|
|
check( i == -35);
|
|
|
|
check( tr_bencDictFindStr( &dest, "s5", &s ));
|
|
|
|
check( strcmp( "abc", s ) == 0 );
|
|
|
|
check( tr_bencDictFindStr( &dest, "s6", &s ));
|
|
|
|
check( strcmp( "xyz", s ) == 0 );
|
|
|
|
check( tr_bencDictFindStr( &dest, "s7", &s ));
|
|
|
|
check( strcmp( "127.0.0.1", s ) == 0 );
|
|
|
|
check( tr_bencDictFindStr( &dest, "s8", &s ));
|
|
|
|
check( strcmp( "ghi", s ) == 0 );
|
|
|
|
|
|
|
|
tr_bencFree( &dest );
|
|
|
|
tr_bencFree( &src );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
static int
|
2008-09-06 07:26:25 +00:00
|
|
|
testStackSmash( int depth )
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
int err;
|
|
|
|
uint8_t * in;
|
2008-01-31 02:24:43 +00:00
|
|
|
const uint8_t * end;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc val;
|
|
|
|
char * saved;
|
2008-01-31 02:24:43 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
in = tr_new( uint8_t, depth * 2 + 1 );
|
|
|
|
for( i = 0; i < depth; ++i )
|
|
|
|
{
|
2008-01-31 02:24:43 +00:00
|
|
|
in[i] = 'l';
|
2008-09-23 19:11:04 +00:00
|
|
|
in[depth + i] = 'e';
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
in[depth * 2] = '\0';
|
|
|
|
err = tr_bencParse( in, in + ( depth * 2 ), &val, &end );
|
2008-01-31 02:24:43 +00:00
|
|
|
check( !err );
|
2008-09-23 19:11:04 +00:00
|
|
|
check( end == in + ( depth * 2 ) );
|
2009-06-02 01:48:48 +00:00
|
|
|
saved = tr_bencToStr( &val, TR_FMT_BENC, &len );
|
2008-01-31 02:24:43 +00:00
|
|
|
check( !strcmp( saved, (char*)in ) );
|
|
|
|
tr_free( in );
|
|
|
|
tr_free( saved );
|
|
|
|
tr_bencFree( &val );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-29 23:05:32 +00:00
|
|
|
static int
|
|
|
|
testBool( void )
|
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
int64_t intVal;
|
|
|
|
tr_bool boolVal;
|
|
|
|
|
|
|
|
tr_bencInitDict( &top, 0 );
|
|
|
|
|
|
|
|
tr_bencDictAddBool( &top, "key1", FALSE );
|
|
|
|
tr_bencDictAddBool( &top, "key2", 0 );
|
|
|
|
tr_bencDictAddInt ( &top, "key3", TRUE );
|
|
|
|
tr_bencDictAddInt ( &top, "key4", 1 );
|
|
|
|
check( tr_bencDictFindBool( &top, "key1", &boolVal ) )
|
|
|
|
check( !boolVal )
|
|
|
|
check( tr_bencDictFindBool( &top, "key2", &boolVal ) )
|
|
|
|
check( !boolVal )
|
|
|
|
check( tr_bencDictFindBool( &top, "key3", &boolVal ) )
|
|
|
|
check( boolVal )
|
|
|
|
check( tr_bencDictFindBool( &top, "key4", &boolVal ) )
|
|
|
|
check( boolVal )
|
|
|
|
check( tr_bencDictFindInt( &top, "key1", &intVal ) )
|
|
|
|
check( !intVal)
|
|
|
|
check( tr_bencDictFindInt( &top, "key2", &intVal ) )
|
|
|
|
check( !intVal )
|
|
|
|
check( tr_bencDictFindInt( &top, "key3", &intVal ) )
|
|
|
|
check( intVal )
|
|
|
|
check( tr_bencDictFindInt( &top, "key4", &intVal ) )
|
|
|
|
check( intVal )
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-05 23:16:45 +00:00
|
|
|
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" );
|
|
|
|
|
2009-06-02 01:48:48 +00:00
|
|
|
benc = tr_bencToStr( &top, TR_FMT_BENC, &len );
|
2009-04-05 23:16:45 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-01-30 15:39:41 +00:00
|
|
|
int
|
|
|
|
main( void )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-12-12 02:12:17 +00:00
|
|
|
if(( i = testInt( )))
|
|
|
|
return i;
|
|
|
|
|
|
|
|
if(( i = testStr( )))
|
2008-01-31 02:24:43 +00:00
|
|
|
return i;
|
|
|
|
|
2008-12-12 02:12:17 +00:00
|
|
|
if(( i = testParse( )))
|
2008-01-31 02:24:43 +00:00
|
|
|
return i;
|
|
|
|
|
2008-12-12 02:12:17 +00:00
|
|
|
if(( i = testJSON( )))
|
2008-01-30 15:39:41 +00:00
|
|
|
return i;
|
|
|
|
|
2008-12-12 02:12:17 +00:00
|
|
|
if(( i = testMerge( )))
|
2008-03-04 19:29:51 +00:00
|
|
|
return i;
|
|
|
|
|
2009-03-29 23:05:32 +00:00
|
|
|
if(( i = testBool( )))
|
|
|
|
return i;
|
|
|
|
|
2009-04-05 23:16:45 +00:00
|
|
|
if(( i = testParse2( )))
|
|
|
|
return i;
|
|
|
|
|
2008-09-06 07:26:25 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
i = testStackSmash( 1000000 );
|
|
|
|
#else
|
|
|
|
i = testStackSmash( 100000 );
|
|
|
|
#endif
|
|
|
|
if( i )
|
2008-01-30 15:39:41 +00:00
|
|
|
return i;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|