From: Jordan Lee Date: Fri, 1 Feb 2013 05:57:47 +0000 (+0000) Subject: change the test harness' session initialization s.t. we can configure it for per... X-Git-Tag: 2.80~154 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1a99b9c1685695a8d94e30605543423c020463e8;p=transmission change the test harness' session initialization s.t. we can configure it for per-test needs. Add tests for moving completed torrents when downloadDir is a subdirectory of incompleteDir. (Hi, KyleK) --- diff --git a/libtransmission/blocklist-test.c b/libtransmission/blocklist-test.c index b4ad8d625..9bc9f7427 100644 --- a/libtransmission/blocklist-test.c +++ b/libtransmission/blocklist-test.c @@ -23,25 +23,26 @@ static const char * contents2 = "Fox Speed Channel:216.79.131.192-216.79.131.223\n" "Evilcorp:216.88.88.0-216.88.88.255\n"; -static char * -create_blocklist_text_file (const char * basename, const char * contents) +static void +create_text_file (const char * path, const char * contents) { FILE * fp; - char * path; + char * dir; - assert (blocklistDir != NULL); + dir = tr_dirname (path); + tr_mkdirp (dir, 0700); + tr_free (dir); - 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) +address_is_blocked (tr_session * session, const char * address_str) { struct tr_address addr; tr_address_from_string (&addr, address_str); @@ -51,35 +52,44 @@ address_is_blocked (const char * address_str) static int test_parsing (void) { - char * text_file; + char * path; + tr_session * session; + + /* init the session */ + session = libttest_session_init (NULL); + check (!tr_blocklistExists (session)); + check_int_eq (0, tr_blocklistGetRuleCount (session)); - libtransmission_test_session_init_sandbox (); - text_file = create_blocklist_text_file ("level1", contents1); - libtransmission_test_session_init_session (); + /* init the blocklist */ + path = tr_buildPath (tr_sessionGetConfigDir(session), "blocklists", "level1", NULL); + create_text_file (path, contents1); + tr_free (path); + tr_sessionReloadBlocklists (session); + check (tr_blocklistExists (session)); + check_int_eq (4, tr_blocklistGetRuleCount (session)); + /* enable the blocklist */ 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); + /* test blocked addresses */ + check (!address_is_blocked (session, "216.16.1.143")); + check ( address_is_blocked (session, "216.16.1.144")); + check ( address_is_blocked (session, "216.16.1.145")); + check ( address_is_blocked (session, "216.16.1.146")); + check ( address_is_blocked (session, "216.16.1.147")); + check ( address_is_blocked (session, "216.16.1.148")); + check ( address_is_blocked (session, "216.16.1.149")); + check ( address_is_blocked (session, "216.16.1.150")); + check ( address_is_blocked (session, "216.16.1.151")); + check (!address_is_blocked (session, "216.16.1.152")); + check (!address_is_blocked (session, "216.16.1.153")); + check (!address_is_blocked (session, "217.0.0.1")); + check (!address_is_blocked (session, "255.0.0.1")); + + /* cleanup */ + libttest_session_close (session); return 0; } @@ -90,33 +100,39 @@ test_parsing (void) static int test_updating (void) { - char * text_file; + char * path; + tr_session * session; + + /* init the session */ + session = libttest_session_init (NULL); + path = tr_buildPath (tr_sessionGetConfigDir(session), "blocklists", "level1", NULL); - libtransmission_test_session_init_sandbox (); - text_file = create_blocklist_text_file ("level1", contents1); - libtransmission_test_session_init_session (); + /* no blocklist to start with... */ + check_int_eq (0, tr_blocklistGetRuleCount (session)); + + /* test that updated source files will get loaded */ + create_text_file (path, contents1); + tr_sessionReloadBlocklists (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); + create_text_file (path, 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); + create_text_file (path, 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"); + create_text_file (path, "# nothing useful\n"); tr_sessionReloadBlocklists (session); check_int_eq (4, tr_blocklistGetRuleCount (session)); - libtransmission_test_session_close (); - tr_free (text_file); + /* cleanup */ + libttest_session_close (session); + tr_free (path); return 0; } @@ -130,7 +146,5 @@ 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 612b8e03f..e3159ee99 100644 --- a/libtransmission/libtransmission-test.c +++ b/libtransmission/libtransmission-test.c @@ -191,78 +191,90 @@ rm_rf (const char * killme) #define SPEED_G_STR "GB/s" #define SPEED_T_STR "TB/s" -void -libtransmission_test_session_init_formatters (void) +tr_session * +libttest_session_init (tr_variant * settings) { - 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); -} + size_t len; + const char * str; + char * sandbox; + char * path; + tr_quark q; + static bool formatters_inited = false; + tr_session * session; + tr_variant local_settings; + + tr_variantInitDict (&local_settings, 10); + + if (settings == NULL) + settings = &local_settings; + + path = tr_getcwd (); + sandbox = tr_buildPath (path, "sandbox-XXXXXX", NULL); + tr_mkdtemp (sandbox); + tr_free (path); -void -libtransmission_test_session_init_sandbox (void) -{ - char * cwd; + if (!formatters_inited) + { + formatters_inited = true; + 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); + } - /* create a sandbox for the test session */ - cwd = tr_getcwd (); - sandbox = tr_buildPath (cwd, "sandbox-XXXXXX", NULL); - tr_mkdtemp (sandbox); - downloadDir = tr_buildPath (sandbox, "Downloads", NULL); - tr_mkdirp (downloadDir, 0700); - blocklistDir = tr_buildPath (sandbox, "blocklists", NULL); - tr_mkdirp (blocklistDir, 0700); + /* download dir */ + q = TR_KEY_download_dir; + if (tr_variantDictFindStr (settings, q, &str, &len)) + path = tr_strdup_printf ("%s/%*.*s", sandbox, (int)len, (int)len, str); + else + path = tr_buildPath (sandbox, "Downloads", NULL); + tr_mkdirp (path, 0700); + tr_variantDictAddStr (settings, q, path); + tr_free (path); + + /* incomplete dir */ + q = TR_KEY_incomplete_dir; + if (tr_variantDictFindStr (settings, q, &str, &len)) + path = tr_strdup_printf ("%s/%*.*s", sandbox, (int)len, (int)len, str); + else + path = tr_buildPath (sandbox, "Incomplete", NULL); + tr_variantDictAddStr (settings, q, path); + tr_free (path); - /* cleanup locals*/ - tr_free (cwd); -} + path = tr_buildPath (sandbox, "blocklists", NULL); + tr_mkdirp (path, 0700); + tr_free (path); -void -libtransmission_test_session_init_session (void) -{ - tr_variant dict; - - /* 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); - tr_variantDictAddInt (&dict, TR_KEY_message_level, verbose ? TR_LOG_DEBUG : TR_LOG_ERROR); - session = tr_sessionInit ("libtransmission-test", sandbox, !verbose, &dict); - - /* cleanup locals*/ - tr_variantFree (&dict); -} + q = TR_KEY_port_forwarding_enabled; + if (!tr_variantDictFind (settings, q)) + tr_variantDictAddBool (settings, q, false); -void -libtransmission_test_session_init (void) -{ - libtransmission_test_session_init_formatters (); - libtransmission_test_session_init_sandbox (); - libtransmission_test_session_init_session (); + q = TR_KEY_dht_enabled; + if (!tr_variantDictFind (settings, q)) + tr_variantDictAddBool (settings, q, false); + + q = TR_KEY_message_level; + if (!tr_variantDictFind (settings, q)) + tr_variantDictAddInt (settings, q, verbose ? TR_LOG_DEBUG : TR_LOG_ERROR); + + session = tr_sessionInit ("libtransmission-test", sandbox, !verbose, settings); + + tr_free (sandbox); + tr_variantFree (&local_settings); + return session; } void -libtransmission_test_session_close (void) +libttest_session_close (tr_session * session) { + char * path; + + path = tr_strdup (tr_sessionGetConfigDir (session)); tr_sessionClose (session); tr_logFreeQueue (tr_logGetQueue ()); session = NULL; - rm_rf (sandbox); - - tr_free (blocklistDir); - blocklistDir = NULL; - - tr_free (downloadDir); - downloadDir = NULL; - - tr_free (sandbox); - sandbox = NULL; + rm_rf (path); + tr_free (path); } /*** @@ -270,7 +282,7 @@ libtransmission_test_session_close (void) ***/ tr_torrent * -libtransmission_test_zero_torrent_init (void) +libttest_zero_torrent_init (tr_session * session) { int err; int metainfo_len; @@ -325,7 +337,7 @@ libtransmission_test_zero_torrent_init (void) } void -libtransmission_test_zero_torrent_populate (tr_torrent * tor, bool complete) +libttest_zero_torrent_populate (tr_torrent * tor, bool complete) { tr_file_index_t i; @@ -384,8 +396,8 @@ libttest_blockingTorrentVerify (tr_torrent * tor) { bool done = false; - assert (session != NULL); - assert (!tr_amInEventThread (session)); + assert (tor->session != NULL); + assert (!tr_amInEventThread (tor->session)); tr_torrentVerify (tor, onVerifyDone, &done); while (!done) diff --git a/libtransmission/libtransmission-test.h b/libtransmission/libtransmission-test.h index 11958e69f..c209d3993 100644 --- a/libtransmission/libtransmission-test.h +++ b/libtransmission/libtransmission-test.h @@ -66,20 +66,11 @@ int main (void) { \ return runTests (tests, 1); \ } -extern tr_session * session; -extern char * sandbox; -extern char * downloadDir; -extern char * blocklistDir; +tr_session * libttest_session_init (struct tr_variant * settings); +void libttest_session_close (tr_session * session); -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); - -void libtransmission_test_zero_torrent_populate (tr_torrent * tor, bool complete); -tr_torrent * libtransmission_test_zero_torrent_init (void); +void libttest_zero_torrent_populate (tr_torrent * tor, bool complete); +tr_torrent * libttest_zero_torrent_init (tr_session * session); void libttest_blockingTorrentVerify (tr_torrent * tor); diff --git a/libtransmission/move-test.c b/libtransmission/move-test.c index 6af14bf64..23b8c53bc 100644 --- a/libtransmission/move-test.c +++ b/libtransmission/move-test.c @@ -42,8 +42,9 @@ zeroes_completeness_func (tr_torrent * torrent UNUSED, tr_free (path); \ } while (0) -struct test_incomplete_dir_is_subdir_of_download_dir_data +struct test_incomplete_dir_data { + tr_session * session; tr_torrent * tor; tr_block_index_t block; tr_piece_index_t pieceIndex; @@ -53,35 +54,39 @@ struct test_incomplete_dir_is_subdir_of_download_dir_data }; static void -test_incomplete_dir_is_subdir_of_download_dir_threadfunc (void * vdata) +test_incomplete_dir_threadfunc (void * vdata) { - struct test_incomplete_dir_is_subdir_of_download_dir_data * data = vdata; - tr_cacheWriteBlock (session->cache, data->tor, 0, data->offset, data->tor->blockSize, data->buf); + struct test_incomplete_dir_data * data = vdata; + tr_cacheWriteBlock (data->session->cache, data->tor, 0, data->offset, data->tor->blockSize, data->buf); tr_torrentGotBlock (data->tor, data->block); data->done = true; } - static int -test_incomplete_dir_is_subdir_of_download_dir (void) +test_incomplete_dir_impl (const char * incomplete_dir, const char * download_dir) { size_t i; - char * incomplete_dir; + tr_session * session; tr_torrent * tor; tr_completeness completeness; const tr_completeness completeness_unset = -1; const time_t deadline = time(NULL) + 5; + tr_variant settings; /* init the session */ - libtransmission_test_session_init (); - incomplete_dir = tr_buildPath (downloadDir, "incomplete", NULL); - tr_sessionSetIncompleteDir (session, incomplete_dir); - tr_sessionSetIncompleteDirEnabled (session, true); + tr_variantInitDict (&settings, 3); + tr_variantDictAddStr (&settings, TR_KEY_download_dir, download_dir); + tr_variantDictAddStr (&settings, TR_KEY_incomplete_dir, incomplete_dir); + tr_variantDictAddBool (&settings, TR_KEY_incomplete_dir_enabled, true); + session = libttest_session_init (&settings); + tr_variantFree (&settings); + download_dir = tr_sessionGetDownloadDir (session); + incomplete_dir = tr_sessionGetIncompleteDir (session); /* init an incomplete torrent. the test zero_torrent will be missing its first piece */ - tor = libtransmission_test_zero_torrent_init (); - libtransmission_test_zero_torrent_populate (tor, false); + tor = libttest_zero_torrent_init (session); + libttest_zero_torrent_populate (tor, false); check (tr_torrentStat(tor)->leftUntilDone == tor->info.pieceSize); check_file_location (tor, 0, tr_strdup_printf("%s/%s.part", incomplete_dir, tor->info.files[0].name)); check_file_location (tor, 1, tr_buildPath(incomplete_dir, tor->info.files[1].name, NULL)); @@ -94,8 +99,9 @@ test_incomplete_dir_is_subdir_of_download_dir (void) { tr_block_index_t first, last; char * zero_block = tr_new0 (char, tor->blockSize); - struct test_incomplete_dir_is_subdir_of_download_dir_data data; + struct test_incomplete_dir_data data; + data.session = session; data.tor = tor; data.pieceIndex = 0; data.buf = evbuffer_new (); @@ -107,7 +113,7 @@ test_incomplete_dir_is_subdir_of_download_dir (void) data.block = i; data.done = false; data.offset = data.block * tor->blockSize; - tr_runInEventThread (session, test_incomplete_dir_is_subdir_of_download_dir_threadfunc, &data); + tr_runInEventThread (session, test_incomplete_dir_threadfunc, &data); do { tr_wait_msec(50); } while (!data.done); } @@ -123,15 +129,33 @@ test_incomplete_dir_is_subdir_of_download_dir (void) check_int_eq (TR_SEED, completeness); for (i=0; iinfo.fileCount; ++i) - check_file_location (tor, i, tr_buildPath (downloadDir, tor->info.files[i].name, NULL)); + check_file_location (tor, i, tr_buildPath (download_dir, tor->info.files[i].name, NULL)); /* cleanup */ tr_torrentRemove (tor, true, remove); - libtransmission_test_session_close (); - tr_free (incomplete_dir); + libttest_session_close (session); return 0; } +static int +test_incomplete_dir (void) +{ + int rv; + + /* test what happens when incompleteDir is a subdir of downloadDir*/ + if ((rv = test_incomplete_dir_impl ("Downloads/Incomplete", "Downloads"))) + return rv; + + /* test what happens when downloadDir is a subdir of incompleteDir */ + if ((rv = test_incomplete_dir_impl ("Downloads", "Downloads/Complete"))) + return rv; + + /* test what happens when downloadDir and incompleteDir are siblings */ + if ((rv = test_incomplete_dir_impl ("Incomplete", "Downloads"))) + return rv; + + return 0; +} /*** **** @@ -140,7 +164,7 @@ test_incomplete_dir_is_subdir_of_download_dir (void) int main (void) { - const testFunc tests[] = { test_incomplete_dir_is_subdir_of_download_dir }; + const testFunc tests[] = { test_incomplete_dir }; return runTests (tests, NUM_TESTS (tests)); } diff --git a/libtransmission/rename-test.c b/libtransmission/rename-test.c index 132cfda1e..6c3ba5ed1 100644 --- a/libtransmission/rename-test.c +++ b/libtransmission/rename-test.c @@ -20,6 +20,8 @@ **** ***/ +static tr_session * session = NULL; + #define check_have_none(tor, totalSize) \ do { \ const tr_stat * st = tr_torrentStat(tor); \ @@ -126,7 +128,6 @@ create_torrent_from_base64_metainfo (tr_ctor * ctor, const char * metainfo_base6 metainfo = tr_base64_decode (metainfo_base64, -1, &metainfo_len); assert (metainfo != NULL); assert (metainfo_len > 0); - assert (session != NULL); tr_ctorSetMetainfo (ctor, (uint8_t*)metainfo, metainfo_len); tr_ctorSetPaused (ctor, TR_FORCE, true); @@ -485,7 +486,7 @@ test_partial_file (void) **** create our test torrent with an incomplete .part file ***/ - tor = libtransmission_test_zero_torrent_init (); + tor = libttest_zero_torrent_init (session); check_int_eq (totalSize, tor->info.totalSize); check_int_eq (pieceSize, tor->info.pieceSize); check_int_eq (pieceCount, tor->info.pieceCount); @@ -493,7 +494,7 @@ test_partial_file (void) check_streq ("files-filled-with-zeroes/4096", tor->info.files[1].name); check_streq ("files-filled-with-zeroes/512", tor->info.files[2].name); - libtransmission_test_zero_torrent_populate (tor, false); + libttest_zero_torrent_populate (tor, false); fst = tr_torrentFiles (tor, NULL); check_int_eq (length[0] - pieceSize, fst[0].bytesCompleted); check_int_eq (length[1], fst[1].bytesCompleted); @@ -543,9 +544,9 @@ main (void) test_multifile_torrent, test_partial_file }; - libtransmission_test_session_init (); + session = libttest_session_init (NULL); ret = runTests (tests, NUM_TESTS (tests)); - libtransmission_test_session_close (); + libttest_session_close (session); return ret; } diff --git a/libtransmission/rpc-test.c b/libtransmission/rpc-test.c index f5e193e13..fc4681e9f 100644 --- a/libtransmission/rpc-test.c +++ b/libtransmission/rpc-test.c @@ -76,10 +76,14 @@ rpc_response_func (tr_session * session UNUSED, static int test_session_get_and_set (void) { + tr_session * session; const char * json; tr_variant response; tr_variant * args; - tr_torrent * tor = libtransmission_test_zero_torrent_init (); + tr_torrent * tor; + + session = libttest_session_init (NULL); + tor= libttest_zero_torrent_init (session); check (tor != NULL); json = "{\"method\":\"session-get\"}"; @@ -141,6 +145,7 @@ test_session_get_and_set (void) /* cleanup */ tr_torrentRemove (tor, false, NULL); + libttest_session_close (session); return 0; } @@ -151,13 +156,8 @@ test_session_get_and_set (void) int main (void) { - int ret; const testFunc tests[] = { test_list, test_session_get_and_set }; - libtransmission_test_session_init (); - ret = runTests (tests, NUM_TESTS (tests)); - libtransmission_test_session_close (); - - return ret; + return runTests (tests, NUM_TESTS (tests)); }