1 | #include <stdio.h> |
---|
2 | #include "transmission.h" |
---|
3 | #include "bencode.h" |
---|
4 | #include "json.h" |
---|
5 | #include "utils.h" /* tr_free */ |
---|
6 | |
---|
7 | #define VERBOSE 0 |
---|
8 | |
---|
9 | static int test = 0; |
---|
10 | |
---|
11 | #define check(A) { \ |
---|
12 | ++test; \ |
---|
13 | if (A) { \ |
---|
14 | if( VERBOSE ) \ |
---|
15 | fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ |
---|
16 | } else { \ |
---|
17 | if( VERBOSE ) \ |
---|
18 | fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ |
---|
19 | return test; \ |
---|
20 | } \ |
---|
21 | } |
---|
22 | |
---|
23 | static int |
---|
24 | test1( void ) |
---|
25 | { |
---|
26 | const char * in = |
---|
27 | "{\n" |
---|
28 | " \"headers\": {\n" |
---|
29 | " \"type\": \"request\",\n" |
---|
30 | " \"tag\": 666\n" |
---|
31 | " },\n" |
---|
32 | " \"body\": {\n" |
---|
33 | " \"name\": \"torrent-info\",\n" |
---|
34 | " \"arguments\": {\n" |
---|
35 | " \"ids\": [ 7, 10 ]\n" |
---|
36 | " }\n" |
---|
37 | " }\n" |
---|
38 | "}\n"; |
---|
39 | tr_benc top, *headers, *body, *args, *ids; |
---|
40 | const char * str; |
---|
41 | int64_t i; |
---|
42 | const uint8_t * end = NULL; |
---|
43 | const int err = tr_jsonParse( in, strlen(in), &top, &end ); |
---|
44 | |
---|
45 | check( !err ); |
---|
46 | check( tr_bencIsDict( &top ) ); |
---|
47 | check(( headers = tr_bencDictFind( &top, "headers" ))); |
---|
48 | check( tr_bencIsDict( headers ) ); |
---|
49 | check( tr_bencDictFindStr( headers, "type", &str ) ); |
---|
50 | check( !strcmp( str, "request" ) ); |
---|
51 | check( tr_bencDictFindInt( headers, "tag", &i ) ); |
---|
52 | check( i == 666 ); |
---|
53 | check(( body = tr_bencDictFind( &top, "body" ))); |
---|
54 | check( tr_bencDictFindStr( body, "name", &str ) ); |
---|
55 | check( !strcmp( str, "torrent-info" ) ); |
---|
56 | check(( args = tr_bencDictFind( body, "arguments" ))); |
---|
57 | check( tr_bencIsDict( args ) ); |
---|
58 | check(( ids = tr_bencDictFind( args, "ids" ))); |
---|
59 | check( tr_bencIsList( ids ) ); |
---|
60 | check( tr_bencListSize( ids ) == 2 ); |
---|
61 | check( tr_bencGetInt( tr_bencListChild( ids, 0 ), &i ) ); |
---|
62 | check( i == 7 ); |
---|
63 | check( tr_bencGetInt( tr_bencListChild( ids, 1 ), &i ) ); |
---|
64 | check( i == 10 ); |
---|
65 | |
---|
66 | tr_bencFree( &top ); |
---|
67 | return 0; |
---|
68 | } |
---|
69 | |
---|
70 | int |
---|
71 | main( void ) |
---|
72 | { |
---|
73 | int i; |
---|
74 | |
---|
75 | if(( i = test1( ))) |
---|
76 | return i; |
---|
77 | |
---|
78 | return 0; |
---|
79 | } |
---|