| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "runtime" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestStaleAttachmentCleanupDoesNotFollowSwappedDirectory(t *testing.T) { |
| 13 | a, root, _ := uploadFixture(t) |
| 14 | outside := t.TempDir() |
| 15 | attachments := filepath.Join(root, ".reasonix", "attachments") |
| 16 | want := []byte("external victim must survive stale attachment cleanup") |
| 17 | var victim string |
| 18 | a.attachmentIOHook = func() { |
| 19 | entries, err := os.ReadDir(attachments) |
| 20 | if err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | if len(entries) != 1 || entries[0].IsDir() { |
| 24 | t.Fatalf("saved attachment entries = %v", entries) |
| 25 | } |
| 26 | victim = filepath.Join(outside, entries[0].Name()) |
| 27 | if err := os.WriteFile(victim, want, 0o600); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if err := os.Rename(attachments, filepath.Join(root, ".reasonix", "original-attachments")); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | if runtime.GOOS == "windows" { |
| 34 | if out, err := exec.Command("cmd", "/c", "mklink", "/J", attachments, outside).CombinedOutput(); err != nil { |
| 35 | t.Fatalf("create attachment junction: %v: %s", err, out) |
| 36 | } |
| 37 | } else if err := os.Symlink(outside, attachments); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | a.mu.Lock() |
| 41 | a.tabs["a"].SessionGeneration++ |
| 42 | a.mu.Unlock() |
| 43 | } |
| 44 | |
| 45 | data := "data:application/octet-stream;base64," + base64.StdEncoding.EncodeToString([]byte("saved APK fixture")) |
| 46 | if ref, err := a.SavePastedFileForTab("a", "app.apk", data); err == nil || ref != "" { |
| 47 | t.Fatalf("stale write returned ref %q, err %v", ref, err) |
| 48 | } |
| 49 | got, err := os.ReadFile(victim) |
| 50 | if err != nil { |
| 51 | t.Fatalf("stale-owner cleanup deleted the external victim through the swapped attachment directory: %v", err) |
| 52 | } |
| 53 | if string(got) != string(want) { |
| 54 | t.Fatalf("external victim changed: %q", got) |
| 55 | } |
| 56 | } |
| 57 |