~hendrik-grewe/transmission/private-patch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * This file Copyright (C) 2013-2014 Mnemosyne LLC
 *
 * It may be used under the GNU GPL versions 2 or 3
 * or any future license endorsed by Mnemosyne LLC.
 *
 * $Id$
 */

#include <assert.h>
#include <stdio.h>
#include <unistd.h> /* sync() */

#include "transmission.h"
#include "blocklist.h"
#include "net.h"
#include "session.h" /* tr_sessionIsAddressBlocked() */
#include "utils.h"

#include "libtransmission-test.h"

static const char * contents1 =
  "Austin Law Firm:216.16.1.144-216.16.1.151\n"
  "Sargent Controls and Aerospace:216.19.18.0-216.19.18.255\n"
  "Corel Corporation:216.21.157.192-216.21.157.223\n"
  "Fox Speed Channel:216.79.131.192-216.79.131.223\n";

static const char * contents2 =
  "Austin Law Firm:216.16.1.144-216.16.1.151\n"
  "Sargent Controls and Aerospace:216.19.18.0-216.19.18.255\n"
  "Corel Corporation:216.21.157.192-216.21.157.223\n"
  "Fox Speed Channel:216.79.131.192-216.79.131.223\n"
  "Evilcorp:216.88.88.0-216.88.88.255\n";

static void
create_text_file (const char * path, const char * contents)
{
  FILE * fp;
  char * dir;

  dir = tr_dirname (path);
  tr_mkdirp (dir, 0700);
  tr_free (dir);

  tr_remove (path);
  fp = fopen (path, "w+");
  fprintf (fp, "%s", contents);
  fclose (fp);

  sync ();
}

static bool
address_is_blocked (tr_session * session, const char * address_str)
{
  struct tr_address addr;
  tr_address_from_string (&addr, address_str);
  return tr_sessionIsAddressBlocked (session, &addr);
}

static int
test_parsing (void)
{
  char * path;
  tr_session * session;

  /* init the session */
  session = libttest_session_init (NULL);
  check (!tr_blocklistExists (session));
  check_int_eq (0, tr_blocklistGetRuleCount (session));

  /* init the blocklist */
  path = tr_buildPath (tr_sessionGetConfigDir(session), "blocklists", "level1", NULL);
  create_text_file (path, contents1);
  tr_free (path);
  tr_sessionReloadBlocklists (session);
  check (tr_blocklistExists (session));
  check_int_eq (4, tr_blocklistGetRuleCount (session));

  /* enable the blocklist */
  check (!tr_blocklistIsEnabled (session));
  tr_blocklistSetEnabled (session, true);
  check (tr_blocklistIsEnabled (session));

  /* test blocked addresses */
  check (!address_is_blocked (session, "216.16.1.143"));
  check ( address_is_blocked (session, "216.16.1.144"));
  check ( address_is_blocked (session, "216.16.1.145"));
  check ( address_is_blocked (session, "216.16.1.146"));
  check ( address_is_blocked (session, "216.16.1.147"));
  check ( address_is_blocked (session, "216.16.1.148"));
  check ( address_is_blocked (session, "216.16.1.149"));
  check ( address_is_blocked (session, "216.16.1.150"));
  check ( address_is_blocked (session, "216.16.1.151"));
  check (!address_is_blocked (session, "216.16.1.152"));
  check (!address_is_blocked (session, "216.16.1.153"));
  check (!address_is_blocked (session, "217.0.0.1"));
  check (!address_is_blocked (session, "255.0.0.1"));

  /* cleanup */
  libttest_session_close (session);
  return 0;
}

/***
****
***/

static int
test_updating (void)
{
  char * path;
  tr_session * session;

  /* init the session */
  session = libttest_session_init (NULL);
  path = tr_buildPath (tr_sessionGetConfigDir(session), "blocklists", "level1", NULL);

  /* no blocklist to start with... */
  check_int_eq (0, tr_blocklistGetRuleCount (session));

  /* test that updated source files will get loaded */
  create_text_file (path, contents1);
  tr_sessionReloadBlocklists (session);
  check_int_eq (4, tr_blocklistGetRuleCount (session));

  /* test that updated source files will get loaded */
  create_text_file (path, contents2);
  tr_sessionReloadBlocklists (session);
  check_int_eq (5, tr_blocklistGetRuleCount (session));

  /* test that updated source files will get loaded */
  create_text_file (path, contents1);
  tr_sessionReloadBlocklists (session);
  check_int_eq (4, tr_blocklistGetRuleCount (session));

  /* ensure that new files, if bad, get skipped */
  create_text_file (path,  "# nothing useful\n");
  tr_sessionReloadBlocklists (session);
  check_int_eq (4, tr_blocklistGetRuleCount (session));

  /* cleanup */
  libttest_session_close (session);
  tr_free (path);
  return 0;
}

/***
****
***/

int
main (void)
{
  const testFunc tests[] = { test_parsing,
                             test_updating };

  return runTests (tests, NUM_TESTS (tests));
}