mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-10-21 14:33:55 +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:
@@ -1,59 +1,84 @@
|
||||
/* Optional migration differential test. Link against the pre-migration ipset
|
||||
* and cork archives; these are intentionally not production dependencies. */
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// Optional migration differential test. Link against the pre-migration ipset
|
||||
// and cork archives; these are intentionally not production dependencies.
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#include <ipset/ipset.h>
|
||||
|
||||
#include "ipset.h"
|
||||
#include "test_helpers.hpp"
|
||||
|
||||
extern "C" {
|
||||
int verbose = 0;
|
||||
static uint32_t state = 0x725cab91;
|
||||
static uint32_t next_random(void)
|
||||
{
|
||||
state ^= state << 13;
|
||||
state ^= state >> 17;
|
||||
state ^= state << 5;
|
||||
return state;
|
||||
}
|
||||
static void check_family(unsigned width)
|
||||
|
||||
namespace {
|
||||
|
||||
uint32_t g_state = 0x725cab91;
|
||||
|
||||
uint32_t next_random()
|
||||
{
|
||||
struct ss_ipset current = {0};
|
||||
struct ip_set reference;
|
||||
g_state ^= g_state << 13;
|
||||
g_state ^= g_state >> 17;
|
||||
g_state ^= g_state << 5;
|
||||
return g_state;
|
||||
}
|
||||
|
||||
void check_family(unsigned width)
|
||||
{
|
||||
ss_ipset current{};
|
||||
ip_set reference;
|
||||
ipset_init(&reference);
|
||||
for (unsigned round = 0; round < 2000; round++) {
|
||||
uint8_t bytes[16];
|
||||
for (unsigned i = 0; i < sizeof(bytes); i++) bytes[i] = (uint8_t)next_random();
|
||||
unsigned prefix = next_random() % (width + 1);
|
||||
bool present = (next_random() & 1) != 0;
|
||||
for (unsigned i = 0; i < sizeof(bytes); i++) {
|
||||
bytes[i] = static_cast<uint8_t>(next_random());
|
||||
}
|
||||
const unsigned prefix = next_random() % (width + 1);
|
||||
const bool present = (next_random() & 1) != 0;
|
||||
ss_ipset_assign(¤t, bytes, width, prefix, present);
|
||||
struct cork_ipv4 v4;
|
||||
struct cork_ipv6 v6;
|
||||
memcpy(v4._.u8, bytes, sizeof(v4._.u8));
|
||||
memcpy(v6._.u8, bytes, sizeof(v6._.u8));
|
||||
cork_ipv4 v4;
|
||||
cork_ipv6 v6;
|
||||
std::memcpy(v4._.u8, bytes, sizeof(v4._.u8));
|
||||
std::memcpy(v6._.u8, bytes, sizeof(v6._.u8));
|
||||
if (width == 32) {
|
||||
if (present) ipset_ipv4_add_network(&reference, &v4, prefix);
|
||||
else ipset_ipv4_remove_network(&reference, &v4, prefix);
|
||||
if (present) {
|
||||
ipset_ipv4_add_network(&reference, &v4, prefix);
|
||||
} else {
|
||||
ipset_ipv4_remove_network(&reference, &v4, prefix);
|
||||
}
|
||||
} else {
|
||||
if (present) ipset_ipv6_add_network(&reference, &v6, prefix);
|
||||
else ipset_ipv6_remove_network(&reference, &v6, prefix);
|
||||
if (present) {
|
||||
ipset_ipv6_add_network(&reference, &v6, prefix);
|
||||
} else {
|
||||
ipset_ipv6_remove_network(&reference, &v6, prefix);
|
||||
}
|
||||
}
|
||||
for (unsigned sample = 0; sample < 257; sample++) {
|
||||
if (sample != 0) {
|
||||
for (unsigned i = 0; i < sizeof(bytes); i++) bytes[i] = (uint8_t)next_random();
|
||||
for (unsigned i = 0; i < sizeof(bytes); i++) {
|
||||
bytes[i] = static_cast<uint8_t>(next_random());
|
||||
}
|
||||
}
|
||||
memcpy(v4._.u8, bytes, sizeof(v4._.u8));
|
||||
memcpy(v6._.u8, bytes, sizeof(v6._.u8));
|
||||
bool old = width == 32 ? ipset_contains_ipv4(&reference, &v4) : ipset_contains_ipv6(&reference, &v6);
|
||||
assert(old == ss_ipset_contains(¤t, bytes, width));
|
||||
std::memcpy(v4._.u8, bytes, sizeof(v4._.u8));
|
||||
std::memcpy(v6._.u8, bytes, sizeof(v6._.u8));
|
||||
const bool old = width == 32 ? ipset_contains_ipv4(&reference, &v4) : ipset_contains_ipv6(&reference, &v6);
|
||||
SS_CHECK(old == ss_ipset_contains(¤t, bytes, width));
|
||||
}
|
||||
}
|
||||
ipset_done(&reference);
|
||||
ss_ipset_clear(¤t);
|
||||
}
|
||||
int main(void)
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
ipset_init_library();
|
||||
check_family(32);
|
||||
check_family(128);
|
||||
puts("1,028,000 IPv4/IPv6 membership comparisons matched the original libipset");
|
||||
std::puts("1,028,000 IPv4/IPv6 membership comparisons matched the original libipset");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user