package codec_test import ( "bytes" "encoding/binary" "io" "testing" "github.com/stretchr/testify/require" "github.com/coder/coder/v2/agent/boundarylogproxy/codec" ) func TestRoundTrip(t *testing.T) { t.Parallel() tests := []struct { name string tag codec.Tag data []byte }{ { name: "empty data", tag: codec.TagV1, data: []byte{}, }, { name: "simple data", tag: codec.TagV1, data: []byte("hello world"), }, { name: "binary data", tag: codec.TagV1, data: []byte{0x00, 0x01, 0x02, 0xff, 0xfe}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() var buf bytes.Buffer err := codec.WriteFrame(&buf, tt.tag, tt.data) require.NoError(t, err) readBuf := make([]byte, codec.MaxMessageSizeV1) tag, data, err := codec.ReadFrame(&buf, readBuf) require.NoError(t, err) require.Equal(t, tt.tag, tag) require.Equal(t, tt.data, data) }) } } func TestReadFrameTooLarge(t *testing.T) { t.Parallel() // Hand construct a header that indicates the message size exceeds the maximum // message size for codec.TagV1 by one. We just write the header to buf because // we expect codec.ReadFrame to bail out when reading the invalid length. header := uint32(codec.TagV1)<