@@ -122,4 +122,62 @@ func TestBuildFirewallConfiguration(t *testing.T) {
122122 }
123123 }
124124 })
125+
126+ t .Run ("It filters subnets by address family for dual-stack" , func (t * testing.T ) {
127+ mixedSubnets := []string {"172.16.0.0/12" , "fd00::/8" , "10.0.0.0/8" , "2001:db8::/32" }
128+
129+ // IPv4 pass (IPv6=false) should only include IPv4 subnets
130+ optIPv4 := & RootOptions {
131+ IncomingProxyPort : 1234 ,
132+ OutgoingProxyPort : 2345 ,
133+ SubnetsToIgnore : mixedSubnets ,
134+ IPTablesMode : IPTablesModeLegacy ,
135+ IPv6 : false ,
136+ }
137+ config , err := BuildFirewallConfiguration (optIPv4 )
138+ if err != nil {
139+ t .Fatalf ("Unexpected error: %s" , err )
140+ }
141+ expectedIPv4 := []string {"172.16.0.0/12" , "10.0.0.0/8" }
142+ if ! reflect .DeepEqual (config .SubnetsToIgnore , expectedIPv4 ) {
143+ t .Fatalf ("Expected IPv4 subnets %v but got %v" , expectedIPv4 , config .SubnetsToIgnore )
144+ }
145+
146+ // IPv6 pass (IPv6=true) should only include IPv6 subnets
147+ optIPv6 := & RootOptions {
148+ IncomingProxyPort : 1234 ,
149+ OutgoingProxyPort : 2345 ,
150+ SubnetsToIgnore : mixedSubnets ,
151+ IPTablesMode : IPTablesModeLegacy ,
152+ IPv6 : true ,
153+ }
154+ config , err = BuildFirewallConfiguration (optIPv6 )
155+ if err != nil {
156+ t .Fatalf ("Unexpected error: %s" , err )
157+ }
158+ expectedIPv6 := []string {"fd00::/8" , "2001:db8::/32" }
159+ if ! reflect .DeepEqual (config .SubnetsToIgnore , expectedIPv6 ) {
160+ t .Fatalf ("Expected IPv6 subnets %v but got %v" , expectedIPv6 , config .SubnetsToIgnore )
161+ }
162+ })
163+
164+ t .Run ("It handles IPv4-only subnets with dual-stack enabled" , func (t * testing.T ) {
165+ ipv4Only := []string {"172.16.0.0/12" }
166+
167+ // IPv6 pass should produce empty subnets, not error
168+ opt := & RootOptions {
169+ IncomingProxyPort : 1234 ,
170+ OutgoingProxyPort : 2345 ,
171+ SubnetsToIgnore : ipv4Only ,
172+ IPTablesMode : IPTablesModeLegacy ,
173+ IPv6 : true ,
174+ }
175+ config , err := BuildFirewallConfiguration (opt )
176+ if err != nil {
177+ t .Fatalf ("Unexpected error: %s" , err )
178+ }
179+ if len (config .SubnetsToIgnore ) != 0 {
180+ t .Fatalf ("Expected empty subnets for IPv6 pass with IPv4-only input, got %v" , config .SubnetsToIgnore )
181+ }
182+ })
125183}
0 commit comments