2014-01-19 01:09:44 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2013-2014 Mnemosyne LLC
|
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
2014-01-19 01:29:38 +00:00
|
|
|
* $Id$
|
2014-01-19 01:09:44 +00:00
|
|
|
*/
|
|
|
|
|
2013-01-21 21:11:00 +00:00
|
|
|
#include <assert.h>
|
2008-03-29 21:05:51 +00:00
|
|
|
#include <stdio.h>
|
2014-07-28 04:13:38 +00:00
|
|
|
#include <string.h> /* strlen () */
|
2013-01-21 21:11:00 +00:00
|
|
|
#include <unistd.h> /* sync() */
|
|
|
|
|
2008-03-29 21:05:51 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "blocklist.h"
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "file.h"
|
2008-03-29 21:05:51 +00:00
|
|
|
#include "net.h"
|
2013-01-21 21:11:00 +00:00
|
|
|
#include "session.h" /* tr_sessionIsAddressBlocked() */
|
2008-03-29 21:05:51 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2012-08-18 16:07:05 +00:00
|
|
|
#include "libtransmission-test.h"
|
2008-03-29 21:05:51 +00:00
|
|
|
|
2013-01-21 21:11:00 +00:00
|
|
|
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";
|
2008-03-29 21:05:51 +00:00
|
|
|
|
2013-01-21 21:11:00 +00:00
|
|
|
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";
|
2012-08-18 16:07:05 +00:00
|
|
|
|
2013-02-01 05:57:47 +00:00
|
|
|
static void
|
|
|
|
create_text_file (const char * path, const char * contents)
|
2008-03-29 21:05:51 +00:00
|
|
|
{
|
2014-07-28 04:13:38 +00:00
|
|
|
tr_sys_file_t fd;
|
2013-02-01 05:57:47 +00:00
|
|
|
char * dir;
|
2013-01-21 21:11:00 +00:00
|
|
|
|
2014-07-08 00:08:43 +00:00
|
|
|
dir = tr_sys_path_dirname (path, NULL);
|
2014-09-21 17:55:39 +00:00
|
|
|
tr_sys_dir_create (dir, TR_SYS_DIR_CREATE_PARENTS, 0700, NULL);
|
2013-02-01 05:57:47 +00:00
|
|
|
tr_free (dir);
|
2013-01-21 21:11:00 +00:00
|
|
|
|
2014-07-28 04:13:38 +00:00
|
|
|
fd = tr_sys_file_open (path, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0600, NULL);
|
|
|
|
tr_sys_file_write (fd, contents, strlen (contents), NULL, NULL);
|
|
|
|
tr_sys_file_close (fd, NULL);
|
2013-02-01 05:57:47 +00:00
|
|
|
|
2013-01-21 21:11:00 +00:00
|
|
|
sync ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-02-01 05:57:47 +00:00
|
|
|
address_is_blocked (tr_session * session, const char * address_str)
|
2013-01-21 21:11:00 +00:00
|
|
|
{
|
|
|
|
struct tr_address addr;
|
|
|
|
tr_address_from_string (&addr, address_str);
|
|
|
|
return tr_sessionIsAddressBlocked (session, &addr);
|
2008-03-29 21:05:51 +00:00
|
|
|
}
|
|
|
|
|
2012-08-18 16:07:05 +00:00
|
|
|
static int
|
2013-01-21 21:11:00 +00:00
|
|
|
test_parsing (void)
|
2008-03-29 21:05:51 +00:00
|
|
|
{
|
2013-02-01 05:57:47 +00:00
|
|
|
char * path;
|
|
|
|
tr_session * session;
|
|
|
|
|
|
|
|
/* init the session */
|
|
|
|
session = libttest_session_init (NULL);
|
|
|
|
check (!tr_blocklistExists (session));
|
|
|
|
check_int_eq (0, tr_blocklistGetRuleCount (session));
|
2013-01-21 21:11:00 +00:00
|
|
|
|
2013-02-01 05:57:47 +00:00
|
|
|
/* 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));
|
2013-01-21 21:11:00 +00:00
|
|
|
|
2013-02-01 05:57:47 +00:00
|
|
|
/* enable the blocklist */
|
2013-01-21 21:11:00 +00:00
|
|
|
check (!tr_blocklistIsEnabled (session));
|
|
|
|
tr_blocklistSetEnabled (session, true);
|
|
|
|
check (tr_blocklistIsEnabled (session));
|
|
|
|
|
2013-02-01 05:57:47 +00:00
|
|
|
/* 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);
|
2013-01-21 21:11:00 +00:00
|
|
|
return 0;
|
2008-03-29 21:05:51 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2013-01-21 21:11:00 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2012-08-18 16:07:05 +00:00
|
|
|
|
2013-01-21 21:11:00 +00:00
|
|
|
static int
|
|
|
|
test_updating (void)
|
|
|
|
{
|
2013-02-01 05:57:47 +00:00
|
|
|
char * path;
|
|
|
|
tr_session * session;
|
|
|
|
|
|
|
|
/* init the session */
|
|
|
|
session = libttest_session_init (NULL);
|
|
|
|
path = tr_buildPath (tr_sessionGetConfigDir(session), "blocklists", "level1", NULL);
|
2013-01-21 21:11:00 +00:00
|
|
|
|
2013-02-01 05:57:47 +00:00
|
|
|
/* 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);
|
2013-01-21 21:11:00 +00:00
|
|
|
check_int_eq (4, tr_blocklistGetRuleCount (session));
|
|
|
|
|
|
|
|
/* test that updated source files will get loaded */
|
2013-02-01 05:57:47 +00:00
|
|
|
create_text_file (path, contents2);
|
2013-01-21 21:11:00 +00:00
|
|
|
tr_sessionReloadBlocklists (session);
|
|
|
|
check_int_eq (5, tr_blocklistGetRuleCount (session));
|
|
|
|
|
|
|
|
/* test that updated source files will get loaded */
|
2013-02-01 05:57:47 +00:00
|
|
|
create_text_file (path, contents1);
|
2013-01-21 21:11:00 +00:00
|
|
|
tr_sessionReloadBlocklists (session);
|
|
|
|
check_int_eq (4, tr_blocklistGetRuleCount (session));
|
|
|
|
|
|
|
|
/* ensure that new files, if bad, get skipped */
|
2013-02-01 05:57:47 +00:00
|
|
|
create_text_file (path, "# nothing useful\n");
|
2013-01-21 21:11:00 +00:00
|
|
|
tr_sessionReloadBlocklists (session);
|
|
|
|
check_int_eq (4, tr_blocklistGetRuleCount (session));
|
|
|
|
|
2013-02-01 05:57:47 +00:00
|
|
|
/* cleanup */
|
|
|
|
libttest_session_close (session);
|
|
|
|
tr_free (path);
|
2013-01-21 21:11:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
int
|
|
|
|
main (void)
|
|
|
|
{
|
|
|
|
const testFunc tests[] = { test_parsing,
|
|
|
|
test_updating };
|
|
|
|
|
|
|
|
return runTests (tests, NUM_TESTS (tests));
|
|
|
|
}
|