source: trunk/libtransmission/blocklist-test.c @ 12918

Last change on this file since 12918 was 12229, checked in by jordan, 11 years ago

(trunk libT) API cleanup of the tr_address functions to make them more consistent.

This is loosely related to #2910, but only in the sense of laying the groundwork for #2910's fix...

File size: 3.4 KB
Line 
1#include <stdio.h>
2#include "transmission.h"
3#include "blocklist.h"
4#include "net.h"
5#include "utils.h"
6
7#undef VERBOSE
8
9#ifdef VERBOSE
10  #define check( A ) \
11    { \
12        ++test; \
13        if( A ){ \
14            fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
15        } else { \
16            fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
17            return test; \
18        } \
19    }
20#else
21  #define check( A )\
22    { \
23        ++test; \
24        if( !( A ) ){ \
25            fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
26            return test; \
27        } \
28    }
29#endif
30
31static void
32createTestBlocklist( const char * tmpfile )
33{
34    const char * lines[] = { "Austin Law Firm:216.16.1.144-216.16.1.151",
35                             "Sargent Controls and Aerospace:216.19.18.0-216.19.18.255",
36                             "Corel Corporation:216.21.157.192-216.21.157.223",
37                             "Fox Speed Channel:216.79.131.192-216.79.131.223" };
38    FILE *       out;
39    int          i;
40    const int    lineCount = sizeof( lines ) / sizeof( lines[0] );
41
42    /* create the ascii file to feed to libtransmission */
43    out = fopen( tmpfile, "w+" );
44    for( i = 0; i < lineCount; ++i )
45        fprintf( out, "%s\n", lines[i] );
46    fclose( out );
47}
48
49int
50main( void )
51{
52#ifndef WIN32
53    const char *   tmpfile_txt = "/tmp/transmission-blocklist-test.txt";
54    const char *   tmpfile_bin = "/tmp/transmission-blocklist-test.bin";
55#else
56    const char *   tmpfile_txt = "transmission-blocklist-test.txt";
57    const char *   tmpfile_bin = "transmission-blocklist-test.bin";
58#endif
59    struct tr_address addr;
60    int            test = 0;
61    tr_blocklist * b;
62
63    remove( tmpfile_txt );
64    remove( tmpfile_bin );
65
66    b = _tr_blocklistNew( tmpfile_bin, true );
67    createTestBlocklist( tmpfile_txt );
68    _tr_blocklistSetContent( b, tmpfile_txt );
69
70    /* now run some tests */
71    check( tr_address_from_string( &addr, "216.16.1.143" ) );
72    check( !_tr_blocklistHasAddress( b, &addr ) );
73    check( tr_address_from_string( &addr, "216.16.1.144" ) );
74    check( _tr_blocklistHasAddress( b, &addr ) );
75    check( tr_address_from_string( &addr, "216.16.1.145" ) );
76    check( _tr_blocklistHasAddress( b, &addr ) );
77    check( tr_address_from_string( &addr, "216.16.1.146" ) );
78    check( _tr_blocklistHasAddress( b, &addr ) );
79    check( tr_address_from_string( &addr, "216.16.1.147" ) );
80    check( _tr_blocklistHasAddress( b, &addr ) );
81    check( tr_address_from_string( &addr, "216.16.1.148" ) );
82    check( _tr_blocklistHasAddress( b, &addr ) );
83    check( tr_address_from_string( &addr, "216.16.1.149" ) );
84    check( _tr_blocklistHasAddress( b, &addr ) );
85    check( tr_address_from_string( &addr, "216.16.1.150" ) );
86    check( _tr_blocklistHasAddress( b, &addr ) );
87    check( tr_address_from_string( &addr, "216.16.1.151" ) );
88    check( _tr_blocklistHasAddress( b, &addr ) );
89    check( tr_address_from_string( &addr, "216.16.1.152" ) );
90    check( !_tr_blocklistHasAddress( b, &addr ) );
91    check( tr_address_from_string( &addr, "216.16.1.153" ) );
92    check( !_tr_blocklistHasAddress( b, &addr ) );
93    check( tr_address_from_string( &addr, "217.0.0.1" ) );
94    check( !_tr_blocklistHasAddress( b, &addr ) );
95    check( tr_address_from_string( &addr, "255.0.0.1" ) );
96
97    /* cleanup */
98    _tr_blocklistFree( b );
99    remove( tmpfile_txt );
100    remove( tmpfile_bin );
101    return 0;
102}
103
Note: See TracBrowser for help on using the repository browser.