Foundation / checks (client) (push) Successful in 5s
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m27s
Foundation / postgres-tools (push) Successful in 30s
Foundation / server-image (push) Successful in 4m16s
Foundation / image (push) Successful in 4m54s
199 lines
8.8 KiB
C++
199 lines
8.8 KiB
C++
#include "evo/persistence/postgres.hpp"
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <source_location>
|
|
#include <stdexcept>
|
|
#include <string_view>
|
|
void run_region_lease_store_tests();
|
|
void run_account_control_store_tests();
|
|
void run_life_reservation_store_tests();
|
|
void run_primitive_publication_tests();
|
|
void run_cellular_publication_tests();
|
|
void run_starting_wildlife_publication_tests();
|
|
void run_population_publication_tests();
|
|
void run_environment_publication_tests();
|
|
void run_probe_handoff_store_tests();
|
|
void run_region_action_tests();
|
|
void run_region_checkpoint_store_tests();
|
|
void run_region_host_tests();
|
|
void run_region_server_process_tests();
|
|
#ifdef EVO_POSTGRES_SESSIONS
|
|
void run_region_session_tests();
|
|
void run_grant_broker_tests();
|
|
#endif
|
|
#ifdef EVO_POSTGRES_M10
|
|
void run_region_m10_session_tests();
|
|
void run_region_handoff_process_tests();
|
|
#endif
|
|
void run_region_recovery_tests();
|
|
void run_region_ops_tests();
|
|
void run_region_clone_tests();
|
|
void run_region_migration_tests();
|
|
void run_region_store_tests();
|
|
void run_region_load_tests();
|
|
void run_region_upgrade_tests();
|
|
namespace {
|
|
using namespace evo::persistence;
|
|
using namespace std::chrono_literals;
|
|
void check(bool value, std::source_location at = std::source_location::current()) {
|
|
if (!value)
|
|
throw std::runtime_error("PostgreSQL test line " + std::to_string(at.line()));
|
|
}
|
|
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
|
std::string env(const char* key) {
|
|
const auto* value = std::getenv(key);
|
|
if (!value)
|
|
throw std::runtime_error("missing test environment");
|
|
return value;
|
|
}
|
|
// M8.3: libpq service files are read strictly (section, keys, TLS mode, size).
|
|
void service_files() {
|
|
const auto path = env("PGHOST") + "/service-test.conf";
|
|
const auto parse = [&](const std::string& text, std::string_view section = "evolution") {
|
|
std::FILE* f = std::fopen(path.c_str(), "wb");
|
|
CHECK(f != nullptr);
|
|
CHECK(std::fwrite(text.data(), 1, text.size(), f) == text.size());
|
|
CHECK(std::fclose(f) == 0);
|
|
return read_pg_service(path, section);
|
|
};
|
|
auto good = parse("# comment\n[other]\nhost=elsewhere\n[evolution]\r\nhost=pg-forge-01\n"
|
|
"hostaddr=172.10.1.127\nport=5432\ndbname=aevolution\n"
|
|
"user=aevolution_runtime_dev\nsslmode=verify-full\n"
|
|
"sslrootcert=/run/postgres/ca.crt\nconnect_timeout=5\n");
|
|
CHECK(good && good->host == "pg-forge-01" && good->hostaddr == "172.10.1.127" &&
|
|
good->port == "5432" && good->database == "aevolution" &&
|
|
good->user == "aevolution_runtime_dev" && good->ca_file == "/run/postgres/ca.crt" &&
|
|
good->password_file.empty());
|
|
const std::string base = "[evolution]\nhost=h\nhostaddr=10.0.0.1\nport=1\ndbname=d\nuser=u\n";
|
|
for (const std::string& bad :
|
|
{base + "password=secret\n", base + "passfile=/x\n", base + "sslmode=require\n",
|
|
base + "options=-cx\n", base + "user=again\n", base + "[evolution]\nhost=h\n",
|
|
base + "connect_timeout=soon\n", base + "no-equals-sign\n", std::string("[evolution\n"),
|
|
std::string("[elsewhere]\nhost=h\n"), base + std::string(5000, '#')})
|
|
CHECK(!parse(bad) && parse(bad).error() == PgError::config);
|
|
CHECK(parse("[evolution]\nhost=/var/run\nsslmode=disable\n")); // Local socket only.
|
|
CHECK(!parse("[evolution]\nhost=remote\nsslmode=disable\n"));
|
|
CHECK(!read_pg_service(env("PGHOST") + "/no-such-file", "evolution"));
|
|
std::remove(path.c_str());
|
|
}
|
|
|
|
void run() {
|
|
service_files();
|
|
PgOptions options{env("PGHOST"), {}, env("PGPORT"), env("PGDATABASE"), env("PGUSER"), {}, {}};
|
|
auto made = Postgres::connect(options, 5s);
|
|
CHECK(made);
|
|
auto db = std::move(*made);
|
|
const std::array<std::string, 1> hostile{"a'); DROP SCHEMA evolution CASCADE; --"};
|
|
auto rows = db->query("SELECT $1::text, NULL::text", hostile, 5s);
|
|
CHECK(rows && rows->size() == 1 && (*rows)[0].size() == 2);
|
|
CHECK((*rows)[0][0] == hostile[0] && !(*rows)[0][1]);
|
|
CHECK(db->query("BEGIN", {}, 5s));
|
|
CHECK(db->query(
|
|
"INSERT INTO evolution.region_leases(world_id,region_id,frame_id) VALUES (1,1,1)", {}, 5s));
|
|
auto duplicate = db->query(
|
|
"INSERT INTO evolution.region_leases(world_id,region_id,frame_id) VALUES (1,1,1)", {}, 5s);
|
|
CHECK(!duplicate && duplicate.error().kind == PgError::sql);
|
|
CHECK(std::string(duplicate.error().sqlstate.data()) == "23505");
|
|
CHECK(db->query("ROLLBACK", {}, 5s));
|
|
rows = db->query("SELECT count(*) FROM evolution.region_leases", {}, 5s);
|
|
CHECK(rows && (*rows)[0][0] == "0");
|
|
for (const auto& value :
|
|
{std::string{"-1"}, std::string{"18446744073709551616"}, std::string{"1.5"}}) {
|
|
const std::array<std::string, 1> parameter{value};
|
|
CHECK(!db->query("SELECT $1::evolution.u64", parameter, 5s));
|
|
}
|
|
rows = db->query("SELECT 18446744073709551615::evolution.u64", {}, 5s);
|
|
CHECK(rows && (*rows)[0][0] == "18446744073709551615");
|
|
CHECK(db->query("SET ROLE evo_runtime_test", {}, 5s));
|
|
auto denied = db->query(
|
|
"INSERT INTO evolution.region_leases(world_id,region_id,frame_id) VALUES (2,2,2)", {}, 5s);
|
|
CHECK(!denied && std::string(denied.error().sqlstate.data()) == "42501");
|
|
CHECK(db->query("RESET ROLE", {}, 5s));
|
|
auto timeout = db->query("SELECT pg_catalog.pg_sleep(2)", {}, 50ms);
|
|
CHECK(!timeout && timeout.error().kind == PgError::unknown);
|
|
auto closed = db->query("SELECT 1", {}, 5s);
|
|
CHECK(!closed && closed.error().kind == PgError::closed);
|
|
made = Postgres::connect(options, 5s);
|
|
CHECK(made);
|
|
auto oversized = (*made)->query("SELECT pg_catalog.generate_series(1,129)", {}, 5s);
|
|
CHECK(!oversized && oversized.error().kind == PgError::unknown);
|
|
made = Postgres::connect(options, 5s);
|
|
CHECK(made);
|
|
auto disconnected = (*made)->query(
|
|
"SELECT pg_catalog.pg_terminate_backend(pg_catalog.pg_backend_pid())", {}, 5s);
|
|
CHECK(!disconnected && disconnected.error().kind == PgError::unknown);
|
|
options.user = "no_such_evo_user";
|
|
CHECK(!Postgres::connect(options, 1s));
|
|
}
|
|
} // namespace
|
|
int main(int argc, char** argv) {
|
|
try {
|
|
if (argc == 2 && std::string_view(argv[1]) == "--starting-wildlife-only") {
|
|
run_starting_wildlife_publication_tests();
|
|
return 0;
|
|
}
|
|
if (argc == 2 && std::string_view(argv[1]) == "--population-only") {
|
|
run_population_publication_tests();
|
|
return 0;
|
|
}
|
|
if (argc == 2 && std::string_view(argv[1]) == "--environment-only") {
|
|
run_environment_publication_tests();
|
|
return 0;
|
|
}
|
|
if (argc == 2 && std::string_view(argv[1]) == "--life-reservations-only") {
|
|
run_life_reservation_store_tests();
|
|
run_primitive_publication_tests();
|
|
std::puts("life reservation adapter integration passed");
|
|
return 0;
|
|
}
|
|
if (argc != 1)
|
|
return 2;
|
|
run();
|
|
std::puts("postgres adapter integration passed");
|
|
run_region_lease_store_tests();
|
|
run_account_control_store_tests();
|
|
run_life_reservation_store_tests();
|
|
run_primitive_publication_tests();
|
|
run_cellular_publication_tests();
|
|
run_population_publication_tests();
|
|
run_environment_publication_tests();
|
|
run_probe_handoff_store_tests();
|
|
run_region_action_tests();
|
|
std::puts("region lease fencing integration passed");
|
|
run_region_checkpoint_store_tests();
|
|
std::puts("region checkpoint publication integration passed");
|
|
run_region_host_tests();
|
|
std::puts("region host integration passed");
|
|
run_region_server_process_tests();
|
|
std::puts("region server process integration passed");
|
|
#ifdef EVO_POSTGRES_SESSIONS
|
|
run_region_session_tests();
|
|
std::puts("region dev session integration passed");
|
|
run_grant_broker_tests();
|
|
std::puts("grant broker integration passed");
|
|
#endif
|
|
#ifdef EVO_POSTGRES_M10
|
|
run_region_m10_session_tests();
|
|
run_region_handoff_process_tests();
|
|
#endif
|
|
run_region_recovery_tests();
|
|
std::puts("region recovery integration passed");
|
|
run_region_ops_tests();
|
|
std::puts("region ops integration passed");
|
|
run_region_clone_tests();
|
|
std::puts("region clone and history integration passed");
|
|
run_region_store_tests();
|
|
std::puts("region store qualification integration passed");
|
|
run_region_load_tests();
|
|
std::puts("region synthetic load integration passed");
|
|
run_region_upgrade_tests();
|
|
std::puts("region upgrade clone comparison integration passed");
|
|
run_region_migration_tests();
|
|
std::puts("region migration, barrier and qualification integration passed");
|
|
return 0;
|
|
} catch (const std::exception& e) {
|
|
std::fprintf(stderr, "%s\n", e.what());
|
|
return 1;
|
|
}
|
|
}
|