java-topology/defects/dovecot/unit/DovecotTest.java
russell@unturf.com 59ae52c7b4 wave16: nova/keystone/crystal/dovecot/wireshark CWE-407 patches + unit tests
nova-0001: scheduler/manager.py selected_hosts list → set (273x, CRITICAL)
nova-0002: scheduler/host_manager.py lowered_hosts_to_force list → set (43x, HIGH)
keystone-0001: api/users.py token_roles list → set (32x, HIGH)
crystal-0001: syntax/parser.cr type_vars Array#includes? → Set (25x, CRITICAL)
crystal-0002: semantic/restrictions.cr discarded Array#includes? → Set (5x, CRITICAL)
dovecot-0001: mail-storage-hooks.c array_lsearch → sort+bsearch (4x, HIGH)
wireshark-0001: proto_data.c GSList → wmem_map_t (8x, HIGH)

7 unit tests: 9/9 PASS
2026-03-30 07:33:49 -04:00

80 lines
3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import java.util.*;
/**
* CWE-407 unit test for dovecot defect.
*
* dovecot-0001: mail-storage-hooks.c mail_user_add_plugin_hooks()
* array_foreach(&module_hooks, hook) {
* array_lsearch(&user->set->mail_plugins, &name, strcmp) // O(P) per hook
* }
* Fix: pre-sort mail_plugins + array_bsearch → O(H log P)
*/
public class DovecotTest {
// Simulate linear scan (defect)
static List<String> addPluginHooksLinear(List<String> moduleHooks,
List<String> mailPlugins) {
List<String> result = new ArrayList<>();
for (String hook : moduleHooks) {
if (mailPlugins.contains(hook)) { // O(P) linear scan
result.add(hook);
}
}
return result;
}
// Simulate sorted + binary search (fix)
static List<String> addPluginHooksBinary(List<String> moduleHooks,
List<String> mailPlugins) {
List<String> sorted = new ArrayList<>(mailPlugins);
Collections.sort(sorted); // sort once O(P log P)
List<String> result = new ArrayList<>();
for (String hook : moduleHooks) {
int idx = Collections.binarySearch(sorted, hook); // O(log P) per hook
if (idx >= 0) {
result.add(hook);
}
}
return result;
}
static void testDovecot0001() throws Exception {
int H = 500; // module hooks
int P = 500; // mail_plugins
List<String> moduleHooks = new ArrayList<>();
List<String> mailPlugins = new ArrayList<>();
for (int i = 0; i < H; i++) moduleHooks.add("hook-" + i);
// half the hooks are in mail_plugins
for (int i = 0; i < P / 2; i++) mailPlugins.add("hook-" + i);
for (int i = P / 2; i < P; i++) mailPlugins.add("plugin-" + i);
// correctness: both paths must return same hooks
List<String> rLinear = addPluginHooksLinear(moduleHooks, mailPlugins);
List<String> rBinary = addPluginHooksBinary(moduleHooks, mailPlugins);
Collections.sort(rLinear);
Collections.sort(rBinary);
assert rLinear.equals(rBinary) : "linear and binary must agree";
// performance
long t0 = System.nanoTime();
for (int r = 0; r < 500; r++) addPluginHooksLinear(moduleHooks, mailPlugins);
long tLinear = System.nanoTime() - t0;
t0 = System.nanoTime();
for (int r = 0; r < 500; r++) addPluginHooksBinary(moduleHooks, mailPlugins);
long tBinary = System.nanoTime() - t0;
double ratio = (double) tLinear / tBinary;
System.out.printf("dovecot-0001: linear=%.3fs binary=%.3fs ratio=%.1f×%n",
tLinear / 1e9, tBinary / 1e9, ratio);
assert ratio > 2 : "Expected >2× speedup, got " + ratio;
System.out.println("PASS dovecot-0001");
}
public static void main(String[] args) throws Exception {
testDovecot0001();
System.out.println("ALL PASS");
}
}