1 | #include <stdio.h> |
---|
2 | #include <string.h> |
---|
3 | #include "transmission.h" |
---|
4 | #include "bencode.h" |
---|
5 | #include "json.h" |
---|
6 | #include "utils.h" /* tr_free */ |
---|
7 | |
---|
8 | #undef VERBOSE |
---|
9 | #include "libtransmission-test.h" |
---|
10 | |
---|
11 | #include "ConvertUTF.h" |
---|
12 | |
---|
13 | static int |
---|
14 | test_utf8( void ) |
---|
15 | { |
---|
16 | const char * in = "{ \"key\": \"Letöltések\" }"; |
---|
17 | tr_benc top; |
---|
18 | const char * str; |
---|
19 | char * json; |
---|
20 | int err; |
---|
21 | |
---|
22 | err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL ); |
---|
23 | check( !err ); |
---|
24 | check( tr_bencIsDict( &top ) ); |
---|
25 | check( tr_bencDictFindStr( &top, "key", &str ) ); |
---|
26 | check( !strcmp( str, "Letöltések" ) ); |
---|
27 | if( !err ) |
---|
28 | tr_bencFree( &top ); |
---|
29 | |
---|
30 | in = "{ \"key\": \"\\u005C\" }"; |
---|
31 | err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL ); |
---|
32 | check( !err ); |
---|
33 | check( tr_bencIsDict( &top ) ); |
---|
34 | check( tr_bencDictFindStr( &top, "key", &str ) ); |
---|
35 | check( !strcmp( str, "\\" ) ); |
---|
36 | if( !err ) |
---|
37 | tr_bencFree( &top ); |
---|
38 | |
---|
39 | /** |
---|
40 | * 1. Feed it JSON-escaped nonascii to the JSON decoder. |
---|
41 | * 2. Confirm that the result is UTF-8. |
---|
42 | * 3. Feed the same UTF-8 back into the JSON encoder. |
---|
43 | * 4. Confirm that the result is JSON-escaped. |
---|
44 | * 5. Dogfood that result back into the parser. |
---|
45 | * 6. Confirm that the result is UTF-8. |
---|
46 | */ |
---|
47 | in = "{ \"key\": \"Let\\u00f6lt\\u00e9sek\" }"; |
---|
48 | err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL ); |
---|
49 | check( !err ); |
---|
50 | check( tr_bencIsDict( &top ) ); |
---|
51 | check( tr_bencDictFindStr( &top, "key", &str ) ); |
---|
52 | check( !strcmp( str, "Letöltések" ) ); |
---|
53 | json = tr_bencToStr( &top, TR_FMT_JSON, NULL ); |
---|
54 | if( !err ) |
---|
55 | tr_bencFree( &top ); |
---|
56 | check( json ); |
---|
57 | check( strstr( json, "\\u00f6" ) != NULL ); |
---|
58 | check( strstr( json, "\\u00e9" ) != NULL ); |
---|
59 | err = tr_jsonParse( NULL, json, strlen( json ), &top, NULL ); |
---|
60 | check( !err ); |
---|
61 | check( tr_bencIsDict( &top ) ); |
---|
62 | check( tr_bencDictFindStr( &top, "key", &str ) ); |
---|
63 | check( !strcmp( str, "Letöltések" ) ); |
---|
64 | if( !err ) |
---|
65 | tr_bencFree( &top ); |
---|
66 | tr_free( json ); |
---|
67 | |
---|
68 | return 0; |
---|
69 | } |
---|
70 | |
---|
71 | static int |
---|
72 | test1( void ) |
---|
73 | { |
---|
74 | const char * in = |
---|
75 | "{\n" |
---|
76 | " \"headers\": {\n" |
---|
77 | " \"type\": \"request\",\n" |
---|
78 | " \"tag\": 666\n" |
---|
79 | " },\n" |
---|
80 | " \"body\": {\n" |
---|
81 | " \"name\": \"torrent-info\",\n" |
---|
82 | " \"arguments\": {\n" |
---|
83 | " \"ids\": [ 7, 10 ]\n" |
---|
84 | " }\n" |
---|
85 | " }\n" |
---|
86 | "}\n"; |
---|
87 | tr_benc top, *headers, *body, *args, *ids; |
---|
88 | const char * str; |
---|
89 | int64_t i; |
---|
90 | const int err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL ); |
---|
91 | |
---|
92 | check( !err ); |
---|
93 | check( tr_bencIsDict( &top ) ); |
---|
94 | check( ( headers = tr_bencDictFind( &top, "headers" ) ) ); |
---|
95 | check( tr_bencIsDict( headers ) ); |
---|
96 | check( tr_bencDictFindStr( headers, "type", &str ) ); |
---|
97 | check( !strcmp( str, "request" ) ); |
---|
98 | check( tr_bencDictFindInt( headers, "tag", &i ) ); |
---|
99 | check( i == 666 ); |
---|
100 | check( ( body = tr_bencDictFind( &top, "body" ) ) ); |
---|
101 | check( tr_bencDictFindStr( body, "name", &str ) ); |
---|
102 | check( !strcmp( str, "torrent-info" ) ); |
---|
103 | check( ( args = tr_bencDictFind( body, "arguments" ) ) ); |
---|
104 | check( tr_bencIsDict( args ) ); |
---|
105 | check( ( ids = tr_bencDictFind( args, "ids" ) ) ); |
---|
106 | check( tr_bencIsList( ids ) ); |
---|
107 | check( tr_bencListSize( ids ) == 2 ); |
---|
108 | check( tr_bencGetInt( tr_bencListChild( ids, 0 ), &i ) ); |
---|
109 | check( i == 7 ); |
---|
110 | check( tr_bencGetInt( tr_bencListChild( ids, 1 ), &i ) ); |
---|
111 | check( i == 10 ); |
---|
112 | |
---|
113 | tr_bencFree( &top ); |
---|
114 | return 0; |
---|
115 | } |
---|
116 | |
---|
117 | static int |
---|
118 | test2( void ) |
---|
119 | { |
---|
120 | tr_benc top; |
---|
121 | const char * in = " "; |
---|
122 | int err; |
---|
123 | |
---|
124 | top.type = 0; |
---|
125 | err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL ); |
---|
126 | |
---|
127 | check( err ); |
---|
128 | check( !tr_bencIsDict( &top ) ); |
---|
129 | |
---|
130 | return 0; |
---|
131 | } |
---|
132 | |
---|
133 | static int |
---|
134 | test3( void ) |
---|
135 | { |
---|
136 | const char * in = "{ \"error\": 2," |
---|
137 | " \"errorString\": \"torrent not registered with this tracker 6UHsVW'*C\"," |
---|
138 | " \"eta\": 262792," |
---|
139 | " \"id\": 25," |
---|
140 | " \"leftUntilDone\": 2275655680 }"; |
---|
141 | tr_benc top; |
---|
142 | const char * str; |
---|
143 | |
---|
144 | const int err = tr_jsonParse( NULL, in, strlen( in ), &top, NULL ); |
---|
145 | check( !err ); |
---|
146 | check( tr_bencDictFindStr( &top, "errorString", &str ) ); |
---|
147 | check( !strcmp( str, "torrent not registered with this tracker 6UHsVW'*C" ) ); |
---|
148 | |
---|
149 | tr_bencFree( &top ); |
---|
150 | return 0; |
---|
151 | } |
---|
152 | |
---|
153 | int |
---|
154 | main( void ) |
---|
155 | { |
---|
156 | const testFunc tests[] = { test_utf8, test1, test2, test3, }; |
---|
157 | return runTests(tests, NUM_TESTS(tests)); |
---|
158 | } |
---|
159 | |
---|