2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright (C) 2013-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <cstddef> // size_t
|
|
|
|
#include <cstdint> // int64_t
|
|
|
|
#include <iterator> // std::inserter
|
|
|
|
#include <set>
|
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/rpcimpl.h>
|
|
|
|
#include <libtransmission/variant.h>
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "gtest/gtest.h"
|
2023-10-30 17:30:10 +00:00
|
|
|
#include "libtransmission/quark.h"
|
2020-08-11 18:11:55 +00:00
|
|
|
#include "test-fixtures.h"
|
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
struct tr_session;
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-15 23:34:22 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
namespace libtransmission::test
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
using RpcTest = SessionTest;
|
|
|
|
|
|
|
|
TEST_F(RpcTest, list)
|
|
|
|
{
|
2022-11-14 20:16:29 +00:00
|
|
|
auto i = int64_t{};
|
2021-11-14 23:28:37 +00:00
|
|
|
auto sv = std::string_view{};
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2023-10-02 21:18:35 +00:00
|
|
|
auto top = tr_rpc_parse_list_str("12"sv);
|
2023-08-21 21:16:54 +00:00
|
|
|
EXPECT_TRUE(top.holds_alternative<int64_t>());
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_TRUE(tr_variantGetInt(&top, &i));
|
|
|
|
EXPECT_EQ(12, i);
|
|
|
|
|
2023-10-02 21:18:35 +00:00
|
|
|
top = tr_rpc_parse_list_str("6,7"sv);
|
2023-08-21 21:16:54 +00:00
|
|
|
EXPECT_TRUE(top.holds_alternative<tr_variant::Vector>());
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(2U, tr_variantListSize(&top));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_TRUE(tr_variantGetInt(tr_variantListChild(&top, 0), &i));
|
|
|
|
EXPECT_EQ(6, i);
|
|
|
|
EXPECT_TRUE(tr_variantGetInt(tr_variantListChild(&top, 1), &i));
|
|
|
|
EXPECT_EQ(7, i);
|
|
|
|
|
2023-10-02 21:18:35 +00:00
|
|
|
top = tr_rpc_parse_list_str("asdf"sv);
|
2023-08-21 21:16:54 +00:00
|
|
|
EXPECT_TRUE(top.holds_alternative<std::string_view>());
|
2021-11-14 23:28:37 +00:00
|
|
|
EXPECT_TRUE(tr_variantGetStrView(&top, &sv));
|
|
|
|
EXPECT_EQ("asdf"sv, sv);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2023-10-02 21:18:35 +00:00
|
|
|
top = tr_rpc_parse_list_str("1,3-5"sv);
|
2023-08-21 21:16:54 +00:00
|
|
|
EXPECT_TRUE(top.holds_alternative<tr_variant::Vector>());
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(4U, tr_variantListSize(&top));
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_TRUE(tr_variantGetInt(tr_variantListChild(&top, 0), &i));
|
|
|
|
EXPECT_EQ(1, i);
|
|
|
|
EXPECT_TRUE(tr_variantGetInt(tr_variantListChild(&top, 1), &i));
|
|
|
|
EXPECT_EQ(3, i);
|
|
|
|
EXPECT_TRUE(tr_variantGetInt(tr_variantListChild(&top, 2), &i));
|
|
|
|
EXPECT_EQ(4, i);
|
|
|
|
EXPECT_TRUE(tr_variantGetInt(tr_variantListChild(&top, 3), &i));
|
|
|
|
EXPECT_EQ(5, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
TEST_F(RpcTest, sessionGet)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const rpc_response_func = [](tr_session* /*session*/, tr_variant* response, void* setme) noexcept
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2023-08-21 04:15:23 +00:00
|
|
|
std::swap(*static_cast<tr_variant*>(setme), *response);
|
2020-08-11 18:11:55 +00:00
|
|
|
};
|
|
|
|
|
2022-03-30 06:30:39 +00:00
|
|
|
auto* tor = zeroTorrentInit(ZeroTorrentState::NoFiles);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_NE(nullptr, tor);
|
|
|
|
|
|
|
|
tr_variant request;
|
|
|
|
tr_variantInitDict(&request, 1);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(&request, TR_KEY_method, "session-get");
|
2020-08-11 18:11:55 +00:00
|
|
|
tr_variant response;
|
|
|
|
tr_rpc_request_exec_json(session_, &request, rpc_response_func, &response);
|
|
|
|
|
2023-08-21 21:16:54 +00:00
|
|
|
EXPECT_TRUE(response.holds_alternative<tr_variant::Map>());
|
2022-11-14 20:16:29 +00:00
|
|
|
tr_variant* args = nullptr;
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_TRUE(tr_variantDictFindDict(&response, TR_KEY_arguments, &args));
|
|
|
|
|
|
|
|
// what we expected
|
2022-08-26 02:27:11 +00:00
|
|
|
auto const expected_keys = std::array<tr_quark, 59>{
|
2020-08-11 18:11:55 +00:00
|
|
|
TR_KEY_alt_speed_down,
|
|
|
|
TR_KEY_alt_speed_enabled,
|
|
|
|
TR_KEY_alt_speed_time_begin,
|
|
|
|
TR_KEY_alt_speed_time_day,
|
|
|
|
TR_KEY_alt_speed_time_enabled,
|
|
|
|
TR_KEY_alt_speed_time_end,
|
|
|
|
TR_KEY_alt_speed_up,
|
2020-11-14 18:43:42 +00:00
|
|
|
TR_KEY_anti_brute_force_enabled,
|
|
|
|
TR_KEY_anti_brute_force_threshold,
|
2020-08-11 18:11:55 +00:00
|
|
|
TR_KEY_blocklist_enabled,
|
|
|
|
TR_KEY_blocklist_size,
|
|
|
|
TR_KEY_blocklist_url,
|
|
|
|
TR_KEY_cache_size_mb,
|
|
|
|
TR_KEY_config_dir,
|
2022-02-20 17:54:20 +00:00
|
|
|
TR_KEY_default_trackers,
|
2020-08-11 18:11:55 +00:00
|
|
|
TR_KEY_dht_enabled,
|
|
|
|
TR_KEY_download_dir,
|
|
|
|
TR_KEY_download_dir_free_space,
|
|
|
|
TR_KEY_download_queue_enabled,
|
|
|
|
TR_KEY_download_queue_size,
|
|
|
|
TR_KEY_encryption,
|
|
|
|
TR_KEY_idle_seeding_limit,
|
|
|
|
TR_KEY_idle_seeding_limit_enabled,
|
|
|
|
TR_KEY_incomplete_dir,
|
|
|
|
TR_KEY_incomplete_dir_enabled,
|
|
|
|
TR_KEY_lpd_enabled,
|
|
|
|
TR_KEY_peer_limit_global,
|
|
|
|
TR_KEY_peer_limit_per_torrent,
|
|
|
|
TR_KEY_peer_port,
|
|
|
|
TR_KEY_peer_port_random_on_start,
|
|
|
|
TR_KEY_pex_enabled,
|
|
|
|
TR_KEY_port_forwarding_enabled,
|
|
|
|
TR_KEY_queue_stalled_enabled,
|
|
|
|
TR_KEY_queue_stalled_minutes,
|
|
|
|
TR_KEY_rename_partial_files,
|
|
|
|
TR_KEY_rpc_version,
|
|
|
|
TR_KEY_rpc_version_minimum,
|
2021-10-13 03:03:28 +00:00
|
|
|
TR_KEY_rpc_version_semver,
|
2021-10-12 21:32:32 +00:00
|
|
|
TR_KEY_script_torrent_added_enabled,
|
|
|
|
TR_KEY_script_torrent_added_filename,
|
2020-08-11 18:11:55 +00:00
|
|
|
TR_KEY_script_torrent_done_enabled,
|
|
|
|
TR_KEY_script_torrent_done_filename,
|
2022-02-14 04:07:12 +00:00
|
|
|
TR_KEY_script_torrent_done_seeding_enabled,
|
|
|
|
TR_KEY_script_torrent_done_seeding_filename,
|
2020-08-11 18:11:55 +00:00
|
|
|
TR_KEY_seed_queue_enabled,
|
|
|
|
TR_KEY_seed_queue_size,
|
|
|
|
TR_KEY_seedRatioLimit,
|
|
|
|
TR_KEY_seedRatioLimited,
|
|
|
|
TR_KEY_session_id,
|
|
|
|
TR_KEY_speed_limit_down,
|
|
|
|
TR_KEY_speed_limit_down_enabled,
|
|
|
|
TR_KEY_speed_limit_up,
|
|
|
|
TR_KEY_speed_limit_up_enabled,
|
|
|
|
TR_KEY_start_added_torrents,
|
2022-08-26 02:27:11 +00:00
|
|
|
TR_KEY_tcp_enabled,
|
2020-08-11 18:11:55 +00:00
|
|
|
TR_KEY_trash_original_torrent_files,
|
|
|
|
TR_KEY_units,
|
|
|
|
TR_KEY_utp_enabled,
|
2021-08-15 09:41:48 +00:00
|
|
|
TR_KEY_version,
|
2020-08-11 18:11:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// what we got
|
|
|
|
std::set<tr_quark> actual_keys;
|
2022-11-14 20:16:29 +00:00
|
|
|
auto key = tr_quark{};
|
|
|
|
tr_variant* val = nullptr;
|
|
|
|
auto n = size_t{};
|
2020-08-11 18:11:55 +00:00
|
|
|
while ((tr_variantDictChild(args, n++, &key, &val)))
|
|
|
|
{
|
|
|
|
actual_keys.insert(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto missing_keys = std::vector<tr_quark>{};
|
2021-08-15 09:41:48 +00:00
|
|
|
std::set_difference(
|
|
|
|
std::begin(expected_keys),
|
|
|
|
std::end(expected_keys),
|
|
|
|
std::begin(actual_keys),
|
|
|
|
std::end(actual_keys),
|
2020-08-11 18:11:55 +00:00
|
|
|
std::inserter(missing_keys, std::begin(missing_keys)));
|
2021-08-15 09:41:48 +00:00
|
|
|
EXPECT_EQ(decltype(missing_keys){}, missing_keys);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
auto unexpected_keys = std::vector<tr_quark>{};
|
2021-08-15 09:41:48 +00:00
|
|
|
std::set_difference(
|
|
|
|
std::begin(actual_keys),
|
|
|
|
std::end(actual_keys),
|
|
|
|
std::begin(expected_keys),
|
|
|
|
std::end(expected_keys),
|
2020-08-11 18:11:55 +00:00
|
|
|
std::inserter(unexpected_keys, std::begin(unexpected_keys)));
|
2021-08-15 09:41:48 +00:00
|
|
|
EXPECT_EQ(decltype(unexpected_keys){}, unexpected_keys);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// cleanup
|
2022-10-23 16:51:35 +00:00
|
|
|
tr_torrentRemove(tor, false, nullptr, nullptr);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 17:30:10 +00:00
|
|
|
TEST_F(RpcTest, torrentGet)
|
|
|
|
{
|
|
|
|
auto const rpc_response_func = [](tr_session* /*session*/, tr_variant* response, void* setme) noexcept
|
|
|
|
{
|
|
|
|
std::swap(*static_cast<tr_variant*>(setme), *response);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto* tor = zeroTorrentInit(ZeroTorrentState::NoFiles);
|
|
|
|
EXPECT_NE(nullptr, tor);
|
|
|
|
|
|
|
|
tr_variant request;
|
|
|
|
tr_variantInitDict(&request, 1);
|
|
|
|
|
|
|
|
tr_variantDictAddStrView(&request, TR_KEY_method, "torrent-get");
|
|
|
|
|
|
|
|
tr_variant* args_in = tr_variantDictAddDict(&request, TR_KEY_arguments, 1);
|
|
|
|
tr_variant* fields = tr_variantDictAddList(args_in, TR_KEY_fields, 1);
|
|
|
|
tr_variantListAddStrView(fields, tr_quark_get_string_view(TR_KEY_id));
|
|
|
|
|
|
|
|
tr_variant response;
|
|
|
|
tr_rpc_request_exec_json(session_, &request, rpc_response_func, &response);
|
|
|
|
|
|
|
|
EXPECT_TRUE(response.holds_alternative<tr_variant::Map>());
|
|
|
|
tr_variant* args = nullptr;
|
|
|
|
EXPECT_TRUE(tr_variantDictFindDict(&response, TR_KEY_arguments, &args));
|
|
|
|
|
|
|
|
tr_variant* torrents = nullptr;
|
|
|
|
EXPECT_TRUE(tr_variantDictFindList(args, TR_KEY_torrents, &torrents));
|
|
|
|
EXPECT_EQ(1UL, tr_variantListSize(torrents));
|
|
|
|
|
|
|
|
tr_variant* first_torrent = tr_variantListChild(torrents, 0);
|
|
|
|
|
|
|
|
EXPECT_TRUE(first_torrent->holds_alternative<tr_variant::Map>());
|
|
|
|
int64_t first_torrent_id = 0;
|
|
|
|
EXPECT_TRUE(tr_variantDictFindInt(first_torrent, TR_KEY_id, &first_torrent_id));
|
|
|
|
EXPECT_EQ(1, first_torrent_id);
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
tr_torrentRemove(tor, false, nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
} // namespace libtransmission::test
|