Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

test(e2e): memory compare refuses traversal, missing and malformed ids, the CLI reports an unknown id and omits retained without the flag, and the snapshot cache survives concurrent loads and file deletion (PIN-399)

Luigi Agosti8a42cf42354e
diff --git a/internal/heapsnap/cache_concurrency_test.go b/internal/heapsnap/cache_concurrency_test.go
new file mode 100644
index 00000000..8c995504
--- /dev/null
+++ b/internal/heapsnap/cache_concurrency_test.go
@@ -0,0 +1,71 @@
+package heapsnap
+
+import (
+ "errors"
+ "os"
+ "path/filepath"
+ "sync"
+ "testing"
+)
+
+func TestCacheServesConcurrentPlainAndRetainedLoads(t *testing.T) {
+ src, err := os.ReadFile(grownFixturePath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ dir := t.TempDir()
+ paths := []string{filepath.Join(dir, "heap_a"+Ext), filepath.Join(dir, "heap_b"+Ext)}
+ for _, p := range paths {
+ if err := os.WriteFile(p, src, 0600); err != nil {
+ t.Fatal(err)
+ }
+ }
+ var c Cache
+ var wg sync.WaitGroup
+ errs := make(chan error, 64)
+ for i := 0; i < 32; i++ {
+ wg.Add(1)
+ go func(i int) {
+ defer wg.Done()
+ base, err := c.Load(paths[i%2], false)
+ if err != nil {
+ errs <- err
+ return
+ }
+ head, err := c.Load(paths[(i+1)%2], i%3 == 0)
+ if err != nil {
+ errs <- err
+ return
+ }
+ if _, err := Compare(base, head, Options{Top: 5, Retained: i%3 == 0}); err != nil {
+ errs <- err
+ }
+ }(i)
+ }
+ wg.Wait()
+ close(errs)
+ for err := range errs {
+ t.Fatal(err)
+ }
+}
+
+func TestCacheReportsADeletedSnapshotAsMissing(t *testing.T) {
+ src, err := os.ReadFile(grownFixturePath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ path := filepath.Join(t.TempDir(), "heap_gone"+Ext)
+ if err := os.WriteFile(path, src, 0600); err != nil {
+ t.Fatal(err)
+ }
+ var c Cache
+ if _, err := c.Load(path, true); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Remove(path); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := c.Load(path, false); !errors.Is(err, os.ErrNotExist) {
+ t.Fatalf("load after delete err = %v, want os.ErrNotExist rather than the cached parse", err)
+ }
+}
diff --git a/tests/e2e/scenarios/api/memory-basic.sh b/tests/e2e/scenarios/api/memory-basic.sh
index 9df88f26..68b42b7c 100644
--- a/tests/e2e/scenarios/api/memory-basic.sh
+++ b/tests/e2e/scenarios/api/memory-basic.sh
@@ -156,4 +156,24 @@ assert_http_status 404 "unknown head id"
assert_json_eq "$RESULT" '.code' 'memory_snapshot_not_found' "unknown id is memory_snapshot_not_found"
assert_json_eq "$RESULT" '.details.id' 'heap_does_not_exist' "refusal names the missing id"
+pt_get "/memory/compare?base=heap_does_not_exist&head=${LEAK_ID}"
+assert_http_status 404 "unknown base id"
+assert_json_eq "$RESULT" '.details.id' 'heap_does_not_exist' "refusal names the missing base id"
+
+pt_get "/memory/compare?base=..%2F..%2Fetc%2Fpasswd&head=${LEAK_ID}"
+assert_http_status 400 "traversal-looking base id refused"
+assert_json_eq "$RESULT" '.code' 'bad_snapshot_id' "traversal id is bad_snapshot_id"
+
+pt_get "/memory/compare?base=${BASE_ID}&head=..%2F${LEAK_ID}"
+assert_http_status 400 "traversal-looking head id refused"
+assert_json_eq "$RESULT" '.code' 'bad_snapshot_id' "traversal head id is bad_snapshot_id"
+
+pt_get "/memory/compare?head=${LEAK_ID}"
+assert_http_status 400 "missing base refused"
+assert_json_eq "$RESULT" '.code' 'bad_snapshot_id' "missing base is bad_snapshot_id"
+
+pt_get "/memory/compare?base=${BASE_ID}&head=${LEAK_ID}&retained=maybe"
+assert_http_status 400 "non-boolean retained refused"
+assert_json_eq "$RESULT" '.code' 'bad_retained' "non-boolean retained is bad_retained"
+
end_test
diff --git a/tests/e2e/scenarios/cli/memory-basic.sh b/tests/e2e/scenarios/cli/memory-basic.sh
index d3943142..03ab13e4 100644
--- a/tests/e2e/scenarios/cli/memory-basic.sh
+++ b/tests/e2e/scenarios/cli/memory-basic.sh
@@ -92,6 +92,20 @@ fi
pt_ok memory compare "$BASE_ID" "$HEAD_ID" --top 5 --retained
assert_output_contains "RETAINED" "--retained adds the retained column"
+pt_ok memory compare "$BASE_ID" "$HEAD_ID" --top 5 --json
+if echo "$PT_OUT" | jq -e '[.constructors[] | has("retainedSize")] | any | not' >/dev/null; then
+ pass_assert "no retainedSize without --retained"
+else
+ fail_assert "retainedSize present without --retained"
+fi
+
+pt_fail memory compare "$BASE_ID" heap_does_not_exist
+if grep -q "404" <<<"$PT_ERR" && grep -q "heap_does_not_exist" <<<"$PT_ERR"; then
+ pass_assert "unknown head id reports 404 naming the id"
+else
+ fail_assert "unknown head id stderr lacks 404 or the id: $PT_ERR"
+fi
+
pt_ok click "#release"
end_test