Skip to content

Commit bd470a1

Browse files
committed
Do not add Compliance Region to the packet if it's a 0 value
1 parent b9f27cc commit bd470a1

File tree

4 files changed

+112
-12
lines changed

4 files changed

+112
-12
lines changed

protocol/conn_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package protocol
2+
3+
import (
4+
"bytes"
5+
"crypto/rand"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestRespond(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
payloadSize int
15+
expectedTotalByteSize int64
16+
expectedLengthHeader uint16
17+
}{
18+
{
19+
name: "minimum packet size is 1024",
20+
payloadSize: 1000,
21+
expectedTotalByteSize: 1024,
22+
expectedLengthHeader: 1016, // minimum size - header or payload tlv + opcode tlv + padding
23+
},
24+
{
25+
name: "packet size over 1024 bytes",
26+
payloadSize: 2000,
27+
expectedTotalByteSize: 2015, // payload TLV + 4 bytes for the Opcode + 8 bytes for header
28+
expectedLengthHeader: 2007, // expectedTotalByteSize - header
29+
},
30+
}
31+
32+
for _, tt := range tests {
33+
t.Run(tt.name, func(t *testing.T) {
34+
require := require.New(t)
35+
36+
var buf bytes.Buffer
37+
payload := make([]byte, tt.payloadSize)
38+
rand.Read(payload)
39+
40+
Respond(&buf, 1, payload)
41+
42+
var pkt Packet
43+
size, err := pkt.ReadFrom(&buf)
44+
require.NoError(err)
45+
require.Equal(tt.expectedTotalByteSize, size)
46+
require.Equal(tt.expectedLengthHeader, pkt.Length)
47+
// the only two non-header fields on the response should be Opcode and Payload
48+
// if other fields are added, it can be a breaking change for the client depending
49+
// the implementation of the protocol.
50+
require.NotEmpty(pkt.Opcode)
51+
require.NotEmpty(pkt.Payload)
52+
53+
require.Empty(pkt.Extra)
54+
require.Empty(pkt.SKI)
55+
require.Empty(pkt.Digest)
56+
require.Empty(pkt.ClientIP)
57+
require.Empty(pkt.ServerIP)
58+
require.Empty(pkt.SNI)
59+
require.Empty(pkt.CertID)
60+
require.Empty(pkt.ForwardingSvc)
61+
require.Empty(pkt.CustomFuncName)
62+
require.Empty(pkt.JaegerSpan)
63+
require.Empty(pkt.ReqContext)
64+
require.Empty(pkt.ComplianceRegion)
65+
})
66+
}
67+
}

protocol/protocol.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,9 @@ func (o *Operation) Bytes() uint16 {
527527
add(tlvLen(len(o.ReqContext)))
528528
}
529529
// compliance region
530-
add(tlvLen(1))
530+
if o.ComplianceRegion != 0 {
531+
add(tlvLen(1))
532+
}
531533
if int(length)+headerSize < paddedLength {
532534
// TODO: Are we sure that's the right behavior?
533535

@@ -602,7 +604,9 @@ func (o *Operation) MarshalBinary() ([]byte, error) {
602604
b = append(b, tlvBytes(TagReqContext, o.ReqContext)...)
603605
}
604606

605-
b = append(b, tlvBytes(TagComplianceRegion, []byte{byte(o.ComplianceRegion)})...)
607+
if o.ComplianceRegion != 0 {
608+
b = append(b, tlvBytes(TagComplianceRegion, []byte{byte(o.ComplianceRegion)})...)
609+
}
606610

607611
if len(b)+headerSize < paddedLength {
608612
// TODO: Are we sure that's the right behavior?

protocol/protocol_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,36 @@ func TestMarshalBinary(t *testing.T) {
4242
require.NoError(err)
4343

4444
var pkt2 Packet
45-
_, err = pkt2.ReadFrom(bytes.NewReader(b))
45+
size, err := pkt2.ReadFrom(bytes.NewReader(b))
4646
require.NoError(err)
4747
require.Equal(pkt.ID, pkt2.ID)
4848
require.Equal(op, pkt2.Operation)
49+
50+
// now do the same test with a 0 value for compliance region
51+
globalOp := Operation{
52+
Opcode: OpECDSASignSHA256,
53+
Payload: payload,
54+
Extra: extra,
55+
Digest: sha256.Sum256([]byte("Digest")),
56+
SKI: sha1.Sum([]byte("SKI")),
57+
ClientIP: net.ParseIP("1.1.1.1").To4(),
58+
ServerIP: net.ParseIP("2.2.2.2").To4(),
59+
SNI: "SNI",
60+
CertID: "SNI",
61+
CustomFuncName: "CustomFuncName",
62+
JaegerSpan: []byte("615f730ad5fe896f:615f730ad5fe896f:1"),
63+
ReqContext: reqCtx,
64+
}
65+
globalPkt := NewPacket(42, globalOp)
66+
gb, err := globalPkt.MarshalBinary()
67+
require.NoError(err)
68+
69+
var globalPkt2 Packet
70+
globalSize, err := globalPkt2.ReadFrom(bytes.NewReader(gb))
71+
require.NoError(err)
72+
require.Equal(globalPkt.ID, globalPkt2.ID)
73+
require.Equal(globalOp, globalPkt2.Operation)
74+
75+
// the global op should be 4 bytes smaller because it does not include an ComplianceRegion TLV
76+
require.Equal(size, globalSize+4)
4977
}

tracing/tracing.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ func SpanContextToBinary(sc opentracing.SpanContext) ([]byte, error) {
3939
// SetOperationSpanTags sets span tags with all of the operation's fields
4040
func SetOperationSpanTags(span opentracing.Span, op *protocol.Operation) {
4141
tags := map[string]interface{}{
42-
"operation.opcode": op.Opcode.String(),
43-
"operation.ski": op.SKI,
44-
"operation.digest": fmt.Sprintf("%02x", op.Digest),
45-
"operation.clientip": op.ClientIP,
46-
"operation.serverip": op.ServerIP,
47-
"operation.sni": op.SNI,
48-
"operation.certid": op.CertID,
49-
"operation.customfuncname": op.CustomFuncName,
50-
"operation.forwardingsvc": fmt.Sprintf("%d", op.ForwardingSvc),
42+
"operation.opcode": op.Opcode.String(),
43+
"operation.ski": op.SKI,
44+
"operation.digest": fmt.Sprintf("%02x", op.Digest),
45+
"operation.clientip": op.ClientIP,
46+
"operation.serverip": op.ServerIP,
47+
"operation.sni": op.SNI,
48+
"operation.certid": op.CertID,
49+
"operation.customfuncname": op.CustomFuncName,
50+
"operation.forwardingsvc": fmt.Sprintf("%d", op.ForwardingSvc),
51+
"operation.compliance_region": op.ComplianceRegion.String(),
5152
}
5253
for k, v := range tags {
5354
span.SetTag(k, v)

0 commit comments

Comments
 (0)