source: trunk/libtransmission/magnet-test.c@ 9531

Last change on this file since 9531 was 9531, checked in by charles, 15 years ago

(trunk libT) #2096: add code magnet URL parser and unit tests

File size: 2.5 KB
Line 
1#include <stdio.h>
2#include <string.h>
3#include "transmission.h"
4#include "magnet.h"
5#include "utils.h"
6
7/* #define VERBOSE */
8#undef VERBOSE
9
10static int test = 0;
11
12#ifdef VERBOSE
13 #define check( A ) \
14 { \
15 ++test; \
16 if( A ){ \
17 fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
18 } else { \
19 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
20 return test; \
21 } \
22 }
23#else
24 #define check( A ) \
25 { \
26 ++test; \
27 if( !( A ) ){ \
28 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
29 return test; \
30 } \
31 }
32#endif
33
34static int
35test1( void )
36{
37 int i;
38 const char * uri;
39 tr_magnet_info * info;
40 const int dec[] = { 210, 53, 64, 16, 163, 202, 74, 222, 91, 116,
41 39, 187, 9, 58, 98, 163, 137, 159, 243, 129 };
42
43 uri = "magnet:?xt=urn:btih:"
44 "d2354010a3ca4ade5b7427bb093a62a3899ff381"
45 "&dn=Display%20Name"
46 "&tr=http%3A%2F%2Ftracker.openbittorrent.com%2Fannounce"
47 "&tr=http%3A%2F%2Ftracker.opentracker.org%2Fannounce";
48 info = tr_magnetParse( uri );
49 check( info != NULL )
50 check( info->announceCount == 2 );
51 check( !strcmp( info->announceURLs[0], "http://tracker.openbittorrent.com/announce" ) )
52 check( !strcmp( info->announceURLs[1], "http://tracker.opentracker.org/announce" ) )
53 check( !strcmp( info->displayName, "Display Name" ) )
54 for( i=0; i<20; ++i )
55 check( info->hash[i] == dec[i] );
56 tr_magnetFree( info );
57 info = NULL;
58
59 /* same thing but in base32 encoding */
60 uri = "magnet:?xt=urn:btih:"
61 "2I2UAEFDZJFN4W3UE65QSOTCUOEZ744B"
62 "&dn=Display%20Name"
63 "&tr=http%3A%2F%2Ftracker.openbittorrent.com%2Fannounce"
64 "&tr=http%3A%2F%2Ftracker.opentracker.org%2Fannounce";
65 info = tr_magnetParse( uri );
66 check( info != NULL )
67 check( info->announceCount == 2 );
68 check( !strcmp( info->announceURLs[0], "http://tracker.openbittorrent.com/announce" ) )
69 check( !strcmp( info->announceURLs[1], "http://tracker.opentracker.org/announce" ) )
70 check( !strcmp( info->displayName, "Display Name" ) )
71 for( i=0; i<20; ++i )
72 check( info->hash[i] == dec[i] );
73 tr_magnetFree( info );
74 info = NULL;
75
76 return 0;
77}
78
79int
80main( void )
81{
82 int i;
83
84 if( ( i = test1( ) ) )
85 return i;
86
87#ifdef VERBOSE
88 fprintf( stderr, "magnet-test passed\n" );
89#endif
90 return 0;
91}
92
Note: See TracBrowser for help on using the repository browser.