2012-10-14 23:38:52 +00:00
|
|
|
#include <string.h> /* strlen() */
|
2008-05-11 22:42:53 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "bencode.h"
|
2008-05-18 16:44:30 +00:00
|
|
|
#include "json.h"
|
2008-05-11 22:42:53 +00:00
|
|
|
#include "utils.h" /* tr_free */
|
|
|
|
|
2009-01-23 18:44:15 +00:00
|
|
|
#undef VERBOSE
|
2012-08-18 16:07:05 +00:00
|
|
|
#include "libtransmission-test.h"
|
2008-05-11 22:42:53 +00:00
|
|
|
|
2008-08-19 23:11:35 +00:00
|
|
|
#include "ConvertUTF.h"
|
|
|
|
|
2008-08-18 03:10:59 +00:00
|
|
|
static int
|
|
|
|
test_utf8( void )
|
|
|
|
{
|
2008-12-30 22:07:39 +00:00
|
|
|
const char * in = "{ \"key\": \"Letöltések\" }";
|
|
|
|
tr_benc top;
|
|
|
|
const char * str;
|
|
|
|
char * json;
|
|
|
|
int err;
|
2008-08-18 03:10:59 +00:00
|
|
|
|
2009-06-01 18:57:37 +00:00
|
|
|
err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL );
|
2008-08-18 03:10:59 +00:00
|
|
|
check( !err );
|
|
|
|
check( tr_bencIsDict( &top ) );
|
2008-08-18 03:44:09 +00:00
|
|
|
check( tr_bencDictFindStr( &top, "key", &str ) );
|
2012-10-14 18:56:37 +00:00
|
|
|
check_streq( "Letöltések", str );
|
2008-08-19 23:11:35 +00:00
|
|
|
if( !err )
|
|
|
|
tr_bencFree( &top );
|
2008-08-18 03:10:59 +00:00
|
|
|
|
2008-08-19 23:11:35 +00:00
|
|
|
in = "{ \"key\": \"\\u005C\" }";
|
2009-06-01 18:57:37 +00:00
|
|
|
err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL );
|
2008-08-19 23:11:35 +00:00
|
|
|
check( !err );
|
|
|
|
check( tr_bencIsDict( &top ) );
|
|
|
|
check( tr_bencDictFindStr( &top, "key", &str ) );
|
2012-10-14 18:56:37 +00:00
|
|
|
check_streq( "\\", str );
|
2008-08-18 03:10:59 +00:00
|
|
|
if( !err )
|
|
|
|
tr_bencFree( &top );
|
|
|
|
|
2008-08-19 23:11:35 +00:00
|
|
|
/**
|
|
|
|
* 1. Feed it JSON-escaped nonascii to the JSON decoder.
|
|
|
|
* 2. Confirm that the result is UTF-8.
|
|
|
|
* 3. Feed the same UTF-8 back into the JSON encoder.
|
|
|
|
* 4. Confirm that the result is JSON-escaped.
|
|
|
|
* 5. Dogfood that result back into the parser.
|
|
|
|
* 6. Confirm that the result is UTF-8.
|
2008-09-23 19:11:04 +00:00
|
|
|
*/
|
2008-08-19 23:11:35 +00:00
|
|
|
in = "{ \"key\": \"Let\\u00f6lt\\u00e9sek\" }";
|
2009-06-01 18:57:37 +00:00
|
|
|
err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL );
|
2008-08-19 23:11:35 +00:00
|
|
|
check( !err );
|
|
|
|
check( tr_bencIsDict( &top ) );
|
|
|
|
check( tr_bencDictFindStr( &top, "key", &str ) );
|
2012-10-14 18:56:37 +00:00
|
|
|
check_streq( "Letöltések", str );
|
2009-06-02 01:48:48 +00:00
|
|
|
json = tr_bencToStr( &top, TR_FMT_JSON, NULL );
|
2008-08-19 23:11:35 +00:00
|
|
|
if( !err )
|
|
|
|
tr_bencFree( &top );
|
|
|
|
check( json );
|
|
|
|
check( strstr( json, "\\u00f6" ) != NULL );
|
|
|
|
check( strstr( json, "\\u00e9" ) != NULL );
|
2009-06-01 18:57:37 +00:00
|
|
|
err = tr_jsonParse( NULL, json, strlen( json ), &top, NULL );
|
2008-08-19 23:11:35 +00:00
|
|
|
check( !err );
|
|
|
|
check( tr_bencIsDict( &top ) );
|
|
|
|
check( tr_bencDictFindStr( &top, "key", &str ) );
|
2012-10-14 18:56:37 +00:00
|
|
|
check_streq( "Letöltések", str );
|
2008-08-19 23:11:35 +00:00
|
|
|
if( !err )
|
|
|
|
tr_bencFree( &top );
|
|
|
|
tr_free( json );
|
|
|
|
|
2008-08-18 03:10:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-11 22:42:53 +00:00
|
|
|
static int
|
|
|
|
test1( void )
|
|
|
|
{
|
|
|
|
const char * in =
|
|
|
|
"{\n"
|
|
|
|
" \"headers\": {\n"
|
|
|
|
" \"type\": \"request\",\n"
|
|
|
|
" \"tag\": 666\n"
|
|
|
|
" },\n"
|
|
|
|
" \"body\": {\n"
|
|
|
|
" \"name\": \"torrent-info\",\n"
|
|
|
|
" \"arguments\": {\n"
|
|
|
|
" \"ids\": [ 7, 10 ]\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc top, *headers, *body, *args, *ids;
|
2008-05-11 22:42:53 +00:00
|
|
|
const char * str;
|
2008-09-23 19:11:04 +00:00
|
|
|
int64_t i;
|
2009-06-01 18:57:37 +00:00
|
|
|
const int err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL );
|
2008-05-11 22:42:53 +00:00
|
|
|
|
|
|
|
check( !err );
|
|
|
|
check( tr_bencIsDict( &top ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
check( ( headers = tr_bencDictFind( &top, "headers" ) ) );
|
2008-05-11 22:42:53 +00:00
|
|
|
check( tr_bencIsDict( headers ) );
|
|
|
|
check( tr_bencDictFindStr( headers, "type", &str ) );
|
2012-10-14 18:56:37 +00:00
|
|
|
check_streq( "request", str );
|
2008-05-11 22:42:53 +00:00
|
|
|
check( tr_bencDictFindInt( headers, "tag", &i ) );
|
2012-10-14 18:56:37 +00:00
|
|
|
check_int_eq( 666, i );
|
2008-09-23 19:11:04 +00:00
|
|
|
check( ( body = tr_bencDictFind( &top, "body" ) ) );
|
2008-05-11 22:42:53 +00:00
|
|
|
check( tr_bencDictFindStr( body, "name", &str ) );
|
2012-10-14 18:56:37 +00:00
|
|
|
check_streq( "torrent-info", str );
|
2008-09-23 19:11:04 +00:00
|
|
|
check( ( args = tr_bencDictFind( body, "arguments" ) ) );
|
2008-05-11 22:42:53 +00:00
|
|
|
check( tr_bencIsDict( args ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
check( ( ids = tr_bencDictFind( args, "ids" ) ) );
|
2008-05-11 22:42:53 +00:00
|
|
|
check( tr_bencIsList( ids ) );
|
2012-10-14 18:56:37 +00:00
|
|
|
check_int_eq( 2, tr_bencListSize( ids ) );
|
2008-05-11 22:42:53 +00:00
|
|
|
check( tr_bencGetInt( tr_bencListChild( ids, 0 ), &i ) );
|
2012-10-14 18:56:37 +00:00
|
|
|
check_int_eq( 7, i );
|
2008-05-11 22:42:53 +00:00
|
|
|
check( tr_bencGetInt( tr_bencListChild( ids, 1 ), &i ) );
|
2012-10-14 18:56:37 +00:00
|
|
|
check_int_eq( 10, i );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-11 22:42:53 +00:00
|
|
|
tr_bencFree( &top );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-18 16:47:27 +00:00
|
|
|
static int
|
|
|
|
test2( void )
|
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
const char * in = " ";
|
2009-04-05 23:16:45 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
top.type = 0;
|
2009-06-01 18:57:37 +00:00
|
|
|
err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL );
|
2009-02-18 16:47:27 +00:00
|
|
|
|
|
|
|
check( err );
|
|
|
|
check( !tr_bencIsDict( &top ) );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-15 19:27:47 +00:00
|
|
|
static int
|
|
|
|
test3( void )
|
|
|
|
{
|
|
|
|
const char * in = "{ \"error\": 2,"
|
|
|
|
" \"errorString\": \"torrent not registered with this tracker 6UHsVW'*C\","
|
|
|
|
" \"eta\": 262792,"
|
|
|
|
" \"id\": 25,"
|
|
|
|
" \"leftUntilDone\": 2275655680 }";
|
|
|
|
tr_benc top;
|
|
|
|
const char * str;
|
|
|
|
|
|
|
|
const int err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL );
|
|
|
|
check( !err );
|
|
|
|
check( tr_bencDictFindStr( &top, "errorString", &str ) );
|
2012-10-14 18:56:37 +00:00
|
|
|
check_streq( "torrent not registered with this tracker 6UHsVW'*C", str );
|
2010-04-15 19:27:47 +00:00
|
|
|
|
|
|
|
tr_bencFree( &top );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-11 22:42:53 +00:00
|
|
|
int
|
|
|
|
main( void )
|
|
|
|
{
|
2012-08-18 16:07:05 +00:00
|
|
|
const testFunc tests[] = { test_utf8, test1, test2, test3, };
|
|
|
|
return runTests(tests, NUM_TESTS(tests));
|
2008-05-11 22:42:53 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|