Changeset 13825
- Timestamp:
- Jan 21, 2013, 9:11:00 PM (8 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/blocklist-test.c
r13824 r13825 1 #include <assert.h> 1 2 #include <stdio.h> 3 #include <unistd.h> /* sync() */ 4 2 5 #include "transmission.h" 3 6 #include "blocklist.h" 4 7 #include "net.h" 8 #include "session.h" /* tr_sessionIsAddressBlocked() */ 5 9 #include "utils.h" 6 10 7 11 #include "libtransmission-test.h" 8 12 9 #ifndef WIN32 10 #define TEMPDIR_PREFIX "/tmp/"11 #else 12 #define TEMPDIR_PREFIX13 #endif 13 static 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"; 14 18 15 #define TEMPFILE_TXT TEMPDIR_PREFIX "transmission-blocklist-test.txt" 16 #define TEMPFILE_BIN TEMPDIR_PREFIX "transmission-blocklist-test.bin" 19 static 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"; 17 25 18 static void19 create TestBlocklist (const char * tmpfile)26 static char * 27 create_blocklist_text_file (const char * basename, const char * contents) 20 28 { 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; 28 31 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 43 static bool 44 address_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); 34 49 } 35 50 36 51 static int 37 test BlockList(void)52 test_parsing (void) 38 53 { 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; 43 55 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 (); 46 59 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)); 50 63 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)); 77 66 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; 83 84 } 84 85 85 MAIN_SINGLE_TEST (testBlockList) 86 /*** 87 **** 88 ***/ 86 89 90 static int 91 test_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 127 int 128 main (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 112 112 113 113 tr_session * session = NULL; 114 static char * sandbox = NULL; 114 char * sandbox = NULL; 115 char * downloadDir = NULL; 116 char * blocklistDir = NULL; 115 117 116 118 static char* … … 188 190 189 191 void 190 libtransmission_test_session_init (void) 191 { 192 char * cwd; 193 char * downloadDir; 194 tr_variant dict; 195 192 libtransmission_test_session_init_formatters (void) 193 { 196 194 tr_formatter_mem_init (MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR); 197 195 tr_formatter_size_init (DISK_K,DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR); 198 196 tr_formatter_speed_init (SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR); 197 } 198 199 void 200 libtransmission_test_session_init_sandbox (void) 201 { 202 char * cwd; 199 203 200 204 /* create a sandbox for the test session */ … … 204 208 downloadDir = tr_buildPath (sandbox, "Downloads", NULL); 205 209 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 217 void 218 libtransmission_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); 209 228 tr_variantDictAddStr (&dict, TR_KEY_download_dir, downloadDir); 210 229 tr_variantDictAddBool (&dict, TR_KEY_port_forwarding_enabled, false); 211 230 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); 213 233 214 234 /* cleanup locals*/ 215 235 tr_variantFree (&dict); 216 tr_free (downloadDir); 217 tr_free (cwd); 236 } 237 238 void 239 libtransmission_test_session_init (void) 240 { 241 libtransmission_test_session_init_formatters (); 242 libtransmission_test_session_init_sandbox (); 243 libtransmission_test_session_init_session (); 218 244 } 219 245 … … 223 249 tr_sessionClose (session); 224 250 tr_freeMessageList (tr_getQueuedMessages ()); 251 session = NULL; 252 225 253 rm_rf (sandbox); 254 255 tr_free (blocklistDir); 256 blocklistDir = NULL; 257 258 tr_free (downloadDir); 259 downloadDir = NULL; 260 226 261 tr_free (sandbox); 262 sandbox = NULL; 227 263 } 228 264 -
trunk/libtransmission/libtransmission-test.h
r13814 r13825 68 68 69 69 extern tr_session * session; 70 void libtransmission_test_session_init (void); 70 extern char * sandbox; 71 extern char * downloadDir; 72 extern char * blocklistDir; 73 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 71 79 void libtransmission_test_session_close (void); 72 80 tr_torrent * libtransmission_test_zero_torrent_init (void);
Note: See TracChangeset
for help on using the changeset viewer.