Changeset 13916


Ignore:
Timestamp:
Feb 1, 2013, 5:57:47 AM (8 years ago)
Author:
jordan
Message:

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)

Location:
trunk/libtransmission
Files:
6 edited

Legend:

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

    r13825 r13916  
    2424  "Evilcorp:216.88.88.0-216.88.88.255\n";
    2525
    26 static char *
    27 create_blocklist_text_file (const char * basename, const char * contents)
     26static void
     27create_text_file (const char * path, const char * contents)
    2828{
    2929  FILE * fp;
    30   char * path;
     30  char * dir;
    3131
    32   assert (blocklistDir != NULL);
     32  dir = tr_dirname (path);
     33  tr_mkdirp (dir, 0700);
     34  tr_free (dir);
    3335
    34   path = tr_buildPath (blocklistDir, basename, NULL);
    3536  remove (path);
    3637  fp = fopen (path, "w+");
    3738  fprintf (fp, "%s", contents);
    3839  fclose (fp);
     40
    3941  sync ();
    40   return path;
    4142}
    4243
    4344static bool
    44 address_is_blocked (const char * address_str)
     45address_is_blocked (tr_session * session, const char * address_str)
    4546{
    4647  struct tr_address addr;
     
    5253test_parsing (void)
    5354{
    54   char * text_file;
     55  char * path;
     56  tr_session * session;
    5557
    56   libtransmission_test_session_init_sandbox ();
    57   text_file = create_blocklist_text_file ("level1", contents1);
    58   libtransmission_test_session_init_session ();
     58  /* init the session */
     59  session = libttest_session_init (NULL);
     60  check (!tr_blocklistExists (session));
     61  check_int_eq (0, tr_blocklistGetRuleCount (session));
    5962
     63  /* init the blocklist */
     64  path = tr_buildPath (tr_sessionGetConfigDir(session), "blocklists", "level1", NULL);
     65  create_text_file (path, contents1);
     66  tr_free (path);
     67  tr_sessionReloadBlocklists (session);
     68  check (tr_blocklistExists (session));
     69  check_int_eq (4, tr_blocklistGetRuleCount (session));
     70
     71  /* enable the blocklist */
    6072  check (!tr_blocklistIsEnabled (session));
    6173  tr_blocklistSetEnabled (session, true);
    6274  check (tr_blocklistIsEnabled (session));
    6375
    64   check (tr_blocklistExists (session));
    65   check_int_eq (4, tr_blocklistGetRuleCount (session));
     76  /* test blocked addresses */
     77  check (!address_is_blocked (session, "216.16.1.143"));
     78  check ( address_is_blocked (session, "216.16.1.144"));
     79  check ( address_is_blocked (session, "216.16.1.145"));
     80  check ( address_is_blocked (session, "216.16.1.146"));
     81  check ( address_is_blocked (session, "216.16.1.147"));
     82  check ( address_is_blocked (session, "216.16.1.148"));
     83  check ( address_is_blocked (session, "216.16.1.149"));
     84  check ( address_is_blocked (session, "216.16.1.150"));
     85  check ( address_is_blocked (session, "216.16.1.151"));
     86  check (!address_is_blocked (session, "216.16.1.152"));
     87  check (!address_is_blocked (session, "216.16.1.153"));
     88  check (!address_is_blocked (session, "217.0.0.1"));
     89  check (!address_is_blocked (session, "255.0.0.1"));
    6690
    67   check (!address_is_blocked ("216.16.1.143"));
    68   check ( address_is_blocked ("216.16.1.144"));
    69   check ( address_is_blocked ("216.16.1.145"));
    70   check ( address_is_blocked ("216.16.1.146"));
    71   check ( address_is_blocked ("216.16.1.147"));
    72   check ( address_is_blocked ("216.16.1.148"));
    73   check ( address_is_blocked ("216.16.1.149"));
    74   check ( address_is_blocked ("216.16.1.150"));
    75   check ( address_is_blocked ("216.16.1.151"));
    76   check (!address_is_blocked ("216.16.1.152"));
    77   check (!address_is_blocked ("216.16.1.153"));
    78   check (!address_is_blocked ("217.0.0.1"));
    79   check (!address_is_blocked ("255.0.0.1"));
    80 
    81   libtransmission_test_session_close ();
    82   tr_free (text_file);
     91  /* cleanup */
     92  libttest_session_close (session);
    8393  return 0;
    8494}
     
    91101test_updating (void)
    92102{
    93   char * text_file;
     103  char * path;
     104  tr_session * session;
    94105
    95   libtransmission_test_session_init_sandbox ();
    96   text_file = create_blocklist_text_file ("level1", contents1);
    97   libtransmission_test_session_init_session ();
     106  /* init the session */
     107  session = libttest_session_init (NULL);
     108  path = tr_buildPath (tr_sessionGetConfigDir(session), "blocklists", "level1", NULL);
     109
     110  /* no blocklist to start with... */
     111  check_int_eq (0, tr_blocklistGetRuleCount (session));
     112
     113  /* test that updated source files will get loaded */
     114  create_text_file (path, contents1);
     115  tr_sessionReloadBlocklists (session);
    98116  check_int_eq (4, tr_blocklistGetRuleCount (session));
    99117
    100118  /* test that updated source files will get loaded */
    101   tr_free (text_file);
    102   text_file = create_blocklist_text_file ("level1", contents2);
     119  create_text_file (path, contents2);
    103120  tr_sessionReloadBlocklists (session);
    104121  check_int_eq (5, tr_blocklistGetRuleCount (session));
    105122
    106123  /* test that updated source files will get loaded */
    107   tr_free (text_file);
    108   text_file = create_blocklist_text_file ("level1", contents1);
     124  create_text_file (path, contents1);
    109125  tr_sessionReloadBlocklists (session);
    110126  check_int_eq (4, tr_blocklistGetRuleCount (session));
    111127
    112128  /* ensure that new files, if bad, get skipped */
    113   tr_free (text_file);
    114   text_file = create_blocklist_text_file ("level1", "# nothing useful\n");
     129  create_text_file (path,  "# nothing useful\n");
    115130  tr_sessionReloadBlocklists (session);
    116131  check_int_eq (4, tr_blocklistGetRuleCount (session));
    117132
    118   libtransmission_test_session_close ();
    119   tr_free (text_file);
     133  /* cleanup */
     134  libttest_session_close (session);
     135  tr_free (path);
    120136  return 0;
    121137}
     
    131147                             test_updating };
    132148
    133   libtransmission_test_session_init_formatters ();
    134 
    135149  return runTests (tests, NUM_TESTS (tests));
    136150}
  • trunk/libtransmission/libtransmission-test.c

    r13913 r13916  
    192192#define SPEED_T_STR "TB/s"
    193193
     194tr_session *
     195libttest_session_init (tr_variant * settings)
     196{
     197  size_t len;
     198  const char * str;
     199  char * sandbox;
     200  char * path;
     201  tr_quark q;
     202  static bool formatters_inited = false;
     203  tr_session * session;
     204  tr_variant local_settings;
     205
     206  tr_variantInitDict (&local_settings, 10);
     207
     208  if (settings == NULL)
     209    settings = &local_settings;
     210
     211  path = tr_getcwd ();
     212  sandbox = tr_buildPath (path, "sandbox-XXXXXX", NULL);
     213  tr_mkdtemp (sandbox);
     214  tr_free (path);
     215
     216  if (!formatters_inited)
     217    {
     218      formatters_inited = true;
     219      tr_formatter_mem_init (MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR);
     220      tr_formatter_size_init (DISK_K,DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR);
     221      tr_formatter_speed_init (SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR);
     222    }
     223
     224  /* download dir */
     225  q = TR_KEY_download_dir;
     226  if (tr_variantDictFindStr (settings, q, &str, &len))
     227    path = tr_strdup_printf ("%s/%*.*s", sandbox, (int)len, (int)len, str);
     228  else
     229    path = tr_buildPath (sandbox, "Downloads", NULL);
     230  tr_mkdirp (path, 0700);
     231  tr_variantDictAddStr (settings, q, path);
     232  tr_free (path);
     233
     234  /* incomplete dir */
     235  q = TR_KEY_incomplete_dir;
     236  if (tr_variantDictFindStr (settings, q, &str, &len))
     237    path = tr_strdup_printf ("%s/%*.*s", sandbox, (int)len, (int)len, str);
     238  else
     239    path = tr_buildPath (sandbox, "Incomplete", NULL);
     240  tr_variantDictAddStr (settings, q, path);
     241  tr_free (path);
     242
     243  path = tr_buildPath (sandbox, "blocklists", NULL);
     244  tr_mkdirp (path, 0700);
     245  tr_free (path);
     246
     247  q = TR_KEY_port_forwarding_enabled;
     248  if (!tr_variantDictFind (settings, q))
     249    tr_variantDictAddBool (settings, q, false);
     250
     251  q = TR_KEY_dht_enabled;
     252  if (!tr_variantDictFind (settings, q))
     253    tr_variantDictAddBool (settings, q, false);
     254
     255  q = TR_KEY_message_level;
     256  if (!tr_variantDictFind (settings, q))
     257    tr_variantDictAddInt (settings, q, verbose ? TR_LOG_DEBUG : TR_LOG_ERROR);
     258
     259  session = tr_sessionInit ("libtransmission-test", sandbox, !verbose, settings);
     260
     261  tr_free (sandbox);
     262  tr_variantFree (&local_settings);
     263  return session;
     264}
     265
    194266void
    195 libtransmission_test_session_init_formatters (void)
    196 {
    197   tr_formatter_mem_init (MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR);
    198   tr_formatter_size_init (DISK_K,DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR);
    199   tr_formatter_speed_init (SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR);
    200 }
    201 
    202 void
    203 libtransmission_test_session_init_sandbox (void)
    204 {
    205   char * cwd;
    206 
    207   /* create a sandbox for the test session */
    208   cwd = tr_getcwd ();
    209   sandbox = tr_buildPath (cwd, "sandbox-XXXXXX", NULL);
    210   tr_mkdtemp (sandbox);
    211   downloadDir = tr_buildPath (sandbox, "Downloads", NULL);
    212   tr_mkdirp (downloadDir, 0700);
    213   blocklistDir = tr_buildPath (sandbox, "blocklists", NULL);
    214   tr_mkdirp (blocklistDir, 0700);
    215 
    216   /* cleanup locals*/
    217   tr_free (cwd);
    218 }
    219 
    220 void
    221 libtransmission_test_session_init_session (void)
    222 {
    223   tr_variant dict;
    224 
    225   /* libtransmission_test_session_init_sandbox() has to be called first */
    226   assert (sandbox != NULL);
    227   assert (session == NULL);
    228 
    229   /* init the session */
    230   tr_variantInitDict    (&dict, 4);
    231   tr_variantDictAddStr  (&dict, TR_KEY_download_dir, downloadDir);
    232   tr_variantDictAddBool (&dict, TR_KEY_port_forwarding_enabled, false);
    233   tr_variantDictAddBool (&dict, TR_KEY_dht_enabled, false);
    234   tr_variantDictAddInt  (&dict, TR_KEY_message_level, verbose ? TR_LOG_DEBUG : TR_LOG_ERROR);
    235   session = tr_sessionInit ("libtransmission-test", sandbox, !verbose, &dict);
    236 
    237   /* cleanup locals*/
    238   tr_variantFree (&dict);
    239 }
    240 
    241 void
    242 libtransmission_test_session_init (void)
    243 {
    244   libtransmission_test_session_init_formatters ();
    245   libtransmission_test_session_init_sandbox ();
    246   libtransmission_test_session_init_session ();
    247 }
    248 
    249 void
    250 libtransmission_test_session_close (void)
    251 {
     267libttest_session_close (tr_session * session)
     268{
     269  char * path;
     270
     271  path = tr_strdup (tr_sessionGetConfigDir (session));
    252272  tr_sessionClose (session);
    253273  tr_logFreeQueue (tr_logGetQueue ());
    254274  session = NULL;
    255275
    256   rm_rf (sandbox);
    257 
    258   tr_free (blocklistDir);
    259   blocklistDir = NULL;
    260 
    261   tr_free (downloadDir);
    262   downloadDir = NULL;
    263 
    264   tr_free (sandbox);
    265   sandbox = NULL;
     276  rm_rf (path);
     277  tr_free (path);
    266278}
    267279
     
    271283
    272284tr_torrent *
    273 libtransmission_test_zero_torrent_init (void)
     285libttest_zero_torrent_init (tr_session * session)
    274286{
    275287  int err;
     
    326338
    327339void
    328 libtransmission_test_zero_torrent_populate (tr_torrent * tor, bool complete)
     340libttest_zero_torrent_populate (tr_torrent * tor, bool complete)
    329341{
    330342  tr_file_index_t i;
     
    385397  bool done = false;
    386398
    387   assert (session != NULL);
    388   assert (!tr_amInEventThread (session));
     399  assert (tor->session != NULL);
     400  assert (!tr_amInEventThread (tor->session));
    389401
    390402  tr_torrentVerify (tor, onVerifyDone, &done);
  • trunk/libtransmission/libtransmission-test.h

    r13913 r13916  
    6767}
    6868
    69 extern tr_session * session;
    70 extern char * sandbox;
    71 extern char * downloadDir;
    72 extern char * blocklistDir;
     69tr_session * libttest_session_init (struct tr_variant * settings);
     70void         libttest_session_close (tr_session * session);
    7371
    74 void libtransmission_test_session_init_formatters (void);
    75 void libtransmission_test_session_init_sandbox (void);
    76 void libtransmission_test_session_init_session (void);
    77 void libtransmission_test_session_init (void); /* utility; calls the other 3 */
    78 
    79 void libtransmission_test_session_close (void);
    80 
    81 void         libtransmission_test_zero_torrent_populate (tr_torrent * tor, bool complete);
    82 tr_torrent * libtransmission_test_zero_torrent_init (void);
     72void         libttest_zero_torrent_populate (tr_torrent * tor, bool complete);
     73tr_torrent * libttest_zero_torrent_init (tr_session * session);
    8374
    8475void         libttest_blockingTorrentVerify (tr_torrent * tor);
  • trunk/libtransmission/move-test.c

    r13913 r13916  
    4343  } while (0)
    4444
    45 struct test_incomplete_dir_is_subdir_of_download_dir_data
     45struct test_incomplete_dir_data
    4646{
     47  tr_session * session;
    4748  tr_torrent * tor;
    4849  tr_block_index_t block;
     
    5455
    5556static void
    56 test_incomplete_dir_is_subdir_of_download_dir_threadfunc (void * vdata)
     57test_incomplete_dir_threadfunc (void * vdata)
    5758{
    58   struct test_incomplete_dir_is_subdir_of_download_dir_data * data = vdata;
    59   tr_cacheWriteBlock (session->cache, data->tor, 0, data->offset, data->tor->blockSize, data->buf);
     59  struct test_incomplete_dir_data * data = vdata;
     60  tr_cacheWriteBlock (data->session->cache, data->tor, 0, data->offset, data->tor->blockSize, data->buf);
    6061  tr_torrentGotBlock (data->tor, data->block);
    6162  data->done = true;
    6263}
    6364 
    64 
    6565static int
    66 test_incomplete_dir_is_subdir_of_download_dir (void)
     66test_incomplete_dir_impl (const char * incomplete_dir, const char * download_dir)
    6767{
    6868  size_t i;
    69   char * incomplete_dir;
     69  tr_session * session;
    7070  tr_torrent * tor;
    7171  tr_completeness completeness;
    7272  const tr_completeness completeness_unset = -1;
    7373  const time_t deadline = time(NULL) + 5;
     74  tr_variant settings;
    7475
    7576  /* init the session */
    76   libtransmission_test_session_init ();
    77   incomplete_dir = tr_buildPath (downloadDir, "incomplete", NULL);
    78   tr_sessionSetIncompleteDir (session, incomplete_dir);
    79   tr_sessionSetIncompleteDirEnabled (session, true);
     77  tr_variantInitDict (&settings, 3);
     78  tr_variantDictAddStr (&settings, TR_KEY_download_dir, download_dir);
     79  tr_variantDictAddStr (&settings, TR_KEY_incomplete_dir, incomplete_dir);
     80  tr_variantDictAddBool (&settings, TR_KEY_incomplete_dir_enabled, true);
     81  session = libttest_session_init (&settings);
     82  tr_variantFree (&settings);
     83  download_dir = tr_sessionGetDownloadDir (session);
     84  incomplete_dir = tr_sessionGetIncompleteDir (session);
    8085
    8186  /* init an incomplete torrent.
    8287     the test zero_torrent will be missing its first piece */
    83   tor = libtransmission_test_zero_torrent_init ();
    84   libtransmission_test_zero_torrent_populate (tor, false);
     88  tor = libttest_zero_torrent_init (session);
     89  libttest_zero_torrent_populate (tor, false);
    8590  check (tr_torrentStat(tor)->leftUntilDone == tor->info.pieceSize);
    8691  check_file_location (tor, 0, tr_strdup_printf("%s/%s.part", incomplete_dir, tor->info.files[0].name));
     
    95100    tr_block_index_t first, last;
    96101    char * zero_block = tr_new0 (char, tor->blockSize);
    97     struct test_incomplete_dir_is_subdir_of_download_dir_data data;
     102    struct test_incomplete_dir_data data;
    98103
     104    data.session = session;
    99105    data.tor = tor;
    100106    data.pieceIndex = 0;
     
    108114        data.done = false;
    109115        data.offset = data.block * tor->blockSize;
    110         tr_runInEventThread (session, test_incomplete_dir_is_subdir_of_download_dir_threadfunc, &data);
     116        tr_runInEventThread (session, test_incomplete_dir_threadfunc, &data);
    111117        do { tr_wait_msec(50); } while (!data.done);
    112118      }
     
    124130  check_int_eq (TR_SEED, completeness);
    125131  for (i=0; i<tor->info.fileCount; ++i)
    126     check_file_location (tor, i, tr_buildPath (downloadDir, tor->info.files[i].name, NULL));
     132    check_file_location (tor, i, tr_buildPath (download_dir, tor->info.files[i].name, NULL));
    127133
    128134  /* cleanup */
    129135  tr_torrentRemove (tor, true, remove);
    130   libtransmission_test_session_close ();
    131   tr_free (incomplete_dir);
     136  libttest_session_close (session);
    132137  return 0;
    133138}
    134139
     140static int
     141test_incomplete_dir (void)
     142{
     143  int rv;
     144
     145  /* test what happens when incompleteDir is a subdir of downloadDir*/
     146  if ((rv = test_incomplete_dir_impl ("Downloads/Incomplete", "Downloads")))
     147    return rv;
     148
     149  /* test what happens when downloadDir is a subdir of incompleteDir */
     150  if ((rv = test_incomplete_dir_impl ("Downloads", "Downloads/Complete")))
     151    return rv;
     152
     153  /* test what happens when downloadDir and incompleteDir are siblings */
     154  if ((rv = test_incomplete_dir_impl ("Incomplete", "Downloads")))
     155    return rv;
     156
     157  return 0;
     158}
    135159
    136160/***
     
    141165main (void)
    142166{
    143   const testFunc tests[] = { test_incomplete_dir_is_subdir_of_download_dir };
     167  const testFunc tests[] = { test_incomplete_dir };
    144168
    145169  return runTests (tests, NUM_TESTS (tests));
  • trunk/libtransmission/rename-test.c

    r13913 r13916  
    2020****
    2121***/
     22
     23static tr_session * session = NULL;
    2224
    2325#define check_have_none(tor, totalSize) \
     
    127129  assert (metainfo != NULL);
    128130  assert (metainfo_len > 0);
    129   assert (session != NULL);
    130131  tr_ctorSetMetainfo (ctor, (uint8_t*)metainfo, metainfo_len);
    131132  tr_ctorSetPaused (ctor, TR_FORCE, true);
     
    486487  ***/
    487488
    488   tor = libtransmission_test_zero_torrent_init ();
     489  tor = libttest_zero_torrent_init (session);
    489490  check_int_eq (totalSize, tor->info.totalSize);
    490491  check_int_eq (pieceSize, tor->info.pieceSize);
     
    494495  check_streq ("files-filled-with-zeroes/512",     tor->info.files[2].name);
    495496
    496   libtransmission_test_zero_torrent_populate (tor, false);
     497  libttest_zero_torrent_populate (tor, false);
    497498  fst = tr_torrentFiles (tor, NULL);
    498499  check_int_eq (length[0] - pieceSize, fst[0].bytesCompleted);
     
    544545                             test_partial_file };
    545546
    546   libtransmission_test_session_init ();
     547  session = libttest_session_init (NULL);
    547548  ret = runTests (tests, NUM_TESTS (tests));
    548   libtransmission_test_session_close ();
     549  libttest_session_close (session);
    549550
    550551  return ret;
  • trunk/libtransmission/rpc-test.c

    r13814 r13916  
    7777test_session_get_and_set (void)
    7878{
     79  tr_session * session;
    7980  const char * json;
    8081  tr_variant response;
    8182  tr_variant * args;
    82   tr_torrent * tor = libtransmission_test_zero_torrent_init ();
     83  tr_torrent * tor;
     84
     85  session = libttest_session_init (NULL);
     86  tor= libttest_zero_torrent_init (session);
    8387  check (tor != NULL);
    8488
     
    142146  /* cleanup */
    143147  tr_torrentRemove (tor, false, NULL);
     148  libttest_session_close (session);
    144149  return 0;
    145150}
     
    152157main (void)
    153158{
    154   int ret;
    155159  const testFunc tests[] = { test_list,
    156160                             test_session_get_and_set };
    157161
    158   libtransmission_test_session_init ();
    159   ret = runTests (tests, NUM_TESTS (tests));
    160   libtransmission_test_session_close ();
    161 
    162   return ret;
     162  return runTests (tests, NUM_TESTS (tests));
    163163}
Note: See TracChangeset for help on using the changeset viewer.