Changeset 13916
- Timestamp:
- Feb 1, 2013, 5:57:47 AM (8 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/blocklist-test.c
r13825 r13916 24 24 "Evilcorp:216.88.88.0-216.88.88.255\n"; 25 25 26 static char *27 create_ blocklist_text_file (const char * basename, const char * contents)26 static void 27 create_text_file (const char * path, const char * contents) 28 28 { 29 29 FILE * fp; 30 char * path;30 char * dir; 31 31 32 assert (blocklistDir != NULL); 32 dir = tr_dirname (path); 33 tr_mkdirp (dir, 0700); 34 tr_free (dir); 33 35 34 path = tr_buildPath (blocklistDir, basename, NULL);35 36 remove (path); 36 37 fp = fopen (path, "w+"); 37 38 fprintf (fp, "%s", contents); 38 39 fclose (fp); 40 39 41 sync (); 40 return path;41 42 } 42 43 43 44 static bool 44 address_is_blocked ( const char * address_str)45 address_is_blocked (tr_session * session, const char * address_str) 45 46 { 46 47 struct tr_address addr; … … 52 53 test_parsing (void) 53 54 { 54 char * text_file; 55 char * path; 56 tr_session * session; 55 57 56 libtransmission_test_session_init_sandbox (); 57 text_file = create_blocklist_text_file ("level1", contents1); 58 libtransmission_test_session_init_session (); 58 /* init the session */ 59 session = libttest_session_init (NULL); 60 check (!tr_blocklistExists (session)); 61 check_int_eq (0, tr_blocklistGetRuleCount (session)); 59 62 63 /* init the blocklist */ 64 path = tr_buildPath (tr_sessionGetConfigDir(session), "blocklists", "level1", NULL); 65 create_text_file (path, contents1); 66 tr_free (path); 67 tr_sessionReloadBlocklists (session); 68 check (tr_blocklistExists (session)); 69 check_int_eq (4, tr_blocklistGetRuleCount (session)); 70 71 /* enable the blocklist */ 60 72 check (!tr_blocklistIsEnabled (session)); 61 73 tr_blocklistSetEnabled (session, true); 62 74 check (tr_blocklistIsEnabled (session)); 63 75 64 check (tr_blocklistExists (session)); 65 check_int_eq (4, tr_blocklistGetRuleCount (session)); 76 /* test blocked addresses */ 77 check (!address_is_blocked (session, "216.16.1.143")); 78 check ( address_is_blocked (session, "216.16.1.144")); 79 check ( address_is_blocked (session, "216.16.1.145")); 80 check ( address_is_blocked (session, "216.16.1.146")); 81 check ( address_is_blocked (session, "216.16.1.147")); 82 check ( address_is_blocked (session, "216.16.1.148")); 83 check ( address_is_blocked (session, "216.16.1.149")); 84 check ( address_is_blocked (session, "216.16.1.150")); 85 check ( address_is_blocked (session, "216.16.1.151")); 86 check (!address_is_blocked (session, "216.16.1.152")); 87 check (!address_is_blocked (session, "216.16.1.153")); 88 check (!address_is_blocked (session, "217.0.0.1")); 89 check (!address_is_blocked (session, "255.0.0.1")); 66 90 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); 91 /* cleanup */ 92 libttest_session_close (session); 83 93 return 0; 84 94 } … … 91 101 test_updating (void) 92 102 { 93 char * text_file; 103 char * path; 104 tr_session * session; 94 105 95 libtransmission_test_session_init_sandbox (); 96 text_file = create_blocklist_text_file ("level1", contents1); 97 libtransmission_test_session_init_session (); 106 /* init the session */ 107 session = libttest_session_init (NULL); 108 path = tr_buildPath (tr_sessionGetConfigDir(session), "blocklists", "level1", NULL); 109 110 /* no blocklist to start with... */ 111 check_int_eq (0, tr_blocklistGetRuleCount (session)); 112 113 /* test that updated source files will get loaded */ 114 create_text_file (path, contents1); 115 tr_sessionReloadBlocklists (session); 98 116 check_int_eq (4, tr_blocklistGetRuleCount (session)); 99 117 100 118 /* test that updated source files will get loaded */ 101 tr_free (text_file); 102 text_file = create_blocklist_text_file ("level1", contents2); 119 create_text_file (path, contents2); 103 120 tr_sessionReloadBlocklists (session); 104 121 check_int_eq (5, tr_blocklistGetRuleCount (session)); 105 122 106 123 /* test that updated source files will get loaded */ 107 tr_free (text_file); 108 text_file = create_blocklist_text_file ("level1", contents1); 124 create_text_file (path, contents1); 109 125 tr_sessionReloadBlocklists (session); 110 126 check_int_eq (4, tr_blocklistGetRuleCount (session)); 111 127 112 128 /* ensure that new files, if bad, get skipped */ 113 tr_free (text_file); 114 text_file = create_blocklist_text_file ("level1", "# nothing useful\n"); 129 create_text_file (path, "# nothing useful\n"); 115 130 tr_sessionReloadBlocklists (session); 116 131 check_int_eq (4, tr_blocklistGetRuleCount (session)); 117 132 118 libtransmission_test_session_close (); 119 tr_free (text_file); 133 /* cleanup */ 134 libttest_session_close (session); 135 tr_free (path); 120 136 return 0; 121 137 } … … 131 147 test_updating }; 132 148 133 libtransmission_test_session_init_formatters ();134 135 149 return runTests (tests, NUM_TESTS (tests)); 136 150 } -
trunk/libtransmission/libtransmission-test.c
r13913 r13916 192 192 #define SPEED_T_STR "TB/s" 193 193 194 tr_session * 195 libttest_session_init (tr_variant * settings) 196 { 197 size_t len; 198 const char * str; 199 char * sandbox; 200 char * path; 201 tr_quark q; 202 static bool formatters_inited = false; 203 tr_session * session; 204 tr_variant local_settings; 205 206 tr_variantInitDict (&local_settings, 10); 207 208 if (settings == NULL) 209 settings = &local_settings; 210 211 path = tr_getcwd (); 212 sandbox = tr_buildPath (path, "sandbox-XXXXXX", NULL); 213 tr_mkdtemp (sandbox); 214 tr_free (path); 215 216 if (!formatters_inited) 217 { 218 formatters_inited = true; 219 tr_formatter_mem_init (MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR); 220 tr_formatter_size_init (DISK_K,DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR); 221 tr_formatter_speed_init (SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR); 222 } 223 224 /* download dir */ 225 q = TR_KEY_download_dir; 226 if (tr_variantDictFindStr (settings, q, &str, &len)) 227 path = tr_strdup_printf ("%s/%*.*s", sandbox, (int)len, (int)len, str); 228 else 229 path = tr_buildPath (sandbox, "Downloads", NULL); 230 tr_mkdirp (path, 0700); 231 tr_variantDictAddStr (settings, q, path); 232 tr_free (path); 233 234 /* incomplete dir */ 235 q = TR_KEY_incomplete_dir; 236 if (tr_variantDictFindStr (settings, q, &str, &len)) 237 path = tr_strdup_printf ("%s/%*.*s", sandbox, (int)len, (int)len, str); 238 else 239 path = tr_buildPath (sandbox, "Incomplete", NULL); 240 tr_variantDictAddStr (settings, q, path); 241 tr_free (path); 242 243 path = tr_buildPath (sandbox, "blocklists", NULL); 244 tr_mkdirp (path, 0700); 245 tr_free (path); 246 247 q = TR_KEY_port_forwarding_enabled; 248 if (!tr_variantDictFind (settings, q)) 249 tr_variantDictAddBool (settings, q, false); 250 251 q = TR_KEY_dht_enabled; 252 if (!tr_variantDictFind (settings, q)) 253 tr_variantDictAddBool (settings, q, false); 254 255 q = TR_KEY_message_level; 256 if (!tr_variantDictFind (settings, q)) 257 tr_variantDictAddInt (settings, q, verbose ? TR_LOG_DEBUG : TR_LOG_ERROR); 258 259 session = tr_sessionInit ("libtransmission-test", sandbox, !verbose, settings); 260 261 tr_free (sandbox); 262 tr_variantFree (&local_settings); 263 return session; 264 } 265 194 266 void 195 libtransmission_test_session_init_formatters (void) 196 { 197 tr_formatter_mem_init (MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR); 198 tr_formatter_size_init (DISK_K,DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR); 199 tr_formatter_speed_init (SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR); 200 } 201 202 void 203 libtransmission_test_session_init_sandbox (void) 204 { 205 char * cwd; 206 207 /* create a sandbox for the test session */ 208 cwd = tr_getcwd (); 209 sandbox = tr_buildPath (cwd, "sandbox-XXXXXX", NULL); 210 tr_mkdtemp (sandbox); 211 downloadDir = tr_buildPath (sandbox, "Downloads", NULL); 212 tr_mkdirp (downloadDir, 0700); 213 blocklistDir = tr_buildPath (sandbox, "blocklists", NULL); 214 tr_mkdirp (blocklistDir, 0700); 215 216 /* cleanup locals*/ 217 tr_free (cwd); 218 } 219 220 void 221 libtransmission_test_session_init_session (void) 222 { 223 tr_variant dict; 224 225 /* libtransmission_test_session_init_sandbox() has to be called first */ 226 assert (sandbox != NULL); 227 assert (session == NULL); 228 229 /* init the session */ 230 tr_variantInitDict (&dict, 4); 231 tr_variantDictAddStr (&dict, TR_KEY_download_dir, downloadDir); 232 tr_variantDictAddBool (&dict, TR_KEY_port_forwarding_enabled, false); 233 tr_variantDictAddBool (&dict, TR_KEY_dht_enabled, false); 234 tr_variantDictAddInt (&dict, TR_KEY_message_level, verbose ? TR_LOG_DEBUG : TR_LOG_ERROR); 235 session = tr_sessionInit ("libtransmission-test", sandbox, !verbose, &dict); 236 237 /* cleanup locals*/ 238 tr_variantFree (&dict); 239 } 240 241 void 242 libtransmission_test_session_init (void) 243 { 244 libtransmission_test_session_init_formatters (); 245 libtransmission_test_session_init_sandbox (); 246 libtransmission_test_session_init_session (); 247 } 248 249 void 250 libtransmission_test_session_close (void) 251 { 267 libttest_session_close (tr_session * session) 268 { 269 char * path; 270 271 path = tr_strdup (tr_sessionGetConfigDir (session)); 252 272 tr_sessionClose (session); 253 273 tr_logFreeQueue (tr_logGetQueue ()); 254 274 session = NULL; 255 275 256 rm_rf (sandbox); 257 258 tr_free (blocklistDir); 259 blocklistDir = NULL; 260 261 tr_free (downloadDir); 262 downloadDir = NULL; 263 264 tr_free (sandbox); 265 sandbox = NULL; 276 rm_rf (path); 277 tr_free (path); 266 278 } 267 279 … … 271 283 272 284 tr_torrent * 273 libt ransmission_test_zero_torrent_init (void)285 libttest_zero_torrent_init (tr_session * session) 274 286 { 275 287 int err; … … 326 338 327 339 void 328 libt ransmission_test_zero_torrent_populate (tr_torrent * tor, bool complete)340 libttest_zero_torrent_populate (tr_torrent * tor, bool complete) 329 341 { 330 342 tr_file_index_t i; … … 385 397 bool done = false; 386 398 387 assert ( session != NULL);388 assert (!tr_amInEventThread ( session));399 assert (tor->session != NULL); 400 assert (!tr_amInEventThread (tor->session)); 389 401 390 402 tr_torrentVerify (tor, onVerifyDone, &done); -
trunk/libtransmission/libtransmission-test.h
r13913 r13916 67 67 } 68 68 69 extern tr_session * session; 70 extern char * sandbox; 71 extern char * downloadDir; 72 extern char * blocklistDir; 69 tr_session * libttest_session_init (struct tr_variant * settings); 70 void libttest_session_close (tr_session * session); 73 71 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 79 void libtransmission_test_session_close (void); 80 81 void libtransmission_test_zero_torrent_populate (tr_torrent * tor, bool complete); 82 tr_torrent * libtransmission_test_zero_torrent_init (void); 72 void libttest_zero_torrent_populate (tr_torrent * tor, bool complete); 73 tr_torrent * libttest_zero_torrent_init (tr_session * session); 83 74 84 75 void libttest_blockingTorrentVerify (tr_torrent * tor); -
trunk/libtransmission/move-test.c
r13913 r13916 43 43 } while (0) 44 44 45 struct test_incomplete_dir_ is_subdir_of_download_dir_data45 struct test_incomplete_dir_data 46 46 { 47 tr_session * session; 47 48 tr_torrent * tor; 48 49 tr_block_index_t block; … … 54 55 55 56 static void 56 test_incomplete_dir_ is_subdir_of_download_dir_threadfunc (void * vdata)57 test_incomplete_dir_threadfunc (void * vdata) 57 58 { 58 struct test_incomplete_dir_ is_subdir_of_download_dir_data * data = vdata;59 tr_cacheWriteBlock ( session->cache, data->tor, 0, data->offset, data->tor->blockSize, data->buf);59 struct test_incomplete_dir_data * data = vdata; 60 tr_cacheWriteBlock (data->session->cache, data->tor, 0, data->offset, data->tor->blockSize, data->buf); 60 61 tr_torrentGotBlock (data->tor, data->block); 61 62 data->done = true; 62 63 } 63 64 64 65 65 static int 66 test_incomplete_dir_i s_subdir_of_download_dir (void)66 test_incomplete_dir_impl (const char * incomplete_dir, const char * download_dir) 67 67 { 68 68 size_t i; 69 char * incomplete_dir;69 tr_session * session; 70 70 tr_torrent * tor; 71 71 tr_completeness completeness; 72 72 const tr_completeness completeness_unset = -1; 73 73 const time_t deadline = time(NULL) + 5; 74 tr_variant settings; 74 75 75 76 /* init the session */ 76 libtransmission_test_session_init (); 77 incomplete_dir = tr_buildPath (downloadDir, "incomplete", NULL); 78 tr_sessionSetIncompleteDir (session, incomplete_dir); 79 tr_sessionSetIncompleteDirEnabled (session, true); 77 tr_variantInitDict (&settings, 3); 78 tr_variantDictAddStr (&settings, TR_KEY_download_dir, download_dir); 79 tr_variantDictAddStr (&settings, TR_KEY_incomplete_dir, incomplete_dir); 80 tr_variantDictAddBool (&settings, TR_KEY_incomplete_dir_enabled, true); 81 session = libttest_session_init (&settings); 82 tr_variantFree (&settings); 83 download_dir = tr_sessionGetDownloadDir (session); 84 incomplete_dir = tr_sessionGetIncompleteDir (session); 80 85 81 86 /* init an incomplete torrent. 82 87 the test zero_torrent will be missing its first piece */ 83 tor = libt ransmission_test_zero_torrent_init ();84 libt ransmission_test_zero_torrent_populate (tor, false);88 tor = libttest_zero_torrent_init (session); 89 libttest_zero_torrent_populate (tor, false); 85 90 check (tr_torrentStat(tor)->leftUntilDone == tor->info.pieceSize); 86 91 check_file_location (tor, 0, tr_strdup_printf("%s/%s.part", incomplete_dir, tor->info.files[0].name)); … … 95 100 tr_block_index_t first, last; 96 101 char * zero_block = tr_new0 (char, tor->blockSize); 97 struct test_incomplete_dir_ is_subdir_of_download_dir_data data;102 struct test_incomplete_dir_data data; 98 103 104 data.session = session; 99 105 data.tor = tor; 100 106 data.pieceIndex = 0; … … 108 114 data.done = false; 109 115 data.offset = data.block * tor->blockSize; 110 tr_runInEventThread (session, test_incomplete_dir_ is_subdir_of_download_dir_threadfunc, &data);116 tr_runInEventThread (session, test_incomplete_dir_threadfunc, &data); 111 117 do { tr_wait_msec(50); } while (!data.done); 112 118 } … … 124 130 check_int_eq (TR_SEED, completeness); 125 131 for (i=0; i<tor->info.fileCount; ++i) 126 check_file_location (tor, i, tr_buildPath (download Dir, tor->info.files[i].name, NULL));132 check_file_location (tor, i, tr_buildPath (download_dir, tor->info.files[i].name, NULL)); 127 133 128 134 /* cleanup */ 129 135 tr_torrentRemove (tor, true, remove); 130 libtransmission_test_session_close (); 131 tr_free (incomplete_dir); 136 libttest_session_close (session); 132 137 return 0; 133 138 } 134 139 140 static int 141 test_incomplete_dir (void) 142 { 143 int rv; 144 145 /* test what happens when incompleteDir is a subdir of downloadDir*/ 146 if ((rv = test_incomplete_dir_impl ("Downloads/Incomplete", "Downloads"))) 147 return rv; 148 149 /* test what happens when downloadDir is a subdir of incompleteDir */ 150 if ((rv = test_incomplete_dir_impl ("Downloads", "Downloads/Complete"))) 151 return rv; 152 153 /* test what happens when downloadDir and incompleteDir are siblings */ 154 if ((rv = test_incomplete_dir_impl ("Incomplete", "Downloads"))) 155 return rv; 156 157 return 0; 158 } 135 159 136 160 /*** … … 141 165 main (void) 142 166 { 143 const testFunc tests[] = { test_incomplete_dir _is_subdir_of_download_dir};167 const testFunc tests[] = { test_incomplete_dir }; 144 168 145 169 return runTests (tests, NUM_TESTS (tests)); -
trunk/libtransmission/rename-test.c
r13913 r13916 20 20 **** 21 21 ***/ 22 23 static tr_session * session = NULL; 22 24 23 25 #define check_have_none(tor, totalSize) \ … … 127 129 assert (metainfo != NULL); 128 130 assert (metainfo_len > 0); 129 assert (session != NULL);130 131 tr_ctorSetMetainfo (ctor, (uint8_t*)metainfo, metainfo_len); 131 132 tr_ctorSetPaused (ctor, TR_FORCE, true); … … 486 487 ***/ 487 488 488 tor = libt ransmission_test_zero_torrent_init ();489 tor = libttest_zero_torrent_init (session); 489 490 check_int_eq (totalSize, tor->info.totalSize); 490 491 check_int_eq (pieceSize, tor->info.pieceSize); … … 494 495 check_streq ("files-filled-with-zeroes/512", tor->info.files[2].name); 495 496 496 libt ransmission_test_zero_torrent_populate (tor, false);497 libttest_zero_torrent_populate (tor, false); 497 498 fst = tr_torrentFiles (tor, NULL); 498 499 check_int_eq (length[0] - pieceSize, fst[0].bytesCompleted); … … 544 545 test_partial_file }; 545 546 546 libtransmission_test_session_init ();547 session = libttest_session_init (NULL); 547 548 ret = runTests (tests, NUM_TESTS (tests)); 548 libt ransmission_test_session_close ();549 libttest_session_close (session); 549 550 550 551 return ret; -
trunk/libtransmission/rpc-test.c
r13814 r13916 77 77 test_session_get_and_set (void) 78 78 { 79 tr_session * session; 79 80 const char * json; 80 81 tr_variant response; 81 82 tr_variant * args; 82 tr_torrent * tor = libtransmission_test_zero_torrent_init (); 83 tr_torrent * tor; 84 85 session = libttest_session_init (NULL); 86 tor= libttest_zero_torrent_init (session); 83 87 check (tor != NULL); 84 88 … … 142 146 /* cleanup */ 143 147 tr_torrentRemove (tor, false, NULL); 148 libttest_session_close (session); 144 149 return 0; 145 150 } … … 152 157 main (void) 153 158 { 154 int ret;155 159 const testFunc tests[] = { test_list, 156 160 test_session_get_and_set }; 157 161 158 libtransmission_test_session_init (); 159 ret = runTests (tests, NUM_TESTS (tests)); 160 libtransmission_test_session_close (); 161 162 return ret; 162 return runTests (tests, NUM_TESTS (tests)); 163 163 }
Note: See TracChangeset
for help on using the changeset viewer.