65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package ruleio
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/prism/proxy/internal/store"
|
|
)
|
|
|
|
func TestParseLine(t *testing.T) {
|
|
r, err := ParseLine("DOMAIN-SUFFIX,google.com,country:HK")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r.RuleType != "DOMAIN-SUFFIX" || r.Payload != "google.com" || r.Target != "country:HK" {
|
|
t.Fatalf("unexpected %+v", r)
|
|
}
|
|
}
|
|
|
|
func TestParseLineSkipsMatch(t *testing.T) {
|
|
_, err := ParseLine("MATCH,DIRECT")
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
}
|
|
|
|
func TestParseText(t *testing.T) {
|
|
rules, errs := ParseText("# comment\nDOMAIN,google.com,DIRECT\n\nDOMAIN-SUFFIX,foo.com,PROXY")
|
|
if len(errs) != 0 {
|
|
t.Fatal(errs)
|
|
}
|
|
if len(rules) != 2 {
|
|
t.Fatalf("got %d rules", len(rules))
|
|
}
|
|
}
|
|
|
|
func TestParseJSONDoc(t *testing.T) {
|
|
rules, err := ParseJSON([]byte(`{"version":1,"rules":[{"rule_type":"DOMAIN","payload":"a.com","target":"DIRECT"}]}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(rules) != 1 || rules[0].Target != "DIRECT" {
|
|
t.Fatalf("unexpected %+v", rules)
|
|
}
|
|
}
|
|
|
|
func TestExportText(t *testing.T) {
|
|
text := ExportText([]store.Rule{{
|
|
RuleType: "DOMAIN-SUFFIX",
|
|
Payload: "x.com",
|
|
Target: "PROXY",
|
|
Enabled: true,
|
|
}})
|
|
if !strings.Contains(text, "DOMAIN-SUFFIX,x.com,PROXY") {
|
|
t.Fatalf("got %q", text)
|
|
}
|
|
}
|
|
|
|
func TestToStoreRulesDefaultsEnabled(t *testing.T) {
|
|
rules, errs := ToStoreRules([]ExportRule{{RuleType: "DOMAIN", Payload: "a.com", Target: "DIRECT"}})
|
|
if len(errs) != 0 || len(rules) != 1 || !rules[0].Enabled {
|
|
t.Fatalf("rules=%+v errs=%v", rules, errs)
|
|
}
|
|
}
|