1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-03-03 18:25:35 +00:00

(trunk) move sandboxed session creation/teardown into libtransmission-test.[ch] so that it can be reused as a fixture by future tests

This commit is contained in:
Jordan Lee 2013-01-20 04:41:38 +00:00
parent 5093f99652
commit ac28b6df6e
3 changed files with 113 additions and 85 deletions

View file

@ -95,3 +95,108 @@ runTests (const testFunc * const tests, int numTests)
return 0; /* All tests passed */ return 0; /* All tests passed */
} }
/***
****
***/
#include <sys/types.h> /* stat(), opendir() */
#include <sys/stat.h> /* stat() */
#include <dirent.h> /* opendir() */
#include <unistd.h> /* getcwd() */
#include <errno.h>
#include <string.h> /* 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);
}

View file

@ -65,4 +65,9 @@ int main (void) { \
return runTests (tests, 1); \ return runTests (tests, 1); \
} }
extern tr_session * session;
void libtransmission_test_session_init (void);
void libtransmission_test_session_close (void);
#endif /* !LIBTRANSMISSION_TEST_H */ #endif /* !LIBTRANSMISSION_TEST_H */

View file

@ -4,10 +4,7 @@
#include <string.h> /* strcmp() */ #include <string.h> /* strcmp() */
#include <stdio.h> #include <stdio.h>
#include <sys/types.h> /* stat(), opendir() */ #include <unistd.h> /* sync() */
#include <sys/stat.h> /* stat() */
#include <dirent.h> /* opendir() */
#include <unistd.h> /* getcwd() */
#include "transmission.h" #include "transmission.h"
#include "resume.h" #include "resume.h"
@ -17,29 +14,6 @@
#include "libtransmission-test.h" #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 int
main (void) main (void)
{ {
int ret; int ret;
char * cwd;
char * sandbox;
char * downloadDir;
tr_variant dict;
const testFunc tests[] = { test_single_filename_torrent, const testFunc tests[] = { test_single_filename_torrent,
test_multifile_torrent }; test_multifile_torrent };
/* create a sandbox for the test session */ libtransmission_test_session_init ();
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 */
ret = runTests (tests, NUM_TESTS (tests)); 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; return ret;
} }