| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "net/http/httptest" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/google/uuid" |
| 12 | ) |
| 13 | |
| 14 | func TestMobileCLIMediaReadsOnlyCurrentProjectMedia(t *testing.T) { |
| 15 | s := testServer(t) |
| 16 | project, err := filepath.EvalSymlinks(t.TempDir()) |
| 17 | if err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | if err := writePrivate(filepath.Join(s.profile, "environment.json"), map[string]string{"directory": project}); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | id := uuid.NewString() |
| 24 | if _, err := s.mobileCLI.create(s.profile, project, id); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | record, err := s.mobileCLI.record(s.profile, id) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | record.Turns = []mobileTurn{{ID: uuid.NewString(), Prompt: "查看预览", Text: " `outside.png` `fake.png` `../other.png`"}} |
| 32 | if err := writePrivate(mobileRecordPath(s.profile, id), record); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | payload := append([]byte("\x89PNG\r\n\x1a\n"), make([]byte, mobileMediaChunk+14)...) |
| 36 | if err := os.WriteFile(filepath.Join(project, "frame one.png"), payload, 0600); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | check := func(ref string, offset int64, version string, status int) *httptest.ResponseRecorder { |
| 40 | t.Helper() |
| 41 | body, _ := json.Marshal(mediaChunkRequest{Reference: ref, Offset: offset, Version: version}) |
| 42 | w := request(s, "POST", "/api/mobile/cli/sessions/"+id+"/media", s.origin, string(body), true) |
| 43 | if w.Code != status { |
| 44 | t.Fatalf("%s: got %d, want %d", ref, w.Code, status) |
| 45 | } |
| 46 | return w |
| 47 | } |
| 48 | var first, last struct { |
| 49 | Data []byte |
| 50 | Version string |
| 51 | Mime string |
| 52 | } |
| 53 | if err := json.Unmarshal(check("frame%20one.png", 0, "", 200).Body.Bytes(), &first); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if err := json.Unmarshal(check("frame%20one.png", int64(len(first.Data)), first.Version, 200).Body.Bytes(), &last); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | if first.Mime != "image/png" || !bytes.Equal(append(first.Data, last.Data...), payload) { |
| 60 | t.Fatal("incomplete image") |
| 61 | } |
| 62 | check("frame%20one.png", int64(len(first.Data)), "stale", 409) |
| 63 | check("unmentioned.png", 0, "", 403) |
| 64 | check("../other.png", 0, "", 403) |
| 65 | if err := os.WriteFile(filepath.Join(project, "fake.png"), []byte("plain text"), 0600); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | check("fake.png", 0, "", 415) |
| 69 | outside := filepath.Join(t.TempDir(), "outside.png") |
| 70 | if err := os.WriteFile(outside, payload, 0600); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | if err := os.Symlink(outside, filepath.Join(project, "outside.png")); err == nil { |
| 74 | check("outside.png", 0, "", 404) |
| 75 | } |
| 76 | s.account.lock() |
| 77 | check("frame%20one.png", 0, "", 423) |
| 78 | } |