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

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

(libT) re-apply jhujhiti's IPv6 patch. This merges in my tr_port cleanup, so any new bugs are mine :/

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