2014-01-19 01:09:44 +00:00
|
|
|
/*
|
2017-11-27 22:22:44 +00:00
|
|
|
* This file Copyright (C) 2013-2017 Mnemosyne LLC
|
2014-01-19 01:09:44 +00:00
|
|
|
*
|
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-12-13 15:22:39 +00:00
|
|
|
#include <errno.h>
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <stdio.h>
|
2014-06-08 20:01:10 +00:00
|
|
|
#include <stdlib.h> /* mkstemp() */
|
2014-12-13 15:22:39 +00:00
|
|
|
#include <string.h> /* strcmp() */
|
2014-06-08 20:01:10 +00:00
|
|
|
|
2014-12-13 15:22:39 +00:00
|
|
|
#ifndef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <unistd.h> /* sync() */
|
2014-12-13 15:22:39 +00:00
|
|
|
#endif
|
2012-12-14 04:34:42 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
2014-12-04 19:58:34 +00:00
|
|
|
#include "crypto-utils.h"
|
2014-07-28 04:13:38 +00:00
|
|
|
#include "error.h"
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "file.h"
|
2013-01-31 05:11:32 +00:00
|
|
|
#include "platform.h" /* TR_PATH_DELIMETER */
|
2013-01-26 23:08:51 +00:00
|
|
|
#include "torrent.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2013-01-31 21:58:25 +00:00
|
|
|
#include "trevent.h"
|
2014-12-13 15:22:39 +00:00
|
|
|
#include "variant.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "libtransmission-test.h"
|
|
|
|
|
|
|
|
bool verbose = false;
|
|
|
|
|
|
|
|
int current_test = 0;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool should_print(bool pass)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!pass)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2012-12-14 04:34:42 +00:00
|
|
|
#ifdef VERBOSE
|
2017-04-19 12:04:45 +00:00
|
|
|
return true;
|
2012-12-14 04:34:42 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2012-12-14 04:34:42 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-05-30 16:21:14 +00:00
|
|
|
bool libtest_check(char const* file, int line, bool pass, bool condition, char const* condition_str)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-05-30 16:21:14 +00:00
|
|
|
if (should_print(pass))
|
|
|
|
{
|
2019-02-10 11:05:16 +00:00
|
|
|
fprintf(stderr, "%s %s:%d: %s (%s)\n", pass ? "PASS" : "FAIL", file, line, condition_str, condition ? "true" : "false");
|
2017-05-30 16:21:14 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-05-30 16:21:14 +00:00
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool libtest_check_bool(char const* file, int line, bool pass, bool lhs, bool rhs, char const* lhs_str, char const* op_str,
|
|
|
|
char const* rhs_str)
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (should_print(pass))
|
|
|
|
{
|
2017-05-30 16:21:14 +00:00
|
|
|
fprintf(stderr, "%s %s:%d: %s %s %s (%s %s %s)\n", pass ? "PASS" : "FAIL", file, line, lhs_str, op_str, rhs_str,
|
|
|
|
lhs ? "true" : "false", op_str, rhs ? "true" : "false");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return pass;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 19:48:02 +00:00
|
|
|
bool libtest_check_str(char const* file, int line, bool pass, char const* lhs, char const* rhs, char const* lhs_str,
|
|
|
|
char const* op_str, char const* rhs_str)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (should_print(pass))
|
|
|
|
{
|
2017-05-29 19:48:02 +00:00
|
|
|
char const* const lhs_quote = lhs != NULL ? "\"" : "";
|
|
|
|
char const* const rhs_quote = rhs != NULL ? "\"" : "";
|
|
|
|
|
|
|
|
fprintf(stderr, "%s %s:%d: %s %s %s (%s%s%s %s %s%s%s)\n", pass ? "PASS" : "FAIL", file, line, lhs_str, op_str, rhs_str,
|
|
|
|
lhs_quote, lhs != NULL ? lhs : "NULL", lhs_quote, op_str, rhs_quote, rhs != NULL ? rhs : "NULL", rhs_quote);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return pass;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 17:09:19 +00:00
|
|
|
static void print_mem(FILE* stream, void const* data, size_t size)
|
|
|
|
{
|
|
|
|
if (data == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stream, "NULL");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
fprintf(stream, "(no bytes)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t const* byte_data = data;
|
|
|
|
|
|
|
|
fprintf(stream, "x'");
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
fprintf(stream, "%02x", (unsigned int)byte_data[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stream, "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool libtest_check_mem(char const* file, int line, bool pass, void const* lhs, void const* rhs, size_t size,
|
|
|
|
char const* lhs_str, char const* op_str, char const* rhs_str)
|
|
|
|
{
|
|
|
|
if (should_print(pass))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s %s:%d: %s %s %s (", pass ? "PASS" : "FAIL", file, line, lhs_str, op_str, rhs_str);
|
|
|
|
print_mem(stderr, lhs, size);
|
|
|
|
fprintf(stderr, " %s ", op_str);
|
|
|
|
print_mem(stderr, rhs, size);
|
|
|
|
fprintf(stderr, ")\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
2017-05-29 20:22:36 +00:00
|
|
|
bool libtest_check_int(char const* file, int line, bool pass, intmax_t lhs, intmax_t rhs, char const* lhs_str,
|
|
|
|
char const* op_str, char const* rhs_str)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (should_print(pass))
|
|
|
|
{
|
2017-05-29 20:22:36 +00:00
|
|
|
fprintf(stderr, "%s %s:%d: %s %s %s (%jd %s %jd)\n", pass ? "PASS" : "FAIL", file, line, lhs_str, op_str, rhs_str, lhs,
|
|
|
|
op_str, rhs);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return pass;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 20:39:51 +00:00
|
|
|
bool libtest_check_uint(char const* file, int line, bool pass, uintmax_t lhs, uintmax_t rhs, char const* lhs_str,
|
|
|
|
char const* op_str, char const* rhs_str)
|
2016-03-29 03:04:54 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (should_print(pass))
|
|
|
|
{
|
2017-05-29 20:39:51 +00:00
|
|
|
fprintf(stderr, "%s %s:%d: %s %s %s (%ju %s %ju)\n", pass ? "PASS" : "FAIL", file, line, lhs_str, op_str, rhs_str, lhs,
|
|
|
|
op_str, rhs);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2016-03-29 03:04:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return pass;
|
2016-03-29 03:04:54 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 14:42:05 +00:00
|
|
|
bool libtest_check_ptr(char const* file, int line, bool pass, void const* lhs, void const* rhs, char const* lhs_str,
|
|
|
|
char const* op_str, char const* rhs_str)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (should_print(pass))
|
|
|
|
{
|
2017-05-30 14:42:05 +00:00
|
|
|
fprintf(stderr, "%s %s:%d: %s %s %s (%p %s %p)\n", pass ? "PASS" : "FAIL", file, line, lhs_str, op_str, rhs_str, lhs,
|
|
|
|
op_str, rhs);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return pass;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int runTests(testFunc const* const tests, int numTests)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-05-29 17:05:24 +00:00
|
|
|
int ret = 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
(void)current_test; /* Use test even if we don't have any tests to run */
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < numTests; i++)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-29 17:05:24 +00:00
|
|
|
if ((*tests[i])() != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-29 17:05:24 +00:00
|
|
|
++ret;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-05-29 17:05:24 +00:00
|
|
|
return ret;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2013-01-20 04:41:38 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static char* tr_getcwd(void)
|
2013-01-20 04:41:38 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* result;
|
|
|
|
tr_error* error = NULL;
|
2013-01-20 04:41:38 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
result = tr_sys_dir_get_current(&error);
|
2013-01-20 04:41:38 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (result == NULL)
|
2013-01-20 04:41:38 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "getcwd error: \"%s\"", error->message);
|
|
|
|
tr_error_free(error);
|
|
|
|
result = tr_strdup("");
|
2013-01-20 04:41:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return result;
|
2013-01-20 04:41:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
char* libtest_sandbox_create(void)
|
2014-06-08 19:09:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* path = tr_getcwd();
|
|
|
|
char* sandbox = tr_buildPath(path, "sandbox-XXXXXX", NULL);
|
|
|
|
tr_free(path);
|
|
|
|
tr_sys_dir_create_temp(sandbox, NULL);
|
2017-11-27 22:22:44 +00:00
|
|
|
return tr_sys_path_native_separators(sandbox);
|
2014-06-08 19:09:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void rm_rf(char const* killme)
|
2013-01-20 04:41:38 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_path_info info;
|
2013-01-20 04:41:38 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_sys_path_get_info(killme, 0, &info, NULL))
|
2013-01-20 04:41:38 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_dir_t odir;
|
2013-01-20 04:41:38 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (info.type == TR_SYS_PATH_IS_DIRECTORY && (odir = tr_sys_dir_open(killme, NULL)) != TR_BAD_SYS_DIR)
|
2013-01-20 04:41:38 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* name;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
while ((name = tr_sys_dir_read_name(odir, NULL)) != NULL)
|
2013-01-20 04:41:38 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
|
2013-01-20 04:41:38 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* tmp = tr_buildPath(killme, name, NULL);
|
|
|
|
rm_rf(tmp);
|
|
|
|
tr_free(tmp);
|
2013-01-20 04:41:38 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_sys_dir_close(odir, NULL);
|
2013-01-20 04:41:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "cleanup: removing %s\n", killme);
|
|
|
|
}
|
2013-01-20 04:41:38 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_path_remove(killme, NULL);
|
2013-01-20 04:41:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void libtest_sandbox_destroy(char const* sandbox)
|
2014-06-08 19:09:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
rm_rf(sandbox);
|
2014-06-08 19:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2013-01-21 00:00:00 +00:00
|
|
|
#define MEM_K 1024
|
|
|
|
#define MEM_K_STR "KiB"
|
|
|
|
#define MEM_M_STR "MiB"
|
|
|
|
#define MEM_G_STR "GiB"
|
|
|
|
#define MEM_T_STR "TiB"
|
|
|
|
|
|
|
|
#define DISK_K 1000
|
|
|
|
#define DISK_K_STR "kB"
|
|
|
|
#define DISK_M_STR "MB"
|
|
|
|
#define DISK_G_STR "GB"
|
|
|
|
#define DISK_T_STR "TB"
|
|
|
|
|
|
|
|
#define SPEED_K 1000
|
|
|
|
#define SPEED_K_STR "kB/s"
|
|
|
|
#define SPEED_M_STR "MB/s"
|
|
|
|
#define SPEED_G_STR "GB/s"
|
|
|
|
#define SPEED_T_STR "TB/s"
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session* libttest_session_init(tr_variant* settings)
|
2013-01-20 04:41:38 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t len;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
2017-04-19 12:04:45 +00:00
|
|
|
char* sandbox;
|
|
|
|
char* path;
|
|
|
|
tr_quark q;
|
|
|
|
static bool formatters_inited = false;
|
|
|
|
tr_session* session;
|
|
|
|
tr_variant local_settings;
|
|
|
|
|
|
|
|
tr_variantInitDict(&local_settings, 10);
|
|
|
|
|
|
|
|
if (settings == NULL)
|
|
|
|
{
|
|
|
|
settings = &local_settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
sandbox = libtest_sandbox_create();
|
|
|
|
|
|
|
|
if (!formatters_inited)
|
|
|
|
{
|
|
|
|
formatters_inited = true;
|
|
|
|
tr_formatter_mem_init(MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR);
|
|
|
|
tr_formatter_size_init(DISK_K, DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR);
|
|
|
|
tr_formatter_speed_init(SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR);
|
|
|
|
}
|
2013-02-01 05:57:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* download dir */
|
|
|
|
q = TR_KEY_download_dir;
|
2013-02-01 05:57:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindStr(settings, q, &str, &len))
|
|
|
|
{
|
|
|
|
path = tr_strdup_printf("%s/%*.*s", sandbox, (int)len, (int)len, str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
path = tr_buildPath(sandbox, "Downloads", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_sys_dir_create(path, TR_SYS_DIR_CREATE_PARENTS, 0700, NULL);
|
|
|
|
tr_variantDictAddStr(settings, q, path);
|
|
|
|
tr_free(path);
|
|
|
|
|
|
|
|
/* incomplete dir */
|
|
|
|
q = TR_KEY_incomplete_dir;
|
|
|
|
|
|
|
|
if (tr_variantDictFindStr(settings, q, &str, &len))
|
|
|
|
{
|
|
|
|
path = tr_strdup_printf("%s/%*.*s", sandbox, (int)len, (int)len, str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
path = tr_buildPath(sandbox, "Incomplete", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variantDictAddStr(settings, q, path);
|
|
|
|
tr_free(path);
|
|
|
|
|
|
|
|
path = tr_buildPath(sandbox, "blocklists", NULL);
|
|
|
|
tr_sys_dir_create(path, TR_SYS_DIR_CREATE_PARENTS, 0700, NULL);
|
|
|
|
tr_free(path);
|
|
|
|
|
|
|
|
q = TR_KEY_port_forwarding_enabled;
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFind(settings, q) == NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddBool(settings, q, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
q = TR_KEY_dht_enabled;
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFind(settings, q) == NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantDictAddBool(settings, q, false);
|
|
|
|
}
|
2013-02-01 05:57:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
q = TR_KEY_message_level;
|
2013-01-21 21:11:00 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantDictFind(settings, q) == NULL)
|
2013-02-01 05:57:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(settings, q, verbose ? TR_LOG_DEBUG : TR_LOG_ERROR);
|
2013-02-01 05:57:47 +00:00
|
|
|
}
|
2013-01-21 00:00:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
session = tr_sessionInit(sandbox, !verbose, settings);
|
|
|
|
|
|
|
|
tr_free(sandbox);
|
|
|
|
tr_variantFree(&local_settings);
|
|
|
|
return session;
|
2013-01-20 04:41:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void libttest_session_close(tr_session* session)
|
2013-01-20 04:41:38 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* sandbox;
|
2013-02-01 05:57:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
sandbox = tr_strdup(tr_sessionGetConfigDir(session));
|
|
|
|
tr_sessionClose(session);
|
|
|
|
tr_logFreeQueue(tr_logGetQueue());
|
|
|
|
session = NULL;
|
2013-01-21 21:11:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
libtest_sandbox_destroy(sandbox);
|
|
|
|
tr_free(sandbox);
|
2013-01-20 04:41:38 +00:00
|
|
|
}
|
2013-01-21 00:00:00 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* libttest_zero_torrent_init(tr_session* session)
|
2013-01-21 00:00:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int err;
|
|
|
|
size_t metainfo_len;
|
|
|
|
char* metainfo;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* metainfo_base64;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* tor;
|
|
|
|
tr_ctor* ctor;
|
|
|
|
|
|
|
|
/*
|
|
|
|
1048576 files-filled-with-zeroes/1048576
|
|
|
|
4096 files-filled-with-zeroes/4096
|
|
|
|
512 files-filled-with-zeroes/512
|
|
|
|
*/
|
|
|
|
metainfo_base64 =
|
|
|
|
"ZDg6YW5ub3VuY2UzMTpodHRwOi8vd3d3LmV4YW1wbGUuY29tL2Fubm91bmNlMTA6Y3JlYXRlZCBi"
|
|
|
|
"eTI1OlRyYW5zbWlzc2lvbi8yLjYxICgxMzQwNykxMzpjcmVhdGlvbiBkYXRlaTEzNTg3MDQwNzVl"
|
|
|
|
"ODplbmNvZGluZzU6VVRGLTg0OmluZm9kNTpmaWxlc2xkNjpsZW5ndGhpMTA0ODU3NmU0OnBhdGhs"
|
|
|
|
"NzoxMDQ4NTc2ZWVkNjpsZW5ndGhpNDA5NmU0OnBhdGhsNDo0MDk2ZWVkNjpsZW5ndGhpNTEyZTQ6"
|
|
|
|
"cGF0aGwzOjUxMmVlZTQ6bmFtZTI0OmZpbGVzLWZpbGxlZC13aXRoLXplcm9lczEyOnBpZWNlIGxl"
|
|
|
|
"bmd0aGkzMjc2OGU2OnBpZWNlczY2MDpRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJtGExUv1726aj"
|
|
|
|
"/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJtGExUv17"
|
|
|
|
"26aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJtGEx"
|
|
|
|
"Uv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GIQxhJ"
|
|
|
|
"tGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZCS1GI"
|
|
|
|
"QxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8KT9ZC"
|
|
|
|
"S1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9umo/8K"
|
|
|
|
"T9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9e9um"
|
|
|
|
"o/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRhMVL9"
|
|
|
|
"e9umo/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMYSbRh"
|
|
|
|
"MVL9e9umo/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLUYhDGEm0YTFS/XvbpqP/Ck/WQktRiEMY"
|
|
|
|
"SbRhMVL9e9umo/8KT9ZCS1GIQxhJtGExUv1726aj/wpP1kJLOlf5A+Tz30nMBVuNM2hpV3wg/103"
|
|
|
|
"OnByaXZhdGVpMGVlZQ==";
|
|
|
|
|
|
|
|
/* create the torrent ctor */
|
|
|
|
metainfo = tr_base64_decode_str(metainfo_base64, &metainfo_len);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(metainfo != NULL);
|
|
|
|
TR_ASSERT(metainfo_len > 0);
|
|
|
|
TR_ASSERT(session != NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
ctor = tr_ctorNew(session);
|
|
|
|
tr_ctorSetMetainfo(ctor, (uint8_t*)metainfo, metainfo_len);
|
|
|
|
tr_ctorSetPaused(ctor, TR_FORCE, true);
|
|
|
|
|
|
|
|
/* create the torrent */
|
|
|
|
err = 0;
|
|
|
|
tor = tr_torrentNew(ctor, &err, NULL);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(err == 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_free(metainfo);
|
|
|
|
tr_ctorFree(ctor);
|
|
|
|
return tor;
|
2013-01-21 00:00:00 +00:00
|
|
|
}
|
2013-01-26 23:08:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void libttest_zero_torrent_populate(tr_torrent* tor, bool complete)
|
2013-01-26 23:08:51 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_file_index_t i = 0; i < tor->info.fileCount; ++i)
|
2013-01-26 23:08:51 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int err;
|
|
|
|
tr_sys_file_t fd;
|
|
|
|
char* path;
|
|
|
|
char* dirname;
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_file const* file = &tor->info.files[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (!complete && i == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
path = tr_strdup_printf("%s%c%s.part", tor->currentDir, TR_PATH_DELIMITER, file->name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
path = tr_strdup_printf("%s%c%s", tor->currentDir, TR_PATH_DELIMITER, file->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
dirname = tr_sys_path_dirname(path, NULL);
|
|
|
|
tr_sys_dir_create(dirname, TR_SYS_DIR_CREATE_PARENTS, 0700, NULL);
|
|
|
|
fd = tr_sys_file_open(path, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0600, NULL);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (uint64_t j = 0; j < file->length; ++j)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
tr_sys_file_write(fd, (!complete && i == 0 && j < tor->info.pieceSize) ? "\1" : "\0", 1, NULL, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_sys_file_close(fd, NULL);
|
|
|
|
|
|
|
|
tr_free(dirname);
|
|
|
|
tr_free(path);
|
|
|
|
|
|
|
|
path = tr_torrentFindFile(tor, i);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(path != NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
err = errno;
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_sys_path_exists(path, NULL));
|
2017-04-19 12:04:45 +00:00
|
|
|
errno = err;
|
|
|
|
tr_free(path);
|
2013-01-26 23:08:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
libttest_sync();
|
|
|
|
libttest_blockingTorrentVerify(tor);
|
2013-01-26 23:08:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (complete)
|
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_torrentStat(tor)->leftUntilDone == 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_torrentStat(tor)->leftUntilDone == tor->info.pieceSize);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-26 23:08:51 +00:00
|
|
|
}
|
2013-01-31 21:58:25 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void onVerifyDone(tr_torrent* tor UNUSED, bool aborted UNUSED, void* done)
|
2013-01-31 21:58:25 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
*(bool*)done = true;
|
2013-01-31 21:58:25 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void libttest_blockingTorrentVerify(tr_torrent* tor)
|
2013-01-31 21:58:25 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tor->session != NULL);
|
|
|
|
TR_ASSERT(!tr_amInEventThread(tor->session));
|
2013-01-31 21:58:25 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool done = false;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrentVerify(tor, onVerifyDone, &done);
|
2013-01-31 21:58:25 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (!done)
|
|
|
|
{
|
|
|
|
tr_wait_msec(10);
|
|
|
|
}
|
2013-01-31 21:58:25 +00:00
|
|
|
}
|
2014-06-08 19:09:41 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void build_parent_dir(char const* path)
|
2014-06-08 19:09:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* dir;
|
|
|
|
tr_error* error = NULL;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const tmperr = errno;
|
2014-06-08 19:09:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dir = tr_sys_path_dirname(path, NULL);
|
|
|
|
tr_sys_dir_create(dir, TR_SYS_DIR_CREATE_PARENTS, 0700, &error);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(error == NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(dir);
|
2014-06-08 19:09:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
errno = tmperr;
|
2014-06-08 20:01:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void libtest_create_file_with_contents(char const* path, void const* payload, size_t n)
|
2014-06-08 20:01:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_t fd;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const tmperr = errno;
|
2014-06-08 20:01:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
build_parent_dir(path);
|
2014-06-08 20:01:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +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, payload, n, NULL, NULL);
|
|
|
|
tr_sys_file_close(fd, NULL);
|
2014-06-08 19:09:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
libttest_sync();
|
2014-06-08 19:09:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
errno = tmperr;
|
2014-06-08 19:09:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void libtest_create_file_with_string_contents(char const* path, char const* str)
|
2014-06-08 20:01:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
libtest_create_file_with_contents(path, str, strlen(str));
|
2014-06-08 20:01:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void libtest_create_tmpfile_with_contents(char* tmpl, void const* payload, size_t n)
|
2014-06-08 20:01:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_t fd;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const tmperr = errno;
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t n_left = n;
|
|
|
|
tr_error* error = NULL;
|
|
|
|
|
|
|
|
build_parent_dir(tmpl);
|
2014-06-08 20:01:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
fd = tr_sys_file_open_temp(tmpl, NULL);
|
2014-06-08 20:01:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (n_left > 0)
|
2014-06-08 20:01:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t n;
|
|
|
|
|
|
|
|
if (!tr_sys_file_write(fd, payload, n_left, &n, &error))
|
2014-06-08 20:01:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "Error writing '%s': %s\n", tmpl, error->message);
|
|
|
|
tr_error_free(error);
|
|
|
|
break;
|
2014-06-08 20:01:10 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
n_left -= n;
|
2014-06-08 20:01:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_close(fd, NULL);
|
|
|
|
|
|
|
|
libttest_sync();
|
2014-06-08 20:01:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
errno = tmperr;
|
2014-06-08 20:01:10 +00:00
|
|
|
}
|
2014-12-13 15:22:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void libttest_sync(void)
|
2014-12-13 15:22:39 +00:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
sync();
|
2014-12-13 15:22:39 +00:00
|
|
|
#endif
|
|
|
|
}
|