feat(transport): negotiate bounded automatic message chunking
This commit is contained in:
1 parent
474afb3484
commit
8acffac985
20 files changed
+1332
-38
No files matched your search
@@ -0,0 +1,177 @@
|
||||
package agentwire
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
)
|
||||
|
||||
func TestLogicalSendCeilingBeforeAnyPacket_BH_CHUNK_02(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
result := make(chan incoming, 1)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
raw, err := websocket.Accept(w, r, &websocket.AcceptOptions{Subprotocols: []string{Subprotocol}})
|
||||
if err != nil {
|
||||
result <- incoming{err: err}
|
||||
return
|
||||
}
|
||||
connection, err := Wrap(raw, 2048, nil)
|
||||
if err != nil {
|
||||
_ = raw.CloseNow()
|
||||
result <- incoming{err: err}
|
||||
return
|
||||
}
|
||||
defer connection.CloseNow()
|
||||
_, payload, err := connection.Read(ctx)
|
||||
result <- incoming{payload: payload, err: err}
|
||||
}))
|
||||
defer server.Close()
|
||||
raw, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(server.URL, "http"), &websocket.DialOptions{Subprotocols: []string{Subprotocol}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
connection, err := Wrap(raw, 2048, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer connection.CloseNow()
|
||||
if err := connection.Write(ctx, websocket.MessageBinary, make([]byte, 2049)); !errors.Is(err, ErrMessageTooBig) {
|
||||
t.Fatal("logical send limit bypassed")
|
||||
}
|
||||
if err := connection.Write(ctx, websocket.MessageBinary, []byte("still usable")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case frame := <-result:
|
||||
if frame.err != nil || !bytes.Equal(frame.payload, []byte("still usable")) {
|
||||
t.Fatal("oversized prefix was sent or connection was poisoned")
|
||||
}
|
||||
case <-ctx.Done():
|
||||
t.Fatal("receiver did not receive next valid message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloseStopsBlockedReadPump_RACE_CHUNK_03(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
sent := make(chan struct{})
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
connection, err := websocket.Accept(w, r, &websocket.AcceptOptions{Subprotocols: []string{Subprotocol}})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer connection.CloseNow()
|
||||
for i := 0; i < 3; i++ {
|
||||
if err := connection.Write(ctx, websocket.MessageBinary, []byte{0, 0x51}); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
close(sent)
|
||||
_, _, _ = connection.Read(ctx)
|
||||
}))
|
||||
defer server.Close()
|
||||
raw, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(server.URL, "http"), &websocket.DialOptions{Subprotocols: []string{Subprotocol}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
connection, err := Wrap(raw, 1<<20, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer connection.CloseNow()
|
||||
select {
|
||||
case <-sent:
|
||||
case <-ctx.Done():
|
||||
t.Fatal("peer did not fill delivery queue")
|
||||
}
|
||||
for len(connection.read) == 0 {
|
||||
select {
|
||||
case <-time.After(time.Millisecond):
|
||||
case <-ctx.Done():
|
||||
t.Fatal("delivery queue never filled")
|
||||
}
|
||||
}
|
||||
if err := connection.Close(websocket.StatusNormalClosure, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case _, ok := <-connection.read:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
case <-ctx.Done():
|
||||
t.Fatal("read pump leaked after close with stopped consumer")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMalformedWireClosesWithoutDelivery_BH_CHUNK_03(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, scenario := range []struct {
|
||||
name string
|
||||
packet []byte
|
||||
want error
|
||||
}{
|
||||
{"invalid kind", []byte{0xff, 1}, ErrPacket},
|
||||
{"oversized declaration", []byte{1, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 1}, ErrMessageTooBig},
|
||||
{"oversized wire packet", make([]byte, PacketBytes+1), websocket.ErrMessageTooBig},
|
||||
} {
|
||||
t.Run(scenario.name, func(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
result := make(chan incoming, 1)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
raw, err := websocket.Accept(w, r, &websocket.AcceptOptions{Subprotocols: []string{Subprotocol}})
|
||||
if err != nil {
|
||||
result <- incoming{err: err}
|
||||
return
|
||||
}
|
||||
connection, err := Wrap(raw, 1<<20, nil)
|
||||
if err != nil {
|
||||
_ = raw.CloseNow()
|
||||
result <- incoming{err: err}
|
||||
return
|
||||
}
|
||||
defer connection.CloseNow()
|
||||
_, payload, err := connection.Read(ctx)
|
||||
result <- incoming{payload: payload, err: err}
|
||||
}))
|
||||
defer server.Close()
|
||||
raw, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(server.URL, "http"), &websocket.DialOptions{Subprotocols: []string{Subprotocol}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer raw.CloseNow()
|
||||
if err := raw.Write(ctx, websocket.MessageBinary, scenario.packet); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, _, closed := raw.Read(ctx)
|
||||
wantCode := websocket.StatusPolicyViolation
|
||||
if !errors.Is(scenario.want, ErrPacket) {
|
||||
wantCode = websocket.StatusMessageTooBig
|
||||
}
|
||||
if websocket.CloseStatus(closed) != wantCode {
|
||||
t.Fatalf("close code = %v, want %v", closed, wantCode)
|
||||
}
|
||||
select {
|
||||
case frame := <-result:
|
||||
if frame.payload != nil || !errors.Is(frame.err, scenario.want) {
|
||||
t.Fatalf("malformed frame delivered or wrong error: %v", frame.err)
|
||||
}
|
||||
case <-ctx.Done():
|
||||
t.Fatal("malformed packet did not close reader")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user