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

Last change on this file since 5485 was 5485, checked in by charles, 14 years ago

encapsulate the blocklist a little better so that we can add more blocklists in the future if necessary

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