Files
CoopAllTheThings/tests/shared_memory_test.cpp
BlackMark 2802fbddf6 Add misc unit tests + harden the WAV chunk walk
Fills small coverage gaps:
- shared_memory_test: SharedMemory create-or-open aliasing, move (steal + empty
  the source, no double-free), reset, open-missing.
- wav_test: malformed input -- truncation, bad magic, missing data chunk,
  over-long data size (clamps), odd-sized chunk (word-align skip), and a corrupt
  ~4 GB chunk_size. The reader gains an advance guard so that last case can't wrap
  pos on a 32-bit size_t (x86) or spin the walk; it stops cleanly.
- tool_paths_test: deployed_artifact_path resolution -- next-to-exe, one-dir-up,
  and the not-found fallback -- with real marker files.
- audio_ring_test: an overrun-at-the-seam case (write head near the end: a
  wrapping push that fits vs. an over-capacity wrapping push dropped whole),
  exercising the wrap split + overrun together, not just at offset 0.

The injector bitness check isn't added as a unit test: is_wow64_process is
file-local and the real WOW64 path needs a 32-bit target, so it stays
inspection-covered (and exercised by the x86 injection path).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-24 02:05:35 +02:00

81 lines
2.7 KiB
C++

// Unit test for the SharedMemory RAII wrapper (common/include/coop/shared_memory.hpp): create-or-open
// aliasing, move (steal handles + leave the source empty, no double-free), reset, and open-missing.
#include <cstdint>
#include <cstdio>
#include <string>
#include <windows.h>
#include "coop/shared_memory.hpp"
using namespace coop;
namespace
{
int g_failures = 0;
void check(bool ok, const char* what)
{
std::printf("%s %s\n", ok ? " ok:" : "FAIL:", what);
if (!ok)
{
++g_failures;
}
}
std::wstring uniq_name()
{
return L"Local\\coop_shmtest_" + std::to_wstring(GetCurrentProcessId());
}
} // namespace
int main()
{
const std::wstring name = uniq_name();
constexpr std::size_t kSize = 4096;
check(!SharedMemory{}.valid(), "default-constructed: not valid");
{
SharedMemory a;
check(a.create(name, kSize) && a.valid(), "create a named section");
a.as<std::uint32_t>()[0] = 0xC0FFEEu;
// open() the same name -> a second view of the SAME section (aliases a's memory).
SharedMemory b;
check(b.open(name, kSize) && b.valid(), "open the existing section");
check(b.as<std::uint32_t>()[0] == 0xC0FFEEu, "open aliases the same memory (sees the write)");
b.as<std::uint32_t>()[1] = 0x1234u;
check(a.as<std::uint32_t>()[1] == 0x1234u, "writes through one view are visible in the other");
// create() again with the same name -> opens the existing section (create-or-open).
SharedMemory c;
check(c.create(name, kSize) && c.valid() && c.as<std::uint32_t>()[0] == 0xC0FFEEu,
"create-or-open: a second create sees the existing data");
// Move: the destination owns the mapping, the source is emptied (no double-free at scope end).
const void* a_ptr = a.data();
SharedMemory moved = std::move(a);
check(moved.valid() && moved.data() == a_ptr, "move-construct steals the view");
check(!a.valid() && a.data() == nullptr, "moved-from source is emptied");
check(moved.as<std::uint32_t>()[0] == 0xC0FFEEu, "moved view still maps the section");
SharedMemory move_assigned;
move_assigned.create(uniq_name() + L"_x", kSize); // give it something to reset first
move_assigned = std::move(moved);
check(move_assigned.valid() && !moved.valid(), "move-assign steals and empties the source");
check(move_assigned.as<std::uint32_t>()[1] == 0x1234u, "move-assigned view sees prior writes");
move_assigned.reset();
check(!move_assigned.valid() && move_assigned.data() == nullptr, "reset releases the mapping");
}
// open() a name that no longer exists (all handles closed at scope end above) -> false.
{
SharedMemory gone;
check(!gone.open(name, kSize), "open a non-existent section returns false");
}
std::printf(g_failures == 0 ? "PASS shared_memory_test\n" : "FAILED shared_memory_test (%d)\n", g_failures);
return g_failures == 0 ? 0 : 1;
}