source: trunk/libtransmission/blocklist-test.c

Last change on this file was 14382, checked in by mikedld, 6 years ago

Fix compilation on Windows

This should not affect non-Win32 platforms in any way.
As for Win32 (both MinGW and MSVC), this should hopefully allow for
unpatched compilation. Correct functioning is not yet guaranteed though.

  • Property svn:keywords set to Date Rev Author Id
File size: 4.5 KB
Line 
1/*
2 * This file Copyright (C) 2013-2014 Mnemosyne LLC
3 *
4 * It may be used under the GNU GPL versions 2 or 3
5 * or any future license endorsed by Mnemosyne LLC.
6 *
7 * $Id: blocklist-test.c 14382 2014-12-13 15:22:39Z mikedld $
8 */
9
10#include <assert.h>
11#include <stdio.h>
12#include <string.h> /* strlen () */
13
14#include "transmission.h"
15#include "blocklist.h"
16#include "file.h"
17#include "net.h"
18#include "session.h" /* tr_sessionIsAddressBlocked() */
19#include "utils.h"
20
21#include "libtransmission-test.h"
22
23static const char * contents1 =
24  "Austin Law Firm:216.16.1.144-216.16.1.151\n"
25  "Sargent Controls and Aerospace:216.19.18.0-216.19.18.255\n"
26  "Corel Corporation:216.21.157.192-216.21.157.223\n"
27  "Fox Speed Channel:216.79.131.192-216.79.131.223\n";
28
29static const char * contents2 =
30  "Austin Law Firm:216.16.1.144-216.16.1.151\n"
31  "Sargent Controls and Aerospace:216.19.18.0-216.19.18.255\n"
32  "Corel Corporation:216.21.157.192-216.21.157.223\n"
33  "Fox Speed Channel:216.79.131.192-216.79.131.223\n"
34  "Evilcorp:216.88.88.0-216.88.88.255\n";
35
36static void
37create_text_file (const char * path, const char * contents)
38{
39  tr_sys_file_t fd;
40  char * dir;
41
42  dir = tr_sys_path_dirname (path, NULL);
43  tr_sys_dir_create (dir, TR_SYS_DIR_CREATE_PARENTS, 0700, NULL);
44  tr_free (dir);
45
46  fd = tr_sys_file_open (path, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0600, NULL);
47  tr_sys_file_write (fd, contents, strlen (contents), NULL, NULL);
48  tr_sys_file_close (fd, NULL);
49
50  libttest_sync ();
51}
52
53static bool
54address_is_blocked (tr_session * session, const char * address_str)
55{
56  struct tr_address addr;
57  tr_address_from_string (&addr, address_str);
58  return tr_sessionIsAddressBlocked (session, &addr);
59}
60
61static int
62test_parsing (void)
63{
64  char * path;
65  tr_session * session;
66
67  /* init the session */
68  session = libttest_session_init (NULL);
69  check (!tr_blocklistExists (session));
70  check_int_eq (0, tr_blocklistGetRuleCount (session));
71
72  /* init the blocklist */
73  path = tr_buildPath (tr_sessionGetConfigDir(session), "blocklists", "level1", NULL);
74  create_text_file (path, contents1);
75  tr_free (path);
76  tr_sessionReloadBlocklists (session);
77  check (tr_blocklistExists (session));
78  check_int_eq (4, tr_blocklistGetRuleCount (session));
79
80  /* enable the blocklist */
81  check (!tr_blocklistIsEnabled (session));
82  tr_blocklistSetEnabled (session, true);
83  check (tr_blocklistIsEnabled (session));
84
85  /* test blocked addresses */
86  check (!address_is_blocked (session, "216.16.1.143"));
87  check ( address_is_blocked (session, "216.16.1.144"));
88  check ( address_is_blocked (session, "216.16.1.145"));
89  check ( address_is_blocked (session, "216.16.1.146"));
90  check ( address_is_blocked (session, "216.16.1.147"));
91  check ( address_is_blocked (session, "216.16.1.148"));
92  check ( address_is_blocked (session, "216.16.1.149"));
93  check ( address_is_blocked (session, "216.16.1.150"));
94  check ( address_is_blocked (session, "216.16.1.151"));
95  check (!address_is_blocked (session, "216.16.1.152"));
96  check (!address_is_blocked (session, "216.16.1.153"));
97  check (!address_is_blocked (session, "217.0.0.1"));
98  check (!address_is_blocked (session, "255.0.0.1"));
99
100  /* cleanup */
101  libttest_session_close (session);
102  return 0;
103}
104
105/***
106****
107***/
108
109static int
110test_updating (void)
111{
112  char * path;
113  tr_session * session;
114
115  /* init the session */
116  session = libttest_session_init (NULL);
117  path = tr_buildPath (tr_sessionGetConfigDir(session), "blocklists", "level1", NULL);
118
119  /* no blocklist to start with... */
120  check_int_eq (0, tr_blocklistGetRuleCount (session));
121
122  /* test that updated source files will get loaded */
123  create_text_file (path, contents1);
124  tr_sessionReloadBlocklists (session);
125  check_int_eq (4, tr_blocklistGetRuleCount (session));
126
127  /* test that updated source files will get loaded */
128  create_text_file (path, contents2);
129  tr_sessionReloadBlocklists (session);
130  check_int_eq (5, tr_blocklistGetRuleCount (session));
131
132  /* test that updated source files will get loaded */
133  create_text_file (path, contents1);
134  tr_sessionReloadBlocklists (session);
135  check_int_eq (4, tr_blocklistGetRuleCount (session));
136
137  /* ensure that new files, if bad, get skipped */
138  create_text_file (path,  "# nothing useful\n");
139  tr_sessionReloadBlocklists (session);
140  check_int_eq (4, tr_blocklistGetRuleCount (session));
141
142  /* cleanup */
143  libttest_session_close (session);
144  tr_free (path);
145  return 0;
146}
147
148/***
149****
150***/
151
152int
153main (void)
154{
155  const testFunc tests[] = { test_parsing,
156                             test_updating };
157
158  return runTests (tests, NUM_TESTS (tests));
159}
Note: See TracBrowser for help on using the repository browser.