2012-12-05 17:29:46 +00:00
|
|
|
#include <string.h> /* strlen () */
|
2008-05-11 22:42:53 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "utils.h" /* tr_free */
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2008-05-11 22:42:53 +00:00
|
|
|
|
2012-08-18 16:07:05 +00:00
|
|
|
#include "libtransmission-test.h"
|
2008-05-11 22:42:53 +00:00
|
|
|
|
2012-11-11 04:20:15 +00:00
|
|
|
static int
|
|
|
|
test_elements (void)
|
|
|
|
{
|
|
|
|
const char * in;
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant top;
|
2012-11-11 04:20:15 +00:00
|
|
|
const char * str;
|
|
|
|
bool f;
|
|
|
|
double d;
|
|
|
|
int64_t i;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
in = "{ \"string\": \"hello world\","
|
|
|
|
" \"escaped\": \"bell \\b formfeed \\f linefeed \\n carriage return \\r tab \\t\","
|
|
|
|
" \"int\": 5, "
|
|
|
|
" \"float\": 6.5, "
|
|
|
|
" \"true\": true, "
|
|
|
|
" \"false\": false, "
|
|
|
|
" \"null\": null }";
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
err = tr_variantFromJson (&top, in, strlen(in));
|
2012-11-11 04:20:15 +00:00
|
|
|
check_int_eq (0, err);
|
2012-12-14 04:34:42 +00:00
|
|
|
check (tr_variantIsDict (&top));
|
2012-11-11 04:20:15 +00:00
|
|
|
str = NULL;
|
2012-12-14 04:34:42 +00:00
|
|
|
check (tr_variantDictFindStr (&top, "string", &str, NULL));
|
2012-11-11 04:20:15 +00:00
|
|
|
check_streq ("hello world", str);
|
2012-12-14 04:34:42 +00:00
|
|
|
check (tr_variantDictFindStr (&top, "escaped", &str, NULL));
|
2012-11-11 04:20:15 +00:00
|
|
|
check_streq ("bell \b formfeed \f linefeed \n carriage return \r tab \t", str);
|
|
|
|
i = 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
check (tr_variantDictFindInt (&top, "int", &i));
|
2012-11-11 04:20:15 +00:00
|
|
|
check_int_eq (5, i);
|
|
|
|
d = 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
check (tr_variantDictFindReal (&top, "float", &d));
|
2012-11-11 04:20:15 +00:00
|
|
|
check_int_eq (65, ((int)(d*10)));
|
|
|
|
f = false;
|
2012-12-14 04:34:42 +00:00
|
|
|
check (tr_variantDictFindBool (&top, "true", &f));
|
2012-11-11 04:20:15 +00:00
|
|
|
check_int_eq (true, f);
|
2012-12-14 04:34:42 +00:00
|
|
|
check (tr_variantDictFindBool (&top, "false", &f));
|
2012-11-11 04:20:15 +00:00
|
|
|
check_int_eq (false, f);
|
2012-12-14 04:34:42 +00:00
|
|
|
check (tr_variantDictFindStr (&top, "null", &str, NULL));
|
2012-11-11 04:20:15 +00:00
|
|
|
check_streq ("", str);
|
|
|
|
|
|
|
|
if (!err)
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantFree (&top);
|
2008-08-19 23:11:35 +00:00
|
|
|
|
2012-11-11 04:20:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-08-18 03:10:59 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
test_utf8 (void)
|
2008-08-18 03:10:59 +00:00
|
|
|
{
|
2008-12-30 22:07:39 +00:00
|
|
|
const char * in = "{ \"key\": \"Letöltések\" }";
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant top;
|
2008-12-30 22:07:39 +00:00
|
|
|
const char * str;
|
|
|
|
char * json;
|
|
|
|
int err;
|
2008-08-18 03:10:59 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
err = tr_variantFromJson (&top, in, strlen(in));
|
2012-12-05 17:29:46 +00:00
|
|
|
check (!err);
|
2012-12-14 04:34:42 +00:00
|
|
|
check (tr_variantIsDict (&top));
|
|
|
|
check (tr_variantDictFindStr (&top, "key", &str, NULL));
|
2012-12-05 17:29:46 +00:00
|
|
|
check_streq ("Letöltések", str);
|
|
|
|
if (!err)
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantFree (&top);
|
2008-08-18 03:10:59 +00:00
|
|
|
|
2008-08-19 23:11:35 +00:00
|
|
|
in = "{ \"key\": \"\\u005C\" }";
|
2012-12-14 04:34:42 +00:00
|
|
|
err = tr_variantFromJson (&top, in, strlen(in));
|
2012-12-05 17:29:46 +00:00
|
|
|
check (!err);
|
2012-12-14 04:34:42 +00:00
|
|
|
check (tr_variantIsDict (&top));
|
|
|
|
check (tr_variantDictFindStr (&top, "key", &str, NULL));
|
2012-12-05 17:29:46 +00:00
|
|
|
check_streq ("\\", str);
|
|
|
|
if (!err)
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantFree (&top);
|
2008-08-18 03:10:59 +00:00
|
|
|
|
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\" }";
|
2012-12-14 04:34:42 +00:00
|
|
|
err = tr_variantFromJson (&top, in, strlen(in));
|
2012-12-05 17:29:46 +00:00
|
|
|
check (!err);
|
2012-12-14 04:34:42 +00:00
|
|
|
check (tr_variantIsDict (&top));
|
|
|
|
check (tr_variantDictFindStr (&top, "key", &str, NULL));
|
2012-12-05 17:29:46 +00:00
|
|
|
check_streq ("Letöltések", str);
|
2012-12-14 04:34:42 +00:00
|
|
|
json = tr_variantToStr (&top, TR_VARIANT_FMT_JSON, NULL);
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!err)
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantFree (&top);
|
2012-12-05 17:29:46 +00:00
|
|
|
check (json);
|
|
|
|
check (strstr (json, "\\u00f6") != NULL);
|
|
|
|
check (strstr (json, "\\u00e9") != NULL);
|
2012-12-14 04:34:42 +00:00
|
|
|
err = tr_variantFromJson (&top, json, strlen(json));
|
2012-12-05 17:29:46 +00:00
|
|
|
check (!err);
|
2012-12-14 04:34:42 +00:00
|
|
|
check (tr_variantIsDict (&top));
|
|
|
|
check (tr_variantDictFindStr (&top, "key", &str, NULL));
|
2012-12-05 17:29:46 +00:00
|
|
|
check_streq ("Letöltések", str);
|
|
|
|
if (!err)
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantFree (&top);
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (json);
|
2008-08-19 23:11:35 +00:00
|
|
|
|
2008-08-18 03:10:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-11 22:42:53 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
test1 (void)
|
2008-05-11 22:42:53 +00:00
|
|
|
{
|
|
|
|
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";
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant 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;
|
2012-12-14 04:34:42 +00:00
|
|
|
const int err = tr_variantFromJson (&top, in, strlen(in));
|
2012-12-05 17:29:46 +00:00
|
|
|
|
|
|
|
check (!err);
|
2012-12-14 04:34:42 +00:00
|
|
|
check (tr_variantIsDict (&top));
|
|
|
|
check ((headers = tr_variantDictFind (&top, "headers")));
|
|
|
|
check (tr_variantIsDict (headers));
|
|
|
|
check (tr_variantDictFindStr (headers, "type", &str, NULL));
|
2012-12-05 17:29:46 +00:00
|
|
|
check_streq ("request", str);
|
2012-12-14 04:34:42 +00:00
|
|
|
check (tr_variantDictFindInt (headers, "tag", &i));
|
2012-12-05 17:29:46 +00:00
|
|
|
check_int_eq (666, i);
|
2012-12-14 04:34:42 +00:00
|
|
|
check ((body = tr_variantDictFind (&top, "body")));
|
|
|
|
check (tr_variantDictFindStr (body, "name", &str, NULL));
|
2012-12-05 17:29:46 +00:00
|
|
|
check_streq ("torrent-info", str);
|
2012-12-14 04:34:42 +00:00
|
|
|
check ((args = tr_variantDictFind (body, "arguments")));
|
|
|
|
check (tr_variantIsDict (args));
|
|
|
|
check ((ids = tr_variantDictFind (args, "ids")));
|
|
|
|
check (tr_variantIsList (ids));
|
|
|
|
check_int_eq (2, tr_variantListSize (ids));
|
|
|
|
check (tr_variantGetInt (tr_variantListChild (ids, 0), &i));
|
2012-12-05 17:29:46 +00:00
|
|
|
check_int_eq (7, i);
|
2012-12-14 04:34:42 +00:00
|
|
|
check (tr_variantGetInt (tr_variantListChild (ids, 1), &i));
|
2012-12-05 17:29:46 +00:00
|
|
|
check_int_eq (10, i);
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantFree (&top);
|
2008-05-11 22:42:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-18 16:47:27 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
test2 (void)
|
2009-02-18 16:47:27 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant top;
|
2009-02-18 16:47:27 +00:00
|
|
|
const char * in = " ";
|
2009-04-05 23:16:45 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
top.type = 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
err = tr_variantFromJson (&top, in, strlen(in));
|
2009-02-18 16:47:27 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
check (err);
|
2012-12-14 04:34:42 +00:00
|
|
|
check (!tr_variantIsDict (&top));
|
2009-02-18 16:47:27 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-15 19:27:47 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
test3 (void)
|
2010-04-15 19:27:47 +00:00
|
|
|
{
|
|
|
|
const char * in = "{ \"error\": 2,"
|
|
|
|
" \"errorString\": \"torrent not registered with this tracker 6UHsVW'*C\","
|
|
|
|
" \"eta\": 262792,"
|
|
|
|
" \"id\": 25,"
|
|
|
|
" \"leftUntilDone\": 2275655680 }";
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant top;
|
2010-04-15 19:27:47 +00:00
|
|
|
const char * str;
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
const int err = tr_variantFromJson (&top, in, strlen(in));
|
2012-12-05 17:29:46 +00:00
|
|
|
check (!err);
|
2012-12-14 04:34:42 +00:00
|
|
|
check (tr_variantDictFindStr (&top, "errorString", &str, NULL));
|
2012-12-05 17:29:46 +00:00
|
|
|
check_streq ("torrent not registered with this tracker 6UHsVW'*C", str);
|
2010-04-15 19:27:47 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantFree (&top);
|
2010-04-15 19:27:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-27 23:21:13 +00:00
|
|
|
static int
|
|
|
|
test_unescape (void)
|
|
|
|
{
|
|
|
|
const char * in = "{ \"string-1\": \"\\/usr\\/lib\" }";
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant top;
|
2012-11-27 23:21:13 +00:00
|
|
|
const char * str;
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
const int err = tr_variantFromJson (&top, in, strlen(in));
|
2012-11-27 23:21:13 +00:00
|
|
|
check_int_eq (0, err);
|
2012-12-14 04:34:42 +00:00
|
|
|
check (tr_variantDictFindStr (&top, "string-1", &str, NULL));
|
2012-11-27 23:21:13 +00:00
|
|
|
check_streq ("/usr/lib", str);
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variantFree (&top);
|
2012-11-27 23:21:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-11 22:42:53 +00:00
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
main (void)
|
2008-05-11 22:42:53 +00:00
|
|
|
{
|
2012-11-11 04:20:15 +00:00
|
|
|
const testFunc tests[] = { test_elements,
|
|
|
|
test_utf8,
|
|
|
|
test1,
|
|
|
|
test2,
|
2012-11-27 23:21:13 +00:00
|
|
|
test3,
|
|
|
|
test_unescape };
|
2012-11-11 04:20:15 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return runTests (tests, NUM_TESTS (tests));
|
2008-05-11 22:42:53 +00:00
|
|
|
}
|