import java.util.*; /** * CWE-407 unit test for HAProxy http_ana.c defect. * * haproxy-0001: src/http_ana.c http_manage_client_side_cookies() * while (srv) { if memcmp(val_beg, srv->cookie, ...) } — O(S) linked-list * walk per HTTP request to match a cookie value to a backend server. * Fix: build struct eb_root cookies_tree at config-time (same as cfgdiag.c * already does for diagnostic purposes); use ebis_lookup() for O(log S) lookup. */ public class HaproxyTest { // Simulate defect: O(S) linked-list walk per request static String cookieLookup_list(List servers, String cookieVal) { // servers: list of {name, cookie} for (String[] srv : servers) { // O(S) — defect if (srv[1] != null && srv[1].equals(cookieVal)) { return srv[0]; } } return null; } // Simulate fix: O(1) hash map (models eb-tree O(log S)) static String cookieLookup_tree(Map cookieIndex, String cookieVal) { return cookieIndex.get(cookieVal); // O(1) / O(log S) with eb-tree } static void testHaproxy0001() throws Exception { int S = 1000; // backend servers List servers = new ArrayList<>(); Map cookieIndex = new HashMap<>(); for (int i = 0; i < S; i++) { String name = "backend" + i; String cookie = "srv" + String.format("%04d", i); servers.add(new String[]{name, cookie}); cookieIndex.put(cookie, name); } // Request targets last server (worst case for linear scan) String target = "srv" + String.format("%04d", S - 1); // correctness String r1 = cookieLookup_list(servers, target); String r2 = cookieLookup_tree(cookieIndex, target); assert r1 != null && r1.equals(r2) : "list and tree must agree: " + r1 + " vs " + r2; assert cookieLookup_list(servers, "srvXXXX") == null; assert cookieLookup_tree(cookieIndex, "srvXXXX") == null; // performance: simulate R HTTP requests int R = 50_000; long t0 = System.nanoTime(); for (int r = 0; r < R; r++) cookieLookup_list(servers, target); long tList = System.nanoTime() - t0; t0 = System.nanoTime(); for (int r = 0; r < R; r++) cookieLookup_tree(cookieIndex, target); long tTree = System.nanoTime() - t0; double ratio = (double) tList / tTree; System.out.printf("haproxy-0001: list=%.3fs tree=%.3fs ratio=%.1f×%n", tList / 1e9, tTree / 1e9, ratio); assert ratio > 20 : "Expected >20× speedup, got " + ratio; System.out.println("PASS haproxy-0001"); } public static void main(String[] args) throws Exception { testHaproxy0001(); System.out.println("ALL PASS"); } }