mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-10-21 22:43:48 +00:00
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.
This commit is contained in:
+52
-34
@@ -1,59 +1,77 @@
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "ipset.h"
|
||||
int verbose = 0;
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// IP prefix set (src/ipset.cpp) checked against an exhaustive bitmap oracle.
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
static uint32_t state = 42;
|
||||
static uint32_t
|
||||
next_random(void)
|
||||
{
|
||||
state = state * 1664525u + 1013904223u;
|
||||
return state;
|
||||
#include "ipset.h"
|
||||
#include "test_helpers.hpp"
|
||||
|
||||
extern "C" {
|
||||
int verbose = 0;
|
||||
}
|
||||
|
||||
static void
|
||||
check_family(unsigned width)
|
||||
namespace {
|
||||
|
||||
uint32_t g_state = 42;
|
||||
|
||||
uint32_t next_random()
|
||||
{
|
||||
struct ss_ipset set = {0};
|
||||
uint8_t address[16] = {0};
|
||||
bool oracle[256] = {false};
|
||||
unsigned last = width / 8 - 1;
|
||||
/* Independent exhaustive bitmap oracle over a /24 or /120. Prefix writes
|
||||
* can cover the whole space; host deletion must punch holes in them. */
|
||||
g_state = g_state * 1664525u + 1013904223u;
|
||||
return g_state;
|
||||
}
|
||||
|
||||
void check_family(unsigned width)
|
||||
{
|
||||
ss_ipset set{};
|
||||
uint8_t address[16] = {};
|
||||
bool oracle[256] = {};
|
||||
const unsigned last = width / 8 - 1;
|
||||
// Independent exhaustive bitmap oracle over a /24 or /120. Prefix writes
|
||||
// can cover the whole space; host deletion must punch holes in them.
|
||||
for (unsigned round = 0; round < 2000; round++) {
|
||||
unsigned host = next_random() >> 24;
|
||||
unsigned bits = next_random() % 9;
|
||||
bool present = (next_random() >> 31) != 0;
|
||||
unsigned mask = bits == 0 ? 0 : (255u << (8 - bits)) & 255u;
|
||||
address[last] = (uint8_t)host;
|
||||
const unsigned host = next_random() >> 24;
|
||||
const unsigned bits = next_random() % 9;
|
||||
const bool present = (next_random() >> 31) != 0;
|
||||
const unsigned mask = bits == 0 ? 0 : (255u << (8 - bits)) & 255u;
|
||||
address[last] = static_cast<uint8_t>(host);
|
||||
ss_ipset_assign(&set, address, width, width - 8 + bits, present);
|
||||
for (unsigned i = 0; i < 256; i++) {
|
||||
if ((i & mask) == (host & mask)) {
|
||||
oracle[i] = present;
|
||||
}
|
||||
address[last] = (uint8_t)i;
|
||||
assert(ss_ipset_contains(&set, address, width) == oracle[i]);
|
||||
address[last] = static_cast<uint8_t>(i);
|
||||
SS_CHECK(ss_ipset_contains(&set, address, width) == oracle[i]);
|
||||
}
|
||||
}
|
||||
ss_ipset_assign(&set, address, width, 0, true);
|
||||
memset(address, 255, sizeof(address));
|
||||
assert(ss_ipset_contains(&set, address, width));
|
||||
std::memset(address, 255, sizeof(address));
|
||||
SS_CHECK(ss_ipset_contains(&set, address, width));
|
||||
ss_ipset_assign(&set, address, width, width, false);
|
||||
assert(!ss_ipset_contains(&set, address, width));
|
||||
SS_CHECK(!ss_ipset_contains(&set, address, width));
|
||||
address[last]--;
|
||||
assert(ss_ipset_contains(&set, address, width));
|
||||
SS_CHECK(ss_ipset_contains(&set, address, width));
|
||||
ss_ipset_assign(&set, address, width, width + 1, false);
|
||||
assert(ss_ipset_contains(&set, address, width));
|
||||
SS_CHECK(ss_ipset_contains(&set, address, width));
|
||||
ss_ipset_clear(&set);
|
||||
ss_ipset_clear(&set);
|
||||
assert(!ss_ipset_contains(&set, address, width));
|
||||
SS_CHECK(!ss_ipset_contains(&set, address, width));
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
void test_ipv4()
|
||||
{
|
||||
check_family(32);
|
||||
}
|
||||
|
||||
void test_ipv6()
|
||||
{
|
||||
check_family(128);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
SS_RUN(test_ipv4);
|
||||
SS_RUN(test_ipv6);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user