@@ -188,46 +188,6 @@ func TestApplyWorkerFirewallRules_Basic(t *testing.T) {
188188 }
189189}
190190
191- // TestApplyWorkerFirewallRules_WithAllowedPorts 验证额外端口映射规则。
192- func TestApplyWorkerFirewallRules_WithAllowedPorts (t * testing.T ) {
193- ns , origNS := newTestNetNS (t )
194- defer cleanupTestNetNS (t , ns , origNS )
195-
196- gwIP := net .ParseIP ("10.0.0.1" )
197- bridgeGW := net .ParseIP ("172.18.0.1" )
198- allowedPorts := []uint16 {8080 , 8443 }
199-
200- err := ApplyWorkerFirewallRules (ns , gwIP , bridgeGW , 22 , allowedPorts )
201- if err != nil {
202- t .Fatalf ("ApplyWorkerFirewallRules failed: %v" , err )
203- }
204-
205- conn , err := nftables .New (nftables .WithNetNSFd (int (ns )))
206- if err != nil {
207- t .Fatalf ("open nftables conn: %v" , err )
208- }
209-
210- tbl := getTableByName (t , conn , "cloudproxy" )
211- if tbl == nil {
212- t .Fatal ("cloudproxy table not found" )
213- }
214-
215- inputChain := getChainByName (t , conn , tbl , "input" )
216- if inputChain == nil {
217- t .Fatal ("input chain not found" )
218- }
219-
220- inputRules , err := conn .GetRules (tbl , inputChain )
221- if err != nil {
222- t .Fatalf ("get input rules: %v" , err )
223- }
224-
225- // 基础规则 5 条 + 2 个额外端口 = 7 条
226- expectedMin := 5 + len (allowedPorts )
227- if len (inputRules ) < expectedMin {
228- t .Fatalf ("input rules = %d, want >= %d" , len (inputRules ), expectedMin )
229- }
230- }
231191
232192// TestApplyWorkerFirewallRules_InvalidNetNS 验证无效 netns handle 返回错误。
233193func TestApplyWorkerFirewallRules_InvalidNetNS (t * testing.T ) {
@@ -439,60 +399,7 @@ func TestApplyWorkerFirewallRules_CustomSSHPort(t *testing.T) {
439399 }
440400}
441401
442- // TestApplyWorkerFirewallRules_NilAllowedPorts 验证 nil allowedPorts 不 panic。
443- func TestApplyWorkerFirewallRules_NilAllowedPorts (t * testing.T ) {
444- ns , origNS := newTestNetNS (t )
445- defer cleanupTestNetNS (t , ns , origNS )
446402
447- gwIP := net .ParseIP ("10.0.0.1" )
448- bridgeGW := net .ParseIP ("172.18.0.1" )
449-
450- // nil allowedPorts
451- err := ApplyWorkerFirewallRules (ns , gwIP , bridgeGW , 22 , nil )
452- if err != nil {
453- t .Fatalf ("ApplyWorkerFirewallRules with nil allowedPorts failed: %v" , err )
454- }
455- }
456-
457- // TestApplyWorkerFirewallRules_EmptyAllowedPorts 验证空 allowedPorts 切片。
458- func TestApplyWorkerFirewallRules_EmptyAllowedPorts (t * testing.T ) {
459- ns , origNS := newTestNetNS (t )
460- defer cleanupTestNetNS (t , ns , origNS )
461-
462- gwIP := net .ParseIP ("10.0.0.1" )
463- bridgeGW := net .ParseIP ("172.18.0.1" )
464-
465- // 空切片
466- err := ApplyWorkerFirewallRules (ns , gwIP , bridgeGW , 22 , []uint16 {})
467- if err != nil {
468- t .Fatalf ("ApplyWorkerFirewallRules with empty allowedPorts failed: %v" , err )
469- }
470-
471- conn , err := nftables .New (nftables .WithNetNSFd (int (ns )))
472- if err != nil {
473- t .Fatalf ("open nftables conn: %v" , err )
474- }
475-
476- tbl := getTableByName (t , conn , "cloudproxy" )
477- if tbl == nil {
478- t .Fatal ("cloudproxy table not found" )
479- }
480-
481- inputChain := getChainByName (t , conn , tbl , "input" )
482- if inputChain == nil {
483- t .Fatal ("input chain not found" )
484- }
485-
486- inputRules , err := conn .GetRules (tbl , inputChain )
487- if err != nil {
488- t .Fatalf ("get input rules: %v" , err )
489- }
490-
491- // lo + ct_established + gwIP + bridgeGW + SSH = 5 条(无额外端口)
492- if len (inputRules ) != 5 {
493- t .Fatalf ("input rules = %d, want 5" , len (inputRules ))
494- }
495- }
496403
497404// TestApplyThenCleanupThenApply 验证清理后可以重新应用规则。
498405func TestApplyThenCleanupThenApply (t * testing.T ) {
0 commit comments