java-topology/defects/testcafe/tests/test-testcafe-cwe407.py
russell@unturf.com b79fddfb51 browser-automation wave 2: testcafe-0001 + webdriverio-0002
testcafe-0001: Selector filterNodes (string-filter branch) and
  expandSelectorResults both dedup via Array.indexOf on growing result
  arrays. filterNodes: O(N*M) per selector filter. expandSelectorResults:
  O(N^2 * K^2) worst case when derivatives unique. Fix: Set<Node> keyed
  by object identity. Bench: 398x at N=2000 filter, 1966x at N=K=150
  expand.

webdriverio-0002: MSPO aggregator dedups per-test entries via Array.find
  on growing bucket array. O(N^2) per test bucket, same pattern repeats
  in unknown-suite merger. Fix: companion Map<bucketKey, Set<selector>>
  for O(1) dedup. Bench: 493x at N=2000.

UNDF IDs: 1290 (testcafe), 1291 (webdriverio-0002). All 17 tests pass.
2026-04-22 18:31:53 -04:00

91 lines
3.2 KiB
Python

#!/usr/bin/env python3
# UNDF: UNDF-2026-000001290 (testcafe-0001)
#
# CWE-407: Algorithmic Complexity
#
# Defect:
# testcafe-0001: Selector filterNodes (string-filter branch) uses
# matchingArr.indexOf(node) > -1 per node, O(N*M).
# expandSelectorResults uses result.indexOf(deriv) < 0 on
# growing result array, worst O(N^2 * K^2) unique case.
#
# Fix:
# Replace Array.indexOf with Set<Node> membership test. Object identity
# semantics preserved (Set keys by reference).
#
# Complexity gate (from bench/results.txt on this machine):
# filterNodes N=M=2000: defective=30.3ms, fixed=0.08ms.
# expandSelectorResults N=K=150: defective=4985ms, fixed=2.5ms.
# Fixed must stay well sub-linear in N*M / N*K.
import importlib.util
import os
import sys
import 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-testcafe-0001.py")
class TestTestcafe0001FilterCorrectness(unittest.TestCase):
def test_filter_fixed_produces_same_result_as_defective(self):
Node = _mod.Node
matching = [Node(i) for i in range(5)]
all_nodes = matching + [Node(100 + i) for i in range(5)]
matchingSet = set(matching)
result = [n for n in all_nodes if n in matchingSet]
self.assertEqual([n.id for n in result], [0, 1, 2, 3, 4])
class TestTestcafe0001ExpandCorrectness(unittest.TestCase):
def test_expand_preserves_insertion_order_and_dedups(self):
Node = _mod.Node
a = Node(1)
b = Node(2)
c = Node(3)
# Source: each 'node' has derivatives, with overlaps between nodes
derivatives_per_node = [[a, b], [b, c], [c, a]]
seen = set()
result = []
for derivs in derivatives_per_node:
for d in derivs:
if d not in seen:
seen.add(d)
result.append(d)
self.assertEqual([n.id for n in result], [1, 2, 3])
class TestTestcafe0001ComplexityGate(unittest.TestCase):
def test_filter_fixed_wallclock_N2000(self):
t_s = min(_mod.bench_filter_fixed(2000, 1000) for _ in range(3))
self.assertLess(t_s * 1000, 5.0,
f"fixed took {t_s*1000:.3f}ms at N=2000 M=1000, expected <5ms")
def test_expand_fixed_wallclock_N100(self):
t_s = min(_mod.bench_expand_fixed(100, 100) for _ in range(3))
self.assertLess(t_s * 1000, 5.0,
f"fixed took {t_s*1000:.3f}ms at N=K=100, expected <5ms")
def test_filter_fixed_scaling_linear(self):
t_100 = min(_mod.bench_filter_fixed(100, 50) for _ in range(3))
t_500 = min(_mod.bench_filter_fixed(500, 250) for _ in range(3))
ratio = t_500 / t_100 if t_100 > 0 else float("inf")
self.assertLess(ratio, 17.5,
f"fixed N=500/N=100 ratio {ratio:.2f}x, expected <17.5x (O(N))")
if __name__ == "__main__":
unittest.main(verbosity=2)