func TestReadFrameLargerHeader(t *testing.T) {
	buf := makeFrame(Version, minHeaderLength+12, TypeStream,
		int(StreamStderr), 1024)
	frame, err := ReadFrame(bytes.NewReader(buf))
	assert.Nil(t, err)
	header := &frame.Header
	assert.Equal(t, Version, header.Version)
	assert.Equal(t, minHeaderLength+12, header.HeaderLength)
	assert.Equal(t, TypeStream, FrameType(header.Type))
	assert.Equal(t, StreamStderr, Stream(header.Opcode))
	assert.Equal(t, 1024, header.PayloadLength)
	assert.Equal(t, 1024, len(frame.Payload))
}
TestLargerHeader makes sure we can read a larger header than minHeaderLength
without any issue.
package api
import (
	"bytes"
	"encoding/binary"
	"io"
	"testing"
	"github.com/stretchr/testify/assert"
)
func makeFrame(v, hl int, t FrameType, op, pl int) []byte {
	buf := make([]byte, hl+pl)
	binary.BigEndian.PutUint16(buf[0:2], uint16(v))
	buf[2] = byte(hl / 4)
	buf[6] = byte(t)
	buf[7] = byte(op)
	binary.BigEndian.PutUint32(buf[8:8+4], uint32(pl))
	return buf
}
func setFlags(data []byte, flags uint8) {
	b := data[6]
	b |= flags
	data[6] = b
}
func getFlags(data []byte) uint8 {
	return data[6] & 0xf0
}
func makeStreamFrame(v, hl int, op Stream, pl int) []byte {
	return makeFrame(v, hl, TypeStream, int(op), pl)
}
func makeFrameReader(t FrameType, op, pl int) io.Reader {
	frame := makeFrame(Version, minHeaderLength, t, op, pl)
	return bytes.NewReader(frame)
}
func TestReadFrame(t *testing.T) {
	reader := makeFrameReader(TypeStream, int(StreamStderr), 1024)
	frame, err := ReadFrame(reader)
	assert.Nil(t, err)
	header := &frame.Header
	assert.Equal(t, Version, header.Version)
	assert.Equal(t, minHeaderLength, header.HeaderLength)
	assert.Equal(t, TypeStream, FrameType(header.Type))
	assert.Equal(t, StreamStderr, Stream(header.Opcode))
	assert.Equal(t, 1024, header.PayloadLength)
	assert.Equal(t, 1024, len(frame.Payload))
}
func TestReadFrameFlags(t *testing.T) {
	buf := makeFrame(Version, minHeaderLength, TypeResponse, int(CmdSignal), 16)
	setFlags(buf, flagInError)
	r := bytes.NewReader(buf)
	frame, err := ReadFrame(r)
	assert.Nil(t, err)
	assert.Equal(t, true, frame.Header.InError)
}
func TestReadFrameErrorPaths(t *testing.T) {
	frame, err := ReadFrame(bytes.NewReader(nil))
	assert.Nil(t, frame)
	assert.NotNil(t, err)
	buf := makeStreamFrame(Version, minHeaderLength, StreamStderr, 1024)
	frame, err = ReadFrame(bytes.NewReader(buf[0:10]))
	assert.Nil(t, frame)
	assert.NotNil(t, err)
	buf = makeStreamFrame(Version, minHeaderLength, StreamStderr, 1024)
	frame, err = ReadFrame(bytes.NewReader(buf[0:512]))
	assert.Nil(t, frame)
	assert.NotNil(t, err)
	buf = makeStreamFrame(0x8fff, minHeaderLength, StreamStderr, 1024)
	frame, err = ReadFrame(bytes.NewReader(buf))
	assert.Nil(t, frame)
	assert.NotNil(t, err)
	buf = makeStreamFrame(0, minHeaderLength, StreamStderr, 1024)
	frame, err = ReadFrame(bytes.NewReader(buf))
	assert.Nil(t, frame)
	assert.NotNil(t, err)
	buf = makeFrame(Version, minHeaderLength, TypeMax, 0, 1024)
	frame, err = ReadFrame(bytes.NewReader(buf))
	assert.Nil(t, frame)
	assert.NotNil(t, err)
}