package owrt import ( "io/ioutil" "os" "strings" "testing" ) const ( ruleA = "iptables -A PREROUTING -i br-dds -p tcp -m tcp --dport 443 -j DNAT --to-destination 10.100.10.1:8443" ruleB = "iptables -A PREROUTING -i br-dds -p tcp -m tcp --dport 80 -j DNAT --to-destination 10.100.10.1:8080" ) func TestFWCustomRuleCreate(t *testing.T) { exec := createMockExecutor("", "", 0) uci := NewUCIWithExecutor(exec, "/tmp/myCustomRuleFile") _, sErr := os.Stat(uci.CustomFirewallFile) if os.IsExist(sErr) { rErr := os.Remove(uci.CustomFirewallFile) if rErr != nil { t.Fatal("Error cleaning temporary file") } } custom := NewUCIFirewallCustomRule(uci) custom.Name = "TestRule" custom.Rule = ruleA create := custom.Create() if create != nil { t.Fatalf("UCIFirewallCustomRule.Create() failed !\n%s", create.Error()) } sv := custom.Save() if sv != nil { t.Fatalf("%s", sv.Error()) } } func TestFWCustomRuleUpdate(t *testing.T) { exec := createMockExecutor("", "", 0) uci := NewUCIWithExecutor(exec, "/tmp/myCustomRuleFile") custom := NewUCIFirewallCustomRule(uci) custom.Name = "SecondRule" custom.Rule = ruleB if cr := custom.Create(); cr != nil { t.Fatalf("UCIFirewallCustomRule.Create() failed !\n%s", cr.Error()) } b, err := ioutil.ReadFile(uci.CustomFirewallFile) if err != nil { t.Fatalf("%s", err.Error()) } if !strings.Contains(string(b), ruleB) { t.Fatalf("Rule is not present in %s file", uci.CustomFirewallFile) } custom.Rule = ruleA if uErr := custom.Update(); uErr != nil { t.Fatalf("UCIFirewallCustomRule.Update() faild ! %s", uErr.Error()) } sv := custom.Save() if sv != nil { t.Fatalf("%s", sv.Error()) } }