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

Last change on this file since 7783 was 7783, checked in by charles, 13 years ago

(trunk libT) fix some code that the newly-added gcc warnings didn't like

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