diff --git a/arguments_test.go b/arguments_test.go new file mode 100644 index 0000000..7a2cb60 --- /dev/null +++ b/arguments_test.go @@ -0,0 +1,19 @@ +package main + +import ( + "testing" +) + +func TestArgs(t *testing.T) { + var a ArgSrvs + + (&a).Set("aa") + + if len(a) != 1 { + t.Fail() + } + + if a[0] != "aa" { + t.Fail() + } +} diff --git a/parse_test.go b/parse_test.go new file mode 100644 index 0000000..61d1080 --- /dev/null +++ b/parse_test.go @@ -0,0 +1,61 @@ +package main + +import ( + "testing" +) + +func TestParseAddr(t *testing.T) { + var err error + var s1, s2 string + s1, s2, err = parse_addr("udp:123.2.3.4:321") + t.Logf("parse result %s, %s\n", s1, s2) + if err != nil { + t.Fail() + } + + s1, s2, err = parse_addr("tcp:123.2.3.4:321") + t.Logf("parse result %s, %s\n", s1, s2) + if err != nil { + t.Fail() + } + + _, _, err = parse_addr("1.2.3.4:333") + t.Log(err) + if err == nil { + t.Fail() + } + + _, _, err = parse_addr("cc:1.2.3.4:33") + t.Log(err) + if err == nil { + t.Fail() + } + + _, _, err = parse_addr("tcp:1.2.3.4:33:33") + t.Log(err) + if err == nil { + t.Fail() + } +} + +func TestParseServer(t *testing.T) { + _, err := parse_server("aa.txt:tcp:1.2.3.4:32") + t.Log(err) + if err == nil { + t.Fail() + } + sv, err := parse_server("noexists.txt,tcp:1.2.3.4:32") + if err != nil { + t.Log(err) + t.Fail() + } + if sv.Addr != "1.2.3.4:32" { + t.Fail() + } + if sv.Proto != "tcp" { + t.Fail() + } + if sv.domains != nil { + t.Fail() + } +} diff --git a/server_test.go b/server_test.go new file mode 100644 index 0000000..c59b06d --- /dev/null +++ b/server_test.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + "github.com/miekg/dns" + "testing" +) + +func TestInblacklist(t *testing.T) { + logger = NewLogger("", false) + Blacklist_ips = Kv{"1.2.3.4": 1, "2.3.4.5": 1} + test_ips := map[string]bool{ + "1.2.3.4": true, + "2.3.4.5": true, + "2.3.4.1": false, + "1.2.4.3": false, + } + + for ip, r := range test_ips { + msg := new(dns.Msg) + s := fmt.Sprintf("example.com. IN A %s", ip) + rr, err := dns.NewRR(s) + if err != nil { + t.Error(err) + } + msg.Answer = append(msg.Answer, rr) + if in_blacklist(msg) != r { + t.Errorf("%s must match in %v result\n", ip, r) + } + } +} diff --git a/upstream_test.go b/upstream_test.go new file mode 100644 index 0000000..d559507 --- /dev/null +++ b/upstream_test.go @@ -0,0 +1,43 @@ +package main + +import ( + "testing" +) + +func TestServerMathNil(t *testing.T) { + srv := UpstreamServer{} // initial with nil + + domains := []string{"twitter.com", "google.com", "abc.com"} + + for _, d := range domains { + if srv.match(d) { + t.Errorf("%s must match in false result\n", d) + } + } +} + +func TestServerMatch(t *testing.T) { + d := Kv{"twitter.com": 1, "google.com": 1} + srv := UpstreamServer{domains: d} + + test_domains := map[string]bool{ + "twitter.com": true, + "pbs.twitter.com": true, + "abc.pbs.twitter.com": true, + "efg.abc.pbs.twitter.com": true, + "google.com": true, + "plus.google.com": true, + "cc.plus.google.com": true, + "dd.cc.plus.google.com": true, + "twitter.abc.com": false, + "twitter.com.aa.com": false, + "google.com.cccc.com": false, + "google.com.aeddasdfc3.com": false, + } + + for d, r := range test_domains { + if srv.match(d) != r { + t.Errorf("%s must match in %v result\n", d, r) + } + } +}