Files
shadowsocks-libev/tests/test_ref_cpp.cpp
T
Max Lv ee7539155d C++20 port 16/16: config parsing on arenas, unit tests in C++ (#3080)
* Rename test_jconf.c to test_jconf_cpp.cpp

* Parse the config file on arenas

read_jconf now reads the file and builds the JSON tree in a scratch arena
that is released before it returns, and copies the parsed strings into one
process-lifetime arena instead of a malloc per string. parse_addr and the
config parser share one address splitter.

Port test_jconf to C++ and cover every field copied out of the parse tree,
a re-read, and the densest file the size limit accepts.

* tests: rename remaining C unit tests to .cpp (no content changes)

* tests: port remaining C unit tests to the C++ test harness

Convert acl, acl_literal, aead2022, base64, buffer, cache (legacy
reference), crypto, ipset, ipset_reference, json, netutils, ppbloom,
process, ref, ssurl and utils tests to test_helpers.hpp. ctest names are
unchanged; every assertion is kept. Build the ported sources with the
strict C++ warning set.

* Run clang-tidy on every ported test

The test regex skipped file names with digits (test_aead2022_cpp.cpp,
test_base64_cpp.cpp); include them, fix the two optional accesses it then
reports, and build test_jconf with the strict warning set.

* docs: per-connection RSS on Linux against the C relay

* test_jconf: check read_jconf results before using them
2026-10-15 10:17:23 +08:00

73 lines
2.2 KiB
C++

// SPDX-License-Identifier: GPL-3.0-or-later
// Intrusive reference counting (tests/reference/ss_ref.h): assignment,
// move and release helpers used by the legacy relay code.
#include <cstdlib>
#include "ss_ref.h"
#include "test_helpers.hpp"
namespace {
struct test_object_t {
ss_ref_t ref;
int *destroyed;
};
void destroy_object(void *object)
{
auto *value = static_cast<test_object_t *>(object);
++*value->destroyed;
std::free(value);
}
void *new_object(int *destroyed)
{
auto *value = static_cast<test_object_t *>(std::malloc(sizeof(test_object_t)));
SS_CHECK(value != nullptr);
ss_ref_init(value, destroy_object);
value->destroyed = destroyed;
return value;
}
void test_ref_lifecycle()
{
int destroyed = 0;
void *table = new_object(&destroyed);
void *relay = nullptr;
ss_ref_assign(&relay, table);
ss_ref_assign(&relay, relay); // Self-assignment must preserve ownership.
ss_ptr_reset(&table, ss_ref_release); // Simulate eviction.
SS_CHECK(table == nullptr && destroyed == 0);
ss_ptr_reset(&relay, ss_ref_release);
SS_CHECK(relay == nullptr && destroyed == 1);
ss_ptr_reset(&relay, ss_ref_release); // Repeated cleanup is harmless.
SS_CHECK(destroyed == 1);
void *query = new_object(&destroyed);
relay = new_object(&destroyed);
ss_ptr_move(&relay, &query, ss_ref_release);
SS_CHECK(query == nullptr && relay != nullptr && destroyed == 2);
ss_ptr_move(&relay, &relay, ss_ref_release); // Self-move.
SS_CHECK(relay != nullptr && destroyed == 2);
ss_ref_assign(&query, relay);
ss_ptr_move(&relay, &query, ss_ref_release); // Two owners, same object.
SS_CHECK(query == nullptr && relay != nullptr && destroyed == 2);
ss_ref_assign(&relay, nullptr);
SS_CHECK(relay == nullptr && destroyed == 3);
relay = new_object(&destroyed);
ss_ptr_move(&relay, &query, ss_ref_release); // Move an empty source.
SS_CHECK(relay == nullptr && query == nullptr && destroyed == 4);
SS_CHECK(ss_ref_retain(nullptr) == nullptr);
ss_ref_release(nullptr);
ss_ptr_reset(&relay, nullptr); // Legacy crypto has no session/callback.
}
} // namespace
int main()
{
SS_RUN(test_ref_lifecycle);
return 0;
}