"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);
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;
}
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;
}
const testFunc tests[] = { test_parsing,
test_updating };
- libtransmission_test_session_init_formatters ();
-
return runTests (tests, NUM_TESTS (tests));
}
#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);
}
/***
***/
tr_torrent *
-libtransmission_test_zero_torrent_init (void)
+libttest_zero_torrent_init (tr_session * session)
{
int err;
int metainfo_len;
}
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;
{
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)
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);
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;
};
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));
{
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 ();
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);
}
check_int_eq (TR_SEED, completeness);
for (i=0; i<tor->info.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;
+}
/***
****
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));
}
****
***/
+static tr_session * session = NULL;
+
#define check_have_none(tor, totalSize) \
do { \
const tr_stat * st = tr_torrentStat(tor); \
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);
**** 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);
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);
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;
}
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\"}";
/* cleanup */
tr_torrentRemove (tor, false, NULL);
+ libttest_session_close (session);
return 0;
}
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));
}