#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { std::span as_bytes(std::string_view text) { return std::as_bytes(std::span{text.data(), text.size()}); } struct ScopedTempDir { std::filesystem::path path; ScopedTempDir() { static std::atomic seq{0}; const auto id = seq.fetch_add(1, std::memory_order_relaxed); const auto ts = std::chrono::steady_clock::now().time_since_epoch().count(); path = std::filesystem::temp_directory_path() / ("void_crypto_async_" + std::to_string(ts) + "_" + std::to_string(id)); std::filesystem::create_directories(path); } ~ScopedTempDir() { std::error_code ec; std::filesystem::remove_all(path, ec); } }; Void::Coro::Task run_hash_stream( Void::Crypto::HashAlgorithm alg, Void::IO::Driver& driver, Void::IO::ByteStream& stream, Void::Async::OperationOptions op, Void::CryptoAsync::StreamHashOptions hash_opts, std::optional>& out) { out = co_await Void::CryptoAsync::hash_stream( alg, driver, stream, std::move(op), std::move(hash_opts)); } Void::Coro::Task run_hash_file( Void::CryptoAsync::AsyncFileHasher& hasher, Void::Crypto::HashAlgorithm alg, std::filesystem::path path, Void::Async::OperationOptions op, std::optional>& out, std::atomic* done_counter = nullptr) { out = co_await hasher.hash_file(alg, std::move(path), std::move(op)); if (done_counter != nullptr) { done_counter->fetch_add(1, std::memory_order_release); } } } // namespace Void::Testing::Test crypto_async_hash_stream_eof_errors_and_cancellation() { using namespace Void::Crypto; using namespace Void::CryptoAsync; Void::Async::ManualExecutor exec; Void::Time::ManualClock clock; Void::IO::BackendOptions opts{}; opts.preference = Void::IO::BackendPreference::mock; opts.limits.max_owned_buffer_bytes = 128 * 1024; auto drv = Void::IO::Driver::create(exec, opts, clock); Void::Testing::require(drv.has_value()); auto* mock = (*drv)->mock_controller(); Void::Testing::require(mock != nullptr); // 1. Multi-chunk stream matches sha256("hello async world"). { auto stream = (*drv)->create_mock_stream(); Void::Testing::require(stream.has_value()); std::optional> outcome; Void::Async::TaskScope scope{exec}; auto handle = scope.spawn( run_hash_stream(HashAlgorithm::Sha256, **drv, *stream, {}, StreamHashOptions{.chunk_bytes = 8}, outcome)); Void::Testing::require(handle.has_value()); (void)exec.drain(); Void::Testing::require( mock->complete_read(*stream, as_bytes("hello "), false)); (void)exec.drain(); Void::Testing::require( mock->complete_read(*stream, as_bytes("async "), false)); (void)exec.drain(); Void::Testing::require( mock->complete_read(*stream, as_bytes("world"), true)); (void)exec.drain(); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(outcome.has_value() && outcome->has_value()); auto expected = sha256("hello async world"); Void::Testing::require(expected.has_value()); Void::Testing::expect_eq(**outcome, *expected); } // 2. Partial read then error must never produce a digest. { auto stream = (*drv)->create_mock_stream(); Void::Testing::require(stream.has_value()); std::optional> outcome; Void::Async::TaskScope scope{exec}; auto handle = scope.spawn( run_hash_stream(HashAlgorithm::Sha256, **drv, *stream, {}, StreamHashOptions{.chunk_bytes = 16}, outcome)); Void::Testing::require(handle.has_value()); (void)exec.drain(); Void::Testing::require( mock->complete_read(*stream, as_bytes("partial"), false)); (void)exec.drain(); Void::Testing::require(mock->fail_read( *stream, Void::IO::make_io_error(Void::IO::IoErrorCode::os_error, "injected read failure"))); (void)exec.drain(); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(outcome.has_value() && !outcome->has_value()); Void::Testing::expect_eq( outcome->error().code().value, static_cast(CryptoErrorCode::io_error)); } // 3. Stop requested racing with EOF completion must not return a digest. { auto stream = (*drv)->create_mock_stream(); Void::Testing::require(stream.has_value()); std::stop_source stop_src; std::optional> outcome; Void::Async::TaskScope scope{exec}; auto handle = scope.spawn(run_hash_stream( HashAlgorithm::Sha256, **drv, *stream, Void::Async::OperationOptions{.stop = stop_src.get_token()}, StreamHashOptions{.chunk_bytes = 16}, outcome)); Void::Testing::require(handle.has_value()); (void)exec.drain(); auto tok = mock->try_acquire_read(*stream); Void::Testing::require(tok.has_value()); stop_src.request_stop(); (void)mock->finish_read_data(*tok, as_bytes("done"), true); (void)exec.drain(); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(outcome.has_value() && !outcome->has_value()); Void::Testing::expect_eq( outcome->error().code().value, static_cast(CryptoErrorCode::cancelled)); } // 4. Deadline expiry between chunks returns timeout. { auto stream = (*drv)->create_mock_stream(); Void::Testing::require(stream.has_value()); const auto deadline = clock.now() + std::chrono::seconds{5}; std::optional> outcome; Void::Async::TaskScope scope{exec}; auto handle = scope.spawn(run_hash_stream( HashAlgorithm::Sha256, **drv, *stream, Void::Async::OperationOptions{.deadline = deadline}, StreamHashOptions{.chunk_bytes = 16}, outcome)); Void::Testing::require(handle.has_value()); (void)exec.drain(); Void::Testing::require( clock.advance(std::chrono::seconds{10}).has_value()); Void::Testing::require( mock->complete_read(*stream, as_bytes("chunk1"), false)); (void)exec.drain(); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(outcome.has_value() && !outcome->has_value()); Void::Testing::expect_eq( outcome->error().code().value, static_cast(CryptoErrorCode::timeout)); } // 5. Buffer budget exhaustion returns resource_exhausted before reading. { auto hold = (*drv)->allocate_buffer(128 * 1024); Void::Testing::require(hold.has_value()); auto stream = (*drv)->create_mock_stream(); Void::Testing::require(stream.has_value()); std::optional> outcome; Void::Async::TaskScope scope{exec}; auto handle = scope.spawn( run_hash_stream(HashAlgorithm::Sha256, **drv, *stream, {}, StreamHashOptions{.chunk_bytes = 1024}, outcome)); Void::Testing::require(handle.has_value()); (void)exec.drain(); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(outcome.has_value() && !outcome->has_value()); Void::Testing::expect_eq( outcome->error().code().value, static_cast(CryptoErrorCode::resource_exhausted)); } Void::Testing::require((*drv)->shutdown_and_join().has_value()); } Void::Testing::Test crypto_async_file_hasher_bounds_and_shutdown() { using namespace Void::Crypto; using namespace Void::CryptoAsync; ScopedTempDir tmp; const auto file_path = tmp.path / "sample.bin"; { std::ofstream out{file_path, std::ios::binary}; std::string payload(200 * 1024, 'V'); out.write(payload.data(), static_cast(payload.size())); } auto sync_digest = sha256File(file_path); Void::Testing::require(sync_digest.has_value()); Void::Async::ManualExecutor exec; AsyncFileHasher hasher{ exec, FileHasherOptions{.max_concurrent = 2, .max_waiting = 4, .buffer_budget_bytes = 256 * 1024, .chunk_bytes = 16 * 1024}}; std::array>, 4> results{}; std::atomic done_count{0}; { Void::Async::TaskScope scope{exec}; for (std::size_t i = 0; i < results.size(); ++i) { auto h = scope.spawn(run_hash_file(hasher, HashAlgorithm::Sha256, file_path, {}, results[i], &done_count)); Void::Testing::require(h.has_value()); } const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds{10}; while (done_count.load(std::memory_order_acquire) < 4 && std::chrono::steady_clock::now() < deadline) { if (exec.drain() == 0) { std::this_thread::sleep_for(std::chrono::milliseconds{1}); } } Void::Testing::require(scope.join_pumping(exec).has_value()); } for (const auto& r : results) { Void::Testing::require(r.has_value() && r->has_value()); Void::Testing::expect_eq(**r, *sync_digest); } // Pre-cancelled admission returns cancelled without starting. { std::stop_source ss; ss.request_stop(); std::optional> cancelled; Void::Async::TaskScope scope{exec}; auto h = scope.spawn(run_hash_file( hasher, HashAlgorithm::Sha256, file_path, Void::Async::OperationOptions{.stop = ss.get_token()}, cancelled)); Void::Testing::require(h.has_value()); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(cancelled.has_value() && !cancelled->has_value()); Void::Testing::expect_eq( cancelled->error().code().value, static_cast(CryptoErrorCode::cancelled)); } // Shutdown stops admission cleanly and leaves zero active/reserved budget. Void::Testing::require(hasher.close_and_join().has_value()); const auto st = hasher.stats(); Void::Testing::expect(st.closing); Void::Testing::expect_eq(st.active, 0U); Void::Testing::expect_eq(st.reserved_buffer_bytes, 0U); { std::optional> after_close; Void::Async::TaskScope scope{exec}; auto h = scope.spawn(run_hash_file(hasher, HashAlgorithm::Sha256, file_path, {}, after_close)); Void::Testing::require(h.has_value()); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(after_close.has_value() && !after_close->has_value()); Void::Testing::expect_eq( after_close->error().code().value, static_cast(CryptoErrorCode::closed)); } // Budget smaller than chunk_bytes fails with resource_exhausted. AsyncFileHasher tiny{ exec, FileHasherOptions{.max_concurrent = 1, .max_waiting = 1, .buffer_budget_bytes = 1024, .chunk_bytes = 4096}}; { std::optional> budget_err; Void::Async::TaskScope scope{exec}; auto h = scope.spawn(run_hash_file(tiny, HashAlgorithm::Sha256, file_path, {}, budget_err)); Void::Testing::require(h.has_value()); Void::Testing::require(scope.join_pumping(exec).has_value()); Void::Testing::require(budget_err.has_value() && !budget_err->has_value()); Void::Testing::expect_eq( budget_err->error().code().value, static_cast(CryptoErrorCode::resource_exhausted)); } Void::Testing::require(tiny.close_and_join().has_value()); }