From 8a3afa261a5f985162af90c6842661238b7b4248 Mon Sep 17 00:00:00 2001 From: Sam Therapy Date: Wed, 21 Sep 2022 14:07:00 +0200 Subject: [PATCH] refactor: make more tests table driven Signed-off-by: Sam Therapy --- .editorconfig | 6 +- cli/cli_test.go | 2 +- query/DNSCrypt_test.go | 31 ++++--- query/HTTPS.go | 7 +- query/HTTPS_test.go | 169 ++++++++++++++++-------------------- query/QUIC_test.go | 136 ++++++++++++++++------------- query/general_test.go | 155 +++++++++++++++++---------------- query/print_test.go | 9 +- query/query_test.go | 187 +++++++++++++++++++++------------------- util/options_test.go | 15 ++-- util/reverseDNS_test.go | 65 ++++++++------ 11 files changed, 407 insertions(+), 375 deletions(-) diff --git a/.editorconfig b/.editorconfig index ebe51d3..9f274b9 100644 --- a/.editorconfig +++ b/.editorconfig @@ -4,9 +4,7 @@ root = true [*] -indent_style = space -indent_size = 2 end_of_line = lf charset = utf-8 -trim_trailing_whitespace = false -insert_final_newline = false \ No newline at end of file +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/cli/cli_test.go b/cli/cli_test.go index eb794a4..4730aa0 100644 --- a/cli/cli_test.go +++ b/cli/cli_test.go @@ -46,7 +46,7 @@ func TestValidSubnet(t *testing.T) { for _, test := range tests { test := test - t.Run("", func(t *testing.T) { + t.Run(test.args[2], func(t *testing.T) { t.Parallel() opts, err := cli.ParseCLI(test.args, "TEST") diff --git a/query/DNSCrypt_test.go b/query/DNSCrypt_test.go index 37df392..a1e7294 100644 --- a/query/DNSCrypt_test.go +++ b/query/DNSCrypt_test.go @@ -15,42 +15,49 @@ func TestDNSCrypt(t *testing.T) { t.Parallel() tests := []struct { - opt util.Options + name string + opts util.Options }{ { + "Valid", util.Options{ Logger: util.InitLogger(0), DNSCrypt: true, Request: util.Request{ - Server: "sdns://AQMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20", - Type: dns.TypeA, - Name: "example.com.", + Server: "sdns://AQMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20", + Type: dns.TypeA, + Name: "example.com.", + Retries: 3, }, }, }, { + "Valid (TCP)", util.Options{ Logger: util.InitLogger(0), DNSCrypt: true, TCP: true, IPv4: true, Request: util.Request{ - Server: "sdns://AQMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20", - Type: dns.TypeAAAA, - Name: "example.com.", + Server: "sdns://AQMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20", + Type: dns.TypeAAAA, + Name: "example.com.", + Retries: 3, }, }, }, { + "Invalid", util.Options{ Logger: util.InitLogger(0), DNSCrypt: true, TCP: true, IPv4: true, Request: util.Request{ - Server: "QMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20", - Type: dns.TypeAAAA, - Name: "example.com.", + Server: "QMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20", + Type: dns.TypeAAAA, + Name: "example.com.", + Retries: 0, }, }, }, @@ -59,10 +66,10 @@ func TestDNSCrypt(t *testing.T) { for _, test := range tests { test := test - t.Run("", func(t *testing.T) { + t.Run(test.name, func(t *testing.T) { t.Parallel() - res, err := query.CreateQuery(test.opt) + res, err := query.CreateQuery(test.opts) if err == nil { assert.Assert(t, res != util.Response{}) } else { diff --git a/query/HTTPS.go b/query/HTTPS.go index 0c56002..4b78bee 100644 --- a/query/HTTPS.go +++ b/query/HTTPS.go @@ -52,7 +52,7 @@ func (r *HTTPSResolver) LookUp(msg *dns.Msg) (util.Response, error) { } if res.StatusCode != http.StatusOK { - return util.Response{}, &errHTTPStatus{res.StatusCode} + return util.Response{}, &ErrHTTPStatus{res.StatusCode} } r.opts.Logger.Debug("https: reading response") @@ -79,10 +79,11 @@ func (r *HTTPSResolver) LookUp(msg *dns.Msg) (util.Response, error) { return resp, nil } -type errHTTPStatus struct { +// ErrHTTPStatus is returned when DoH returns a bad status code. +type ErrHTTPStatus struct { code int } -func (e *errHTTPStatus) Error() string { +func (e *ErrHTTPStatus) Error() string { return fmt.Sprintf("doh server responded with HTTP %d", e.code) } diff --git a/query/HTTPS_test.go b/query/HTTPS_test.go index b844847..ce9473a 100644 --- a/query/HTTPS_test.go +++ b/query/HTTPS_test.go @@ -3,6 +3,7 @@ package query_test import ( + "errors" "testing" "git.froth.zone/sam/awl/query" @@ -11,105 +12,87 @@ import ( "gotest.tools/v3/assert" ) -func TestResolveHTTPS(t *testing.T) { +func TestHTTPS(t *testing.T) { t.Parallel() - var err error - - opts := util.Options{ - HTTPS: true, - Logger: util.InitLogger(0), - Request: util.Request{ - Server: "https://dns9.quad9.net/dns-query", - Type: dns.TypeA, - Name: "git.froth.zone.", + tests := []struct { + name string + opts util.Options + }{ + { + "Good", + util.Options{ + HTTPS: true, + Logger: util.InitLogger(0), + Request: util.Request{ + Server: "https://dns9.quad9.net/dns-query", + Type: dns.TypeA, + Name: "git.froth.zone.", + Retries: 3, + }, + }, + }, + { + "404", + util.Options{ + HTTPS: true, + Logger: util.InitLogger(0), + Request: util.Request{ + Server: "https://dns9.quad9.net/dns", + Type: dns.TypeA, + Name: "git.froth.zone.", + }, + }, + }, + { + "Bad request domain", + util.Options{ + HTTPS: true, + Logger: util.InitLogger(0), + Request: util.Request{ + Server: "dns9.quad9.net/dns-query", + Type: dns.TypeA, + Name: "git.froth.zone", + }, + }, + }, + { + "Bad server domain", + util.Options{ + HTTPS: true, + Logger: util.InitLogger(0), + Request: util.Request{ + Server: "dns9..quad9.net/dns-query", + Type: dns.TypeA, + Name: "git.froth.zone.", + }, + }, }, } - // testCase := util.Request{Server: "https://dns9.quad9.net/dns-query", Type: dns.TypeA, Name: "git.froth.zone."} - resolver, err := query.LoadResolver(opts) - assert.NilError(t, err) - msg := new(dns.Msg) - msg.SetQuestion(opts.Request.Name, opts.Request.Type) - // msg = msg.SetQuestion(testCase.Name, testCase.Type) - res, err := resolver.LookUp(msg) - assert.NilError(t, err) - assert.Assert(t, res != util.Response{}) -} + for _, test := range tests { + test := test -func Test2ResolveHTTPS(t *testing.T) { - t.Parallel() + t.Run(test.name, func(t *testing.T) { + t.Parallel() - opts := util.Options{ - HTTPS: true, - Logger: util.InitLogger(0), - Request: util.Request{Server: "dns9.quad9.net/dns-query", Type: dns.TypeA, Name: "git.froth.zone."}, + resolver, err := query.LoadResolver(test.opts) + assert.NilError(t, err) + + msg := new(dns.Msg) + msg.SetQuestion(test.opts.Request.Name, test.opts.Request.Type) + // msg = msg.SetQuestion(testCase.Name, testCase.Type) + res, err := resolver.LookUp(msg) + + if err == nil { + assert.NilError(t, err) + assert.Assert(t, res != util.Response{}) + } else { + if errors.Is(err, &query.ErrHTTPStatus{}) { + assert.ErrorContains(t, err, "404") + } + assert.Equal(t, res, util.Response{}) + } + }) } - - var err error - - testCase := util.Request{Type: dns.TypeA, Name: "git.froth.zone"} - resolver, err := query.LoadResolver(opts) - assert.NilError(t, err) - - msg := new(dns.Msg) - msg.SetQuestion(testCase.Name, testCase.Type) - // msg = msg.SetQuestion(testCase.Name, testCase.Type) - res, err := resolver.LookUp(msg) - assert.ErrorContains(t, err, "fully qualified") - assert.Equal(t, res, util.Response{}) -} - -func Test3ResolveHTTPS(t *testing.T) { - t.Parallel() - - opts := util.Options{ - HTTPS: true, - Logger: util.InitLogger(0), - Request: util.Request{Server: "dns9..quad9.net/dns-query", Type: dns.TypeA, Name: "git.froth.zone."}, - } - - var err error - - // testCase := - // if the domain is not canonical, make it canonical - // if !strings.HasSuffix(testCase.Name, ".") { - // testCase.Name = fmt.Sprintf("%s.", testCase.Name) - // } - - resolver, err := query.LoadResolver(opts) - assert.NilError(t, err) - - msg := new(dns.Msg) - // msg.SetQuestion(testCase.Name, testCase.Type) - // msg = msg.SetQuestion(testCase.Name, testCase.Type) - res, err := resolver.LookUp(msg) - assert.ErrorContains(t, err, "doh: HTTP request") - assert.Equal(t, res, util.Response{}) -} - -func Test404ResolveHTTPS(t *testing.T) { - t.Parallel() - - var err error - - opts := util.Options{ - HTTPS: true, - Logger: util.InitLogger(0), - Request: util.Request{ - Server: "https://dns9.quad9.net/dns", - Type: dns.TypeA, - Name: "git.froth.zone.", - }, - } - // testCase := util.Request{Server: "https://dns9.quad9.net/dns-query", Type: dns.TypeA, Name: "git.froth.zone."} - resolver, err := query.LoadResolver(opts) - assert.NilError(t, err) - - msg := new(dns.Msg) - msg.SetQuestion(opts.Request.Name, opts.Request.Type) - // msg = msg.SetQuestion(testCase.Name, testCase.Type) - res, err := resolver.LookUp(msg) - assert.ErrorContains(t, err, "404") - assert.Equal(t, res, util.Response{}) } diff --git a/query/QUIC_test.go b/query/QUIC_test.go index eeba532..c30ead3 100644 --- a/query/QUIC_test.go +++ b/query/QUIC_test.go @@ -3,10 +3,6 @@ package query_test import ( - "fmt" - "net" - "strconv" - "strings" "testing" "time" @@ -19,72 +15,90 @@ import ( func TestQuic(t *testing.T) { t.Parallel() - opts := util.Options{ - QUIC: true, - Logger: util.InitLogger(0), - Request: util.Request{Server: "dns.adguard.com", Port: 853, Timeout: 500 * time.Millisecond}, + tests := []struct { + name string + opts util.Options + }{ + { + "Valid", + util.Options{ + QUIC: true, + Logger: util.InitLogger(0), + Request: util.Request{ + Server: "dns.adguard.com", + Type: dns.TypeNS, + Port: 853, + Timeout: 750 * time.Millisecond, + Retries: 3, + }, + }, + }, + { + "Bad domain", + util.Options{ + QUIC: true, + Logger: util.InitLogger(0), + Request: util.Request{ + Server: "dns.//./,,adguard\a.com", + Port: 853, + Type: dns.TypeA, + Name: "git.froth.zone", + Timeout: 100 * time.Millisecond, + Retries: 0, + }, + }, + }, + { + "Not canonical", + util.Options{ + QUIC: true, + Logger: util.InitLogger(0), + Request: util.Request{ + Server: "dns.adguard.com", + Port: 853, + Type: dns.TypeA, + Name: "git.froth.zone", + Timeout: 100 * time.Millisecond, + Retries: 0, + }, + }, + }, + { + "Invalid query domain", + util.Options{ + QUIC: true, + Logger: util.InitLogger(0), + Request: util.Request{ + Server: "example.com", + Port: 853, + Type: dns.TypeA, + Name: "git.froth.zone", + Timeout: 10 * time.Millisecond, + }, + }, + }, } - testCase := util.Request{Server: "dns.//./,,adguard.com", Port: 853, Type: dns.TypeA, Name: "git.froth.zone", Timeout: 100 * time.Millisecond} - testCase2 := util.Request{Server: "dns.adguard.com", Port: 853, Type: dns.TypeA, Name: "git.froth.zone", Timeout: 100 * time.Millisecond} - var testCases []util.Request + for _, test := range tests { + test := test - testCases = append(testCases, testCase) - testCases = append(testCases, testCase2) + t.Run(test.name, func(t *testing.T) { + t.Parallel() - for i := range testCases { - switch i { - case 0: - resolver, err := query.LoadResolver(opts) - assert.NilError(t, err) - // if the domain is not canonical, make it canonical - if !strings.HasSuffix(testCase.Name, ".") { - testCases[i].Name = fmt.Sprintf("%s.", testCases[i].Name) - } - - msg := new(dns.Msg) - msg.SetQuestion(testCase.Name, testCase.Type) - // msg = msg.SetQuestion(testCase.Name, testCase.Type) - res, err := resolver.LookUp(msg) - - assert.ErrorContains(t, err, "fully qualified") - assert.Equal(t, res, util.Response{}) - case 1: - resolver, err := query.LoadResolver(opts) + resolver, err := query.LoadResolver(test.opts) assert.NilError(t, err) - testCase2.Server = net.JoinHostPort(testCase2.Server, strconv.Itoa(opts.Request.Port)) - - // if the domain is not canonical, make it canonical - if !strings.HasSuffix(testCase2.Name, ".") { - testCase2.Name = fmt.Sprintf("%s.", testCase2.Name) - } - msg := new(dns.Msg) - msg.SetQuestion(testCase2.Name, testCase2.Type) + msg.SetQuestion(test.opts.Request.Name, test.opts.Request.Type) res, err := resolver.LookUp(msg) - assert.NilError(t, err) - assert.Assert(t, res != util.Response{}) - } + if err == nil { + assert.NilError(t, err) + assert.Assert(t, res != util.Response{}) + } else { + assert.Assert(t, res == util.Response{}) + } + }) } } - -func TestInvalidQuic(t *testing.T) { - t.Parallel() - - opts := util.Options{ - QUIC: true, - Logger: util.InitLogger(0), - Request: util.Request{Server: "example.com", Port: 853, Type: dns.TypeA, Name: "git.froth.zone", Timeout: 10 * time.Millisecond}, - } - resolver, err := query.LoadResolver(opts) - assert.NilError(t, err) - - msg := new(dns.Msg) - msg.SetQuestion(opts.Request.Name, opts.Request.Type) - res, err := resolver.LookUp(msg) - assert.ErrorContains(t, err, "timeout") - assert.Equal(t, res, util.Response{}) -} diff --git a/query/general_test.go b/query/general_test.go index 31b362c..5068be1 100644 --- a/query/general_test.go +++ b/query/general_test.go @@ -3,6 +3,7 @@ package query_test import ( + "os" "testing" "time" @@ -15,90 +16,95 @@ import ( func TestResolve(t *testing.T) { t.Parallel() - opts := util.Options{ - Logger: util.InitLogger(0), - Request: util.Request{ - Server: "8.8.4.1", - Port: 1, - Type: dns.TypeA, - Name: "example.com.", - Timeout: time.Millisecond * 100, - Retries: 0, - }, - } - resolver, err := query.LoadResolver(opts) - assert.NilError(t, err) - - msg := new(dns.Msg) - msg.SetQuestion(opts.Request.Name, opts.Request.Type) - - _, err = resolver.LookUp(msg) - assert.ErrorContains(t, err, "timeout") -} - -func TestTruncate(t *testing.T) { - t.Parallel() - - opts := util.Options{ - Logger: util.InitLogger(0), - IPv4: true, - Request: util.Request{ - Server: "madns.binarystar.systems", - Port: 5301, - Type: dns.TypeTXT, - Name: "limit.txt.example.", - }, - } - resolver, err := query.LoadResolver(opts) - assert.NilError(t, err) - - msg := new(dns.Msg) - msg.SetQuestion(opts.Request.Name, opts.Request.Type) - res, err := resolver.LookUp(msg) - - assert.NilError(t, err) - assert.Assert(t, res != util.Response{}) -} - -func TestResolveAgain(t *testing.T) { - t.Parallel() - tests := []struct { - opt util.Options + name string + opts util.Options }{ { + "UDP", + util.Options{ + Logger: util.InitLogger(0), + Request: util.Request{ + Server: "8.8.4.4", + Port: 53, + Type: dns.TypeAAAA, + Name: "example.com.", + Retries: 3, + }, + }, + }, + { + "UDP (Bad Cookie)", + util.Options{ + Logger: util.InitLogger(0), + BadCookie: false, + Request: util.Request{ + Server: "b.root-servers.net", + Port: 53, + Type: dns.TypeNS, + Name: "example.com.", + Retries: 3, + }, + EDNS: util.EDNS{ + EnableEDNS: true, + Cookie: true, + }, + }, + }, + { + "UDP (Truncated)", + util.Options{ + Logger: util.InitLogger(0), + IPv4: true, + Request: util.Request{ + Server: "madns.binarystar.systems", + Port: 5301, + Type: dns.TypeTXT, + Name: "limit.txt.example.", + Retries: 3, + }, + }, + }, + { + "TCP", util.Options{ Logger: util.InitLogger(0), TCP: true, Request: util.Request{ - Server: "8.8.4.4", - Port: 53, - Type: dns.TypeA, - Name: "example.com.", - }, - }, - }, - { - util.Options{ - Logger: util.InitLogger(0), - Request: util.Request{ - Server: "8.8.4.4", - Port: 53, - Type: dns.TypeAAAA, - Name: "example.com.", + Server: "8.8.4.4", + Port: 53, + Type: dns.TypeA, + Name: "example.com.", + Retries: 3, }, }, }, { + "TLS", util.Options{ Logger: util.InitLogger(0), TLS: true, Request: util.Request{ - Server: "dns.google", - Port: 853, - Type: dns.TypeAAAA, - Name: "example.com.", + Server: "dns.google", + Port: 853, + Type: dns.TypeAAAA, + Name: "example.com.", + Retries: 3, + }, + }, + }, + { + "Timeout", + util.Options{ + Logger: util.InitLogger(0), + Request: util.Request{ + Server: "8.8.4.1", + Port: 1, + Type: dns.TypeA, + Name: "example.com.", + Timeout: time.Millisecond * 100, + Retries: 0, }, }, }, @@ -107,11 +113,16 @@ func TestResolveAgain(t *testing.T) { for _, test := range tests { test := test - t.Run("", func(t *testing.T) { + t.Run(test.name, func(t *testing.T) { t.Parallel() - res, err := query.CreateQuery(test.opt) - assert.NilError(t, err) - assert.Assert(t, res != util.Response{}) + + res, err := query.CreateQuery(test.opts) + if err == nil { + assert.NilError(t, err) + assert.Assert(t, res != util.Response{}) + } else { + assert.ErrorIs(t, err, os.ErrDeadlineExceeded) + } }) } } diff --git a/query/print_test.go b/query/print_test.go index ebb0ba3..1dd0226 100644 --- a/query/print_test.go +++ b/query/print_test.go @@ -77,8 +77,7 @@ func TestRealPrint(t *testing.T) { Type: dns.StringToType["NS"], Class: 1, Name: "google.com.", - Timeout: 0, - Retries: 0, + Retries: 3, }, EDNS: util.EDNS{ EnableEDNS: false, @@ -110,8 +109,7 @@ func TestRealPrint(t *testing.T) { Type: dns.StringToType["NS"], Class: 1, Name: "freecumextremist.com.", - Timeout: 0, - Retries: 0, + Retries: 3, }, EDNS: util.EDNS{ EnableEDNS: false, @@ -172,8 +170,7 @@ func TestRealPrint(t *testing.T) { Type: dns.StringToType["A"], Class: 1, Name: "froth.zone.", - Timeout: 0, - Retries: 0, + Retries: 3, }, EDNS: util.EDNS{ EnableEDNS: true, diff --git a/query/query_test.go b/query/query_test.go index 5a5183f..51cf527 100644 --- a/query/query_test.go +++ b/query/query_test.go @@ -14,115 +14,128 @@ import ( func TestCreateQ(t *testing.T) { t.Parallel() - in := []util.Options{ + tests := []struct { + name string + opts util.Options + }{ { - Logger: util.InitLogger(0), - HeaderFlags: util.HeaderFlags{ - Z: true, - }, - - YAML: true, - - Request: util.Request{ - Server: "8.8.4.4", - Port: 53, - Type: dns.TypeA, - Name: "example.com.", - }, - Display: util.Display{ - Comments: true, - Question: true, - Opt: true, - Answer: true, - Authority: true, - Additional: true, - Statistics: true, - ShowQuery: true, - }, - EDNS: util.EDNS{ - ZFlag: 1, - BufSize: 1500, - EnableEDNS: true, - Cookie: true, - DNSSEC: true, - Expire: true, - KeepOpen: true, - Nsid: true, - Padding: true, - Version: 0, + "1", + util.Options{ + Logger: util.InitLogger(0), + HeaderFlags: util.HeaderFlags{ + Z: true, + }, + YAML: true, + Request: util.Request{ + Server: "8.8.4.4", + Port: 53, + Type: dns.TypeA, + Name: "example.com.", + Retries: 3, + }, + Display: util.Display{ + Comments: true, + Question: true, + Opt: true, + Answer: true, + Authority: true, + Additional: true, + Statistics: true, + ShowQuery: true, + }, + EDNS: util.EDNS{ + ZFlag: 1, + BufSize: 1500, + EnableEDNS: true, + Cookie: true, + DNSSEC: true, + Expire: true, + KeepOpen: true, + Nsid: true, + Padding: true, + Version: 0, + }, }, }, { - Logger: util.InitLogger(0), - HeaderFlags: util.HeaderFlags{ - Z: true, - }, - XML: true, + "2", + util.Options{ + Logger: util.InitLogger(0), + HeaderFlags: util.HeaderFlags{ + Z: true, + }, + XML: true, - Request: util.Request{ - Server: "8.8.4.4", - Port: 53, - Type: dns.TypeA, - Name: "example.com.", - }, - Display: util.Display{ - Comments: true, - Question: true, - Opt: true, - Answer: true, - Authority: true, - Additional: true, - Statistics: true, - UcodeTranslate: true, - ShowQuery: true, + Request: util.Request{ + Server: "8.8.4.4", + Port: 53, + Type: dns.TypeA, + Name: "example.com.", + Retries: 3, + }, + Display: util.Display{ + Comments: true, + Question: true, + Opt: true, + Answer: true, + Authority: true, + Additional: true, + Statistics: true, + UcodeTranslate: true, + ShowQuery: true, + }, }, }, { - Logger: util.InitLogger(0), - JSON: true, - QUIC: true, + "3", + util.Options{ + Logger: util.InitLogger(0), + JSON: true, + QUIC: true, - Request: util.Request{ - Server: "dns.adguard.com", - Port: 853, - Type: dns.TypeA, - Name: "example.com.", - }, - Display: util.Display{ - Comments: true, - Question: true, - Opt: true, - Answer: true, - Authority: true, - Additional: true, - Statistics: true, - ShowQuery: true, - }, - EDNS: util.EDNS{ - EnableEDNS: true, - DNSSEC: true, - Cookie: true, - Expire: true, - Nsid: true, + Request: util.Request{ + Server: "dns.adguard.com", + Port: 853, + Type: dns.TypeA, + Name: "example.com.", + Retries: 3, + }, + Display: util.Display{ + Comments: true, + Question: true, + Opt: true, + Answer: true, + Authority: true, + Additional: true, + Statistics: true, + ShowQuery: true, + }, + EDNS: util.EDNS{ + EnableEDNS: true, + DNSSEC: true, + Cookie: true, + Expire: true, + Nsid: true, + }, }, }, } - for _, opt := range in { - opt := opt + for _, test := range tests { + test := test - t.Run("", func(t *testing.T) { + t.Run(test.name, func(t *testing.T) { t.Parallel() - res, err := query.CreateQuery(opt) + res, err := query.CreateQuery(test.opts) assert.NilError(t, err) assert.Assert(t, res != util.Response{}) - str, err := query.PrintSpecial(res, opt) + str, err := query.PrintSpecial(res, test.opts) assert.NilError(t, err) assert.Assert(t, str != "") - str, err = query.ToString(res, opt) + str, err = query.ToString(res, test.opts) assert.NilError(t, err) assert.Assert(t, str != "") }) diff --git a/util/options_test.go b/util/options_test.go index 741e7c2..6ec6a0c 100644 --- a/util/options_test.go +++ b/util/options_test.go @@ -17,21 +17,20 @@ func TestSubnet(t *testing.T) { "::0/0", "0", "127.0.0.1/32", + "Invalid", } for _, test := range subnet { test := test + t.Run(test, func(t *testing.T) { t.Parallel() err := util.ParseSubnet(test, new(util.Options)) - assert.NilError(t, err) + if err != nil { + assert.ErrorContains(t, err, "invalid CIDR address") + } else { + assert.NilError(t, err) + } }) } } - -func TestInvalidSub(t *testing.T) { - t.Parallel() - - err := util.ParseSubnet("1", new(util.Options)) - assert.ErrorContains(t, err, "invalid CIDR address") -} diff --git a/util/reverseDNS_test.go b/util/reverseDNS_test.go index 5a77748..e4c1a33 100644 --- a/util/reverseDNS_test.go +++ b/util/reverseDNS_test.go @@ -10,25 +10,43 @@ import ( "gotest.tools/v3/assert" ) -var ( - PTR = dns.StringToType["PTR"] - NAPTR = dns.StringToType["NAPTR"] -) - -func TestIPv4(t *testing.T) { +func TestPTR(t *testing.T) { t.Parallel() - act, err := util.ReverseDNS("8.8.4.4", PTR) - assert.NilError(t, err) - assert.Equal(t, act, "4.4.8.8.in-addr.arpa.", "IPv4 reverse") -} + tests := []struct { + name string + in string + expected string + }{ + { + "IPv4", + "8.8.4.4", "4.4.8.8.in-addr.arpa.", + }, + { + "IPv6", + "2606:4700:4700::1111", "1.1.1.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.7.4.0.0.7.4.6.0.6.2.ip6.arpa.", + }, + { + "Inavlid value", + "AAAAA", "", + }, + } -func TestIPv6(t *testing.T) { - t.Parallel() + for _, test := range tests { + test := test - act, err := util.ReverseDNS("2606:4700:4700::1111", PTR) - assert.NilError(t, err) - assert.Equal(t, act, "1.1.1.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.7.4.0.0.7.4.6.0.6.2.ip6.arpa.", "IPv6 reverse") + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + act, err := util.ReverseDNS(test.in, dns.StringToType["PTR"]) + if err == nil { + assert.NilError(t, err) + } else { + assert.ErrorContains(t, err, "unrecognized address") + } + assert.Equal(t, act, test.expected) + }) + } } func TestNAPTR(t *testing.T) { @@ -48,23 +66,14 @@ func TestNAPTR(t *testing.T) { test := test t.Run(test.in, func(t *testing.T) { t.Parallel() - act, err := util.ReverseDNS(test.in, NAPTR) + act, err := util.ReverseDNS(test.in, dns.StringToType["NAPTR"]) assert.NilError(t, err) assert.Equal(t, test.want, act) }) } } -func TestInvalid(t *testing.T) { - t.Parallel() - - _, err := util.ReverseDNS("AAAAA", 1) - assert.ErrorContains(t, err, "invalid value AAAAA given") -} - -func TestInvalid2(t *testing.T) { - t.Parallel() - - _, err := util.ReverseDNS("1.0", PTR) - assert.ErrorContains(t, err, "PTR reverse") +func TestInvalidAll(t *testing.T) { + _, err := util.ReverseDNS("q", 15236) + assert.ErrorContains(t, err, "invalid value") }