1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
package tlvparse
import (
"testing"
"github.com/pires/go-proxyproto"
)
func TestExtractPSCConnectionID(t *testing.T) {
tests := []struct {
name string
tlvs []proxyproto.TLV
wantPSCConnectionID uint64
wantFound bool
}{
{
name: "nil TLVs",
tlvs: nil,
wantFound: false,
},
{
name: "empty TLVs",
tlvs: []proxyproto.TLV{},
wantFound: false,
},
{
name: "AWS VPC endpoint ID",
tlvs: []proxyproto.TLV{
{
Type: 0xEA,
Value: []byte{0x01, 0x76, 0x70, 0x63, 0x65, 0x2d, 0x61, 0x62, 0x63, 0x31, 0x32, 0x33},
},
},
wantFound: false,
},
{
name: "GCP link ID",
tlvs: []proxyproto.TLV{
{
Type: PP2_TYPE_GCP,
Value: []byte{'\xff', '\xff', '\xff', '\xff', '\xc0', '\xa8', '\x64', '\x02'},
},
},
wantPSCConnectionID: 18446744072646845442,
wantFound: true,
},
{
name: "Multiple TLVs",
tlvs: []proxyproto.TLV{
{ // AWS
Type: 0xEA,
Value: []byte{0x01, 0x76, 0x70, 0x63, 0x65, 0x2d, 0x61, 0x62, 0x63, 0x31, 0x32, 0x33},
},
{ // Azure
Type: 0xEE,
Value: []byte{0x02, 0x01, 0x01, 0x01, 0x01},
},
{ // GCP but wrong length
Type: 0xE0,
Value: []byte{0xff, 0xff, 0xff},
},
{ // Correct
Type: 0xE0,
Value: []byte{'\xff', '\xff', '\xff', '\xff', '\xc0', '\xa8', '\x64', '\x02'},
},
},
wantPSCConnectionID: 18446744072646845442,
wantFound: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
linkID, hasLinkID := ExtractPSCConnectionID(tt.tlvs)
if hasLinkID != tt.wantFound {
t.Errorf("ExtractPSCConnectionID() got1 = %v, want %v", hasLinkID, tt.wantFound)
}
if linkID != tt.wantPSCConnectionID {
t.Errorf("ExtractPSCConnectionID() got = %v, want %v", linkID, tt.wantPSCConnectionID)
}
})
}
}
|