element-web-0001: TextForEvent.tsx user dedup array → Set (15x, HIGH) element-web-0002: TextForEvent.tsx pinned filter indexOf → Set.has (13x, MEDIUM) bun-0001: yarn.zig scoped version two-pass scan → single pass (4x, MEDIUM) 5 tests: 5/5 PASS
124 lines
4.5 KiB
Java
124 lines
4.5 KiB
Java
import java.util.*;
|
||
|
||
/**
|
||
* CWE-407 unit tests for element-web (Matrix client) defects.
|
||
*
|
||
* element-web-0001: TextForEvent.tsx textForPowerEvent()
|
||
* users array + indexOf for dedup → O(U²)
|
||
* Fix: Set-based dedup → O(U)
|
||
*
|
||
* element-web-0002: TextForEvent.tsx textForPinnedEvent()
|
||
* previouslyPinned.indexOf inside filter → O(P×Q)
|
||
* Fix: Set.has() inside filter → O(P+Q)
|
||
*/
|
||
public class ElementWebTest {
|
||
|
||
// --- element-web-0001: user dedup ---
|
||
|
||
static List<String> deduplicateUsersArray(String[] current, String[] previous) {
|
||
List<String> users = new ArrayList<>();
|
||
for (String u : current) {
|
||
if (!users.contains(u)) users.add(u); // O(U) per user
|
||
}
|
||
for (String u : previous) {
|
||
if (!users.contains(u)) users.add(u);
|
||
}
|
||
return users;
|
||
}
|
||
|
||
static List<String> deduplicateUsersSet(String[] current, String[] previous) {
|
||
Set<String> seen = new LinkedHashSet<>(); // preserves insertion order
|
||
for (String u : current) seen.add(u);
|
||
for (String u : previous) seen.add(u);
|
||
return new ArrayList<>(seen);
|
||
}
|
||
|
||
static void testElementWeb0001() throws Exception {
|
||
int U = 1000;
|
||
String[] current = new String[U];
|
||
String[] previous = new String[U];
|
||
for (int i = 0; i < U; i++) {
|
||
current[i] = "@user" + i + ":matrix.org";
|
||
previous[i] = "@user" + (i + U / 2) + ":matrix.org"; // partial overlap
|
||
}
|
||
|
||
// correctness
|
||
List<String> rArr = deduplicateUsersArray(current, previous);
|
||
List<String> rSet = deduplicateUsersSet(current, previous);
|
||
Collections.sort(rArr);
|
||
Collections.sort(rSet);
|
||
assert rArr.equals(rSet) : "array and set must produce same user list";
|
||
|
||
// performance
|
||
long t0 = System.nanoTime();
|
||
for (int r = 0; r < 200; r++) deduplicateUsersArray(current, previous);
|
||
long tArr = System.nanoTime() - t0;
|
||
|
||
t0 = System.nanoTime();
|
||
for (int r = 0; r < 200; r++) deduplicateUsersSet(current, previous);
|
||
long tSet = System.nanoTime() - t0;
|
||
|
||
double ratio = (double) tArr / tSet;
|
||
System.out.printf("element-web-0001: array=%.3fs set=%.3fs ratio=%.1f×%n",
|
||
tArr / 1e9, tSet / 1e9, ratio);
|
||
assert ratio > 10 : "Expected >10× speedup, got " + ratio;
|
||
System.out.println("PASS element-web-0001");
|
||
}
|
||
|
||
// --- element-web-0002: pinned messages filter ---
|
||
|
||
static List<String> newlyPinnedIndexOf(List<String> pinned, List<String> previouslyPinned) {
|
||
List<String> result = new ArrayList<>();
|
||
for (String item : pinned) {
|
||
if (!previouslyPinned.contains(item)) result.add(item); // O(P) per item
|
||
}
|
||
return result;
|
||
}
|
||
|
||
static List<String> newlyPinnedSet(List<String> pinned, List<String> previouslyPinned) {
|
||
Set<String> prevSet = new HashSet<>(previouslyPinned); // O(1) lookup
|
||
List<String> result = new ArrayList<>();
|
||
for (String item : pinned) {
|
||
if (!prevSet.contains(item)) result.add(item);
|
||
}
|
||
return result;
|
||
}
|
||
|
||
static void testElementWeb0002() throws Exception {
|
||
int P = 1000;
|
||
List<String> pinned = new ArrayList<>();
|
||
List<String> previouslyPinned = new ArrayList<>();
|
||
for (int i = 0; i < P; i++) {
|
||
pinned.add("$event" + i);
|
||
previouslyPinned.add("$event" + (i + P / 2));
|
||
}
|
||
|
||
// correctness
|
||
List<String> r1 = newlyPinnedIndexOf(pinned, previouslyPinned);
|
||
List<String> r2 = newlyPinnedSet(pinned, previouslyPinned);
|
||
Collections.sort(r1);
|
||
Collections.sort(r2);
|
||
assert r1.equals(r2) : "indexOf and set paths must agree";
|
||
|
||
// performance
|
||
long t0 = System.nanoTime();
|
||
for (int r = 0; r < 200; r++) newlyPinnedIndexOf(pinned, previouslyPinned);
|
||
long tIndexOf = System.nanoTime() - t0;
|
||
|
||
t0 = System.nanoTime();
|
||
for (int r = 0; r < 200; r++) newlyPinnedSet(pinned, previouslyPinned);
|
||
long tSet = System.nanoTime() - t0;
|
||
|
||
double ratio = (double) tIndexOf / tSet;
|
||
System.out.printf("element-web-0002: indexOf=%.3fs set=%.3fs ratio=%.1f×%n",
|
||
tIndexOf / 1e9, tSet / 1e9, ratio);
|
||
assert ratio > 5 : "Expected >5× speedup, got " + ratio;
|
||
System.out.println("PASS element-web-0002");
|
||
}
|
||
|
||
public static void main(String[] args) throws Exception {
|
||
testElementWeb0001();
|
||
testElementWeb0002();
|
||
System.out.println("ALL PASS");
|
||
}
|
||
}
|