diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b5ac8e..ef756bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,19 @@ CHANGELOG ========= +v3.2.0 (-.08.2021) +------------------- + +## 🚀 New: + +- ✏️ 50% reduce bound checks in the frame's operations. [PR](https://github.com/spiral/goridge/pull/143) + +## 🔨 BC: + +- 💔 Frame now for the internal (but public) operations receive additional `[]byte` slice to skip bound checks. + +--- + v3.1.4 (14.06.2021) ------------------- diff --git a/internal/receive.go b/internal/receive.go index a630988..85ba0b4 100644 --- a/internal/receive.go +++ b/internal/receive.go @@ -31,7 +31,7 @@ func ReceiveFrame(relay io.Reader, fr *frame.Frame) error { } // verify header CRC - if !header.VerifyCRC() { + if !header.VerifyCRC(header.Header()) { return errors.E(op, errors.Str("CRC verification failed")) } diff --git a/pkg/frame/frame.go b/pkg/frame/frame.go index f31314d..f2a4d15 100644 --- a/pkg/frame/frame.go +++ b/pkg/frame/frame.go @@ -32,7 +32,7 @@ func ReadHeader(data []byte) *Frame { // first 12 bytes will be a header // the rest - payload func ReadFrame(data []byte) *Frame { - _ = data[0] + _ = data[11] opt := data[0] & 0x0F // if more than 3, that we have options if opt > 3 { @@ -45,8 +45,8 @@ func ReadFrame(data []byte) *Frame { header: data[:12], payload: data[12:], } - f.header[10] = 0 - f.header[11] = 0 + + f.header[10], f.header[11] = 0, 0 return f } @@ -98,7 +98,6 @@ func (f *Frame) WriteVersion(version Version) { // To erase, we applying bitwise AND to the upper 4 bits and returning result //go:inline func (f *Frame) ReadHL() byte { - _ = f.header[0] // 0101_1111 0000_1111 return f.header[0] & 0x0F } @@ -107,13 +106,11 @@ func (f *Frame) ReadHL() byte { // we can easily apply bitwise OR and set lower 4 bits to needed hl value //go:inline func (f *Frame) writeHl(hl byte) { - _ = f.header[0] f.header[0] |= hl } //go:inline func (f *Frame) incrementHL() { - _ = f.header[0] hl := f.ReadHL() if hl > 15 { panic("header len should be less than 15") @@ -123,7 +120,6 @@ func (f *Frame) incrementHL() { //go:inline func (f *Frame) defaultHL() { - _ = f.header[0] f.writeHl(3) } @@ -131,12 +127,10 @@ func (f *Frame) defaultHL() { // Flags is full 1st byte //go:inline func (f *Frame) ReadFlags() byte { - _ = f.header[1] return f.header[1] } func (f *Frame) WriteFlags(flags ...byte) { - _ = f.header[1] for i := 0; i < len(flags); i++ { f.header[1] |= flags[i] } @@ -176,34 +170,39 @@ func (f *Frame) AppendOptions(opts []byte) { f.header = append(f.header, opts...) } -// last byte after main header and first options byte -const lb = 12 - // ReadOptions ... // f.readHL() - 2 needed to know actual options size // we know, that 2 WORDS is minimal header len // extra WORDS will add extra 32bits to the options (4 bytes) -func (f *Frame) ReadOptions() []uint32 { +func (f *Frame) ReadOptions(header []byte) []uint32 { // we can read options, if there are no options if f.ReadHL() <= 3 { return nil } + + // last byte after main header and first options byte + const lb = 12 + // Get the options len optionLen := f.ReadHL() - 3 // 3 is the default // slice in place - options := make([]uint32, 0, optionLen) + options := make([]uint32, optionLen) // Options starting from 8-th byte // we should scan with 4 byte window (32bit, WORD) - for i := byte(0); i < optionLen*WORD; i += WORD { + for i, j := byte(0), 0; i < optionLen*WORD; i, j = i+WORD, j+1 { // for example // 8 12 16 // 9 13 17 // 10 14 18 // 11 15 19 // For this data, HL will be 3, optionLen will be 12 (3*4) bytes - options = append(options, uint32(f.header[lb+i])|uint32(f.header[lb+i+1])<<8|uint32(f.header[lb+i+2])<<16|uint32(f.header[lb+i+3])<<24) + options[j] |= uint32(header[lb+i]) + options[j] |= uint32(header[lb+i+1]) << 8 + options[j] |= uint32(header[lb+i+2]) << 16 + options[j] |= uint32(header[lb+i+3]) << 24 } + return options } @@ -221,35 +220,35 @@ func (f *Frame) ReadPayloadLen() uint32 { // LE format used to write Payload // Using 4 bytes (2,3,4,5 bytes in the header) //go:inline -func (f *Frame) WritePayloadLen(len uint32) { - _ = f.header[5] - f.header[2] = byte(len) - f.header[3] = byte(len >> 8) - f.header[4] = byte(len >> 16) - f.header[5] = byte(len >> 24) +func (f *Frame) WritePayloadLen(data []byte, len uint32) { + _ = data[5] + data[2] = byte(len) + data[3] = byte(len >> 8) + data[4] = byte(len >> 16) + data[5] = byte(len >> 24) } // WriteCRC .. // Calculating CRC and writing it to the 6th byte (7th reserved) -func (f *Frame) WriteCRC() { +func (f *Frame) WriteCRC(header []byte) { // 6 7 8 9 bytes // 10, 11 reserved - _ = f.header[9] + _ = header[9] - crc := crc32.ChecksumIEEE(f.header[:6]) - f.header[6] = byte(crc) - f.header[7] = byte(crc >> 8) - f.header[8] = byte(crc >> 16) - f.header[9] = byte(crc >> 24) + crc := crc32.ChecksumIEEE(header[:6]) + header[6] = byte(crc) + header[7] = byte(crc >> 8) + header[8] = byte(crc >> 16) + header[9] = byte(crc >> 24) } // VerifyCRC .. // Reading info from 6th byte and verifying it with calculated in-place. Should be equal. // If not - drop the frame as incorrect. -func (f *Frame) VerifyCRC() bool { - _ = f.header[9] +func (f *Frame) VerifyCRC(header []byte) bool { + _ = header[9] - return crc32.ChecksumIEEE(f.header[:6]) == uint32(f.header[6])|uint32(f.header[7])<<8|uint32(f.header[8])<<16|uint32(f.header[9])<<24 + return crc32.ChecksumIEEE(header[:6]) == uint32(header[6])|uint32(header[7])<<8|uint32(header[8])<<16|uint32(header[9])<<24 } // Bytes returns header with payload diff --git a/pkg/frame/frame_test.go b/pkg/frame/frame_test.go index ef69300..0652bad 100644 --- a/pkg/frame/frame_test.go +++ b/pkg/frame/frame_test.go @@ -13,8 +13,8 @@ func TestNewFrame(t *testing.T) { nf := NewFrame() nf.WriteVersion(VERSION_1) nf.WriteFlags(CONTROL) - nf.WritePayloadLen(uint32(len([]byte(TestPayload)))) - nf.WriteCRC() + nf.WritePayloadLen(nf.Header(), uint32(len([]byte(TestPayload)))) + nf.WriteCRC(nf.header) nf.WritePayload([]byte(TestPayload)) @@ -25,16 +25,17 @@ func TestNewFrame(t *testing.T) { assert.Equal(t, rf.ReadVersion(), nf.ReadVersion()) assert.Equal(t, rf.ReadFlags(), nf.ReadFlags()) assert.Equal(t, rf.ReadPayloadLen(), nf.ReadPayloadLen()) - assert.Equal(t, true, rf.VerifyCRC()) + assert.Equal(t, true, rf.VerifyCRC(rf.Header())) + assert.Equal(t, []uint32(nil), rf.ReadOptions(rf.Header())) } func TestFrame_VerifyCRC_Fail(t *testing.T) { nf := NewFrame() // this is the wrong position - nf.WriteCRC() + nf.WriteCRC(nf.Header()) nf.WriteVersion(VERSION_1) nf.WriteFlags(CONTROL) - nf.WritePayloadLen(uint32(len([]byte(TestPayload)))) + nf.WritePayloadLen(nf.Header(), uint32(len([]byte(TestPayload)))) nf.WritePayload([]byte(TestPayload)) @@ -45,22 +46,22 @@ func TestFrame_VerifyCRC_Fail(t *testing.T) { assert.Equal(t, rf.ReadVersion(), nf.ReadVersion()) assert.Equal(t, rf.ReadFlags(), nf.ReadFlags()) assert.Equal(t, rf.ReadPayloadLen(), nf.ReadPayloadLen()) - assert.Equal(t, false, rf.VerifyCRC()) + assert.Equal(t, false, rf.VerifyCRC(rf.Header())) } func TestFrame_Options(t *testing.T) { nf := NewFrame() nf.WriteVersion(1) nf.WriteFlags(CONTROL, CODEC_GOB) - nf.WritePayloadLen(uint32(len([]byte(TestPayload)))) - nf.WriteOptions(323423432) + nf.WritePayloadLen(nf.Header(), uint32(len([]byte(TestPayload)))) + nf.WriteOptions(323423432, 1213231) // test options - options := nf.ReadOptions() - assert.Equal(t, []uint32{323423432}, options) + options := nf.ReadOptions(nf.Header()) + assert.Equal(t, []uint32{323423432, 1213231}, options) // write payload nf.WritePayload([]byte(TestPayload)) - nf.WriteCRC() + nf.WriteCRC(nf.Header()) data := nf.Bytes() rf := ReadFrame(data) @@ -68,21 +69,21 @@ func TestFrame_Options(t *testing.T) { assert.Equal(t, rf.ReadVersion(), nf.ReadVersion()) assert.Equal(t, rf.ReadFlags(), nf.ReadFlags()) assert.Equal(t, rf.ReadPayloadLen(), nf.ReadPayloadLen()) - assert.Equal(t, rf.VerifyCRC(), true) + assert.Equal(t, rf.VerifyCRC(rf.Header()), true) } func TestFrame_Bytes(t *testing.T) { nf := NewFrame() nf.WriteVersion(1) nf.WriteFlags(CONTROL, CODEC_GOB) - nf.WritePayloadLen(uint32(len([]byte(TestPayload)))) + nf.WritePayloadLen(nf.Header(), uint32(len([]byte(TestPayload)))) nf.WriteOptions(323423432) - assert.Equal(t, []uint32{323423432}, nf.ReadOptions()) + assert.Equal(t, []uint32{323423432}, nf.ReadOptions(nf.Header())) nf.WritePayload([]byte(TestPayload)) - nf.WriteCRC() - assert.Equal(t, true, nf.VerifyCRC()) + nf.WriteCRC(nf.Header()) + assert.Equal(t, true, nf.VerifyCRC(nf.Header())) data := nf.Bytes() rf := ReadFrame(data) @@ -90,8 +91,8 @@ func TestFrame_Bytes(t *testing.T) { assert.Equal(t, rf.ReadVersion(), nf.ReadVersion()) assert.Equal(t, rf.ReadFlags(), nf.ReadFlags()) assert.Equal(t, rf.ReadPayloadLen(), nf.ReadPayloadLen()) - assert.Equal(t, true, rf.VerifyCRC()) - assert.Equal(t, []uint32{323423432}, rf.ReadOptions()) + assert.Equal(t, true, rf.VerifyCRC(rf.Header())) + assert.Equal(t, []uint32{323423432}, rf.ReadOptions(rf.Header())) } func BenchmarkCRC32(b *testing.B) { @@ -107,15 +108,33 @@ func BenchmarkFrame_CRC(b *testing.B) { nf := NewFrame() nf.WriteVersion(VERSION_1) nf.WriteFlags(CONTROL, CODEC_GOB) - nf.WritePayloadLen(uint32(len([]byte(TestPayload)))) + nf.WritePayloadLen(nf.Header(), uint32(len([]byte(TestPayload)))) nf.WriteOptions(1000, 1000, 1000, 1000, 1000, 1000) b.ResetTimer() b.ReportAllocs() for i := 0; i < b.N; i++ { - nf.WriteCRC() - if !nf.VerifyCRC() { + nf.WriteCRC(nf.Header()) + if !nf.VerifyCRC(nf.Header()) { + panic("CRC") + } + } +} + +func BenchmarkFrame(b *testing.B) { + b.ResetTimer() + b.ReportAllocs() + + for i := 0; i < b.N; i++ { + nf := NewFrame() + nf.WriteVersion(VERSION_1) + nf.WriteFlags(CONTROL, CODEC_GOB) + nf.WritePayloadLen(nf.Header(), uint32(len([]byte(TestPayload)))) + nf.WriteOptions(1000, 1000, 1000, 1000, 1000, 1000) + nf.WriteCRC(nf.Header()) + + if !nf.VerifyCRC(nf.Header()) { panic("CRC") } } diff --git a/pkg/pipe/pipe_test.go b/pkg/pipe/pipe_test.go index 37f3c56..0bfb5a7 100644 --- a/pkg/pipe/pipe_test.go +++ b/pkg/pipe/pipe_test.go @@ -18,10 +18,10 @@ func TestPipeReceive(t *testing.T) { nf := frame.NewFrame() nf.WriteVersion(frame.VERSION_1) nf.WriteFlags(frame.CONTROL, frame.CODEC_GOB, frame.CODEC_JSON) - nf.WritePayloadLen(uint32(len([]byte(TestPayload)))) + nf.WritePayloadLen(nf.Header(), uint32(len([]byte(TestPayload)))) nf.WritePayload([]byte(TestPayload)) - nf.WriteCRC() - assert.Equal(t, true, nf.VerifyCRC()) + nf.WriteCRC(nf.Header()) + assert.Equal(t, true, nf.VerifyCRC(nf.Header())) go func(frame *frame.Frame) { defer func() { @@ -41,7 +41,7 @@ func TestPipeReceive(t *testing.T) { assert.Equal(t, fr.ReadVersion(), nf.ReadVersion()) assert.Equal(t, fr.ReadFlags(), nf.ReadFlags()) assert.Equal(t, fr.ReadPayloadLen(), nf.ReadPayloadLen()) - assert.Equal(t, true, fr.VerifyCRC()) + assert.Equal(t, true, fr.VerifyCRC(nf.Header())) assert.Equal(t, []byte(TestPayload), fr.Payload()) } @@ -53,11 +53,11 @@ func TestPipeReceiveWithOptions(t *testing.T) { nf := frame.NewFrame() nf.WriteVersion(frame.VERSION_1) nf.WriteFlags(frame.CONTROL, frame.CODEC_GOB, frame.CODEC_JSON) - nf.WritePayloadLen(uint32(len([]byte(TestPayload)))) + nf.WritePayloadLen(nf.Header(), uint32(len([]byte(TestPayload)))) nf.WritePayload([]byte(TestPayload)) nf.WriteOptions(100, 10000, 100000) - nf.WriteCRC() - assert.Equal(t, true, nf.VerifyCRC()) + nf.WriteCRC(nf.Header()) + assert.Equal(t, true, nf.VerifyCRC(nf.Header())) go func(frame *frame.Frame) { defer func() { @@ -77,9 +77,9 @@ func TestPipeReceiveWithOptions(t *testing.T) { assert.Equal(t, fr.ReadVersion(), nf.ReadVersion()) assert.Equal(t, fr.ReadFlags(), nf.ReadFlags()) assert.Equal(t, fr.ReadPayloadLen(), nf.ReadPayloadLen()) - assert.Equal(t, true, fr.VerifyCRC()) + assert.Equal(t, true, fr.VerifyCRC(fr.Header())) assert.Equal(t, []byte(TestPayload), fr.Payload()) - assert.Equal(t, []uint32{100, 10000, 100000}, fr.ReadOptions()) + assert.Equal(t, []uint32{100, 10000, 100000}, fr.ReadOptions(fr.Header())) } func TestPipeCRC_Failed(t *testing.T) { @@ -90,9 +90,9 @@ func TestPipeCRC_Failed(t *testing.T) { nf := frame.NewFrame() nf.WriteVersion(frame.VERSION_1) nf.WriteFlags(frame.CONTROL) - nf.WritePayloadLen(uint32(len([]byte(TestPayload)))) + nf.WritePayloadLen(nf.Header(), uint32(len([]byte(TestPayload)))) - assert.Equal(t, false, nf.VerifyCRC()) + assert.Equal(t, false, nf.VerifyCRC(nf.Header())) nf.WritePayload([]byte(TestPayload)) diff --git a/pkg/rpc/client.go b/pkg/rpc/client.go index a72a26e..c06d230 100644 --- a/pkg/rpc/client.go +++ b/pkg/rpc/client.go @@ -101,9 +101,9 @@ func (c *ClientCodec) WriteRequest(r *rpc.Request, fr.WriteOptions(uint32(r.Seq), uint32(len(r.ServiceMethod))) fr.WriteVersion(frame.VERSION_1) - fr.WritePayloadLen(uint32(buf.Len())) + fr.WritePayloadLen(fr.Header(), uint32(buf.Len())) fr.WritePayload(buf.Bytes()) - fr.WriteCRC() + fr.WriteCRC(fr.Header()) err := c.relay.Send(fr) if err != nil { @@ -123,14 +123,14 @@ func (c *ClientCodec) ReadResponseHeader(r *rpc.Response) error { if err != nil { return errors.E(op, err) } - if !fr.VerifyCRC() { + if !fr.VerifyCRC(fr.Header()) { return errors.E(op, errors.Str("CRC verification failed")) } - // save the fr after CRC verification + // save the frame after CRC verification c.frame = fr - opts := fr.ReadOptions() + opts := fr.ReadOptions(fr.Header()) if len(opts) != 2 { return errors.E(op, errors.Str("should be 2 options. SEQ_ID and METHOD_LEN")) } diff --git a/pkg/rpc/codec.go b/pkg/rpc/codec.go index 19b2a59..369f25b 100644 --- a/pkg/rpc/codec.go +++ b/pkg/rpc/codec.go @@ -147,10 +147,10 @@ func (c *Codec) WriteResponse(r *rpc.Response, body interface{}) error { //nolin //go:inline func (c *Codec) sendBuf(frame *frame.Frame, buf *bytes.Buffer) error { - frame.WritePayloadLen(uint32(buf.Len())) + frame.WritePayloadLen(frame.Header(), uint32(buf.Len())) frame.WritePayload(buf.Bytes()) - frame.WriteCRC() + frame.WriteCRC(frame.Header()) return c.relay.Send(frame) } @@ -167,10 +167,10 @@ func (c *Codec) handleError(r *rpc.Response, fr *frame.Frame, err string) error if err != "" { buf.WriteString(err) } - fr.WritePayloadLen(uint32(buf.Len())) + fr.WritePayloadLen(fr.Header(), uint32(buf.Len())) fr.WritePayload(buf.Bytes()) - fr.WriteCRC() + fr.WriteCRC(fr.Header()) _ = c.relay.Send(fr) return errors.E(op, errors.Str(r.Error)) } @@ -194,7 +194,7 @@ func (c *Codec) ReadRequestHeader(r *rpc.Request) error { // opts[0] sequence ID // opts[1] service method name offset from payload in bytes - opts := f.ReadOptions() + opts := f.ReadOptions(f.Header()) if len(opts) != 2 { return errors.E(op, errors.Str("should be 2 options. SEQ_ID and METHOD_LEN")) } diff --git a/pkg/rpc/decoders.go b/pkg/rpc/decoders.go index 346d2b7..d240c80 100644 --- a/pkg/rpc/decoders.go +++ b/pkg/rpc/decoders.go @@ -13,7 +13,7 @@ import ( func decodeJSON(out interface{}, frame *frame.Frame) error { const op = errors.Op("goridge_decode_json") - opts := frame.ReadOptions() + opts := frame.ReadOptions(frame.Header()) if len(opts) != 2 { return errors.E(op, errors.Str("should be 2 options. SEQ_ID and METHOD_LEN")) } @@ -26,7 +26,7 @@ func decodeJSON(out interface{}, frame *frame.Frame) error { func decodeGob(out interface{}, frame *frame.Frame) error { const op = errors.Op("goridge_decode_gob") - opts := frame.ReadOptions() + opts := frame.ReadOptions(frame.Header()) if len(opts) != 2 { return errors.E(op, errors.Str("should be 2 options. SEQ_ID and METHOD_LEN")) } @@ -44,7 +44,7 @@ func decodeGob(out interface{}, frame *frame.Frame) error { func decodeProto(out interface{}, frame *frame.Frame) error { const op = errors.Op("goridge_decode_proto") - opts := frame.ReadOptions() + opts := frame.ReadOptions(frame.Header()) if len(opts) != 2 { return errors.E(op, errors.Str("should be 2 options. SEQ_ID and METHOD_LEN")) } @@ -68,7 +68,7 @@ func decodeProto(out interface{}, frame *frame.Frame) error { func decodeRaw(out interface{}, frame *frame.Frame) error { const op = errors.Op("goridge_decode_raw") - opts := frame.ReadOptions() + opts := frame.ReadOptions(frame.Header()) if len(opts) != 2 { return errors.E(op, errors.Str("should be 2 options. SEQ_ID and METHOD_LEN")) } @@ -87,7 +87,7 @@ func decodeRaw(out interface{}, frame *frame.Frame) error { func decodeMsgPack(out interface{}, frame *frame.Frame) error { const op = errors.Op("goridge_decodemsgpack") - opts := frame.ReadOptions() + opts := frame.ReadOptions(frame.Header()) if len(opts) != 2 { return errors.E(op, errors.Str("should be 2 options. SEQ_ID and METHOD_LEN")) } diff --git a/pkg/socket/socket_test.go b/pkg/socket/socket_test.go index c7eebc7..3a177b6 100644 --- a/pkg/socket/socket_test.go +++ b/pkg/socket/socket_test.go @@ -19,10 +19,10 @@ func TestSocketRelay(t *testing.T) { nf := frame.NewFrame() nf.WriteVersion(frame.VERSION_1) nf.WriteFlags(frame.CONTROL, frame.CODEC_GOB, frame.CODEC_JSON) - nf.WritePayloadLen(uint32(len([]byte(TestPayload)))) + nf.WritePayloadLen(nf.Header(), uint32(len([]byte(TestPayload)))) nf.WritePayload([]byte(TestPayload)) - nf.WriteCRC() - assert.Equal(t, true, nf.VerifyCRC()) + nf.WriteCRC(nf.Header()) + assert.Equal(t, true, nf.VerifyCRC(nf.Header())) conn, err := net.Dial("tcp", "localhost:10002") assert.NoError(t, err) @@ -45,7 +45,7 @@ func TestSocketRelay(t *testing.T) { assert.Equal(t, fr.ReadVersion(), nf.ReadVersion()) assert.Equal(t, fr.ReadFlags(), nf.ReadFlags()) assert.Equal(t, fr.ReadPayloadLen(), nf.ReadPayloadLen()) - assert.Equal(t, true, fr.VerifyCRC()) + assert.Equal(t, true, fr.VerifyCRC(fr.Header())) assert.Equal(t, []byte(TestPayload), fr.Payload()) } @@ -58,11 +58,11 @@ func TestSocketRelayOptions(t *testing.T) { nf := frame.NewFrame() nf.WriteVersion(frame.VERSION_1) nf.WriteFlags(frame.CONTROL, frame.CODEC_GOB, frame.CODEC_JSON) - nf.WritePayloadLen(uint32(len([]byte(TestPayload)))) + nf.WritePayloadLen(nf.Header(), uint32(len([]byte(TestPayload)))) nf.WritePayload([]byte(TestPayload)) nf.WriteOptions(100, 10000, 100000) - nf.WriteCRC() - assert.Equal(t, true, nf.VerifyCRC()) + nf.WriteCRC(nf.Header()) + assert.Equal(t, true, nf.VerifyCRC(nf.Header())) conn, err := net.Dial("tcp", "localhost:10001") assert.NoError(t, err) @@ -85,9 +85,9 @@ func TestSocketRelayOptions(t *testing.T) { assert.Equal(t, fr.ReadVersion(), nf.ReadVersion()) assert.Equal(t, fr.ReadFlags(), nf.ReadFlags()) assert.Equal(t, fr.ReadPayloadLen(), nf.ReadPayloadLen()) - assert.Equal(t, true, fr.VerifyCRC()) + assert.Equal(t, true, fr.VerifyCRC(fr.Header())) assert.Equal(t, []byte(TestPayload), fr.Payload()) - assert.Equal(t, []uint32{100, 10000, 100000}, fr.ReadOptions()) + assert.Equal(t, []uint32{100, 10000, 100000}, fr.ReadOptions(fr.Header())) } func TestSocketRelayNoPayload(t *testing.T) { @@ -100,8 +100,8 @@ func TestSocketRelayNoPayload(t *testing.T) { nf.WriteVersion(frame.VERSION_1) nf.WriteFlags(frame.CONTROL, frame.CODEC_GOB, frame.CODEC_JSON) nf.WriteOptions(100, 10000, 100000) - nf.WriteCRC() - assert.Equal(t, true, nf.VerifyCRC()) + nf.WriteCRC(nf.Header()) + assert.Equal(t, true, nf.VerifyCRC(nf.Header())) conn, err := net.Dial("tcp", "localhost:12221") assert.NoError(t, err) @@ -124,9 +124,9 @@ func TestSocketRelayNoPayload(t *testing.T) { assert.Equal(t, fr.ReadVersion(), nf.ReadVersion()) assert.Equal(t, fr.ReadFlags(), nf.ReadFlags()) assert.Equal(t, fr.ReadPayloadLen(), nf.ReadPayloadLen()) // should be zero, without error - assert.Equal(t, true, fr.VerifyCRC()) + assert.Equal(t, true, fr.VerifyCRC(fr.Header())) assert.Equal(t, []byte{}, fr.Payload()) // empty - assert.Equal(t, []uint32{100, 10000, 100000}, fr.ReadOptions()) + assert.Equal(t, []uint32{100, 10000, 100000}, fr.ReadOptions(fr.Header())) } func TestSocketRelayWrongCRC(t *testing.T) { @@ -139,7 +139,7 @@ func TestSocketRelayWrongCRC(t *testing.T) { nf.WriteVersion(frame.VERSION_1) nf.WriteFlags(frame.CONTROL, frame.CODEC_GOB, frame.CODEC_JSON) nf.WriteOptions(100, 10000, 100000) - nf.WriteCRC() + nf.WriteCRC(nf.Header()) nf.Header()[6] = 22 // just random wrong CRC directly conn, err := net.Dial("tcp", "localhost:13445")