// Pool scope: two tenants across pg and sqlite checkouts, GUC set/reset, // dirty slot refusal, registry cap, 100k checkout leak check, ORM scoping // and RLS proof strings. #include "mini.hpp" #include #include "tenancy/adapters/db.h" #include "tenancy/orm_scope.h" #include "tenancy/pool_factory.h" #include "tenancy/rls_policy.h" namespace { using molo::tenancy::adapters::db::Backend; using molo::tenancy::adapters::db::DbError; using molo::tenancy::OrmScope; using molo::tenancy::PoolFactory; using molo::tenancy::RlsPolicy; } // namespace TEST(two_tenant_checkout_both_backends) { PoolFactory f; { auto a = f.acquire("acme", Backend::Postgres); CHECK(a.active()); CHECK(a.slug() == "acme"); CHECK(a.guc() == "acme"); CHECK_EQ(f.active_count(), 1u); auto b = f.acquire("globex", Backend::Sqlite); CHECK(b.guc() == "globex"); CHECK(b.backend() == Backend::Sqlite); CHECK_EQ(f.active_count(), 2u); } CHECK_EQ(f.active_count(), 0u); CHECK_EQ(f.tenant_count(), 2u); auto c = f.acquire("globex", Backend::Postgres); CHECK(c.guc() == "globex"); c.release(); CHECK(!c.active()); CHECK_EQ(f.active_count(), 0u); } TEST(dirty_slot_refused) { PoolFactory f; { auto a = f.acquire("acme", Backend::Postgres); } CHECK_EQ(f.slot_count(), 1u); f.DebugDirtySlotForTest(0, "stale-tenant"); bool threw = false; try { auto b = f.acquire("globex", Backend::Postgres); } catch (const DbError& e) { threw = true; CHECK(e.code() == "scope_dirty"); } CHECK(threw); f.DebugDirtySlotForTest(0, ""); auto c = f.acquire("globex", Backend::Postgres); CHECK(c.guc() == "globex"); } TEST(invalid_slug_rejected) { PoolFactory f; bool threw = false; try { auto a = f.acquire("Bad Slug!!", Backend::Postgres); } catch (const DbError& e) { threw = true; CHECK(e.code() == "invalid_tenant"); } CHECK(threw); CHECK_EQ(f.tenant_count(), 0u); } TEST(registry_cap) { PoolFactory f; for (int i = 0; i < 100000; ++i) { auto l = f.acquire("t" + std::to_string(i), Backend::Postgres); } CHECK_EQ(f.tenant_count(), 100000u); CHECK_EQ(f.active_count(), 0u); bool threw = false; try { auto l = f.acquire("one-too-many", Backend::Postgres); } catch (const DbError& e) { threw = true; CHECK(e.code() == "too_many_tenants"); } CHECK(threw); } TEST(checkout_leak_loop) { PoolFactory f; for (int i = 0; i < 100000; ++i) { auto a = f.acquire(i % 2 == 0 ? "acme" : "globex", i % 3 == 0 ? Backend::Sqlite : Backend::Postgres); CHECK(a.guc() == (i % 2 == 0 ? "acme" : "globex")); } CHECK_EQ(f.active_count(), 0u); CHECK_EQ(f.tenant_count(), 2u); CHECK(f.slot_count() <= 4u); } TEST(orm_scope_injection) { std::string scoped = OrmScope::ApplySelect("SELECT * FROM orders", "acme", false); CHECK(scoped.find("tenant_id = 'acme'") != std::string::npos); std::string with_where = OrmScope::ApplySelect("SELECT * FROM orders WHERE total > 10", "acme", false); CHECK(with_where.find("WHERE tenant_id = 'acme' AND") != std::string::npos); std::string with_order = OrmScope::ApplySelect("SELECT * FROM orders ORDER BY id", "acme", false); CHECK(with_order.find("WHERE tenant_id = 'acme'") != std::string::npos); CHECK(with_order.find("ORDER BY") != std::string::npos); std::string upd = OrmScope::ApplyUpdate("UPDATE orders SET total = 1", "acme", false); CHECK(upd.find("tenant_id = 'acme'") != std::string::npos); std::string priv = OrmScope::ApplySelect("SELECT * FROM orders", "acme", true); CHECK(priv == "SELECT * FROM orders"); std::string already = OrmScope::ApplySelect("SELECT * FROM orders WHERE tenant_id = 'x'", "acme", false); CHECK(already.find("AND") == std::string::npos); std::string ins = OrmScope::InsertWithTenant("orders", "total", "?", "acme"); CHECK(ins.find("tenant_id") != std::string::npos); CHECK(ins.find("'acme'") != std::string::npos); CHECK(OrmScope::KeyPrefix("acme") == "tenant:acme:"); CHECK(OrmScope::ScopedChannel("acme", "events") == "tenant:acme:events"); std::string quote = OrmScope::FilterClause("o'brien"); CHECK(quote == "tenant_id = 'o''brien'"); } TEST(rls_proof) { PoolFactory f; auto lease = f.acquire("acme", Backend::Postgres); std::string set_local = RlsPolicy::SetLocal(lease.guc()); CHECK(set_local == "SET LOCAL app.current_tenant = 'acme'"); std::string scoped = OrmScope::ApplySelect("SELECT * FROM orders", lease.guc(), false); CHECK(scoped.find("tenant_id = 'acme'") != std::string::npos); CHECK(RlsPolicy::ResetLocal() == "RESET app.current_tenant"); std::string policy = RlsPolicy::PolicyDdl("orders"); CHECK(policy.find("current_setting('app.current_tenant', true)") != std::string::npos); CHECK(policy.find("tenant_id") != std::string::npos); CHECK(RlsPolicy::EnableRls("orders").find("ENABLE ROW LEVEL SECURITY") != std::string::npos); lease.release(); CHECK(lease.guc() == ""); } TEST_MAIN()