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

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

blocklist cleanup

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