oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package toolproxy
2
3import (
4 "bufio"
5 "bytes"
6 "errors"
7 "net"
8 "strings"
9 "testing"
10)
11
12// The proxy carries a reply as large as an image a tool returns, up to
13// maxReplyFrame and no more, while a request stays bounded by
14// maxToolFrame.
15func TestRelayCarriesAnImageReply(t *testing.T) {
16 image := `{"jsonrpc":"2.0","id":1,"result":{"content":[{"type":"text","text":"header"},{"type":"image","data":"` + strings.Repeat("A", 2_100_000) + `","mimeType":"image/png"}]}}`
17 runner, proxy := net.Pipe()
18 go func() {
19 r := bufio.NewReader(runner)
20 if _, err := r.ReadBytes('\n'); err == nil {
21 runner.Write([]byte(image + "\n"))
22 }
23 runner.Close()
24 }()
25 var out bytes.Buffer
26 in := strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"read_attachment"}}` + "\n")
27 if err := relay(in, &out, proxy); err != nil {
28 t.Fatal(err)
29 }
30 if out.String() != image+"\n" {
31 t.Fatalf("reply of %d bytes", out.Len())
32 }
33
34 // A reply over maxReplyFrame is refused, never passed on.
35 runner, proxy = net.Pipe()
36 go func() {
37 r := bufio.NewReader(runner)
38 if _, err := r.ReadBytes('\n'); err == nil {
39 runner.Write([]byte(`{"jsonrpc":"2.0","id":1,"result":{"content":[{"type":"text","text":"` + strings.Repeat("A", maxReplyFrame) + `"}]}}` + "\n"))
40 }
41 runner.Close()
42 }()
43 out.Reset()
44 in = strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"read_attachment"}}` + "\n")
45 if err := relay(in, &out, proxy); !errors.Is(err, errInvalid) || out.Len() != 0 {
46 t.Fatalf("a reply over the bound: %v, %d bytes out", err, out.Len())
47 }
48
49 runner, proxy = net.Pipe()
50 defer runner.Close()
51 big := `{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"` + strings.Repeat("x", maxToolFrame) + `"}}` + "\n"
52 if err := relay(strings.NewReader(big), &out, proxy); !errors.Is(err, errInvalid) {
53 t.Fatalf("a request over the bound: %v", err)
54 }
55}
56