10
12
static int test = 0;
16
fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
18
fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
19
fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__,\
22
fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__,\
29
34
const uint8_t * end;
31
36
/* good int string */
32
37
tr_snprintf( (char*)buf, sizeof( buf ), "i64e" );
33
err = tr_bencParseInt( buf, buf+4, &end, &val );
38
err = tr_bencParseInt( buf, buf + 4, &end, &val );
35
40
check( val == 64 );
36
41
check( end == buf + 4 );
41
err = tr_bencParseInt( buf, buf+3, &end, &val );
42
check( err == TR_ERROR );
46
err = tr_bencParseInt( buf, buf + 3, &end, &val );
47
check( err == EILSEQ );
43
48
check( val == 888 );
44
49
check( end == NULL );
47
err = tr_bencParseInt( buf, buf+0, &end, &val );
48
check( err == TR_ERROR );
52
err = tr_bencParseInt( buf, buf + 0, &end, &val );
53
check( err == EILSEQ );
49
54
check( val == 888 );
50
55
check( end == NULL );
53
58
tr_snprintf( (char*)buf, sizeof( buf ), "i6z4e" );
54
err = tr_bencParseInt( buf, buf+5, &end, &val );
55
check( err == TR_ERROR );
59
err = tr_bencParseInt( buf, buf + 5, &end, &val );
60
check( err == EILSEQ );
56
61
check( val == 888 );
57
62
check( end == NULL );
59
64
/* negative number */
60
65
tr_snprintf( (char*)buf, sizeof( buf ), "i-3e" );
61
err = tr_bencParseInt( buf, buf+4, &end, &val );
62
check( err == TR_OK );
66
err = tr_bencParseInt( buf, buf + 4, &end, &val );
63
68
check( val == -3 );
64
69
check( end == buf + 4 );
67
72
tr_snprintf( (char*)buf, sizeof( buf ), "i0e" );
68
err = tr_bencParseInt( buf, buf+4, &end, &val );
69
check( err == TR_OK );
73
err = tr_bencParseInt( buf, buf + 4, &end, &val );
71
76
check( end == buf + 3 );
76
81
tr_snprintf( (char*)buf, sizeof( buf ), "i04e" );
77
err = tr_bencParseInt( buf, buf+4, &end, &val );
78
check( err == TR_ERROR );
82
err = tr_bencParseInt( buf, buf + 4, &end, &val );
83
check( err == EILSEQ );
80
85
check( end == NULL );
90
95
const uint8_t * end;
95
100
tr_snprintf( (char*)buf, sizeof( buf ), "4:boat" );
96
err = tr_bencParseStr( buf, buf+6, &end, &str, &len );
97
check( err == TR_OK );
98
check( !strcmp( (char*)str, "boat" ) );
101
err = tr_bencParseStr( buf, buf + 6, &end, &str, &len );
103
check( !strncmp( (char*)str, "boat", len ) );
99
104
check( len == 4 );
100
105
check( end == buf + 6 );
106
110
/* string goes past end of buffer */
107
err = tr_bencParseStr( buf, buf+5, &end, &str, &len );
108
check( err == TR_ERROR );
111
err = tr_bencParseStr( buf, buf + 5, &end, &str, &len );
112
check( err == EILSEQ );
109
113
check( str == NULL );
110
114
check( end == NULL );
113
117
/* empty string */
114
118
tr_snprintf( (char*)buf, sizeof( buf ), "0:" );
115
err = tr_bencParseStr( buf, buf+2, &end, &str, &len );
116
check( err == TR_OK );
119
err = tr_bencParseStr( buf, buf + 2, &end, &str, &len );
119
123
check( end == buf + 2 );
125
128
/* short string */
126
129
tr_snprintf( (char*)buf, sizeof( buf ), "3:boat" );
127
err = tr_bencParseStr( buf, buf+6, &end, &str, &len );
128
check( err == TR_OK );
129
check( !strcmp( (char*)str, "boa" ) );
130
err = tr_bencParseStr( buf, buf + 6, &end, &str, &len );
132
check( !strncmp( (char*)str, "boa", len ) );
130
133
check( len == 3 );
131
134
check( end == buf + 5 );
141
testString( const char * str, int isGood )
143
testString( const char * str,
144
147
const uint8_t * end = NULL;
146
const size_t len = strlen( str );
148
int err = tr_bencParse( str, str+len, &val , &end );
149
const size_t len = strlen( str );
151
int err = tr_bencParse( str, str + len, &val, &end );
154
161
fprintf( stderr, "in: [%s]\n", str );
155
fprintf( stderr, "out:\n%s", tr_bencSaveAsJSON(&val,NULL) );
162
fprintf( stderr, "out:\n%s", tr_bencSaveAsJSON( &val, NULL ) );
157
164
check( end == (const uint8_t*)str + len );
158
165
saved = tr_bencSave( &val, &savedLen );
205
212
tr_snprintf( (char*)buf, sizeof( buf ), "lllee" );
206
err = tr_bencParse( buf, buf + strlen( (char*)buf ), &val , &end );
213
err = tr_bencParse( buf, buf + strlen( (char*)buf ), &val, &end );
208
215
check( end == NULL );
211
218
tr_snprintf( (char*)buf, sizeof( buf ), "le" );
212
err = tr_bencParse( buf, buf + sizeof( buf ), &val , &end );
219
err = tr_bencParse( buf, buf + sizeof( buf ), &val, &end );
214
221
check( end == buf + 2 );
215
222
saved = tr_bencSave( &val, &len );
217
224
tr_free( saved );
218
225
tr_bencFree( &val );
220
if(( err = testString( "llleee", TRUE )))
222
if(( err = testString( "d3:cow3:moo4:spam4:eggse", TRUE )))
224
if(( err = testString( "d4:spaml1:a1:bee", TRUE )))
226
if(( err = testString( "d5:greenli1ei2ei3ee4:spamd1:ai123e3:keyi214eee", TRUE )))
228
if(( err = testString( "d9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee", TRUE )))
230
if(( err = testString( "d8:completei1e8:intervali1800e12:min intervali1800e5:peers0:e", TRUE )))
232
if(( err = testString( "d1:ai0e1:be", FALSE ))) /* odd number of children */
234
if(( err = testString( "", FALSE )))
236
if(( err = testString( " ", FALSE )))
227
if( ( err = testString( "llleee", TRUE ) ) )
229
if( ( err = testString( "d3:cow3:moo4:spam4:eggse", TRUE ) ) )
231
if( ( err = testString( "d4:spaml1:a1:bee", TRUE ) ) )
234
testString( "d5:greenli1ei2ei3ee4:spamd1:ai123e3:keyi214eee",
239
"d9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee",
244
"d8:completei1e8:intervali1800e12:min intervali1800e5:peers0:e",
247
if( ( err = testString( "d1:ai0e1:be", FALSE ) ) ) /* odd number of children
250
if( ( err = testString( "", FALSE ) ) )
252
if( ( err = testString( " ", FALSE ) ) )
239
255
/* nested containers
244
260
err = tr_bencParse( buf, buf + sizeof( buf ), &val, &end );
246
262
check( end == buf + strlen( (const char*)buf ) );
247
check(( child = tr_bencListChild( &val, 0 )));
248
check(( child2 = tr_bencListChild( child, 0 )));
263
check( ( child = tr_bencListChild( &val, 0 ) ) );
264
check( ( child2 = tr_bencListChild( child, 0 ) ) );
249
265
saved = tr_bencSave( &val, &len );
250
266
check( !strcmp( saved, "lld1:ai64e1:bi32eeee" ) );
251
267
tr_free( saved );
281
297
stripWhitespace( char * in )
284
for( out=in; *in; ++in )
301
for( out = in; *in; ++in )
285
302
if( !isspace( *in ) )
291
testJSONSnippet( const char * benc_str, const char * expected )
308
testJSONSnippet( const char * benc_str,
309
const char * expected )
295
314
tr_bencLoad( benc_str, strlen( benc_str ), &top, NULL );
296
315
serialized = tr_bencSaveAsJSON( &top, NULL );
297
316
stripWhitespace( serialized );
299
fprintf( stderr, "benc: %s\n", benc_str );
300
fprintf( stderr, "json: %s\n", serialized );
301
fprintf( stderr, "want: %s\n", expected );
318
fprintf( stderr, "benc: %s\n", benc_str );
319
fprintf( stderr, "json: %s\n", serialized );
320
fprintf( stderr, "want: %s\n", expected );
303
322
check( !strcmp( serialized, expected ) );
304
323
tr_free( serialized );
313
332
const char * benc_str;
314
333
const char * expected;
316
335
benc_str = "i6e";
318
if(( val = testJSONSnippet( benc_str, expected )))
337
if( ( val = testJSONSnippet( benc_str, expected ) ) )
321
340
benc_str = "d5:helloi1e5:worldi2ee";
322
expected = "{\"hello\":1,\"world\":2}";
323
if(( val = testJSONSnippet( benc_str, expected )))
341
expected = "{\"hello\":1,\"world\":2}";
342
if( ( val = testJSONSnippet( benc_str, expected ) ) )
326
345
benc_str = "d5:helloi1e5:worldi2e3:fooli1ei2ei3eee";
327
346
expected = "{\"foo\":[1,2,3],\"hello\":1,\"world\":2}";
328
if(( val = testJSONSnippet( benc_str, expected )))
347
if( ( val = testJSONSnippet( benc_str, expected ) ) )
331
350
benc_str = "d5:helloi1e5:worldi2e3:fooli1ei2ei3ed1:ai0eeee";
332
351
expected = "{\"foo\":[1,2,3,{\"a\":0}],\"hello\":1,\"world\":2}";
333
if(( val = testJSONSnippet( benc_str, expected )))
352
if( ( val = testJSONSnippet( benc_str, expected ) ) )
336
355
benc_str = "d4:argsd6:statusle7:status2lee6:result7:successe";
337
expected = "{\"args\":{\"status\":[],\"status2\":[]},\"result\":\"success\"}";
338
if(( val = testJSONSnippet( benc_str, expected )))
357
"{\"args\":{\"status\":[],\"status2\":[]},\"result\":\"success\"}";
358
if( ( val = testJSONSnippet( benc_str, expected ) ) )
345
testStackSmash( void )
365
testStackSmash( int depth )
352
371
const uint8_t * end;
357
in = tr_new( uint8_t, depth*2 + 1 );
358
for( i=0; i<depth; ++i ) {
375
in = tr_new( uint8_t, depth * 2 + 1 );
376
for( i = 0; i < depth; ++i )
363
err = tr_bencParse( in, in+(depth*2), &val, &end );
381
in[depth * 2] = '\0';
382
err = tr_bencParse( in, in + ( depth * 2 ), &val, &end );
365
check( end == in+(depth*2) );
384
check( end == in + ( depth * 2 ) );
366
385
saved = tr_bencSave( &val, &len );
367
386
check( !strcmp( saved, (char*)in ) );