MCPcopy Create free account
hub / github.com/coder/coder / TestSpeaker_CorruptMessage

Function TestSpeaker_CorruptMessage

vpn/speaker_internal_test.go:226–268  ·  view source on GitHub ↗

TestSpeaker_RawPeer tests the speaker with a peer that we simulate by directly making reads and writes to the other end of the pipe. There should be at least one test that does this, rather than use 2 speakers so that we don't have a bug where we don't adhere to the stated protocol, but both sides h

(t *testing.T)

Source from the content-addressed store, hash-verified

224// than use 2 speakers so that we don't have a bug where we don't adhere to the stated protocol, but
225// both sides have the bug and can still communicate.
226func TestSpeaker_CorruptMessage(t *testing.T) {
227 t.Parallel()
228 mp, tp := net.Pipe()
229 defer mp.Close()
230 defer tp.Close()
231 ctx := testutil.Context(t, testutil.WaitShort)
232 // We're going to use deadlines for this test so that we don't hang the main test thread if
233 // the speaker misbehaves.
234 err := mp.SetReadDeadline(time.Now().Add(testutil.WaitShort))
235 require.NoError(t, err)
236 err = mp.SetWriteDeadline(time.Now().Add(testutil.WaitShort))
237 require.NoError(t, err)
238 logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
239 var tun *speaker[*TunnelMessage, *ManagerMessage, ManagerMessage]
240 errCh := make(chan error, 1)
241 go func() {
242 s, err := newSpeaker[*TunnelMessage, *ManagerMessage](ctx, logger, tp, SpeakerRoleTunnel, SpeakerRoleManager)
243 tun = s
244 errCh <- err
245 }()
246
247 b := make([]byte, 256)
248 n, err := mp.Read(b)
249 require.NoError(t, err)
250 require.Equal(t, expectedHandshake, string(b[:n]))
251
252 _, err = mp.Write([]byte("codervpn manager 1.0\n"))
253 require.NoError(t, err)
254
255 err = testutil.TryReceive(ctx, t, errCh)
256 require.NoError(t, err)
257 tun.start()
258
259 err = binary.Write(mp, binary.BigEndian, uint32(10))
260 require.NoError(t, err)
261 n, err = mp.Write([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
262 require.NoError(t, err)
263 require.EqualValues(t, 10, n)
264
265 // it should hang up on us if we write nonsense
266 _, err = mp.Read(b)
267 require.ErrorIs(t, err, io.EOF)
268}
269
270func TestSpeaker_unaryRPC_mainline(t *testing.T) {
271 t.Parallel()

Callers

nothing calls this directly

Calls 11

ContextFunction · 0.92
TryReceiveFunction · 0.92
newSpeakerFunction · 0.85
SetReadDeadlineMethod · 0.80
SetWriteDeadlineMethod · 0.80
CloseMethod · 0.65
AddMethod · 0.65
ReadMethod · 0.65
WriteMethod · 0.65
startMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected