| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/binary" |
| 6 | "encoding/json" |
| 7 | "io" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/sylvinus/agent-vm/internal/clipboardfiles" |
| 14 | ) |
| 15 | |
| 16 | type closedOutput struct{} |
| 17 | |
| 18 | func (closedOutput) Write([]byte) (int, error) { return 0, io.ErrClosedPipe } |
| 19 | |
| 20 | func TestGuestFileFailedReplyReleasesUnpublishedCopy(t *testing.T) { |
| 21 | isolateClipboardCache(t) |
| 22 | source := filepath.Join(t.TempDir(), "source.txt") |
| 23 | if err := os.WriteFile(source, []byte("retained source"), 0600); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | selection, _ := json.Marshal([]string{source}) |
| 27 | if err := run([]string{"copy", "--paths-stdin"}, bytes.NewReader(selection), closedOutput{}); err == nil { |
| 28 | t.Fatal("closed output accepted a selection") |
| 29 | } |
| 30 | store, err := clipboardfiles.UserCacheStore() |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | entries, err := os.ReadDir(store.Directory) |
| 35 | if err != nil || len(entries) != 1 || entries[0].Name() != ".lock" { |
| 36 | t.Fatal("an unpublished result retained its ownership receipt", err) |
| 37 | } |
| 38 | if data, err := os.ReadFile(source); err != nil || string(data) != "retained source" { |
| 39 | t.Fatal("source changed", err) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestGuestFileSaveRequiresCompleteFrames(t *testing.T) { |
| 44 | isolateClipboardCache(t) |
| 45 | cache := t.TempDir() |
| 46 | t.Setenv("TMPDIR", cache) |
| 47 | t.Setenv("TEMP", cache) |
| 48 | t.Setenv("TMP", cache) |
| 49 | content := []byte{0, 255, 128, '\n'} |
| 50 | var wire bytes.Buffer |
| 51 | binary.Write(&wire, binary.LittleEndian, uint32(len(content))) |
| 52 | wire.Write(content) |
| 53 | truncated := append([]byte(nil), wire.Bytes()...) |
| 54 | binary.Write(&wire, binary.LittleEndian, uint32(0)) |
| 55 | var output bytes.Buffer |
| 56 | if err := run([]string{"save", "图片 ' .bin"}, &wire, &output); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | var result struct { |
| 60 | Paths []string `json:"paths"` |
| 61 | } |
| 62 | if json.Unmarshal(output.Bytes(), &result) != nil || len(result.Paths) != 1 { |
| 63 | t.Fatal("saved file did not return its private path") |
| 64 | } |
| 65 | data, err := os.ReadFile(result.Paths[0]) |
| 66 | if err != nil || !bytes.Equal(data, content) { |
| 67 | t.Fatal("saved bytes changed") |
| 68 | } |
| 69 | selection, _ := json.Marshal(result.Paths) |
| 70 | output.Reset() |
| 71 | if err := run([]string{"link-cached", "--paths-stdin"}, bytes.NewReader(selection), &output); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | var linked struct { |
| 75 | Paths []string `json:"paths"` |
| 76 | } |
| 77 | if json.Unmarshal(output.Bytes(), &linked) != nil || len(linked.Paths) != 1 { |
| 78 | t.Fatal("cache grouping did not return its private path") |
| 79 | } |
| 80 | sourceInfo, _ := os.Lstat(result.Paths[0]) |
| 81 | linkedInfo, err := os.Lstat(linked.Paths[0]) |
| 82 | if err != nil || !os.SameFile(sourceInfo, linkedInfo) { |
| 83 | t.Fatal("cache grouping copied or replaced file data") |
| 84 | } |
| 85 | if err := os.Remove(result.Paths[0]); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | data, err = os.ReadFile(linked.Paths[0]) |
| 89 | if err != nil || !bytes.Equal(data, content) { |
| 90 | t.Fatal("removing the original upload lost the grouped attachment") |
| 91 | } |
| 92 | before, _ := os.ReadDir(cache) |
| 93 | if err := run([]string{"save", "partial.bin"}, bytes.NewReader(truncated), io.Discard); err == nil { |
| 94 | t.Fatal("missing completion frame was accepted") |
| 95 | } |
| 96 | after, _ := os.ReadDir(cache) |
| 97 | if len(after) != len(before) { |
| 98 | t.Fatal("failed save left a partial attachment") |
| 99 | } |
| 100 | for _, name := range []string{"../escape", "/absolute", "file:stream", ""} { |
| 101 | if err := run([]string{"save", name}, bytes.NewReader([]byte{0, 0, 0, 0}), io.Discard); err == nil { |
| 102 | t.Fatal("invalid filename accepted") |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func TestGuestFileOperationsUseStdinAndPreserveContent(t *testing.T) { |
| 108 | isolateClipboardCache(t) |
| 109 | root := t.TempDir() |
| 110 | path := filepath.Join(root, "资料.bin") |
| 111 | content := []byte{0, 255, 128, '\n'} |
| 112 | if err := os.WriteFile(path, content, 0600); err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | selection, _ := json.Marshal([]string{path}) |
| 116 | var wire bytes.Buffer |
| 117 | if err := run([]string{"export", "--paths-stdin"}, bytes.NewReader(selection), &wire); err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | var output bytes.Buffer |
| 121 | if err := run([]string{"import"}, &wire, &output); err != nil { |
| 122 | t.Fatal(err) |
| 123 | } |
| 124 | var result struct { |
| 125 | Paths []string `json:"paths"` |
| 126 | } |
| 127 | if err := json.Unmarshal(output.Bytes(), &result); err != nil || len(result.Paths) != 1 { |
| 128 | t.Fatalf("import: %v", err) |
| 129 | } |
| 130 | defer os.RemoveAll(filepath.Dir(result.Paths[0])) |
| 131 | got, err := os.ReadFile(result.Paths[0]) |
| 132 | if err != nil || !bytes.Equal(got, content) { |
| 133 | t.Fatalf("content changed: %v", err) |
| 134 | } |
| 135 | if result.Paths[0] == path { |
| 136 | t.Fatal("import reused source path") |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | func isolateClipboardCache(t *testing.T) { |
| 141 | t.Helper() |
| 142 | directory := t.TempDir() |
| 143 | for _, name := range []string{"HOME", "USERPROFILE", "LOCALAPPDATA", "XDG_CACHE_HOME"} { |
| 144 | t.Setenv(name, directory) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func TestGuestFileSelectionBoundsAndMissingSourceFail(t *testing.T) { |
| 149 | for _, input := range []string{`["/does/not/exist"]`, `[]`, `{}`, `[] {}`, strings.Repeat(" ", (1<<20)+1)} { |
| 150 | if err := run([]string{"copy", "--paths-stdin"}, strings.NewReader(input), io.Discard); err == nil { |
| 151 | t.Fatal("invalid selection succeeded") |
| 152 | } |
| 153 | } |
| 154 | } |