java-topology/tests/unit/Moad0002UnitTest.java
russell@unturf.com 4888153e40 feat: add unit, integration, and functional test coverage for all 9 MOADs
Each MOAD now has a synthetic defective specimen and fixed specimen
proven from first principles across three test tiers:

Unit (tests/unit/Moad000X*.java):
  - Correctness: defective and fixed produce identical functional output
  - Defect behavior: defective specimen exhibits the defect (measurable)
  - Fix behavior: fixed specimen eliminates the defect

Integration (tests/integration/AllMoadsIntegrationTest.java):
  - All 9 MOADs proven at medium scale (N=500-2000)
  - MOAD-0001: O(N^2) vs O(N) list scan at N=1000
  - MOAD-0002: 500 sessions trample each other (defective) vs coexist (fixed)
  - MOAD-0003: 250 anonymous requests leak auth identity (defective) vs zero (fixed)
  - MOAD-0004: 3000 credential exposures across 1000 requests (defective) vs zero (fixed)
  - MOAD-0005: 500 computes for 500 concurrent misses vs exactly 1
  - MOAD-0006: all 500 passwords extractable from DB (defective) vs unextractable (fixed)
  - MOAD-0007: N=2000 spatial objects, defective visits all 2000 vs O(log N + k)
  - MOAD-0009: 990 wasted firings for 1000 ticks / 10 events vs zero waste
  - MOAD-0011: 10240 NFA steps vs 13 steps on N=12 adversarial input (788x)

Functional (tests/functional/AllMoadsFunctionalTest.java):
  - MOAD-0005: real-thread contention proves herd (defective >1 compute, fixed exactly 1)
  - MOAD-0007: N=50000 spatial objects, 50M defective probes vs 516K fixed (97x speedup)
  - MOAD-0009: 10000 ticks / 10 events, 9990 wasted firings vs zero (1000x ratio)
  - MOAD-0011: N=16 adversarial, 163840 defective steps vs 17 fixed (9638x ratio)

Support algorithms (tests/support/Moad000X*.java):
  - Moad0002Algorithm: shared mutable global state (DefectiveAudioSystem / FixedAudioSystem + Context)
  - Moad0003Algorithm: ThreadLocal not cleared (handleDefective / handleFixed with finally)
  - Moad0004Algorithm: HTTP headers logged verbatim (logDefective / logFixed with CREDENTIAL_HEADERS denylist)
  - Moad0005Algorithm: get+null+compute+put (DefectiveCache HashMap / FixedCache ConcurrentHashMap.computeIfAbsent)
  - Moad0006Algorithm: Base64 password storage (DefectiveCredentialStore / FixedCredentialStore SHA-256+salt)
  - Moad0007Algorithm: linear spatial scan (queryDefective list / queryFixed sorted array + binary search)
  - Moad0009Algorithm: timer-driven polling (runDefectiveScheduler / runFixedEventDriven)
  - Moad0011Algorithm: PCRE nested quantifiers (matchDefective backtracking NFA / matchFixed linear NFA)

Makefile: added unit-moad-0002 through unit-moad-0011 targets,
integration-all-moads, functional-all-moads. integration and functional
targets now depend on all-MOADs variants.
2026-04-12 15:43:19 -04:00

155 lines
7.2 KiB
Java

package unit;
import support.Moad0002Algorithm;
import support.Moad0002Algorithm.Context;
import support.Moad0002Algorithm.DefectiveAudioSystem;
import support.Moad0002Algorithm.DefectiveDisplaySystem;
import support.Moad0002Algorithm.FixedAudioSystem;
import support.Moad0002Algorithm.FixedDisplaySystem;
/**
* Unit tests for MOAD-0002: An Intertangled Defect.
*
* Proves from first principles:
* 1. Defective: AudioSystem.setLocale() mutates state seen by DisplaySystem —
* cross-subsystem interference through shared global state.
* 2. Defective: two independent "contexts" sharing GLOBAL cannot coexist —
* the second write tramples the first silently.
* 3. Fixed: two Context objects carry independent values simultaneously.
* 4. Fixed: AudioSystem and DisplaySystem read from separate Context objects
* without interfering with each other.
*
* No build tool required. Compile and run:
*
* cd tests
* java -m jdk.compiler/com.sun.tools.javac.Main -cp . \
* support/Moad0002Algorithm.java unit/Moad0002UnitTest.java
* java -cp . unit.Moad0002UnitTest
*/
public class Moad0002UnitTest {
private static int passed = 0;
private static int failed = 0;
public static void main(String[] args) {
System.out.println("=== Moad0002UnitTest (An Intertangled Defect) ===\n");
System.out.println("-- Correctness: subsystems read values correctly --");
testDefectiveBasicRead();
testFixedBasicRead();
System.out.println("\n-- Defect: shared global state causes cross-subsystem interference --");
testDefectiveAudioLocaleLeaksToDisplay();
testDefectiveTwoContextsTrample();
System.out.println("\n-- Fix: isolated Context objects prevent interference --");
testFixedTwoContextsCoexist();
testFixedSubsystemsDoNotInterfere();
System.out.printf("\n%d passed, %d failed%n", passed, failed);
if (failed > 0) System.exit(1);
}
// ── Correctness ───────────────────────────────────────────────────────────
static void testDefectiveBasicRead() {
Moad0002Algorithm.resetGlobal();
DefectiveAudioSystem audio = new DefectiveAudioSystem();
audio.setVolume(80);
assertEqual("defective audio: volume reads back correctly", 80, audio.getVolume());
}
static void testFixedBasicRead() {
Context ctx = new Context(70, 200, "fr");
FixedAudioSystem audio = new FixedAudioSystem(ctx);
FixedDisplaySystem display = new FixedDisplaySystem(ctx);
assertEqual("fixed audio: volume", 70, audio.getVolume());
assertEqual("fixed display: brightness", 200, display.getBrightness());
assertEqual("fixed audio: locale", "fr", audio.getLocale());
assertEqual("fixed display: locale", "fr", display.getLocale());
}
// ── Defect ────────────────────────────────────────────────────────────────
static void testDefectiveAudioLocaleLeaksToDisplay() {
Moad0002Algorithm.resetGlobal();
DefectiveAudioSystem audio = new DefectiveAudioSystem();
DefectiveDisplaySystem display = new DefectiveDisplaySystem();
// AudioSystem configures locale — conceptually an audio concern (TTS language)
audio.setLocale("ja");
// DEFECT: DisplaySystem reads the same global — sees audio's mutation
// This is the intertangle: two subsystems that should be independent
// are coupled through a shared mutable state object.
String displayLocale = display.getLocale();
assertEqual("defective: audio locale write leaks into display", "ja", displayLocale);
}
static void testDefectiveTwoContextsTrample() {
Moad0002Algorithm.resetGlobal();
// Two independent "sessions" each configure their own audio subsystem.
DefectiveAudioSystem session1 = new DefectiveAudioSystem();
DefectiveAudioSystem session2 = new DefectiveAudioSystem();
session1.setVolume(30); // session 1 sets volume to 30
session2.setVolume(90); // session 2 sets volume to 90 — TRAMPLES session 1
// DEFECT: session 1 now sees 90, not 30 — its configuration was silently destroyed.
// Two contexts cannot coexist when they share a global state object.
int session1Volume = session1.getVolume();
assertEqual("defective: session2 write tramples session1 volume", 90, session1Volume);
}
// ── Fix ───────────────────────────────────────────────────────────────────
static void testFixedTwoContextsCoexist() {
Context ctx1 = new Context(30, 150, "en");
Context ctx2 = new Context(90, 200, "ja");
FixedAudioSystem s1 = new FixedAudioSystem(ctx1);
FixedAudioSystem s2 = new FixedAudioSystem(ctx2);
// FIX: both values coexist — ctx1 and ctx2 are independent objects
assertEqual("fixed: session1 volume preserved after session2 created", 30, s1.getVolume());
assertEqual("fixed: session2 volume independent of session1", 90, s2.getVolume());
assertEqual("fixed: session1 locale preserved", "en", s1.getLocale());
assertEqual("fixed: session2 locale independent", "ja", s2.getLocale());
}
static void testFixedSubsystemsDoNotInterfere() {
Context audioCtx = new Context(75, 0, "de");
Context displayCtx = new Context(0, 180, "fr");
FixedAudioSystem audio = new FixedAudioSystem(audioCtx);
FixedDisplaySystem display = new FixedDisplaySystem(displayCtx);
// FIX: each subsystem reads only its own context — no cross-contamination
assertEqual("fixed: audio locale unaffected by display context", "de", audio.getLocale());
assertEqual("fixed: display locale unaffected by audio context", "fr", display.getLocale());
assertEqual("fixed: display brightness from own context", 180, display.getBrightness());
assertEqual("fixed: audio volume from own context", 75, audio.getVolume());
}
// ── Helpers ───────────────────────────────────────────────────────────────
static void assertEqual(String label, int expected, int actual) {
if (expected == actual) {
System.out.printf(" PASS: %s%n", label);
passed++;
} else {
System.out.printf(" FAIL: %s — expected %d, got %d%n", label, expected, actual);
failed++;
}
}
static void assertEqual(String label, String expected, String actual) {
if (expected == null ? actual == null : expected.equals(actual)) {
System.out.printf(" PASS: %s%n", label);
passed++;
} else {
System.out.printf(" FAIL: %s — expected '%s', got '%s'%n", label, expected, actual);
failed++;
}
}
}