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 | #define VERBOSE 0 |
---|
9 | |
---|
10 | static int test = 0; |
---|
11 | |
---|
12 | #define check( A ) \ |
---|
13 | { \ |
---|
14 | ++test; \ |
---|
15 | if( A ){ \ |
---|
16 | if( VERBOSE ) \ |
---|
17 | fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__,\ |
---|
18 | __LINE__ );\ |
---|
19 | } else { \ |
---|
20 | fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__,\ |
---|
21 | __LINE__ ); \ |
---|
22 | return test; \ |
---|
23 | } \ |
---|
24 | } |
---|
25 | |
---|
26 | #include "ConvertUTF.h" |
---|
27 | |
---|
28 | static int |
---|
29 | test_utf8( void ) |
---|
30 | { |
---|
31 | const char * in = "{ \"key\": \"Letöltések\" }"; |
---|
32 | tr_benc top; |
---|
33 | const char * str; |
---|
34 | char * json; |
---|
35 | int err; |
---|
36 | struct evbuffer * buf = tr_getBuffer( ); |
---|
37 | |
---|
38 | err = tr_jsonParse( in, strlen( in ), &top, NULL ); |
---|
39 | check( !err ); |
---|
40 | check( tr_bencIsDict( &top ) ); |
---|
41 | check( tr_bencDictFindStr( &top, "key", &str ) ); |
---|
42 | check( !strcmp( str, "Letöltések" ) ); |
---|
43 | if( !err ) |
---|
44 | tr_bencFree( &top ); |
---|
45 | |
---|
46 | in = "{ \"key\": \"\\u005C\" }"; |
---|
47 | err = tr_jsonParse( in, strlen( in ), &top, NULL ); |
---|
48 | check( !err ); |
---|
49 | check( tr_bencIsDict( &top ) ); |
---|
50 | check( tr_bencDictFindStr( &top, "key", &str ) ); |
---|
51 | check( !strcmp( str, "\\" ) ); |
---|
52 | if( !err ) |
---|
53 | tr_bencFree( &top ); |
---|
54 | |
---|
55 | /** |
---|
56 | * 1. Feed it JSON-escaped nonascii to the JSON decoder. |
---|
57 | * 2. Confirm that the result is UTF-8. |
---|
58 | * 3. Feed the same UTF-8 back into the JSON encoder. |
---|
59 | * 4. Confirm that the result is JSON-escaped. |
---|
60 | * 5. Dogfood that result back into the parser. |
---|
61 | * 6. Confirm that the result is UTF-8. |
---|
62 | */ |
---|
63 | in = "{ \"key\": \"Let\\u00f6lt\\u00e9sek\" }"; |
---|
64 | err = tr_jsonParse( in, strlen( in ), &top, NULL ); |
---|
65 | check( !err ); |
---|
66 | check( tr_bencIsDict( &top ) ); |
---|
67 | check( tr_bencDictFindStr( &top, "key", &str ) ); |
---|
68 | check( !strcmp( str, "Letöltések" ) ); |
---|
69 | json = tr_bencSaveAsJSON( &top, buf ); |
---|
70 | if( !err ) |
---|
71 | tr_bencFree( &top ); |
---|
72 | check( json ); |
---|
73 | check( strstr( json, "\\u00f6" ) != NULL ); |
---|
74 | check( strstr( json, "\\u00e9" ) != NULL ); |
---|
75 | err = tr_jsonParse( json, strlen( json ), &top, NULL ); |
---|
76 | check( !err ); |
---|
77 | check( tr_bencIsDict( &top ) ); |
---|
78 | check( tr_bencDictFindStr( &top, "key", &str ) ); |
---|
79 | check( !strcmp( str, "Letöltések" ) ); |
---|
80 | if( !err ) |
---|
81 | tr_bencFree( &top ); |
---|
82 | tr_free( json ); |
---|
83 | |
---|
84 | return 0; |
---|
85 | } |
---|
86 | |
---|
87 | static int |
---|
88 | test1( void ) |
---|
89 | { |
---|
90 | const char * in = |
---|
91 | "{\n" |
---|
92 | " \"headers\": {\n" |
---|
93 | " \"type\": \"request\",\n" |
---|
94 | " \"tag\": 666\n" |
---|
95 | " },\n" |
---|
96 | " \"body\": {\n" |
---|
97 | " \"name\": \"torrent-info\",\n" |
---|
98 | " \"arguments\": {\n" |
---|
99 | " \"ids\": [ 7, 10 ]\n" |
---|
100 | " }\n" |
---|
101 | " }\n" |
---|
102 | "}\n"; |
---|
103 | tr_benc top, *headers, *body, *args, *ids; |
---|
104 | const char * str; |
---|
105 | int64_t i; |
---|
106 | const int err = tr_jsonParse( in, strlen( in ), &top, NULL ); |
---|
107 | |
---|
108 | check( !err ); |
---|
109 | check( tr_bencIsDict( &top ) ); |
---|
110 | check( ( headers = tr_bencDictFind( &top, "headers" ) ) ); |
---|
111 | check( tr_bencIsDict( headers ) ); |
---|
112 | check( tr_bencDictFindStr( headers, "type", &str ) ); |
---|
113 | check( !strcmp( str, "request" ) ); |
---|
114 | check( tr_bencDictFindInt( headers, "tag", &i ) ); |
---|
115 | check( i == 666 ); |
---|
116 | check( ( body = tr_bencDictFind( &top, "body" ) ) ); |
---|
117 | check( tr_bencDictFindStr( body, "name", &str ) ); |
---|
118 | check( !strcmp( str, "torrent-info" ) ); |
---|
119 | check( ( args = tr_bencDictFind( body, "arguments" ) ) ); |
---|
120 | check( tr_bencIsDict( args ) ); |
---|
121 | check( ( ids = tr_bencDictFind( args, "ids" ) ) ); |
---|
122 | check( tr_bencIsList( ids ) ); |
---|
123 | check( tr_bencListSize( ids ) == 2 ); |
---|
124 | check( tr_bencGetInt( tr_bencListChild( ids, 0 ), &i ) ); |
---|
125 | check( i == 7 ); |
---|
126 | check( tr_bencGetInt( tr_bencListChild( ids, 1 ), &i ) ); |
---|
127 | check( i == 10 ); |
---|
128 | |
---|
129 | tr_bencFree( &top ); |
---|
130 | return 0; |
---|
131 | } |
---|
132 | |
---|
133 | int |
---|
134 | main( void ) |
---|
135 | { |
---|
136 | int i; |
---|
137 | |
---|
138 | if( ( i = test_utf8( ) ) ) |
---|
139 | return i; |
---|
140 | |
---|
141 | if( ( i = test1( ) ) ) |
---|
142 | return i; |
---|
143 | |
---|
144 | return 0; |
---|
145 | } |
---|
146 | |
---|