github.com/cilium/cilium@v1.16.2/pkg/hubble/filters/protocol_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Hubble 3 4 package filters 5 6 import ( 7 "context" 8 "testing" 9 10 flowpb "github.com/cilium/cilium/api/v1/flow" 11 v1 "github.com/cilium/cilium/pkg/hubble/api/v1" 12 ) 13 14 func TestFlowProtocolFilter(t *testing.T) { 15 type args struct { 16 f []*flowpb.FlowFilter 17 ev *v1.Event 18 } 19 tests := []struct { 20 name string 21 args args 22 wantErr bool 23 want bool 24 }{ 25 { 26 name: "udp", 27 args: args{ 28 f: []*flowpb.FlowFilter{{Protocol: []string{"udp"}}}, 29 ev: &v1.Event{Event: &flowpb.Flow{ 30 L4: &flowpb.Layer4{Protocol: &flowpb.Layer4_UDP{UDP: &flowpb.UDP{}}}, 31 }}, 32 }, 33 want: true, 34 }, 35 { 36 name: "http", 37 args: args{ 38 f: []*flowpb.FlowFilter{{Protocol: []string{"http"}}}, 39 ev: &v1.Event{Event: &flowpb.Flow{ 40 L4: &flowpb.Layer4{Protocol: &flowpb.Layer4_TCP{TCP: &flowpb.TCP{}}}, 41 L7: &flowpb.Layer7{Record: &flowpb.Layer7_Http{Http: &flowpb.HTTP{}}}, 42 }}, 43 }, 44 want: true, 45 }, 46 { 47 name: "icmp (v4)", 48 args: args{ 49 f: []*flowpb.FlowFilter{{Protocol: []string{"icmp"}}}, 50 ev: &v1.Event{Event: &flowpb.Flow{ 51 L4: &flowpb.Layer4{Protocol: &flowpb.Layer4_ICMPv4{ICMPv4: &flowpb.ICMPv4{}}}, 52 }}, 53 }, 54 want: true, 55 }, 56 { 57 name: "icmp (v6)", 58 args: args{ 59 f: []*flowpb.FlowFilter{{Protocol: []string{"icmp"}}}, 60 ev: &v1.Event{Event: &flowpb.Flow{ 61 L4: &flowpb.Layer4{Protocol: &flowpb.Layer4_ICMPv6{ICMPv6: &flowpb.ICMPv6{}}}, 62 }}, 63 }, 64 want: true, 65 }, 66 { 67 name: "multiple protocols", 68 args: args{ 69 f: []*flowpb.FlowFilter{{Protocol: []string{"tcp", "kafka"}}}, 70 ev: &v1.Event{Event: &flowpb.Flow{ 71 L4: &flowpb.Layer4{Protocol: &flowpb.Layer4_TCP{TCP: &flowpb.TCP{}}}, 72 }}, 73 }, 74 want: true, 75 }, 76 { 77 name: "invalid protocols", 78 args: args{ 79 f: []*flowpb.FlowFilter{{Protocol: []string{"not a protocol"}}}, 80 }, 81 wantErr: true, 82 }, 83 } 84 85 for _, tt := range tests { 86 t.Run(tt.name, func(t *testing.T) { 87 fl, err := BuildFilterList(context.Background(), tt.args.f, []OnBuildFilter{&ProtocolFilter{}}) 88 if (err != nil) != tt.wantErr { 89 t.Errorf("\"%s\" error = %v, wantErr %v", tt.name, err, tt.wantErr) 90 return 91 } 92 if err != nil { 93 return 94 } 95 if got := fl.MatchOne(tt.args.ev); got != tt.want { 96 t.Errorf("\"%s\" got %v, want %v", tt.name, got, tt.want) 97 } 98 }) 99 } 100 }