@@ -1290,3 +1290,43 @@ func TestIPRulesForUBNT(t *testing.T) {
12901290 }
12911291 }
12921292}
1293+
1294+ func TestUpdateMagicsockPortChange (t * testing.T ) {
1295+ nfr := & fakeIPTablesRunner {
1296+ t : t ,
1297+ ipt4 : make (map [string ][]string ),
1298+ ipt6 : make (map [string ][]string ),
1299+ }
1300+ nfr .ipt4 ["filter/ts-input" ] = []string {}
1301+
1302+ r := & linuxRouter {
1303+ logf : logger .Discard ,
1304+ health : new (health.Tracker ),
1305+ netfilterMode : netfilterOn ,
1306+ nfr : nfr ,
1307+ }
1308+
1309+ if err := r .updateMagicsockPort (12345 , "udp4" ); err != nil {
1310+ t .Fatalf ("failed to set initial port: %v" , err )
1311+ }
1312+
1313+ if err := r .updateMagicsockPort (54321 , "udp4" ); err != nil {
1314+ t .Fatalf ("failed to update port: %v" , err )
1315+ }
1316+
1317+ newPortRule := buildMagicsockPortRule (54321 )
1318+ hasNewRule := slices .Contains (nfr .ipt4 ["filter/ts-input" ], newPortRule )
1319+
1320+ if ! hasNewRule {
1321+ t .Errorf ("firewall rule for NEW port 54321 not found.\n Expected: %s\n Actual rules: %v" ,
1322+ newPortRule , nfr .ipt4 ["filter/ts-input" ])
1323+ }
1324+
1325+ oldPortRule := buildMagicsockPortRule (12345 )
1326+ hasOldRule := slices .Contains (nfr .ipt4 ["filter/ts-input" ], oldPortRule )
1327+
1328+ if hasOldRule {
1329+ t .Errorf ("firewall rule for OLD port 12345 still exists (should be deleted).\n Found: %s\n All rules: %v" ,
1330+ oldPortRule , nfr .ipt4 ["filter/ts-input" ])
1331+ }
1332+ }
0 commit comments