#include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { using namespace Void::Security; using namespace Void::SecurityAsync; Void::Coro::Task run_login( Authenticator& auth, LoginInput input, Void::Async::OperationOptions opts, std::optional>& out, std::atomic* done = nullptr) { out = co_await auth.login(std::move(input), std::move(opts)); if (done != nullptr) { done->fetch_add(1, std::memory_order_release); } } Void::Coro::Task run_authenticate( Authenticator& auth, const BearerToken& token, Void::Async::OperationOptions opts, std::optional>& out) { out = co_await auth.authenticate(token, std::move(opts)); } Void::Coro::Task run_revoke( Authenticator& auth, std::string session_id, Void::Async::OperationOptions opts, std::optional>& out) { out = co_await auth.revoke(std::move(session_id), std::move(opts)); } void pump_until(Void::Async::ManualExecutor& exec, const std::atomic& counter, int target) { const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds{15}; while (counter.load(std::memory_order_acquire) < target && std::chrono::steady_clock::now() < deadline) { if (exec.drain() == 0) { std::this_thread::sleep_for(std::chrono::milliseconds{1}); } } } } // namespace Void::Testing::Test security_async_login_auth_revoke_expiry_and_clock_rollback() { auto store = std::make_shared(); auto audit = std::make_shared(64); Void::Async::ManualExecutor exec; Void::Time::ManualClock mono_clock; Void::Time::ManualCivilClock civil_clock{ std::chrono::system_clock::time_point{std::chrono::seconds{1000000}}}; auto alice_hash = hash_password("correct-pw", PasswordHashStrength::Interactive); Void::Testing::require(alice_hash.has_value()); Void::Testing::require( store ->upsert_account(AccountRecord{ .subject = "sub-alice", .normalized_username = "alice", .password_hash = *alice_hash, .roles = {"user"}, .credential_version = 1, .revocation_generation = 1, .policy_generation = 3, .enabled = true, }) .has_value()); AuthenticatorOptions opts{}; opts.dummy_hash_strength = PasswordHashStrength::Interactive; opts.precomputed_dummy_hash = *alice_hash; opts.session.ttl = std::chrono::seconds{3600}; opts.session.max_total_sessions = 4; opts.session.max_per_subject = 2; opts.session.clock_rollback_tolerance = std::chrono::seconds{60}; auto auth = Authenticator::create(store, exec, opts, audit, mono_clock, civil_clock); Void::Testing::require(auth.has_value()); // 1. Unknown account and wrong password produce identical public denial but // distinct internal audit reasons. { std::optional> unknown_res; std::optional> wrong_pw_res; std::atomic done{0}; Void::Async::TaskScope scope{exec}; Void::Testing::require( scope .spawn(run_login(**auth, {"ghost", "any"}, {}, unknown_res, &done)) .has_value()); Void::Testing::require( scope .spawn(run_login(**auth, {"ALICE", "wrong-pw"}, {}, wrong_pw_res, &done)) .has_value()); pump_until(exec, done, 2); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(unknown_res.has_value() && !unknown_res->has_value()); Void::Testing::require(wrong_pw_res.has_value() && !wrong_pw_res->has_value()); Void::Testing::expect_eq(unknown_res->error().code().value, wrong_pw_res->error().code().value); Void::Testing::expect_eq(unknown_res->error().message(), wrong_pw_res->error().message()); const auto events = audit->snapshot(); bool saw_not_found = false; bool saw_invalid_pw = false; for (const auto& ev : events) { if (ev.internal_reason == "account_not_found") { saw_not_found = true; } if (ev.internal_reason == "invalid_password") { saw_invalid_pw = true; } } Void::Testing::expect(saw_not_found); Void::Testing::expect(saw_invalid_pw); } // 2. Successful login (case-insensitive username) + authoritative // authenticate + immediate revocation (zero positive cache). std::optional grant1; { std::optional> res; std::atomic done{0}; Void::Async::TaskScope scope{exec}; Void::Testing::require( scope .spawn(run_login(**auth, {" Alice ", "correct-pw"}, {}, res, &done)) .has_value()); pump_until(exec, done, 1); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(res.has_value() && res->has_value()); grant1.emplace(std::move(**res)); } { std::optional> principal; Void::Async::TaskScope scope{exec}; Void::Testing::require( scope .spawn(run_authenticate(**auth, grant1->bearer_token, {}, principal)) .has_value()); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(principal.has_value() && principal->has_value()); Void::Testing::expect_eq((*principal)->subject, std::string{"sub-alice"}); Void::Testing::expect_eq((*principal)->policy_generation, 3ULL); } // Revoke takes effect immediately on next authenticate call. { std::optional> rev_res; std::optional> after_rev; Void::Async::TaskScope scope{exec}; Void::Testing::require( scope .spawn(run_revoke(**auth, grant1->session_id, {}, rev_res)) .has_value()); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(rev_res.has_value() && rev_res->has_value()); Void::Async::TaskScope scope2{exec}; Void::Testing::require( scope2 .spawn(run_authenticate(**auth, grant1->bearer_token, {}, after_rev)) .has_value()); Void::Testing::require(scope2.join_pumping(exec).has_value()); Void::Testing::require(after_rev.has_value() && !after_rev->has_value()); Void::Testing::expect_eq( after_rev->error().code().value, static_cast(SecurityErrorCode::session_revoked)); } // 3. Per-subject session capacity (max_per_subject = 2) enforced, and // expiry + bounded cleanup reclaims slots. std::optional g2; std::optional g3; for (auto* target : {&g2, &g3}) { std::optional> res; std::atomic done{0}; Void::Async::TaskScope scope{exec}; Void::Testing::require( scope .spawn(run_login(**auth, {"alice", "correct-pw"}, {}, res, &done)) .has_value()); pump_until(exec, done, 1); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(res.has_value() && res->has_value()); target->emplace(std::move(**res)); } { std::optional> over_cap; std::atomic done{0}; Void::Async::TaskScope scope{exec}; Void::Testing::require( scope .spawn(run_login(**auth, {"alice", "correct-pw"}, {}, over_cap, &done)) .has_value()); pump_until(exec, done, 1); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(over_cap.has_value() && !over_cap->has_value()); Void::Testing::expect_eq( over_cap->error().code().value, static_cast( SecurityErrorCode::session_capacity_exceeded)); } // Advance civil clock past TTL -> authenticate denies expired, cleanup // removes sessions. Void::Testing::require( civil_clock.advance(std::chrono::seconds{3601}).has_value()); { std::optional> expired; Void::Async::TaskScope scope{exec}; Void::Testing::require( scope .spawn(run_authenticate(**auth, g2->bearer_token, {}, expired)) .has_value()); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(expired.has_value() && !expired->has_value()); Void::Testing::expect_eq( expired->error().code().value, static_cast(SecurityErrorCode::session_expired)); } auto cleaned = (*auth)->cleanup_expired_step(); Void::Testing::require(cleaned.has_value()); Void::Testing::expect_eq(*cleaned, 3U); // 4. Civil-clock rollback beyond 60s tolerance halts admission until // explicitly reconciled. civil_clock.set(civil_clock.now() - std::chrono::seconds{120}); { std::optional> halted; Void::Async::TaskScope scope{exec}; Void::Testing::require( scope .spawn(run_login(**auth, {"alice", "correct-pw"}, {}, halted)) .has_value()); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(halted.has_value() && !halted->has_value()); Void::Testing::expect_eq( halted->error().code().value, static_cast(SecurityErrorCode::clock_rollback)); } (*auth)->reconcile_clock(civil_clock.now()); // 5. Store outage fails closed. store->set_available(false); { std::optional> outage; Void::Async::TaskScope scope{exec}; Void::Testing::require( scope .spawn(run_login(**auth, {"alice", "correct-pw"}, {}, outage)) .has_value()); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(outage.has_value() && !outage->has_value()); Void::Testing::expect_eq( outage->error().code().value, static_cast(SecurityErrorCode::store_unavailable)); } store->set_available(true); Void::Testing::require((*auth)->close_and_join().has_value()); } Void::Testing::Test security_async_version_race_hash_stop_and_mandatory_audit() { auto store = std::make_shared(); auto audit = std::make_shared(1); // capacity 1! Void::Async::ManualExecutor exec; Void::Time::ManualClock mono_clock; Void::Time::ManualCivilClock civil_clock{ std::chrono::system_clock::time_point{std::chrono::seconds{2000000}}}; auto pw_hash = hash_password("hunter2", PasswordHashStrength::Interactive); Void::Testing::require(pw_hash.has_value()); Void::Testing::require( store ->upsert_account(AccountRecord{ .subject = "sub-bob", .normalized_username = "bob", .password_hash = *pw_hash, .roles = {"member"}, .credential_version = 1, .revocation_generation = 1, .policy_generation = 1, .enabled = true, }) .has_value()); // 1. Stop during in-flight password hash: worker drains to completion, // releases memory budget, discards outcome, and creates zero sessions. { std::promise entered_hash; std::promise release_hash; auto entered_fut = entered_hash.get_future(); auto release_fut = release_hash.get_future().share(); AuthenticatorOptions opts{}; opts.dummy_hash_strength = PasswordHashStrength::Interactive; opts.precomputed_dummy_hash = *pw_hash; opts.before_password_verify_hook = [&]() { entered_hash.set_value(); release_fut.wait(); }; auto auth = Authenticator::create(store, exec, opts, nullptr, mono_clock, civil_clock); Void::Testing::require(auth.has_value()); std::stop_source ss; std::optional> res; std::atomic done{0}; Void::Async::TaskScope scope{exec}; Void::Testing::require( scope .spawn(run_login(**auth, {"bob", "hunter2"}, Void::Async::OperationOptions{ .stop = ss.get_token()}, res, &done)) .has_value()); (void)exec.drain(); // Wait until worker actually holds password job budget. entered_fut.wait(); Void::Testing::expect_eq((*auth)->stats().active_password_jobs, 1U); Void::Testing::expect((*auth)->stats().reserved_password_memory_bytes > 0U); ss.request_stop(); release_hash.set_value(); pump_until(exec, done, 1); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(res.has_value() && !res->has_value()); Void::Testing::expect_eq( res->error().code().value, static_cast(SecurityErrorCode::cancelled)); Void::Testing::expect_eq(store->session_count(), 0U); Void::Testing::expect_eq((*auth)->stats().active_password_jobs, 0U); Void::Testing::expect_eq( (*auth)->stats().reserved_password_memory_bytes, 0U); Void::Testing::require((*auth)->close_and_join().has_value()); } // 2. Credential version change racing with login invalidates session write. { AuthenticatorOptions opts{}; opts.dummy_hash_strength = PasswordHashStrength::Interactive; opts.precomputed_dummy_hash = *pw_hash; opts.before_password_verify_hook = [&]() { (void)store->bump_credential_version("sub-bob"); }; auto auth = Authenticator::create(store, exec, opts, nullptr, mono_clock, civil_clock); Void::Testing::require(auth.has_value()); std::optional> res; std::atomic done{0}; Void::Async::TaskScope scope{exec}; Void::Testing::require( scope .spawn(run_login(**auth, {"bob", "hunter2"}, {}, res, &done)) .has_value()); pump_until(exec, done, 1); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(res.has_value() && !res->has_value()); Void::Testing::expect_eq( res->error().code().value, static_cast(SecurityErrorCode::conflict)); Void::Testing::expect_eq(store->session_count(), 0U); Void::Testing::require((*auth)->close_and_join().has_value()); } // 3. Mandatory fail-closed audit queue full revokes session and fails login. { // Pre-fill the 1-slot audit sink. Void::Testing::require( audit->record(AuditEvent{.event_type = "fill"}).has_value()); AuthenticatorOptions opts{}; opts.dummy_hash_strength = PasswordHashStrength::Interactive; opts.precomputed_dummy_hash = *pw_hash; opts.audit_policy = AuditFailurePolicy::FailClosed; auto auth = Authenticator::create(store, exec, opts, audit, mono_clock, civil_clock); Void::Testing::require(auth.has_value()); std::optional> res; std::atomic done{0}; Void::Async::TaskScope scope{exec}; Void::Testing::require( scope .spawn(run_login(**auth, {"bob", "hunter2"}, {}, res, &done)) .has_value()); pump_until(exec, done, 1); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(res.has_value() && !res->has_value()); Void::Testing::expect_eq( res->error().code().value, static_cast(SecurityErrorCode::audit_failed)); Void::Testing::require((*auth)->close_and_join().has_value()); } }