java-topology/defects/vlc/unit/VlcModuleFindTest.java
russell@unturf.com 9934133dcf whitepaper: 312 sites / 151 ecosystems — wave2+3 defect tables and PDF rebuild
Add 88 new defect entries to HIGH and MEDIUM tables:
  HIGH: mysql-0001/0002, mariadb-0001, redis-0001/0002, valkey-0001/0002, openvpn-0001,
        vlc-0001, prometheus-0001, otel-collector-0001, cockroachdb-0001..0004,
        tidb-0001..0008, kubernetes-0001/0002, go-0001, kotlin-0002, scala-0001,
        allegro5-0001, sdl2-0001, grafana-0001, clickhouse-0001, duckdb-0001,
        mongodb-0001, envoy-0001, istio-0001, cilium-0001, linkerd2-0001,
        linux-0001/0002/0003, tor-0002/0003, curl-0001, julia-0001, lua-0001,
        perl5-0001, nats-0001, spring-0003/0004, tomcat-0001, onos-0002, odl-0002

  MEDIUM: helm-0001, mariadb-0002, openssl-0001/0002, memcached-0001,
          cassandra-0001..0004, flink-0001, storm-0001/0002, zookeeper-0001..0003,
          pip-0001, gradle-0001, nginx-0001, haproxy-0001, caddy-0001, varnish-0001,
          ffmpeg-0001, gstreamer-0001, raylib-0001, love2d-0001, php-0001/0002,
          r-source-0001, cpython-0002, ruby-0001, rabbitmq-0003/0004, activemq-0001,
          ovs-0001, onos-0003, odl-0002, jetty-0001

PDF: 976K
2026-03-27 15:23:43 -04:00

101 lines
3.4 KiB
Java

package unit;
import java.util.HashMap;
import java.util.Map;
/**
* CWE-407 unit test: VLC module_find() / module_exists()
*
* Slow path: O(N) linear scan over full module list (mirrors module_find).
* Fast path: O(1) HashMap lookup (proposed name-indexed hash).
*
* Scenario mirrors aout_New(): 6 sequential module_exists() calls.
* Assert: slowOps > fastOps * 30x at N=500 modules.
*/
public class VlcModuleFindTest {
static long slowOps = 0;
static long fastOps = 0;
// ---- SLOW: linear scan (mirrors module_find) ----
static String[] buildModuleList(int n) {
String[] modules = new String[n];
for (int i = 0; i < n; i++) modules[i] = "module_" + i;
// Put the target names at the end to force worst-case scan
modules[n - 6] = "goom";
modules[n - 5] = "projectm";
modules[n - 4] = "vsxu";
modules[n - 3] = "glspectrum";
modules[n - 2] = "equalizer";
modules[n - 1] = "spatialaudio";
return modules;
}
static boolean slow_module_exists(String[] modules, String name) {
for (String m : modules) {
slowOps++;
if (m.equals(name)) return true;
}
return false;
}
// ---- FAST: hash map lookup ----
static Map<String, Boolean> buildNameHash(String[] modules) {
Map<String, Boolean> map = new HashMap<>();
for (String m : modules) {
fastOps++; // build cost
map.put(m, Boolean.TRUE);
}
return map;
}
static boolean fast_module_exists(Map<String, Boolean> map, String name) {
fastOps++;
return map.containsKey(name);
}
// Simulate aout_New(): 6 sequential module_exists() calls
static final String[] AOUT_CHECKS = {
"goom", "projectm", "vsxu", "glspectrum", "equalizer", "spatialaudio"
};
public static void main(String[] args) {
final int N = 500;
final int Nx = 30; // assert slowOps > fastOps * Nx
String[] modules = buildModuleList(N);
slowOps = 0;
fastOps = 0;
// Slow: 6 linear scans per aout_New, called SESSIONS times (e.g. playlist)
// Each call to module_exists triggers a fresh scan of all N modules.
final int SESSIONS = 20; // 20 audio output creations in a session
for (int s = 0; s < SESSIONS; s++) {
for (String check : AOUT_CHECKS) {
boolean found = slow_module_exists(modules, check);
if (!found) { System.out.println("FAIL slow did not find: " + check); System.exit(1); }
}
}
// Fast: one build + 6 hash lookups per session (index is persistent)
Map<String, Boolean> map = buildNameHash(modules);
for (int s = 0; s < SESSIONS; s++) {
for (String check : AOUT_CHECKS) {
boolean found = fast_module_exists(map, check);
if (!found) { System.out.println("FAIL fast did not find: " + check); System.exit(1); }
}
}
System.out.printf("vlc-0001: slowOps=%d fastOps=%d ratio=%.1f%n",
slowOps, fastOps, (double) slowOps / fastOps);
if (slowOps <= fastOps * Nx) {
System.out.printf("FAIL: expected slowOps > fastOps * %d%n", Nx);
System.exit(1);
}
System.out.printf("1/1 PASS (slowOps=%d > fastOps*%d=%d)%n",
slowOps, Nx, fastOps * Nx);
}
}