Skip to content

Commit

Permalink
Merge pull request #278 from nickysemenza/fix-protocol-bytes-size
Browse files Browse the repository at this point in the history
fix: correctly calculate protocol.Operation Bytes() size
  • Loading branch information
nickysemenza authored Sep 16, 2020
2 parents 463b231 + a4447fd commit 851ae38
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 6 additions & 0 deletions protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,12 @@ func (o *Operation) Bytes() uint16 {
if o.CertID != "" {
add(tlvLen(len([]byte(o.CertID))))
}
if o.CustomFuncName != "" {
add(tlvLen(len([]byte(o.CustomFuncName))))
}
if o.JaegerSpan != nil {
add(tlvLen(len(o.JaegerSpan)))
}
if int(length)+headerSize < paddedLength {
// TODO: Are we sure that's the right behavior?

Expand Down
15 changes: 12 additions & 3 deletions protocol/protocol_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package protocol

import (
"bytes"
"crypto/rand"
"crypto/sha1"
"crypto/sha256"
"net"
Expand All @@ -12,24 +14,31 @@ import (
func TestMarshalBinary(t *testing.T) {
require := require.New(t)

// we want to push the payload over the paddedLength of 1024
// to ensure that the size is calculated correctly.
extra := make([]byte, 100)
payload := make([]byte, 1000)
rand.Read(extra)
rand.Read(payload)
op := Operation{
Opcode: OpECDSASignSHA256,
Payload: []byte("Payload"),
Extra: []byte("Extra"),
Payload: payload,
Extra: extra,
Digest: sha256.Sum256([]byte("Digest")),
SKI: sha1.Sum([]byte("SKI")),
ClientIP: net.ParseIP("1.1.1.1").To4(),
ServerIP: net.ParseIP("2.2.2.2").To4(),
SNI: "SNI",
CertID: "SNI",
CustomFuncName: "CustomFuncName",
JaegerSpan: []byte("615f730ad5fe896f:615f730ad5fe896f:1"),
}
pkt := NewPacket(42, op)
b, err := pkt.MarshalBinary()
require.NoError(err)

var pkt2 Packet
err = pkt2.UnmarshalBinary(b)
_, err = pkt2.ReadFrom(bytes.NewReader(b))
require.NoError(err)
require.Equal(pkt.ID, pkt2.ID)
require.Equal(op, pkt2.Operation)
Expand Down

0 comments on commit 851ae38

Please # to comment.