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