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: libtransmission-test.c 14382 2014-12-13 15:22:39Z mikedld $ |
---|
8 | */ |
---|
9 | |
---|
10 | #include <assert.h> |
---|
11 | #include <errno.h> |
---|
12 | #include <stdio.h> |
---|
13 | #include <stdlib.h> /* mkstemp() */ |
---|
14 | #include <string.h> /* strcmp() */ |
---|
15 | |
---|
16 | #ifndef _WIN32 |
---|
17 | #include <unistd.h> /* sync() */ |
---|
18 | #endif |
---|
19 | |
---|
20 | #include "transmission.h" |
---|
21 | #include "crypto-utils.h" |
---|
22 | #include "error.h" |
---|
23 | #include "file.h" |
---|
24 | #include "platform.h" /* TR_PATH_DELIMETER */ |
---|
25 | #include "torrent.h" |
---|
26 | #include "trevent.h" |
---|
27 | #include "variant.h" |
---|
28 | #include "libtransmission-test.h" |
---|
29 | |
---|
30 | bool verbose = false; |
---|
31 | |
---|
32 | int current_test = 0; |
---|
33 | |
---|
34 | bool |
---|
35 | should_print (bool pass) |
---|
36 | { |
---|
37 | if (!pass) |
---|
38 | return true; |
---|
39 | |
---|
40 | if (verbose) |
---|
41 | return true; |
---|
42 | |
---|
43 | return false; |
---|
44 | #ifdef VERBOSE |
---|
45 | return true; |
---|
46 | #else |
---|
47 | return false; |
---|
48 | #endif |
---|
49 | } |
---|
50 | |
---|
51 | bool |
---|
52 | check_condition_impl (const char * file, int line, bool condition) |
---|
53 | { |
---|
54 | const bool pass = condition; |
---|
55 | |
---|
56 | if (should_print (pass)) |
---|
57 | fprintf (stderr, "%s %s:%d\n", pass?"PASS":"FAIL", file, line); |
---|
58 | |
---|
59 | return pass; |
---|
60 | } |
---|
61 | |
---|
62 | bool |
---|
63 | check_streq_impl (const char * file, int line, const char * expected, const char * actual) |
---|
64 | { |
---|
65 | const bool pass = !tr_strcmp0 (expected, actual); |
---|
66 | |
---|
67 | if (should_print (pass)) { |
---|
68 | if (pass) |
---|
69 | fprintf (stderr, "PASS %s:%d\n", file, line); |
---|
70 | else |
---|
71 | fprintf (stderr, "FAIL %s:%d, expected \"%s\", got \"%s\"\n", file, line, expected?expected:" (null)", actual?actual:" (null)"); |
---|
72 | } |
---|
73 | |
---|
74 | return pass; |
---|
75 | } |
---|
76 | |
---|
77 | bool |
---|
78 | check_int_eq_impl (const char * file, int line, int64_t expected, int64_t actual) |
---|
79 | { |
---|
80 | const bool pass = expected == actual; |
---|
81 | |
---|
82 | if (should_print (pass)) { |
---|
83 | if (pass) |
---|
84 | fprintf (stderr, "PASS %s:%d\n", file, line); |
---|
85 | else |
---|
86 | fprintf (stderr, "FAIL %s:%d, expected \"%"PRId64"\", got \"%"PRId64"\"\n", file, line, expected, actual); |
---|
87 | } |
---|
88 | |
---|
89 | return pass; |
---|
90 | } |
---|
91 | |
---|
92 | bool |
---|
93 | check_ptr_eq_impl (const char * file, int line, const void * expected, const void * actual) |
---|
94 | { |
---|
95 | const bool pass = expected == actual; |
---|
96 | |
---|
97 | if (should_print (pass)) { |
---|
98 | if (pass) |
---|
99 | fprintf (stderr, "PASS %s:%d\n", file, line); |
---|
100 | else |
---|
101 | fprintf (stderr, "FAIL %s:%d, expected \"%p\", got \"%p\"\n", file, line, expected, actual); |
---|
102 | } |
---|
103 | |
---|
104 | return pass; |
---|
105 | } |
---|
106 | |
---|
107 | int |
---|
108 | runTests (const testFunc * const tests, int numTests) |
---|
109 | { |
---|
110 | int i; |
---|
111 | int ret; |
---|
112 | |
---|
113 | (void) current_test; /* Use test even if we don't have any tests to run */ |
---|
114 | |
---|
115 | for (i=0; i<numTests; i++) |
---|
116 | if ((ret = (*tests[i])())) |
---|
117 | return ret; |
---|
118 | |
---|
119 | return 0; /* All tests passed */ |
---|
120 | } |
---|
121 | |
---|
122 | /*** |
---|
123 | **** |
---|
124 | ***/ |
---|
125 | |
---|
126 | static char* |
---|
127 | tr_getcwd (void) |
---|
128 | { |
---|
129 | char * result; |
---|
130 | tr_error * error = NULL; |
---|
131 | |
---|
132 | result = tr_sys_dir_get_current (&error); |
---|
133 | |
---|
134 | if (result == NULL) |
---|
135 | { |
---|
136 | fprintf (stderr, "getcwd error: \"%s\"", error->message); |
---|
137 | tr_error_free (error); |
---|
138 | result = tr_strdup (""); |
---|
139 | } |
---|
140 | |
---|
141 | return result; |
---|
142 | } |
---|
143 | |
---|
144 | char * |
---|
145 | libtest_sandbox_create (void) |
---|
146 | { |
---|
147 | char * path = tr_getcwd (); |
---|
148 | char * sandbox = tr_buildPath (path, "sandbox-XXXXXX", NULL); |
---|
149 | tr_free (path); |
---|
150 | tr_sys_dir_create_temp (sandbox, NULL); |
---|
151 | return sandbox; |
---|
152 | } |
---|
153 | |
---|
154 | static void |
---|
155 | rm_rf (const char * killme) |
---|
156 | { |
---|
157 | tr_sys_path_info info; |
---|
158 | |
---|
159 | if (tr_sys_path_get_info (killme, 0, &info, NULL)) |
---|
160 | { |
---|
161 | tr_sys_dir_t odir; |
---|
162 | |
---|
163 | if (info.type == TR_SYS_PATH_IS_DIRECTORY && |
---|
164 | (odir = tr_sys_dir_open (killme, NULL)) != TR_BAD_SYS_DIR) |
---|
165 | { |
---|
166 | const char * name; |
---|
167 | while ((name = tr_sys_dir_read_name (odir, NULL)) != NULL) |
---|
168 | { |
---|
169 | if (strcmp (name, ".") != 0 && strcmp (name, "..") != 0) |
---|
170 | { |
---|
171 | char * tmp = tr_buildPath (killme, name, NULL); |
---|
172 | rm_rf (tmp); |
---|
173 | tr_free (tmp); |
---|
174 | } |
---|
175 | } |
---|
176 | tr_sys_dir_close (odir, NULL); |
---|
177 | } |
---|
178 | |
---|
179 | if (verbose) |
---|
180 | fprintf (stderr, "cleanup: removing %s\n", killme); |
---|
181 | |
---|
182 | tr_sys_path_remove (killme, NULL); |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | void |
---|
187 | libtest_sandbox_destroy (const char * sandbox) |
---|
188 | { |
---|
189 | rm_rf (sandbox); |
---|
190 | } |
---|
191 | |
---|
192 | /*** |
---|
193 | **** |
---|
194 | ***/ |
---|
195 | |
---|
196 | #define MEM_K 1024 |
---|
197 | #define MEM_B_STR "B" |
---|
198 | #define MEM_K_STR "KiB" |
---|
199 | #define MEM_M_STR "MiB" |
---|
200 | #define MEM_G_STR "GiB" |
---|
201 | #define MEM_T_STR "TiB" |
---|
202 | |
---|
203 | #define DISK_K 1000 |
---|
204 | #define DISK_B_STR "B" |
---|
205 | #define DISK_K_STR "kB" |
---|
206 | #define DISK_M_STR "MB" |
---|
207 | #define DISK_G_STR "GB" |
---|
208 | #define DISK_T_STR "TB" |
---|
209 | |
---|
210 | #define SPEED_K 1000 |
---|
211 | #define SPEED_B_STR "B/s" |
---|
212 | #define SPEED_K_STR "kB/s" |
---|
213 | #define SPEED_M_STR "MB/s" |
---|
214 | #define SPEED_G_STR "GB/s" |
---|
215 | #define SPEED_T_STR "TB/s" |
---|
216 | |
---|
217 | tr_session * |
---|
218 | libttest_session_init (tr_variant * settings) |
---|
219 | { |
---|
220 | size_t len; |
---|
221 | const char * str; |
---|
222 | char * sandbox; |
---|
223 | char * path; |
---|
224 | tr_quark q; |
---|
225 | static bool formatters_inited = false; |
---|
226 | tr_session * session; |
---|
227 | tr_variant local_settings; |
---|
228 | |
---|
229 | tr_variantInitDict (&local_settings, 10); |
---|
230 | |
---|
231 | if (settings == NULL) |
---|
232 | settings = &local_settings; |
---|
233 | |
---|
234 | sandbox = libtest_sandbox_create (); |
---|
235 | |
---|
236 | if (!formatters_inited) |
---|
237 | { |
---|
238 | formatters_inited = true; |
---|
239 | tr_formatter_mem_init (MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR); |
---|
240 | tr_formatter_size_init (DISK_K,DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR); |
---|
241 | tr_formatter_speed_init (SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR); |
---|
242 | } |
---|
243 | |
---|
244 | /* download dir */ |
---|
245 | q = TR_KEY_download_dir; |
---|
246 | if (tr_variantDictFindStr (settings, q, &str, &len)) |
---|
247 | path = tr_strdup_printf ("%s/%*.*s", sandbox, (int)len, (int)len, str); |
---|
248 | else |
---|
249 | path = tr_buildPath (sandbox, "Downloads", NULL); |
---|
250 | tr_sys_dir_create (path, TR_SYS_DIR_CREATE_PARENTS, 0700, NULL); |
---|
251 | tr_variantDictAddStr (settings, q, path); |
---|
252 | tr_free (path); |
---|
253 | |
---|
254 | /* incomplete dir */ |
---|
255 | q = TR_KEY_incomplete_dir; |
---|
256 | if (tr_variantDictFindStr (settings, q, &str, &len)) |
---|
257 | path = tr_strdup_printf ("%s/%*.*s", sandbox, (int)len, (int)len, str); |
---|
258 | else |
---|
259 | path = tr_buildPath (sandbox, "Incomplete", NULL); |
---|
260 | tr_variantDictAddStr (settings, q, path); |
---|
261 | tr_free (path); |
---|
262 | |
---|
263 | path = tr_buildPath (sandbox, "blocklists", NULL); |
---|
264 | tr_sys_dir_create (path, TR_SYS_DIR_CREATE_PARENTS, 0700, NULL); |
---|
265 | tr_free (path); |
---|
266 | |
---|
267 | q = TR_KEY_port_forwarding_enabled; |
---|
268 | if (!tr_variantDictFind (settings, q)) |
---|
269 | tr_variantDictAddBool (settings, q, false); |
---|
270 | |
---|
271 | q = TR_KEY_dht_enabled; |
---|
272 | if (!tr_variantDictFind (settings, q)) |
---|
273 | tr_variantDictAddBool (settings, q, false); |
---|
274 | |
---|
275 | q = TR_KEY_message_level; |
---|
276 | if (!tr_variantDictFind (settings, q)) |
---|
277 | tr_variantDictAddInt (settings, q, verbose ? TR_LOG_DEBUG : TR_LOG_ERROR); |
---|
278 | |
---|
279 | session = tr_sessionInit ("libtransmission-test", sandbox, !verbose, settings); |
---|
280 | |
---|
281 | tr_free (sandbox); |
---|
282 | tr_variantFree (&local_settings); |
---|
283 | return session; |
---|
284 | } |
---|
285 | |
---|
286 | void |
---|
287 | libttest_session_close (tr_session * session) |
---|
288 | { |
---|
289 | char * sandbox; |
---|
290 | |
---|
291 | sandbox = tr_strdup (tr_sessionGetConfigDir (session)); |
---|
292 | tr_sessionClose (session); |
---|
293 | tr_logFreeQueue (tr_logGetQueue ()); |
---|
294 | session = NULL; |
---|
295 | |
---|
296 | libtest_sandbox_destroy (sandbox); |
---|
297 | tr_free (sandbox); |
---|
298 | } |
---|
299 | |
---|
300 | /*** |
---|
301 | **** |
---|
302 | ***/ |
---|
303 | |
---|
304 | tr_torrent * |
---|
305 | libttest_zero_torrent_init (tr_session * session) |
---|
306 | { |
---|
307 | int err; |
---|
308 | size_t metainfo_len; |
---|
309 | char * metainfo; |
---|
310 | const char * metainfo_base64; |
---|
311 | tr_torrent * tor; |
---|
312 | tr_ctor * ctor; |
---|
313 | |
---|
314 | /* |
---|
315 | 1048576 files-filled-with-zeroes/1048576 |
---|
316 | 4096 files-filled-with-zeroes/4096 |
---|
317 | 512 files-filled-with-zeroes/512 |
---|
318 | */ |
---|
319 | metainfo_base64 = |
---|
320 | "ZDg6YW5ub3VuY2UzMTpodHRwOi8vd3d3LmV4YW1wbGUuY29tL2Fubm91bmNlMTA6Y3JlYXRlZCBi" |
---|
321 | "eTI1OlRyYW5zbWlzc2lvbi8yLjYxICgxMzQwNykxMzpjcmVhdGlvbiBkYXRlaTEzNTg3MDQwNzVl" |
---|
322 | "ODplbmNvZGluZzU6VVRGLTg0OmluZm9kNTpmaWxlc2xkNjpsZW5ndGhpMTA0ODU3NmU0OnBhdGhs" |
---|
323 | "NzoxMDQ4NTc2ZWVkNjpsZW5ndGhpNDA5NmU0OnBhdGhsNDo0MDk2ZWVkNjpsZW5ndGhpNTEyZTQ6" |
---|
324 | "cGF0aGwzOjUxMmVlZTQ6bmFtZTI0OmZpbGVzLWZpbGxlZC13aXRoLXplcm9lczEyOnBpZWNlIGxl" |
---|
325 | "bmd0aGkzMjc2OGU2OnBpZWNlczY2MDpRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJtGExUv1726aj" |
---|
326 | "/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJtGExUv17" |
---|
327 | "26aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJtGEx" |
---|
328 | "Uv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJ" |
---|
329 | "tGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GI" |
---|
330 | "QxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZC" |
---|
331 | "S1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8K" |
---|
332 | "T9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9um" |
---|
333 | "o/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9" |
---|
334 | "e9umo/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRh" |
---|
335 | "MVL9e9umo/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMY" |
---|
336 | "SbRhMVL9e9umo/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLOlf5A+Tz30nMBVuNM2hpV3wg/103" |
---|
337 | "OnByaXZhdGVpMGVlZQ=="; |
---|
338 | |
---|
339 | /* create the torrent ctor */ |
---|
340 | metainfo = tr_base64_decode_str (metainfo_base64, &metainfo_len); |
---|
341 | assert (metainfo != NULL); |
---|
342 | assert (metainfo_len > 0); |
---|
343 | assert (session != NULL); |
---|
344 | ctor = tr_ctorNew (session); |
---|
345 | tr_ctorSetMetainfo (ctor, (uint8_t*)metainfo, metainfo_len); |
---|
346 | tr_ctorSetPaused (ctor, TR_FORCE, true); |
---|
347 | |
---|
348 | /* create the torrent */ |
---|
349 | err = 0; |
---|
350 | tor = tr_torrentNew (ctor, &err, NULL); |
---|
351 | assert (!err); |
---|
352 | |
---|
353 | /* cleanup */ |
---|
354 | tr_free (metainfo); |
---|
355 | tr_ctorFree (ctor); |
---|
356 | return tor; |
---|
357 | } |
---|
358 | |
---|
359 | void |
---|
360 | libttest_zero_torrent_populate (tr_torrent * tor, bool complete) |
---|
361 | { |
---|
362 | tr_file_index_t i; |
---|
363 | |
---|
364 | for (i=0; i<tor->info.fileCount; ++i) |
---|
365 | { |
---|
366 | int err; |
---|
367 | uint64_t j; |
---|
368 | tr_sys_file_t fd; |
---|
369 | char * path; |
---|
370 | char * dirname; |
---|
371 | const tr_file * file = &tor->info.files[i]; |
---|
372 | |
---|
373 | if (!complete && (i==0)) |
---|
374 | path = tr_strdup_printf ("%s%c%s.part", tor->currentDir, TR_PATH_DELIMITER, file->name); |
---|
375 | else |
---|
376 | path = tr_strdup_printf ("%s%c%s", tor->currentDir, TR_PATH_DELIMITER, file->name); |
---|
377 | dirname = tr_sys_path_dirname (path, NULL); |
---|
378 | tr_sys_dir_create (dirname, TR_SYS_DIR_CREATE_PARENTS, 0700, NULL); |
---|
379 | fd = tr_sys_file_open (path, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0600, NULL); |
---|
380 | for (j=0; j<file->length; ++j) |
---|
381 | tr_sys_file_write (fd, ((!complete) && (i==0) && (j<tor->info.pieceSize)) ? "\1" : "\0", 1, NULL, NULL); |
---|
382 | tr_sys_file_close (fd, NULL); |
---|
383 | |
---|
384 | tr_free (dirname); |
---|
385 | tr_free (path); |
---|
386 | |
---|
387 | path = tr_torrentFindFile (tor, i); |
---|
388 | assert (path != NULL); |
---|
389 | err = errno; |
---|
390 | assert (tr_sys_path_exists (path, NULL)); |
---|
391 | errno = err; |
---|
392 | tr_free (path); |
---|
393 | } |
---|
394 | |
---|
395 | libttest_sync (); |
---|
396 | libttest_blockingTorrentVerify (tor); |
---|
397 | |
---|
398 | if (complete) |
---|
399 | assert (tr_torrentStat(tor)->leftUntilDone == 0); |
---|
400 | else |
---|
401 | assert (tr_torrentStat(tor)->leftUntilDone == tor->info.pieceSize); |
---|
402 | } |
---|
403 | |
---|
404 | /*** |
---|
405 | **** |
---|
406 | ***/ |
---|
407 | |
---|
408 | static void |
---|
409 | onVerifyDone (tr_torrent * tor UNUSED, bool aborted UNUSED, void * done) |
---|
410 | { |
---|
411 | *(bool*)done = true; |
---|
412 | } |
---|
413 | |
---|
414 | void |
---|
415 | libttest_blockingTorrentVerify (tr_torrent * tor) |
---|
416 | { |
---|
417 | bool done = false; |
---|
418 | |
---|
419 | assert (tor->session != NULL); |
---|
420 | assert (!tr_amInEventThread (tor->session)); |
---|
421 | |
---|
422 | tr_torrentVerify (tor, onVerifyDone, &done); |
---|
423 | while (!done) |
---|
424 | tr_wait_msec (10); |
---|
425 | } |
---|
426 | |
---|
427 | static void |
---|
428 | build_parent_dir (const char* path) |
---|
429 | { |
---|
430 | char * dir; |
---|
431 | tr_error * error = NULL; |
---|
432 | const int tmperr = errno; |
---|
433 | |
---|
434 | dir = tr_sys_path_dirname (path, NULL); |
---|
435 | tr_sys_dir_create (dir, TR_SYS_DIR_CREATE_PARENTS, 0700, &error); |
---|
436 | assert (error == NULL); |
---|
437 | tr_free (dir); |
---|
438 | |
---|
439 | errno = tmperr; |
---|
440 | } |
---|
441 | |
---|
442 | void |
---|
443 | libtest_create_file_with_contents (const char* path, const void* payload, size_t n) |
---|
444 | { |
---|
445 | tr_sys_file_t fd; |
---|
446 | const int tmperr = errno; |
---|
447 | |
---|
448 | build_parent_dir (path); |
---|
449 | |
---|
450 | fd = tr_sys_file_open (path, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0600, NULL); |
---|
451 | tr_sys_file_write (fd, payload, n, NULL, NULL); |
---|
452 | tr_sys_file_close (fd, NULL); |
---|
453 | |
---|
454 | libttest_sync (); |
---|
455 | |
---|
456 | errno = tmperr; |
---|
457 | } |
---|
458 | |
---|
459 | void |
---|
460 | libtest_create_file_with_string_contents (const char * path, const char* str) |
---|
461 | { |
---|
462 | libtest_create_file_with_contents (path, str, strlen(str)); |
---|
463 | } |
---|
464 | |
---|
465 | void |
---|
466 | libtest_create_tmpfile_with_contents (char* tmpl, const void* payload, size_t n) |
---|
467 | { |
---|
468 | tr_sys_file_t fd; |
---|
469 | const int tmperr = errno; |
---|
470 | uint64_t n_left = n; |
---|
471 | tr_error * error = NULL; |
---|
472 | |
---|
473 | build_parent_dir (tmpl); |
---|
474 | |
---|
475 | fd = tr_sys_file_open_temp (tmpl, NULL); |
---|
476 | while (n_left > 0) |
---|
477 | { |
---|
478 | uint64_t n; |
---|
479 | if (!tr_sys_file_write (fd, payload, n_left, &n, &error)) |
---|
480 | { |
---|
481 | fprintf (stderr, "Error writing '%s': %s\n", tmpl, error->message); |
---|
482 | tr_error_free (error); |
---|
483 | break; |
---|
484 | } |
---|
485 | n_left -= n; |
---|
486 | } |
---|
487 | tr_sys_file_close (fd, NULL); |
---|
488 | |
---|
489 | libttest_sync (); |
---|
490 | |
---|
491 | errno = tmperr; |
---|
492 | } |
---|
493 | |
---|
494 | void |
---|
495 | libttest_sync (void) |
---|
496 | { |
---|
497 | #ifndef _WIN32 |
---|
498 | sync (); |
---|
499 | #endif |
---|
500 | } |
---|