mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-10-20 21:43:41 +00:00
* 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
72 lines
2.2 KiB
C++
72 lines
2.2 KiB
C++
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// ACL literal rules (full:/suffix:), CIDR sets and runtime IP edits
|
|
// (src/acl.cpp).
|
|
#include <cstdio>
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
#include "config.h" // SS_ENABLE_REGEX
|
|
#endif
|
|
|
|
#include "acl.h"
|
|
#include "test_helpers.hpp"
|
|
|
|
extern "C" {
|
|
int verbose = 0;
|
|
}
|
|
|
|
namespace {
|
|
|
|
void write_file(const char *path, const char *content)
|
|
{
|
|
FILE *file = std::fopen(path, "w");
|
|
SS_CHECK(file != nullptr);
|
|
std::fputs(content, file);
|
|
std::fclose(file);
|
|
}
|
|
|
|
void test_acl_literal(const char *argv0)
|
|
{
|
|
char path[4096];
|
|
SS_CHECK(std::snprintf(path, sizeof(path), "%s.acl", argv0) < static_cast<int>(sizeof(path)));
|
|
write_file(path,
|
|
"[black_list]\nfull:Exact.example\nsuffix:blocked.example\n10.0.0.0/8\n"
|
|
"2001:db8::/32\n[white_list]\nsuffix:allowed.example\n"
|
|
"[outbound_block_list]\nfull:outbound.example\n");
|
|
SS_CHECK(init_acl(path) == 0);
|
|
SS_CHECK(acl_match_host("EXACT.example") == 1);
|
|
SS_CHECK(acl_match_host("sub.exact.example") == 0);
|
|
SS_CHECK(acl_match_host("blocked.example") == 1);
|
|
SS_CHECK(acl_match_host("a.blocked.example") == 1);
|
|
SS_CHECK(acl_match_host("notblocked.example") == 0);
|
|
SS_CHECK(acl_match_host("blocked.example.evil") == 0);
|
|
SS_CHECK(acl_match_host("a.allowed.example") == -1);
|
|
SS_CHECK(outbound_block_match_host("outbound.example") == 1);
|
|
SS_CHECK(outbound_block_match_host("a.outbound.example") == 0);
|
|
SS_CHECK(acl_match_host("10.42.1.2") == 1);
|
|
SS_CHECK(acl_remove_ip("10.42.1.2") == 0);
|
|
SS_CHECK(acl_match_host("10.42.1.2") == 0);
|
|
SS_CHECK(acl_match_host("10.42.1.3") == 1);
|
|
SS_CHECK(acl_add_ip("10.42.1.2") == 0);
|
|
SS_CHECK(acl_match_host("10.42.1.2") == 1);
|
|
SS_CHECK(acl_remove_ip("2001:db8::42") == 0);
|
|
SS_CHECK(acl_match_host("2001:db8::42") == 0);
|
|
SS_CHECK(acl_match_host("2001:db8::43") == 1);
|
|
free_acl();
|
|
#if !SS_ENABLE_REGEX
|
|
write_file(path, "[black_list]\n10.0.0.0/8\n^old\\.regex$\n");
|
|
SS_CHECK(init_acl(path) == -1);
|
|
SS_CHECK(acl_match_host("10.1.1.1") == 0);
|
|
#endif
|
|
std::remove(path);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
SS_CHECK(argc >= 1);
|
|
test_acl_literal(argv[0]);
|
|
std::printf("ok test_acl_literal\n");
|
|
return 0;
|
|
}
|