java-topology/defects/jasmine/tests/test-jasmine-cwe407.py
russell@unturf.com d67ec93a5d test-frameworks wave 3: vitest + testng + jasmine + libcheck (4 patches)
vitest-0001: coverage-v8 coverage.result.find inside merged.result.forEach
  -> Map<url, result> lookup. Bench: 824x at N=M=10000 coverage entries.

testng-0001: DynamicGraph.toDot freeNodes.contains inside two for-each
  loops -> Map<T, String> color lookup via getOrDefault. Bench: 64x at N=2000.

jasmine-0001: SpyRegistry.spyOnAllFunctions propertiesToSkip.indexOf inside
  Array.filter + .concat growth across D prototype levels -> Set.has + O(1)
  growth. Bench: 61x at D=10, P=300.

check-0001: libcheck suite_tcase linear strcmp scan over tclst List
  -> parallel hashtable for O(1) lookup amortized. Bench: 117x at N=1000.
  Shipped as design sketch; full integration requires companion hashtable.

Also ships whitepaper/outreach/test-harness-survey.md documenting 14
clean-scan frameworks across Clojure, OCaml, Haskell, Erlang, Go, F#,
Julia, Shell, Lua, JS. Scope covered 61 targets across 30+ languages.

UNDF IDs: 1292 (check), 1293 (jasmine), 1294 (testng), 1295 (vitest).
All 12 tests pass.
2026-04-23 08:54:44 -04:00

80 lines
2.7 KiB
Python

#!/usr/bin/env python3
# UNDF: UNDF-2026-000001293 (jasmine-0001)
#
# CWE-407: Algorithmic Complexity
#
# Defect:
# jasmine-0001: SpyRegistry.spyOnAllFunctions walks prototype chain,
# filtering via propertiesToSkip.indexOf(prop) inside an
# Array.filter + concat. For chain depth D with P properties
# per level, cost is O(D * P^2).
#
# Fix:
# Replace propertiesToSkip Array with Set; filter lookup and growth both
# O(1). Total cost drops to O(D * P).
#
# Complexity gate (from bench/results.txt):
# D=10, P=300: defective=54ms, fixed=0.9ms (61x).
# Fixed must complete in <5ms at D=5, P=200. k-scaling <17.5x.
import importlib.util, os, sys, unittest
HERE = os.path.dirname(os.path.abspath(__file__))
BENCH = os.path.join(os.path.dirname(HERE), "bench")
sys.path.insert(0, BENCH)
def _load(fname):
path = os.path.join(BENCH, fname)
spec = importlib.util.spec_from_file_location(fname, path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
_mod = _load("bench-jasmine-0001.py")
class TestJasmine0001Correctness(unittest.TestCase):
def test_set_filter_matches_list_indexof_filter(self):
# Both filters should produce the same surviving set of properties
seen_list = []
seen_set = set()
levels = [
["a", "b", "c"],
["b", "c", "d", "e"], # b, c are duplicates
["d", "e", "f", "g"],
]
result_list = []
result_set = []
for level in levels:
new_list = [p for p in level if p not in seen_list]
seen_list = seen_list + new_list
result_list.extend(new_list)
new_set = [p for p in level if p not in seen_set]
for p in new_set:
seen_set.add(p)
result_set.extend(new_set)
self.assertEqual(result_list, result_set)
self.assertEqual(result_set, ["a", "b", "c", "d", "e", "f", "g"])
class TestJasmine0001ComplexityGate(unittest.TestCase):
def test_fixed_wallclock_D5_P200(self):
t_s = min(_mod.bench_fixed(5, 200) for _ in range(3))
self.assertLess(t_s * 1000, 5.0,
f"fixed took {t_s*1000:.3f}ms at D=5 P=200, expected <5ms")
def test_fixed_scaling_linear(self):
t_small = min(_mod.bench_fixed(5, 100) for _ in range(3))
t_large = min(_mod.bench_fixed(5, 500) for _ in range(3))
ratio = t_large / t_small if t_small > 0 else float("inf")
# 5x P-scaling should remain <17.5x (O(P), not O(P^2))
self.assertLess(ratio, 17.5,
f"fixed P=500/P=100 ratio {ratio:.2f}x, expected <17.5x")
if __name__ == "__main__":
unittest.main(verbosity=2)