@@ -62,7 +62,7 @@ func TestCompileEnforcesSelectorsAndLongestPrefixOwnership(t *testing.T) {
6262 }
6363 for _ , test := range tests {
6464 t .Run (test .name , func (t * testing.T ) {
65- if got := policy .runIn (& test .pkt , 0 ); got != test .want {
65+ if got := policy .filter . RunIn (& test .pkt , 0 ); got != test .want {
6666 t .Fatalf ("RunIn() = %v, want %v" , got , test .want )
6767 }
6868 })
@@ -76,7 +76,7 @@ func TestCompileInactiveDestinationsAndExplicitSourceValidation(t *testing.T) {
7676 }}}
7777 policy := mustCompile (t , cfg , netip.Addr {}, false , nil )
7878 pkt := parsedPacket (ipproto .TCP , "10.0.0.2" , "10.0.0.1" , 40000 , 22 , packet .TCPSyn )
79- if got := policy .runIn (& pkt , 0 ); got != filter .Drop {
79+ if got := policy .filter . RunIn (& pkt , 0 ); got != filter .Drop {
8080 t .Fatalf ("inactive destination grant accepted packet: %v" , got )
8181 }
8282
@@ -104,41 +104,56 @@ func TestPolicyStateSharingAndIsolation(t *testing.T) {
104104 t .Fatalf ("RunOut() = %v, want accept" , got )
105105 }
106106 reply := parsedPacket (ipproto .UDP , "10.0.0.2" , "10.0.0.1" , 53 , 41000 , 0 )
107- if got := base .runIn (& reply , 0 ); got != filter .Accept {
107+ if got := base .filter . RunIn (& reply , 0 ); got != filter .Accept {
108108 t .Fatalf ("reply through base = %v, want accept" , got )
109109 }
110110
111111 shared := mustCompile (t , cfg , netip.Addr {}, true , base )
112- if got := shared .runIn (& reply , 0 ); got != filter .Accept {
112+ if got := shared .filter . RunIn (& reply , 0 ); got != filter .Accept {
113113 t .Fatalf ("reply through shared restrictive policy = %v, want accept" , got )
114114 }
115115 isolated := mustCompile (t , cfg , netip.Addr {}, true , nil )
116- if got := isolated .runIn (& reply , 0 ); got != filter .Drop {
116+ if got := isolated .filter . RunIn (& reply , 0 ); got != filter .Drop {
117117 t .Fatalf ("reply through isolated restrictive policy = %v, want drop" , got )
118118 }
119119 otherProfile := mustCompile (t , cfg , netip.Addr {}, false , nil )
120- if got := otherProfile .runIn (& reply , 0 ); got != filter .Drop {
120+ if got := otherProfile .filter . RunIn (& reply , 0 ); got != filter .Drop {
121121 t .Fatalf ("independent profile reused flow state: %v" , got )
122122 }
123123}
124124
125- func TestPolicyRequiresExplicitICMPGrant (t * testing.T ) {
125+ func TestPolicyUsesUpstreamICMPSourceDestinationSemantics (t * testing.T ) {
126126 cfg := filterTestConfig ()
127- cfg .PacketFilter = wireguardcfg.PacketFilter {Grants : []wireguardcfg.Grant {{
128- Src : []string {"peer:alpha" }, Dst : []string {"self" }, IP : []string {"tcp:22" },
129- }}}
130127 requestBytes := icmp4Packet (8 , "10.0.0.2" , "10.0.0.1" )
131128 var request packet.Parsed
132129 request .Decode (requestBytes )
133- policy := mustCompile (t , cfg , netip.Addr {}, false , nil )
134- if got := policy .runIn (& request , 0 ); got != filter .Drop {
135- t .Fatalf ("ICMP request through TCP-only grant = %v, want drop" , got )
136- }
137130
138- cfg .PacketFilter .Grants [0 ].IP = []string {"icmp:*" }
139- policy = mustCompile (t , cfg , netip.Addr {}, false , nil )
140- if got := policy .runIn (& request , 0 ); got != filter .Accept {
141- t .Fatalf ("ICMP request through explicit grant = %v, want accept" , got )
131+ tests := []struct {
132+ name string
133+ permission string
134+ want filter.Response
135+ }{
136+ {name : "no grants" , want : filter .Drop },
137+ {name : "TCP grant" , permission : "tcp:22" , want : filter .Accept },
138+ {name : "UDP grant" , permission : "udp:53" , want : filter .Accept },
139+ {name : "SCTP grant" , permission : "sctp:9" , want : filter .Accept },
140+ {name : "portless grant" , permission : "gre:*" , want : filter .Accept },
141+ {name : "explicit ICMP grant" , permission : "icmp:*" , want : filter .Accept },
142+ }
143+ for _ , test := range tests {
144+ t .Run (test .name , func (t * testing.T ) {
145+ if test .permission == "" {
146+ cfg .PacketFilter = wireguardcfg.PacketFilter {}
147+ } else {
148+ cfg .PacketFilter = wireguardcfg.PacketFilter {Grants : []wireguardcfg.Grant {{
149+ Src : []string {"peer:alpha" }, Dst : []string {"self" }, IP : []string {test .permission },
150+ }}}
151+ }
152+ policy := mustCompile (t , cfg , netip.Addr {}, false , nil )
153+ if got := policy .filter .RunIn (& request , 0 ); got != test .want {
154+ t .Fatalf ("ICMP request with %q = %v, want %v" , test .permission , got , test .want )
155+ }
156+ })
142157 }
143158}
144159
@@ -148,19 +163,19 @@ func TestPolicyPreservesUpstreamTCPAndICMPSemantics(t *testing.T) {
148163 policy := mustCompile (t , cfg , netip.Addr {}, false , nil )
149164
150165 tcpContinuation := parsedPacket (ipproto .TCP , "10.0.0.2" , "10.0.0.1" , 22 , 40000 , packet .TCPAck )
151- if got := policy .runIn (& tcpContinuation , 0 ); got != filter .Accept {
166+ if got := policy .filter . RunIn (& tcpContinuation , 0 ); got != filter .Accept {
152167 t .Fatalf ("TCP continuation = %v, want accept" , got )
153168 }
154169 tcpSYN := parsedPacket (ipproto .TCP , "10.0.0.2" , "10.0.0.1" , 22 , 40000 , packet .TCPSyn )
155- if got := policy .runIn (& tcpSYN , 0 ); got != filter .Drop {
170+ if got := policy .filter . RunIn (& tcpSYN , 0 ); got != filter .Drop {
156171 t .Fatalf ("TCP SYN = %v, want drop" , got )
157172 }
158173 icmpReply := parsedPacket (ipproto .ICMPv4 , "10.0.0.2" , "10.0.0.1" , 0 , 0 , 0 )
159174 icmpReply .StuffForTesting (28 )
160175 // ICMP echo reply type.
161176 icmpReplyBytes := icmp4Packet (0 , "10.0.0.2" , "10.0.0.1" )
162177 icmpReply .Decode (icmpReplyBytes )
163- if got := policy .runIn (& icmpReply , 0 ); got != filter .Accept {
178+ if got := policy .filter . RunIn (& icmpReply , 0 ); got != filter .Accept {
164179 t .Fatalf ("ICMP response = %v, want accept" , got )
165180 }
166181}
0 commit comments