v0.5.0 c37833b

proto_limits_test.go RawHistoryFolder

1package sessionwire
2
3import (
4	"bytes"
5	"testing"
6
7	"github.com/bnema/vev/internal/domain"
8	"github.com/bnema/vev/internal/protocol"
9	"github.com/bnema/vev/internal/protocol/wire"
10	"github.com/stretchr/testify/require"
11)
12
13func testOutput(data []byte) protocol.Output {
14	return protocol.Output{Epoch: 1, New: 1, Full: true, Size: domain.Size{Cols: 80, Rows: 24}, Context: testUIOutputContext(), Data: data}
15}
16
17// asyncScriptedTransport carries the async and owned-synchronous send
18// capabilities so tests can prove every server send mode shares the
19// negotiated ceiling checks.
20type asyncScriptedTransport struct {
21	*scriptedTransport
22	async       []wire.Envelope
23	synchronous []wire.Envelope
24}
25
26func (t *asyncScriptedTransport) SendAsync(envelope wire.Envelope) error {
27	t.async = append(t.async, envelope)
28	return nil
29}
30
31func (t *asyncScriptedTransport) SendSynchronous(envelope wire.Envelope) error {
32	t.synchronous = append(t.synchronous, envelope)
33	return nil
34}
35
36// TestServerSendEnforcesNegotiatedCeilings proves the normal, async, and
37// owned-synchronous send paths all refuse envelopes and output data above
38// the negotiated ceilings before any transport call.
39func TestServerSendEnforcesNegotiatedCeilings(t *testing.T) {
40	envelopeBound := protoCeilings{maxReceiveEnvelopeBytes: 8, outputDataLimit: uint64(protocol.MaxOutputDataLen)}
41	outputBound := protoCeilings{maxReceiveEnvelopeBytes: wire.AbsoluteEnvelopeLimit, outputDataLimit: 4}
42
43	t.Run("normal send", func(t *testing.T) {
44		raw := &scriptedTransport{}
45		conn := &serverConnection{raw: raw, ceilings: envelopeBound}
46		require.ErrorIs(t, conn.SendServer(protocol.ErrorMsg{Code: protocol.ErrInternal, Text: "oversized"}), wire.ErrScanLength)
47		require.Zero(t, raw.sentLen())
48		conn.ceilings = outputBound
49		require.ErrorIs(t, conn.SendOutput(testOutput([]byte("12345"))), errOutputDataExceedsLimit)
50		require.Zero(t, raw.sentLen())
51	})
52	t.Run("async and owned synchronous send", func(t *testing.T) {
53		raw := &asyncScriptedTransport{scriptedTransport: &scriptedTransport{}}
54		conn := &serverConnection{raw: raw, ceilings: envelopeBound}
55		require.ErrorIs(t, conn.SendServerAsync(protocol.ErrorMsg{Code: protocol.ErrInternal, Text: "oversized"}), wire.ErrScanLength)
56		require.ErrorIs(t, conn.SendServerSynchronous(protocol.ErrorMsg{Code: protocol.ErrInternal, Text: "oversized"}), wire.ErrScanLength)
57		require.Empty(t, raw.async)
58		require.Empty(t, raw.synchronous)
59		conn.ceilings = outputBound
60		require.ErrorIs(t, conn.SendOutputAsync(testOutput([]byte("12345"))), errOutputDataExceedsLimit)
61		require.ErrorIs(t, conn.SendOutputSynchronous(testOutput([]byte("12345"))), errOutputDataExceedsLimit)
62		require.Empty(t, raw.async)
63		require.Empty(t, raw.synchronous)
64	})
65	t.Run("within ceilings", func(t *testing.T) {
66		raw := &scriptedTransport{}
67		conn := &serverConnection{raw: raw, ceilings: defaultProtoCeilings()}
68		require.NoError(t, conn.SendServer(protocol.Pong{}))
69		require.NoError(t, conn.SendOutput(testOutput([]byte("12345"))))
70		require.Equal(t, 2, raw.sentLen())
71	})
72}
73
74// TestTypedReceiveEnforcesNegotiatedCeilings proves both typed receive paths
75// apply the negotiated envelope ceiling before decode and the negotiated
76// output-data ceiling before decompression.
77func TestTypedReceiveEnforcesNegotiatedCeilings(t *testing.T) {
78	largeInput := mustEncodeClient(t, protocol.Input{InputSeq: 1, Data: bytes.Repeat([]byte("x"), 64)})
79	largeOutput := mustEncodeServer(t, testOutput(bytes.Repeat([]byte("x"), 64)))
80	smallOutput := mustEncodeServer(t, testOutput([]byte("12345")))
81
82	t.Run("server receive envelope ceiling", func(t *testing.T) {
83		raw := &scriptedTransport{recv: []wire.Envelope{{Payload: largeInput}}}
84		conn := &serverConnection{raw: raw, ceilings: protoCeilings{maxReceiveEnvelopeBytes: 16, outputDataLimit: uint64(protocol.MaxOutputDataLen)}}
85		conn.preambleOnce.Do(func() {})
86		_, err := conn.ReceiveClient()
87		var failure *protocol.DecodeFailure
88		require.ErrorAs(t, err, &failure)
89		require.ErrorIs(t, failure.Err, wire.ErrScanLength)
90	})
91	t.Run("client receive envelope ceiling", func(t *testing.T) {
92		raw := &scriptedTransport{recv: []wire.Envelope{{Payload: largeOutput}}}
93		conn := &clientConnection{raw: raw, ceilings: protoCeilings{maxReceiveEnvelopeBytes: 16, outputDataLimit: uint64(protocol.MaxOutputDataLen)}}
94		conn.preambleOnce.Do(func() {})
95		_, err := conn.ReceiveServer()
96		var failure *protocol.DecodeFailure
97		require.ErrorAs(t, err, &failure)
98		require.ErrorIs(t, failure.Err, wire.ErrScanLength)
99	})
100	t.Run("client receive output data ceiling", func(t *testing.T) {
101		raw := &scriptedTransport{recv: []wire.Envelope{{Payload: smallOutput}}}
102		conn := &clientConnection{raw: raw, ceilings: protoCeilings{maxReceiveEnvelopeBytes: wire.AbsoluteEnvelopeLimit, outputDataLimit: 4}}
103		conn.preambleOnce.Do(func() {})
104		_, err := conn.ReceiveServer()
105		var failure *protocol.DecodeFailure
106		require.ErrorAs(t, err, &failure)
107		require.ErrorIs(t, failure.Err, errOutputDataExceedsLimit)
108	})
109	t.Run("within ceilings decodes", func(t *testing.T) {
110		serverRaw := &scriptedTransport{recv: []wire.Envelope{{Payload: largeInput}}}
111		server := &serverConnection{raw: serverRaw, ceilings: defaultProtoCeilings()}
112		server.preambleOnce.Do(func() {})
113		got, err := server.ReceiveClient()
114		require.NoError(t, err)
115		require.Equal(t, []byte(bytes.Repeat([]byte("x"), 64)), got.(protocol.Input).Data)
116
117		clientRaw := &scriptedTransport{recv: []wire.Envelope{{Payload: smallOutput}}}
118		client := &clientConnection{raw: clientRaw, ceilings: defaultProtoCeilings()}
119		client.preambleOnce.Do(func() {})
120		decoded, err := client.ReceiveServer()
121		require.NoError(t, err)
122		require.Equal(t, []byte("12345"), decoded.(protocol.Output).Data)
123	})
124}
125
126// TestClientSendAndCapabilitiesUseNegotiatedCeilings proves the client send
127// path and advertised capabilities reflect the negotiated ceilings.
128func TestClientSendAndCapabilitiesUseNegotiatedCeilings(t *testing.T) {
129	raw := &scriptedTransport{}
130	conn := &clientConnection{raw: raw, ceilings: protoCeilings{maxReceiveEnvelopeBytes: 8, outputDataLimit: 1234}}
131	conn.preambleOnce.Do(func() {})
132	require.ErrorIs(t, conn.SendClient(protocol.Input{InputSeq: 1, Data: []byte("hello")}), wire.ErrScanLength)
133	require.Zero(t, raw.sentLen())
134	require.Equal(t, 1234, conn.Capabilities().OutputDataLimit)
135
136	server := &serverConnection{raw: &scriptedTransport{}, ceilings: protoCeilings{maxReceiveEnvelopeBytes: 1, outputDataLimit: 4321}}
137	require.Equal(t, 4321, server.Capabilities().OutputDataLimit)
138}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 12

Tags 11

↑↓ moveenter openesc close