From: Jordan Lee Date: Mon, 21 Jan 2013 21:11:00 +0000 (+0000) Subject: (libT) #4632 'custom blocklist generates 2x # of rules': add unit test (currently... X-Git-Tag: 2.80~245 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5d9335201d27beaa22ccf0a80b813c2d1e2e2fdb;p=transmission (libT) #4632 'custom blocklist generates 2x # of rules': add unit test (currently failing) to test for this issue. --- diff --git a/libtransmission/blocklist-test.c b/libtransmission/blocklist-test.c index 9b2b80884..b4ad8d625 100644 --- a/libtransmission/blocklist-test.c +++ b/libtransmission/blocklist-test.c @@ -1,86 +1,136 @@ +#include #include +#include /* sync() */ + #include "transmission.h" #include "blocklist.h" #include "net.h" +#include "session.h" /* tr_sessionIsAddressBlocked() */ #include "utils.h" #include "libtransmission-test.h" -#ifndef WIN32 - #define TEMPDIR_PREFIX "/tmp/" -#else - #define TEMPDIR_PREFIX -#endif +static const char * contents1 = + "Austin Law Firm:216.16.1.144-216.16.1.151\n" + "Sargent Controls and Aerospace:216.19.18.0-216.19.18.255\n" + "Corel Corporation:216.21.157.192-216.21.157.223\n" + "Fox Speed Channel:216.79.131.192-216.79.131.223\n"; -#define TEMPFILE_TXT TEMPDIR_PREFIX "transmission-blocklist-test.txt" -#define TEMPFILE_BIN TEMPDIR_PREFIX "transmission-blocklist-test.bin" +static const char * contents2 = + "Austin Law Firm:216.16.1.144-216.16.1.151\n" + "Sargent Controls and Aerospace:216.19.18.0-216.19.18.255\n" + "Corel Corporation:216.21.157.192-216.21.157.223\n" + "Fox Speed Channel:216.79.131.192-216.79.131.223\n" + "Evilcorp:216.88.88.0-216.88.88.255\n"; -static void -createTestBlocklist (const char * tmpfile) +static char * +create_blocklist_text_file (const char * basename, const char * contents) { - const char * lines[] = { "Austin Law Firm:216.16.1.144-216.16.1.151", - "Sargent Controls and Aerospace:216.19.18.0-216.19.18.255", - "Corel Corporation:216.21.157.192-216.21.157.223", - "Fox Speed Channel:216.79.131.192-216.79.131.223" }; - FILE * out; - int i; - const int lineCount = sizeof (lines) / sizeof (lines[0]); - - /* create the ascii file to feed to libtransmission */ - out = fopen (tmpfile, "w+"); - for (i = 0; i < lineCount; ++i) - fprintf (out, "%s\n", lines[i]); - fclose (out); + FILE * fp; + char * path; + + assert (blocklistDir != NULL); + + path = tr_buildPath (blocklistDir, basename, NULL); + remove (path); + fp = fopen (path, "w+"); + fprintf (fp, "%s", contents); + fclose (fp); + sync (); + return path; +} + +static bool +address_is_blocked (const char * address_str) +{ + struct tr_address addr; + tr_address_from_string (&addr, address_str); + return tr_sessionIsAddressBlocked (session, &addr); } static int -testBlockList (void) +test_parsing (void) { - const char * tmpfile_txt = TEMPFILE_TXT; - const char * tmpfile_bin = TEMPFILE_BIN; - struct tr_address addr; - tr_blocklistFile * b; - - remove (tmpfile_txt); - remove (tmpfile_bin); - - b = tr_blocklistFileNew (tmpfile_bin, true); - createTestBlocklist (tmpfile_txt); - tr_blocklistFileSetContent (b, tmpfile_txt); - - /* now run some tests */ - check (tr_address_from_string (&addr, "216.16.1.143")); - check (!tr_blocklistFileHasAddress (b, &addr)); - check (tr_address_from_string (&addr, "216.16.1.144")); - check (tr_blocklistFileHasAddress (b, &addr)); - check (tr_address_from_string (&addr, "216.16.1.145")); - check (tr_blocklistFileHasAddress (b, &addr)); - check (tr_address_from_string (&addr, "216.16.1.146")); - check (tr_blocklistFileHasAddress (b, &addr)); - check (tr_address_from_string (&addr, "216.16.1.147")); - check (tr_blocklistFileHasAddress (b, &addr)); - check (tr_address_from_string (&addr, "216.16.1.148")); - check (tr_blocklistFileHasAddress (b, &addr)); - check (tr_address_from_string (&addr, "216.16.1.149")); - check (tr_blocklistFileHasAddress (b, &addr)); - check (tr_address_from_string (&addr, "216.16.1.150")); - check (tr_blocklistFileHasAddress (b, &addr)); - check (tr_address_from_string (&addr, "216.16.1.151")); - check (tr_blocklistFileHasAddress (b, &addr)); - check (tr_address_from_string (&addr, "216.16.1.152")); - check (!tr_blocklistFileHasAddress (b, &addr)); - check (tr_address_from_string (&addr, "216.16.1.153")); - check (!tr_blocklistFileHasAddress (b, &addr)); - check (tr_address_from_string (&addr, "217.0.0.1")); - check (!tr_blocklistFileHasAddress (b, &addr)); - check (tr_address_from_string (&addr, "255.0.0.1")); - - /* cleanup */ - tr_blocklistFileFree (b); - remove (tmpfile_txt); - remove (tmpfile_bin); - return 0; + char * text_file; + + libtransmission_test_session_init_sandbox (); + text_file = create_blocklist_text_file ("level1", contents1); + libtransmission_test_session_init_session (); + + check (!tr_blocklistIsEnabled (session)); + tr_blocklistSetEnabled (session, true); + check (tr_blocklistIsEnabled (session)); + + check (tr_blocklistExists (session)); + check_int_eq (4, tr_blocklistGetRuleCount (session)); + + check (!address_is_blocked ("216.16.1.143")); + check ( address_is_blocked ("216.16.1.144")); + check ( address_is_blocked ("216.16.1.145")); + check ( address_is_blocked ("216.16.1.146")); + check ( address_is_blocked ("216.16.1.147")); + check ( address_is_blocked ("216.16.1.148")); + check ( address_is_blocked ("216.16.1.149")); + check ( address_is_blocked ("216.16.1.150")); + check ( address_is_blocked ("216.16.1.151")); + check (!address_is_blocked ("216.16.1.152")); + check (!address_is_blocked ("216.16.1.153")); + check (!address_is_blocked ("217.0.0.1")); + check (!address_is_blocked ("255.0.0.1")); + + libtransmission_test_session_close (); + tr_free (text_file); + return 0; } -MAIN_SINGLE_TEST (testBlockList) +/*** +**** +***/ +static int +test_updating (void) +{ + char * text_file; + + libtransmission_test_session_init_sandbox (); + text_file = create_blocklist_text_file ("level1", contents1); + libtransmission_test_session_init_session (); + check_int_eq (4, tr_blocklistGetRuleCount (session)); + + /* test that updated source files will get loaded */ + tr_free (text_file); + text_file = create_blocklist_text_file ("level1", contents2); + tr_sessionReloadBlocklists (session); + check_int_eq (5, tr_blocklistGetRuleCount (session)); + + /* test that updated source files will get loaded */ + tr_free (text_file); + text_file = create_blocklist_text_file ("level1", contents1); + tr_sessionReloadBlocklists (session); + check_int_eq (4, tr_blocklistGetRuleCount (session)); + + /* ensure that new files, if bad, get skipped */ + tr_free (text_file); + text_file = create_blocklist_text_file ("level1", "# nothing useful\n"); + tr_sessionReloadBlocklists (session); + check_int_eq (4, tr_blocklistGetRuleCount (session)); + + libtransmission_test_session_close (); + tr_free (text_file); + return 0; +} + +/*** +**** +***/ + +int +main (void) +{ + const testFunc tests[] = { test_parsing, + test_updating }; + + libtransmission_test_session_init_formatters (); + + return runTests (tests, NUM_TESTS (tests)); +} diff --git a/libtransmission/libtransmission-test.c b/libtransmission/libtransmission-test.c index 361842e0f..422c992c7 100644 --- a/libtransmission/libtransmission-test.c +++ b/libtransmission/libtransmission-test.c @@ -111,7 +111,9 @@ runTests (const testFunc * const tests, int numTests) #include "variant.h" tr_session * session = NULL; -static char * sandbox = NULL; +char * sandbox = NULL; +char * downloadDir = NULL; +char * blocklistDir = NULL; static char* tr_getcwd (void) @@ -187,15 +189,17 @@ rm_rf (const char * killme) #define SPEED_T_STR "TB/s" void -libtransmission_test_session_init (void) +libtransmission_test_session_init_formatters (void) { - char * cwd; - char * downloadDir; - tr_variant dict; - tr_formatter_mem_init (MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR); tr_formatter_size_init (DISK_K,DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR); tr_formatter_speed_init (SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR); +} + +void +libtransmission_test_session_init_sandbox (void) +{ + char * cwd; /* create a sandbox for the test session */ cwd = tr_getcwd (); @@ -203,18 +207,40 @@ libtransmission_test_session_init (void) tr_mkdtemp (sandbox); downloadDir = tr_buildPath (sandbox, "Downloads", NULL); tr_mkdirp (downloadDir, 0700); + blocklistDir = tr_buildPath (sandbox, "blocklists", NULL); + tr_mkdirp (blocklistDir, 0700); + + /* cleanup locals*/ + tr_free (cwd); +} + +void +libtransmission_test_session_init_session (void) +{ + tr_variant dict; - /* create a test session */ - tr_variantInitDict (&dict, 3); + /* libtransmission_test_session_init_sandbox() has to be called first */ + assert (sandbox != NULL); + assert (session == NULL); + + /* init the session */ + tr_variantInitDict (&dict, 4); tr_variantDictAddStr (&dict, TR_KEY_download_dir, downloadDir); tr_variantDictAddBool (&dict, TR_KEY_port_forwarding_enabled, false); tr_variantDictAddBool (&dict, TR_KEY_dht_enabled, false); - session = tr_sessionInit ("rename-test", sandbox, true, &dict); + tr_variantDictAddInt (&dict, TR_KEY_message_level, verbose ? TR_MSG_DBG : TR_MSG_ERR); + session = tr_sessionInit ("libtransmission-test", sandbox, !verbose, &dict); /* cleanup locals*/ tr_variantFree (&dict); - tr_free (downloadDir); - tr_free (cwd); +} + +void +libtransmission_test_session_init (void) +{ + libtransmission_test_session_init_formatters (); + libtransmission_test_session_init_sandbox (); + libtransmission_test_session_init_session (); } void @@ -222,8 +248,18 @@ libtransmission_test_session_close (void) { tr_sessionClose (session); tr_freeMessageList (tr_getQueuedMessages ()); + session = NULL; + rm_rf (sandbox); + + tr_free (blocklistDir); + blocklistDir = NULL; + + tr_free (downloadDir); + downloadDir = NULL; + tr_free (sandbox); + sandbox = NULL; } /*** diff --git a/libtransmission/libtransmission-test.h b/libtransmission/libtransmission-test.h index 3994c675f..49b9544ff 100644 --- a/libtransmission/libtransmission-test.h +++ b/libtransmission/libtransmission-test.h @@ -67,7 +67,15 @@ int main (void) { \ } extern tr_session * session; -void libtransmission_test_session_init (void); +extern char * sandbox; +extern char * downloadDir; +extern char * blocklistDir; + +void libtransmission_test_session_init_formatters (void); +void libtransmission_test_session_init_sandbox (void); +void libtransmission_test_session_init_session (void); +void libtransmission_test_session_init (void); /* utility; calls the other 3 */ + void libtransmission_test_session_close (void); tr_torrent * libtransmission_test_zero_torrent_init (void);