Changeset 13825


Ignore:
Timestamp:
Jan 21, 2013, 9:11:00 PM (8 years ago)
Author:
jordan
Message:

(libT) #4632 'custom blocklist generates 2x # of rules': add unit test (currently failing) to test for this issue.

Location:
trunk/libtransmission
Files:
3 edited

Legend:

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

    r13824 r13825  
     1#include <assert.h>
    12#include <stdio.h>
     3#include <unistd.h> /* sync() */
     4
    25#include "transmission.h"
    36#include "blocklist.h"
    47#include "net.h"
     8#include "session.h" /* tr_sessionIsAddressBlocked() */
    59#include "utils.h"
    610
    711#include "libtransmission-test.h"
    812
    9 #ifndef WIN32
    10     #define TEMPDIR_PREFIX "/tmp/"
    11 #else
    12     #define TEMPDIR_PREFIX
    13 #endif
     13static const char * contents1 =
     14  "Austin Law Firm:216.16.1.144-216.16.1.151\n"
     15  "Sargent Controls and Aerospace:216.19.18.0-216.19.18.255\n"
     16  "Corel Corporation:216.21.157.192-216.21.157.223\n"
     17  "Fox Speed Channel:216.79.131.192-216.79.131.223\n";
    1418
    15 #define TEMPFILE_TXT  TEMPDIR_PREFIX "transmission-blocklist-test.txt"
    16 #define TEMPFILE_BIN  TEMPDIR_PREFIX "transmission-blocklist-test.bin"
     19static const char * contents2 =
     20  "Austin Law Firm:216.16.1.144-216.16.1.151\n"
     21  "Sargent Controls and Aerospace:216.19.18.0-216.19.18.255\n"
     22  "Corel Corporation:216.21.157.192-216.21.157.223\n"
     23  "Fox Speed Channel:216.79.131.192-216.79.131.223\n"
     24  "Evilcorp:216.88.88.0-216.88.88.255\n";
    1725
    18 static void
    19 createTestBlocklist (const char * tmpfile)
     26static char *
     27create_blocklist_text_file (const char * basename, const char * contents)
    2028{
    21     const char * lines[] = { "Austin Law Firm:216.16.1.144-216.16.1.151",
    22                              "Sargent Controls and Aerospace:216.19.18.0-216.19.18.255",
    23                              "Corel Corporation:216.21.157.192-216.21.157.223",
    24                              "Fox Speed Channel:216.79.131.192-216.79.131.223" };
    25     FILE *       out;
    26     int          i;
    27     const int    lineCount = sizeof (lines) / sizeof (lines[0]);
     29  FILE * fp;
     30  char * path;
    2831
    29     /* create the ascii file to feed to libtransmission */
    30     out = fopen (tmpfile, "w+");
    31     for (i = 0; i < lineCount; ++i)
    32         fprintf (out, "%s\n", lines[i]);
    33     fclose (out);
     32  assert (blocklistDir != NULL);
     33
     34  path = tr_buildPath (blocklistDir, basename, NULL);
     35  remove (path);
     36  fp = fopen (path, "w+");
     37  fprintf (fp, "%s", contents);
     38  fclose (fp);
     39  sync ();
     40  return path;
     41}
     42
     43static bool
     44address_is_blocked (const char * address_str)
     45{
     46  struct tr_address addr;
     47  tr_address_from_string (&addr, address_str);
     48  return tr_sessionIsAddressBlocked (session, &addr);
    3449}
    3550
    3651static int
    37 testBlockList (void)
     52test_parsing (void)
    3853{
    39     const char * tmpfile_txt = TEMPFILE_TXT;
    40     const char * tmpfile_bin = TEMPFILE_BIN;
    41     struct tr_address addr;
    42     tr_blocklistFile * b;
     54  char * text_file;
    4355
    44     remove (tmpfile_txt);
    45     remove (tmpfile_bin);
     56  libtransmission_test_session_init_sandbox ();
     57  text_file = create_blocklist_text_file ("level1", contents1);
     58  libtransmission_test_session_init_session ();
    4659
    47     b = tr_blocklistFileNew (tmpfile_bin, true);
    48     createTestBlocklist (tmpfile_txt);
    49     tr_blocklistFileSetContent (b, tmpfile_txt);
     60  check (!tr_blocklistIsEnabled (session));
     61  tr_blocklistSetEnabled (session, true);
     62  check (tr_blocklistIsEnabled (session));
    5063
    51     /* now run some tests */
    52     check (tr_address_from_string (&addr, "216.16.1.143"));
    53     check (!tr_blocklistFileHasAddress (b, &addr));
    54     check (tr_address_from_string (&addr, "216.16.1.144"));
    55     check (tr_blocklistFileHasAddress (b, &addr));
    56     check (tr_address_from_string (&addr, "216.16.1.145"));
    57     check (tr_blocklistFileHasAddress (b, &addr));
    58     check (tr_address_from_string (&addr, "216.16.1.146"));
    59     check (tr_blocklistFileHasAddress (b, &addr));
    60     check (tr_address_from_string (&addr, "216.16.1.147"));
    61     check (tr_blocklistFileHasAddress (b, &addr));
    62     check (tr_address_from_string (&addr, "216.16.1.148"));
    63     check (tr_blocklistFileHasAddress (b, &addr));
    64     check (tr_address_from_string (&addr, "216.16.1.149"));
    65     check (tr_blocklistFileHasAddress (b, &addr));
    66     check (tr_address_from_string (&addr, "216.16.1.150"));
    67     check (tr_blocklistFileHasAddress (b, &addr));
    68     check (tr_address_from_string (&addr, "216.16.1.151"));
    69     check (tr_blocklistFileHasAddress (b, &addr));
    70     check (tr_address_from_string (&addr, "216.16.1.152"));
    71     check (!tr_blocklistFileHasAddress (b, &addr));
    72     check (tr_address_from_string (&addr, "216.16.1.153"));
    73     check (!tr_blocklistFileHasAddress (b, &addr));
    74     check (tr_address_from_string (&addr, "217.0.0.1"));
    75     check (!tr_blocklistFileHasAddress (b, &addr));
    76     check (tr_address_from_string (&addr, "255.0.0.1"));
     64  check (tr_blocklistExists (session));
     65  check_int_eq (4, tr_blocklistGetRuleCount (session));
    7766
    78     /* cleanup */
    79     tr_blocklistFileFree (b);
    80     remove (tmpfile_txt);
    81     remove (tmpfile_bin);
    82     return 0;
     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);
     83  return 0;
    8384}
    8485
    85 MAIN_SINGLE_TEST (testBlockList)
     86/***
     87****
     88***/
    8689
     90static int
     91test_updating (void)
     92{
     93  char * text_file;
     94
     95  libtransmission_test_session_init_sandbox ();
     96  text_file = create_blocklist_text_file ("level1", contents1);
     97  libtransmission_test_session_init_session ();
     98  check_int_eq (4, tr_blocklistGetRuleCount (session));
     99
     100  /* test that updated source files will get loaded */
     101  tr_free (text_file);
     102  text_file = create_blocklist_text_file ("level1", contents2);
     103  tr_sessionReloadBlocklists (session);
     104  check_int_eq (5, tr_blocklistGetRuleCount (session));
     105
     106  /* test that updated source files will get loaded */
     107  tr_free (text_file);
     108  text_file = create_blocklist_text_file ("level1", contents1);
     109  tr_sessionReloadBlocklists (session);
     110  check_int_eq (4, tr_blocklistGetRuleCount (session));
     111
     112  /* ensure that new files, if bad, get skipped */
     113  tr_free (text_file);
     114  text_file = create_blocklist_text_file ("level1", "# nothing useful\n");
     115  tr_sessionReloadBlocklists (session);
     116  check_int_eq (4, tr_blocklistGetRuleCount (session));
     117
     118  libtransmission_test_session_close ();
     119  tr_free (text_file);
     120  return 0;
     121}
     122
     123/***
     124****
     125***/
     126
     127int
     128main (void)
     129{
     130  const testFunc tests[] = { test_parsing,
     131                             test_updating };
     132
     133  libtransmission_test_session_init_formatters ();
     134
     135  return runTests (tests, NUM_TESTS (tests));
     136}
  • trunk/libtransmission/libtransmission-test.c

    r13814 r13825  
    112112
    113113tr_session * session = NULL;
    114 static char * sandbox = NULL;
     114char * sandbox = NULL;
     115char * downloadDir = NULL;
     116char * blocklistDir = NULL;
    115117
    116118static char*
     
    188190
    189191void
    190 libtransmission_test_session_init (void)
    191 {
    192   char * cwd;
    193   char * downloadDir;
    194   tr_variant dict;
    195 
     192libtransmission_test_session_init_formatters (void)
     193{
    196194  tr_formatter_mem_init (MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR);
    197195  tr_formatter_size_init (DISK_K,DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR);
    198196  tr_formatter_speed_init (SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR);
     197}
     198
     199void
     200libtransmission_test_session_init_sandbox (void)
     201{
     202  char * cwd;
    199203
    200204  /* create a sandbox for the test session */
     
    204208  downloadDir = tr_buildPath (sandbox, "Downloads", NULL);
    205209  tr_mkdirp (downloadDir, 0700);
    206 
    207   /* create a test session */
    208   tr_variantInitDict    (&dict, 3);
     210  blocklistDir = tr_buildPath (sandbox, "blocklists", NULL);
     211  tr_mkdirp (blocklistDir, 0700);
     212
     213  /* cleanup locals*/
     214  tr_free (cwd);
     215}
     216
     217void
     218libtransmission_test_session_init_session (void)
     219{
     220  tr_variant dict;
     221
     222  /* libtransmission_test_session_init_sandbox() has to be called first */
     223  assert (sandbox != NULL);
     224  assert (session == NULL);
     225
     226  /* init the session */
     227  tr_variantInitDict    (&dict, 4);
    209228  tr_variantDictAddStr  (&dict, TR_KEY_download_dir, downloadDir);
    210229  tr_variantDictAddBool (&dict, TR_KEY_port_forwarding_enabled, false);
    211230  tr_variantDictAddBool (&dict, TR_KEY_dht_enabled, false);
    212   session = tr_sessionInit ("rename-test", sandbox, true, &dict);
     231  tr_variantDictAddInt  (&dict, TR_KEY_message_level, verbose ? TR_MSG_DBG : TR_MSG_ERR);
     232  session = tr_sessionInit ("libtransmission-test", sandbox, !verbose, &dict);
    213233
    214234  /* cleanup locals*/
    215235  tr_variantFree (&dict);
    216   tr_free (downloadDir);
    217   tr_free (cwd);
     236}
     237
     238void
     239libtransmission_test_session_init (void)
     240{
     241  libtransmission_test_session_init_formatters ();
     242  libtransmission_test_session_init_sandbox ();
     243  libtransmission_test_session_init_session ();
    218244}
    219245
     
    223249  tr_sessionClose (session);
    224250  tr_freeMessageList (tr_getQueuedMessages ());
     251  session = NULL;
     252
    225253  rm_rf (sandbox);
     254
     255  tr_free (blocklistDir);
     256  blocklistDir = NULL;
     257
     258  tr_free (downloadDir);
     259  downloadDir = NULL;
     260
    226261  tr_free (sandbox);
     262  sandbox = NULL;
    227263}
    228264
  • trunk/libtransmission/libtransmission-test.h

    r13814 r13825  
    6868
    6969extern tr_session * session;
    70 void libtransmission_test_session_init (void);
     70extern char * sandbox;
     71extern char * downloadDir;
     72extern char * blocklistDir;
     73
     74void libtransmission_test_session_init_formatters (void);
     75void libtransmission_test_session_init_sandbox (void);
     76void libtransmission_test_session_init_session (void);
     77void libtransmission_test_session_init (void); /* utility; calls the other 3 */
     78
    7179void libtransmission_test_session_close (void);
    7280tr_torrent * libtransmission_test_zero_torrent_init (void);
Note: See TracChangeset for help on using the changeset viewer.