Restrict parse value to strtoll with restrictions on input.

This commit is contained in:
rakshasa
2026-09-30 12:01:13 +02:00
committed by Jari Sundell
parent 44d51171e1
commit c5d54fbb97
16 changed files with 440 additions and 60 deletions
+65
View File
@@ -0,0 +1,65 @@
#include "config.h"
#include "test/src/test_command_groups.h"
#include <torrent/torrent.h>
#include "control.h"
#include "globals.h"
#include "rpc/parse_commands.h"
CPPUNIT_TEST_SUITE_REGISTRATION(TestCommandGroups);
void initialize_command_groups();
static void
call_set(const char* value) {
torrent::Object::list_type args;
args.push_back(torrent::Object(int64_t{0}));
args.push_back(torrent::Object(std::string(value)));
rpc::commands.call_command("choke_group.up.max.set", torrent::Object::create_list_range(args.begin(), args.end()));
}
static int64_t
call_get(const char* key) {
return rpc::commands.call_command(key, torrent::Object(int64_t{0})).as_value();
}
void
TestCommandGroups::setUp() {
torrent::initialize_main_thread();
torrent::initialize();
if (control == nullptr)
control = new Control;
if (!rpc::commands.has("choke_group.up.max.set"))
initialize_command_groups();
}
void
TestCommandGroups::tearDown() {
torrent::cleanup();
}
void
TestCommandGroups::test_max_unchoked_in_range() {
call_set("50");
CPPUNIT_ASSERT_EQUAL(int64_t{50}, call_get("choke_group.up.max"));
CPPUNIT_ASSERT_EQUAL(int64_t{0}, call_get("choke_group.up.max.unlimited"));
call_set("-1");
CPPUNIT_ASSERT_EQUAL(int64_t{1}, call_get("choke_group.up.max.unlimited"));
}
void
TestCommandGroups::test_max_unchoked_out_of_range() {
call_set("50");
CPPUNIT_ASSERT_THROW(call_set("4294967296"), torrent::input_error);
CPPUNIT_ASSERT_EQUAL(int64_t{50}, call_get("choke_group.up.max"));
CPPUNIT_ASSERT_THROW(call_set("-2"), torrent::input_error);
CPPUNIT_ASSERT_EQUAL(int64_t{50}, call_get("choke_group.up.max"));
}
+17
View File
@@ -0,0 +1,17 @@
#include "test/helpers/test_fixture.h"
class TestCommandGroups : public test_fixture {
CPPUNIT_TEST_SUITE(TestCommandGroups);
CPPUNIT_TEST(test_max_unchoked_in_range);
CPPUNIT_TEST(test_max_unchoked_out_of_range);
CPPUNIT_TEST_SUITE_END();
public:
void setUp();
void tearDown();
void test_max_unchoked_in_range();
void test_max_unchoked_out_of_range();
};
+73
View File
@@ -0,0 +1,73 @@
#include "config.h"
#include "test/src/test_command_tracker.h"
#include <torrent/runtime/network_config.h>
#include <torrent/torrent.h>
#include "control.h"
#include "globals.h"
#include "command_helpers.h"
#include "rpc/parse_commands.h"
CPPUNIT_TEST_SUITE_REGISTRATION(TestCommandTracker);
void initialize_command_tracker();
static void
call_set(const char* value) {
rpc::commands.call_command("dht.override_port.set", torrent::Object(std::string(value)));
}
static uint16_t
override_port() {
return torrent::runtime::network_config()->override_dht_port();
}
void
TestCommandTracker::setUp() {
torrent::initialize_main_thread();
torrent::initialize();
if (control == nullptr)
control = new Control;
if (!rpc::commands.has("dht.override_port.set"))
initialize_command_tracker();
}
void
TestCommandTracker::tearDown() {
torrent::cleanup();
}
void
TestCommandTracker::test_dht_override_port_in_range() {
call_set("6881");
CPPUNIT_ASSERT_EQUAL(uint16_t{6881}, override_port());
call_set("65535");
CPPUNIT_ASSERT_EQUAL(uint16_t{65535}, override_port());
}
void
TestCommandTracker::test_dht_override_port_out_of_range() {
call_set("6881");
CPPUNIT_ASSERT_THROW(call_set("70000"), torrent::input_error);
CPPUNIT_ASSERT_EQUAL(uint16_t{6881}, override_port());
CPPUNIT_ASSERT_THROW(call_set("-1"), torrent::input_error);
CPPUNIT_ASSERT_EQUAL(uint16_t{6881}, override_port());
}
void
TestCommandTracker::test_checked_port_value() {
CPPUNIT_ASSERT_EQUAL(uint16_t{0}, checked_port_value(0, "test"));
CPPUNIT_ASSERT_EQUAL(uint16_t{6881}, checked_port_value(6881, "test"));
CPPUNIT_ASSERT_EQUAL(uint16_t{65535}, checked_port_value(65535, "test"));
CPPUNIT_ASSERT_THROW(checked_port_value(-1, "test"), torrent::input_error);
CPPUNIT_ASSERT_THROW(checked_port_value(65536, "test"), torrent::input_error);
CPPUNIT_ASSERT_THROW(checked_port_value(4294967296, "test"), torrent::input_error);
}
+19
View File
@@ -0,0 +1,19 @@
#include "test/helpers/test_fixture.h"
class TestCommandTracker : public test_fixture {
CPPUNIT_TEST_SUITE(TestCommandTracker);
CPPUNIT_TEST(test_dht_override_port_in_range);
CPPUNIT_TEST(test_dht_override_port_out_of_range);
CPPUNIT_TEST(test_checked_port_value);
CPPUNIT_TEST_SUITE_END();
public:
void setUp();
void tearDown();
void test_dht_override_port_in_range();
void test_dht_override_port_out_of_range();
void test_checked_port_value();
};
+68
View File
@@ -0,0 +1,68 @@
#include "config.h"
#include "test/src/test_download_list.h"
#include <cstdio>
#include <torrent/download.h>
#include <torrent/download_info.h>
#include <torrent/hash_string.h>
#include <torrent/object.h>
#include <torrent/torrent.h>
#include "control.h"
#include "core/download.h"
#include "globals.h"
CPPUNIT_TEST_SUITE_REGISTRATION(TestDownloadList);
static std::string
insert_download(core::DownloadList* list) {
torrent::Object info = torrent::Object::create_map();
info.insert_key("name", std::string("test_download_list"));
info.insert_key("length", int64_t{16});
info.insert_key("piece length", int64_t{262144});
info.insert_key("pieces", std::string(20, char(0)));
auto* object = new torrent::Object(torrent::Object::create_map());
object->insert_key("info", info);
auto download = torrent::download_add(object, 0);
list->insert(new core::Download(download));
char buffer[41];
for (unsigned int i = 0; i < torrent::HashString::size_data; i++)
snprintf(buffer + i * 2, 3, "%02x", static_cast<unsigned char>(download.info()->hash()[i]));
return std::string(buffer, 40);
}
void
TestDownloadList::setUp() {
torrent::initialize_main_thread();
torrent::initialize();
if (control == nullptr)
control = new Control;
m_hex = insert_download(&m_list);
}
void
TestDownloadList::tearDown() {
m_list.clear();
torrent::cleanup();
}
void
TestDownloadList::test_find_hex() {
CPPUNIT_ASSERT(m_list.find_hex(m_hex.c_str()) != m_list.end());
}
void
TestDownloadList::test_find_hex_wrong_length() {
CPPUNIT_ASSERT(m_list.find_hex((m_hex + "f").c_str()) == m_list.end());
CPPUNIT_ASSERT(m_list.find_hex((m_hex + m_hex).c_str()) == m_list.end());
CPPUNIT_ASSERT(m_list.find_hex(m_hex.substr(0, 39).c_str()) == m_list.end());
}
+24
View File
@@ -0,0 +1,24 @@
#include <string>
#include "core/download_list.h"
#include "test/helpers/test_fixture.h"
class TestDownloadList : public test_fixture {
CPPUNIT_TEST_SUITE(TestDownloadList);
CPPUNIT_TEST(test_find_hex);
CPPUNIT_TEST(test_find_hex_wrong_length);
CPPUNIT_TEST_SUITE_END();
public:
void setUp();
void tearDown();
void test_find_hex();
void test_find_hex_wrong_length();
private:
core::DownloadList m_list;
std::string m_hex;
};