Changeset 13812


Ignore:
Timestamp:
Jan 20, 2013, 4:41:38 AM (8 years ago)
Author:
jordan
Message:

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

Location:
trunk/libtransmission
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/libtransmission-test.c

    r13667 r13812  
    9696  return 0; /* All tests passed */
    9797}
     98
     99/***
     100****
     101***/
     102
     103#include <sys/types.h> /* stat(), opendir() */
     104#include <sys/stat.h> /* stat() */
     105#include <dirent.h> /* opendir() */
     106#include <unistd.h> /* getcwd() */
     107
     108#include <errno.h>
     109#include <string.h> /* strcmp() */
     110
     111#include "variant.h"
     112
     113tr_session * session = NULL;
     114static char * sandbox = NULL;
     115
     116static char*
     117tr_getcwd (void)
     118{
     119  char * result;
     120  char buf[2048];
     121
     122#ifdef WIN32
     123  result = _getcwd (buf, sizeof (buf));
     124#else
     125  result = getcwd (buf, sizeof (buf));
     126#endif
     127
     128  if (result == NULL)
     129    {
     130      fprintf (stderr, "getcwd error: \"%s\"", tr_strerror (errno));
     131      *buf = '\0';
     132    }
     133
     134  return tr_strdup (buf);
     135}
     136
     137static void
     138rm_rf (const char * killme)
     139{
     140  struct stat sb;
     141
     142  if (!stat (killme, &sb))
     143    {
     144      DIR * odir;
     145
     146      if (S_ISDIR (sb.st_mode) && ((odir = opendir (killme))))
     147        {
     148          struct dirent *d;
     149          for (d = readdir(odir); d != NULL; d=readdir(odir))
     150            {
     151              if (d->d_name && strcmp(d->d_name,".") && strcmp(d->d_name,".."))
     152                {
     153                  char * tmp = tr_buildPath (killme, d->d_name, NULL);
     154                  rm_rf (tmp);
     155                  tr_free (tmp);
     156                }
     157            }
     158          closedir (odir);
     159        }
     160
     161      if (verbose)
     162        fprintf (stderr, "cleanup: removing %s\n", killme);
     163
     164      remove (killme);
     165    }
     166}
     167
     168void
     169libtransmission_test_session_init (void)
     170{
     171  char * cwd;
     172  char * downloadDir;
     173  tr_variant dict;
     174
     175  /* create a sandbox for the test session */
     176  cwd = tr_getcwd ();
     177  sandbox = tr_buildPath (cwd, "sandbox-XXXXXX", NULL);
     178  tr_mkdtemp (sandbox);
     179  downloadDir = tr_buildPath (sandbox, "Downloads", NULL);
     180  tr_mkdirp (downloadDir, 0700);
     181
     182  /* create a test session */
     183  tr_variantInitDict    (&dict, 3);
     184  tr_variantDictAddStr  (&dict, TR_KEY_download_dir, downloadDir);
     185  tr_variantDictAddBool (&dict, TR_KEY_port_forwarding_enabled, false);
     186  tr_variantDictAddBool (&dict, TR_KEY_dht_enabled, false);
     187  session = tr_sessionInit ("rename-test", sandbox, true, &dict);
     188
     189  /* cleanup locals*/
     190  tr_variantFree (&dict);
     191  tr_free (downloadDir);
     192  tr_free (cwd);
     193}
     194
     195void
     196libtransmission_test_session_close (void)
     197{
     198  tr_sessionClose (session);
     199  tr_freeMessageList (tr_getQueuedMessages ());
     200  rm_rf (sandbox);
     201  tr_free (sandbox);
     202}
  • trunk/libtransmission/libtransmission-test.h

    r13667 r13812  
    6666}
    6767
     68extern tr_session * session;
     69void libtransmission_test_session_init (void);
     70void libtransmission_test_session_close (void);
     71
     72
    6873#endif /* !LIBTRANSMISSION_TEST_H */
  • trunk/libtransmission/rename-test.c

    r13807 r13812  
    55#include <stdio.h>
    66
    7 #include <sys/types.h> /* stat(), opendir() */
    8 #include <sys/stat.h> /* stat() */
    9 #include <dirent.h> /* opendir() */
    10 #include <unistd.h> /* getcwd() */
     7#include <unistd.h> /* sync() */
    118
    129#include "transmission.h"
     
    1714
    1815#include "libtransmission-test.h"
    19 
    20 static tr_session * session = NULL;
    21 
    22 static char*
    23 tr_getcwd (void)
    24 {
    25   char * result;
    26   char buf[2048];
    27 
    28 #ifdef WIN32
    29   result = _getcwd (buf, sizeof (buf));
    30 #else
    31   result = getcwd (buf, sizeof (buf));
    32 #endif
    33 
    34   if (result == NULL)
    35     {
    36       fprintf (stderr, "getcwd error: \"%s\"", tr_strerror (errno));
    37       *buf = '\0';
    38     }
    39 
    40   return tr_strdup (buf);
    41 }
    4216
    4317/***
     
    450424***/
    451425
    452 static void
    453 rm_rf (const char * killme)
    454 {
    455   struct stat sb;
    456 
    457   if (!stat (killme, &sb))
    458     {
    459       DIR * odir;
    460 
    461       if (S_ISDIR (sb.st_mode) && ((odir = opendir (killme))))
    462         {
    463           struct dirent *d;
    464           for (d = readdir(odir); d != NULL; d=readdir(odir))
    465             {
    466               if (d->d_name && strcmp(d->d_name,".") && strcmp(d->d_name,".."))
    467                 {
    468                   char * tmp = tr_buildPath (killme, d->d_name, NULL);
    469                   rm_rf (tmp);
    470                   tr_free (tmp);
    471                 }
    472             }
    473           closedir (odir);
    474         }
    475 
    476       if (verbose)
    477         fprintf (stderr, "cleanup: removing %s\n", killme);
    478 
    479       remove (killme);
    480     }
    481 }
    482 
    483426int
    484427main (void)
    485428{
    486429  int ret;
    487   char * cwd;
    488   char * sandbox;
    489   char * downloadDir;
    490   tr_variant dict;
    491430  const testFunc tests[] = { test_single_filename_torrent,
    492431                             test_multifile_torrent };
    493432
    494   /* create a sandbox for the test session */
    495   cwd = tr_getcwd ();
    496   sandbox = tr_buildPath (cwd, "sandbox-XXXXXX", NULL);
    497   tr_mkdtemp (sandbox);
    498   downloadDir = tr_buildPath (sandbox, "Downloads", NULL);
    499   tr_mkdirp (downloadDir, 0700);
    500 
    501   /* create a test session */
    502   tr_variantInitDict    (&dict, 3);
    503   tr_variantDictAddStr  (&dict, TR_KEY_download_dir, downloadDir);
    504   tr_variantDictAddBool (&dict, TR_KEY_port_forwarding_enabled, false);
    505   tr_variantDictAddBool (&dict, TR_KEY_dht_enabled, false);
    506   session = tr_sessionInit ("rename-test", sandbox, true, &dict);
    507 
    508   /* run the tests */
     433  libtransmission_test_session_init ();
    509434  ret = runTests (tests, NUM_TESTS (tests));
    510 
    511   /* cleanup */
    512   tr_sessionClose (session);
    513   tr_freeMessageList (tr_getQueuedMessages ());
    514   tr_variantFree (&dict);
    515   rm_rf (sandbox);
    516   tr_free (downloadDir);
    517   tr_free (sandbox);
    518   tr_free (cwd);
     435  libtransmission_test_session_close ();
     436
    519437  return ret;
    520438}
Note: See TracChangeset for help on using the changeset viewer.