From: Jordan Lee Date: Sun, 20 Jan 2013 04:41:38 +0000 (+0000) Subject: (trunk) move sandboxed session creation/teardown into libtransmission-test.[ch] so... X-Git-Tag: 2.80~258 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ac28b6df6ee354066bbd5425c986a6378c7d082f;p=transmission (trunk) move sandboxed session creation/teardown into libtransmission-test.[ch] so that it can be reused as a fixture by future tests --- diff --git a/libtransmission/libtransmission-test.c b/libtransmission/libtransmission-test.c index 0e8855f2c..1c892e018 100644 --- a/libtransmission/libtransmission-test.c +++ b/libtransmission/libtransmission-test.c @@ -95,3 +95,108 @@ runTests (const testFunc * const tests, int numTests) return 0; /* All tests passed */ } + +/*** +**** +***/ + +#include /* stat(), opendir() */ +#include /* stat() */ +#include /* opendir() */ +#include /* getcwd() */ + +#include +#include /* strcmp() */ + +#include "variant.h" + +tr_session * session = NULL; +static char * sandbox = NULL; + +static char* +tr_getcwd (void) +{ + char * result; + char buf[2048]; + +#ifdef WIN32 + result = _getcwd (buf, sizeof (buf)); +#else + result = getcwd (buf, sizeof (buf)); +#endif + + if (result == NULL) + { + fprintf (stderr, "getcwd error: \"%s\"", tr_strerror (errno)); + *buf = '\0'; + } + + return tr_strdup (buf); +} + +static void +rm_rf (const char * killme) +{ + struct stat sb; + + if (!stat (killme, &sb)) + { + DIR * odir; + + if (S_ISDIR (sb.st_mode) && ((odir = opendir (killme)))) + { + struct dirent *d; + for (d = readdir(odir); d != NULL; d=readdir(odir)) + { + if (d->d_name && strcmp(d->d_name,".") && strcmp(d->d_name,"..")) + { + char * tmp = tr_buildPath (killme, d->d_name, NULL); + rm_rf (tmp); + tr_free (tmp); + } + } + closedir (odir); + } + + if (verbose) + fprintf (stderr, "cleanup: removing %s\n", killme); + + remove (killme); + } +} + +void +libtransmission_test_session_init (void) +{ + char * cwd; + char * downloadDir; + tr_variant dict; + + /* 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); + + /* create a test session */ + tr_variantInitDict (&dict, 3); + 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); + + /* cleanup locals*/ + tr_variantFree (&dict); + tr_free (downloadDir); + tr_free (cwd); +} + +void +libtransmission_test_session_close (void) +{ + tr_sessionClose (session); + tr_freeMessageList (tr_getQueuedMessages ()); + rm_rf (sandbox); + tr_free (sandbox); +} diff --git a/libtransmission/libtransmission-test.h b/libtransmission/libtransmission-test.h index fbc413c8f..ade4d2d70 100644 --- a/libtransmission/libtransmission-test.h +++ b/libtransmission/libtransmission-test.h @@ -65,4 +65,9 @@ int main (void) { \ return runTests (tests, 1); \ } +extern tr_session * session; +void libtransmission_test_session_init (void); +void libtransmission_test_session_close (void); + + #endif /* !LIBTRANSMISSION_TEST_H */ diff --git a/libtransmission/rename-test.c b/libtransmission/rename-test.c index de811fa5e..babc4b897 100644 --- a/libtransmission/rename-test.c +++ b/libtransmission/rename-test.c @@ -4,10 +4,7 @@ #include /* strcmp() */ #include -#include /* stat(), opendir() */ -#include /* stat() */ -#include /* opendir() */ -#include /* getcwd() */ +#include /* sync() */ #include "transmission.h" #include "resume.h" @@ -17,29 +14,6 @@ #include "libtransmission-test.h" -static tr_session * session = NULL; - -static char* -tr_getcwd (void) -{ - char * result; - char buf[2048]; - -#ifdef WIN32 - result = _getcwd (buf, sizeof (buf)); -#else - result = getcwd (buf, sizeof (buf)); -#endif - - if (result == NULL) - { - fprintf (stderr, "getcwd error: \"%s\"", tr_strerror (errno)); - *buf = '\0'; - } - - return tr_strdup (buf); -} - /*** **** ***/ @@ -449,72 +423,16 @@ test_multifile_torrent (void) **** ***/ -static void -rm_rf (const char * killme) -{ - struct stat sb; - - if (!stat (killme, &sb)) - { - DIR * odir; - - if (S_ISDIR (sb.st_mode) && ((odir = opendir (killme)))) - { - struct dirent *d; - for (d = readdir(odir); d != NULL; d=readdir(odir)) - { - if (d->d_name && strcmp(d->d_name,".") && strcmp(d->d_name,"..")) - { - char * tmp = tr_buildPath (killme, d->d_name, NULL); - rm_rf (tmp); - tr_free (tmp); - } - } - closedir (odir); - } - - if (verbose) - fprintf (stderr, "cleanup: removing %s\n", killme); - - remove (killme); - } -} - int main (void) { int ret; - char * cwd; - char * sandbox; - char * downloadDir; - tr_variant dict; const testFunc tests[] = { test_single_filename_torrent, test_multifile_torrent }; - /* 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); - - /* create a test session */ - tr_variantInitDict (&dict, 3); - 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); - - /* run the tests */ + libtransmission_test_session_init (); ret = runTests (tests, NUM_TESTS (tests)); + libtransmission_test_session_close (); - /* cleanup */ - tr_sessionClose (session); - tr_freeMessageList (tr_getQueuedMessages ()); - tr_variantFree (&dict); - rm_rf (sandbox); - tr_free (downloadDir); - tr_free (sandbox); - tr_free (cwd); return ret; }