Files
shadowsocks-libev/tests/test_cache_reference_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

161 lines
3.3 KiB
C++

// SPDX-License-Identifier: GPL-3.0-or-later
// Basic behaviour of the legacy cache (tests/reference/cache.cpp): create,
// insert, lookup, remove and eviction.
#include <cerrno>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include "cache.h"
#include "test_helpers.hpp"
extern "C" {
int verbose = 0;
}
namespace {
char *dup_string(const char *text)
{
const size_t size = std::strlen(text) + 1;
auto *copy = static_cast<char *>(std::malloc(size));
SS_CHECK(copy != nullptr);
std::memcpy(copy, text, size);
return copy;
}
// The legacy API takes writable keys; copy literals into a buffer.
struct Key {
char text[16];
size_t length;
explicit Key(const char *literal) : text{}, length(std::strlen(literal))
{
SS_CHECK(length < sizeof(text));
std::memcpy(text, literal, length);
}
};
void insert(struct cache *c, const char *key, const char *value)
{
Key k(key);
cache_insert(c, k.text, k.length, dup_string(value));
}
void lookup(struct cache *c, const char *key, char **result)
{
Key k(key);
cache_lookup(c, k.text, k.length, result);
}
int exists(struct cache *c, const char *key)
{
Key k(key);
return cache_key_exist(c, k.text, k.length);
}
void remove_key(struct cache *c, const char *key)
{
Key k(key);
cache_remove(c, k.text, k.length);
}
void test_create_delete()
{
struct cache *c = nullptr;
SS_CHECK(cache_create(&c, 100, nullptr) == 0);
SS_CHECK(c != nullptr);
SS_CHECK(cache_delete(c, 0) == 0);
}
void test_create_null()
{
SS_CHECK(cache_create(nullptr, 100, nullptr) == EINVAL);
}
void test_insert_lookup()
{
struct cache *c = nullptr;
cache_create(&c, 100, nullptr);
insert(c, "key1", "test_data");
char *result = nullptr;
lookup(c, "key1", &result);
SS_CHECK(result != nullptr);
SS_CHECK(std::strcmp(result, "test_data") == 0);
cache_delete(c, 0);
}
void test_key_exist()
{
struct cache *c = nullptr;
cache_create(&c, 100, nullptr);
insert(c, "mykey", "value");
SS_CHECK(exists(c, "mykey") == 1);
SS_CHECK(exists(c, "nokey") == 0);
cache_delete(c, 0);
}
void test_remove()
{
struct cache *c = nullptr;
cache_create(&c, 100, nullptr);
insert(c, "rmkey", "to_remove");
SS_CHECK(exists(c, "rmkey") == 1);
remove_key(c, "rmkey");
SS_CHECK(exists(c, "rmkey") == 0);
cache_delete(c, 0);
}
void test_lookup_missing()
{
struct cache *c = nullptr;
cache_create(&c, 100, nullptr);
char *result = reinterpret_cast<char *>(static_cast<uintptr_t>(0xdeadbeef));
lookup(c, "missing", &result);
SS_CHECK(result == nullptr);
cache_delete(c, 0);
}
void test_eviction()
{
struct cache *c = nullptr;
cache_create(&c, 3, nullptr);
// Fill the cache.
insert(c, "k1", "v1");
insert(c, "k2", "v2");
insert(c, "k3", "v3");
// This triggers eviction of the oldest entry.
insert(c, "k4", "v4");
SS_CHECK(exists(c, "k1") == 0);
SS_CHECK(exists(c, "k4") == 1);
cache_delete(c, 0);
}
} // namespace
int main()
{
SS_RUN(test_create_delete);
SS_RUN(test_create_null);
SS_RUN(test_insert_lookup);
SS_RUN(test_key_exist);
SS_RUN(test_remove);
SS_RUN(test_lookup_missing);
SS_RUN(test_eviction);
return 0;
}