@@ -922,6 +922,111 @@ func TestMatchTransportPortRange(t *testing.T) {
922922 }
923923}
924924
925+ func TestMatchUdp (t * testing.T ) {
926+ var tests = []struct {
927+ desc string
928+ m Match
929+ out string
930+ }{
931+ {
932+ desc : "source port 80" ,
933+ m : UdpSourcePort (80 ),
934+ out : "udp_src=80" ,
935+ },
936+ {
937+ desc : "source port 65535" ,
938+ m : UdpSourcePort (65535 ),
939+ out : "udp_src=65535" ,
940+ },
941+ {
942+ desc : "destination port 22" ,
943+ m : UdpDestinationPort (22 ),
944+ out : "udp_dst=22" ,
945+ },
946+ {
947+ desc : "destination port 8080" ,
948+ m : UdpDestinationPort (8080 ),
949+ out : "udp_dst=8080" ,
950+ },
951+ {
952+ desc : "source port range 16/0xfff0 (16-31)" ,
953+ m : UdpSourceMaskedPort (0x10 , 0xfff0 ),
954+ out : "udp_src=0x0010/0xfff0" ,
955+ },
956+ {
957+ desc : "destination port range 16/0xfff0 (16-31)" ,
958+ m : UdpDestinationMaskedPort (0x10 , 0xfff0 ),
959+ out : "udp_dst=0x0010/0xfff0" ,
960+ },
961+ }
962+
963+ for _ , tt := range tests {
964+ t .Run (tt .desc , func (t * testing.T ) {
965+ out , err := tt .m .MarshalText ()
966+ if err != nil {
967+ t .Fatalf ("unexpected error: %v" , err )
968+ }
969+
970+ if want , got := tt .out , string (out ); want != got {
971+ t .Fatalf ("unexpected Match output:\n - want: %q\n - got: %q" ,
972+ want , got )
973+ }
974+ })
975+ }
976+ }
977+
978+ func TestMatchUdpPortRange (t * testing.T ) {
979+ var tests = []struct {
980+ desc string
981+ pr TransportPortRanger
982+ m []Match
983+ }{
984+ {
985+ desc : "destination port range 16-31" ,
986+ pr : UdpDestinationPortRange (16 , 31 ),
987+ m : []Match {
988+ UdpDestinationMaskedPort (0x10 , 0xfff0 ),
989+ },
990+ },
991+ {
992+ desc : "source port range 16-31" ,
993+ pr : UdpSourcePortRange (16 , 31 ),
994+ m : []Match {
995+ UdpSourceMaskedPort (0x10 , 0xfff0 ),
996+ },
997+ },
998+ {
999+ desc : "destination port range 16-32" ,
1000+ pr : UdpDestinationPortRange (16 , 32 ),
1001+ m : []Match {
1002+ UdpDestinationMaskedPort (0x10 , 0xfff0 ),
1003+ UdpDestinationMaskedPort (0x20 , 0xffff ),
1004+ },
1005+ },
1006+ {
1007+ desc : "source port range 16-32" ,
1008+ pr : UdpSourcePortRange (16 , 32 ),
1009+ m : []Match {
1010+ UdpSourceMaskedPort (0x10 , 0xfff0 ),
1011+ UdpSourceMaskedPort (0x20 , 0xffff ),
1012+ },
1013+ },
1014+ }
1015+
1016+ for _ , tt := range tests {
1017+ t .Run (tt .desc , func (t * testing.T ) {
1018+ m , err := tt .pr .MaskedPorts ()
1019+ if err != nil {
1020+ t .Fatalf ("unexpected error: %v" , err )
1021+ }
1022+ if want , got := tt .m , m ; ! reflect .DeepEqual (want , got ) {
1023+ t .Fatalf ("unexpected Match:\n - want: %q\n - got: %q" ,
1024+ want , got )
1025+ }
1026+ })
1027+ }
1028+ }
1029+
9251030func TestMatchVLANTCI (t * testing.T ) {
9261031 var tests = []struct {
9271032 desc string
0 commit comments