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