awl/cmd/misc_test.go
Sam Therapy 21047275bf
All checks were successful
Mirror Push / mirror (push) Successful in 6s
Test / test (stable) (push) Successful in 41s
Test / test (oldstable) (push) Successful in 52s
fix(quic): A workaround for adguard not working over QUIC
Signed-off-by: Sam Therapy <sam@samtherapy.net>
2024-03-15 18:20:03 +01:00

195 lines
4.9 KiB
Go

// SPDX-License-Identifier: BSD-3-Clause
package cli_test
import (
"strings"
"testing"
cli "dns.froth.zone/awl/cmd"
"dns.froth.zone/awl/pkg/util"
"github.com/miekg/dns"
"gotest.tools/v3/assert"
)
func TestParseArgs(t *testing.T) {
t.Parallel()
args := []string{
"go.dev",
"AAAA",
"@1.1.1.1",
"+ignore",
}
opts := new(util.Options)
opts.Logger = util.InitLogger(0)
err := cli.ParseMiscArgs(args, opts)
assert.NilError(t, err)
assert.Equal(t, opts.Request.Name, "go.dev.")
assert.Equal(t, opts.Request.Type, dns.StringToType["AAAA"])
assert.Equal(t, opts.Request.Server, "1.1.1.1")
assert.Equal(t, opts.Truncate, true)
}
func TestParseNoInput(t *testing.T) {
t.Parallel()
args := []string{}
opts := new(util.Options)
opts.Logger = util.InitLogger(0)
err := cli.ParseMiscArgs(args, opts)
assert.NilError(t, err)
assert.Equal(t, opts.Request.Name, ".")
assert.Equal(t, opts.Request.Type, dns.StringToType["NS"])
}
func TestParseA(t *testing.T) {
t.Parallel()
args := []string{
"golang.org.",
}
opts := new(util.Options)
opts.Logger = util.InitLogger(0)
err := cli.ParseMiscArgs(args, opts)
assert.NilError(t, err)
assert.Equal(t, opts.Request.Name, "golang.org.")
assert.Equal(t, opts.Request.Type, dns.StringToType["A"])
}
func TestParsePTR(t *testing.T) {
t.Parallel()
args := []string{"8.8.8.8"}
opts := new(util.Options)
opts.Logger = util.InitLogger(0)
opts.Reverse = true
err := cli.ParseMiscArgs(args, opts)
assert.NilError(t, err)
assert.Equal(t, opts.Request.Type, dns.StringToType["PTR"])
}
func TestParseInvalidPTR(t *testing.T) {
t.Parallel()
args := []string{"8.88.8"}
opts := new(util.Options)
opts.Logger = util.InitLogger(0)
opts.Reverse = true
err := cli.ParseMiscArgs(args, opts)
assert.ErrorContains(t, err, "unrecognized address")
}
func TestDefaultServer(t *testing.T) {
t.Parallel()
tests := []struct {
in string
want string
}{
{"DNSCrypt", "sdns://AQMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20"},
{"TLS", "dns.google"},
{"HTTPS", "https://dns.cloudflare.com"},
{"QUIC", "dns.froth.zone"},
}
for _, test := range tests {
test := test
t.Run(test.in, func(t *testing.T) {
t.Parallel()
args := []string{}
opts := new(util.Options)
opts.Logger = util.InitLogger(0)
switch test.in {
case "DNSCrypt":
opts.DNSCrypt = true
case "TLS":
opts.TLS = true
case "HTTPS":
opts.HTTPS = true
case "QUIC":
opts.QUIC = true
}
err := cli.ParseMiscArgs(args, opts)
assert.NilError(t, err)
assert.Equal(t, opts.Request.Server, test.want)
})
}
}
func TestFlagSetting(t *testing.T) {
t.Parallel()
tests := []struct {
in string
expected string
over string
}{
{"@sdns://AQMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20", "sdns://AQMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20", "DNSCrypt"},
{"@tls://dns.google", "dns.google", "TLS"},
{"@https://dns.cloudflare.com/dns-query", "https://dns.cloudflare.com/dns-query", "HTTPS"},
{"@https://dns.example.net/a", "https://dns.example.net/a", "HTTPS with a set path"},
{"@quic://dns.adguard.com", "dns.adguard.com", "QUIC"},
{"@tcp://dns.froth.zone", "dns.froth.zone", "TCP"},
{"@udp://dns.example.com", "dns.example.com", "UDP"},
}
for _, test := range tests {
test := test
t.Run(test.over, func(t *testing.T) {
t.Parallel()
opts := new(util.Options)
opts.Logger = util.InitLogger(0)
err := cli.ParseMiscArgs([]string{test.in}, opts)
assert.NilError(t, err)
switch {
case strings.HasPrefix(test.over, "DNSCrypt"):
assert.Assert(t, opts.DNSCrypt)
assert.Equal(t, opts.Request.Server, test.expected)
case strings.HasPrefix(test.over, "TLS"):
assert.Assert(t, opts.TLS)
assert.Equal(t, opts.Request.Server, test.expected)
case strings.HasPrefix(test.over, "HTTPS"):
assert.Assert(t, opts.HTTPS)
assert.Equal(t, opts.Request.Server, test.expected)
case strings.HasPrefix(test.over, "QUIC"):
assert.Assert(t, opts.QUIC)
assert.Equal(t, opts.Request.Server, test.expected)
case strings.HasPrefix(test.over, "TCP"):
assert.Assert(t, opts.TCP)
assert.Equal(t, opts.Request.Server, test.expected)
case strings.HasPrefix(test.over, "UDP"):
assert.Assert(t, true)
assert.Equal(t, opts.Request.Server, test.expected)
}
})
}
}
func FuzzParseArgs(f *testing.F) {
cases := []string{
"go.dev",
"AAAA",
"@1.1.1.1",
"+ignore",
"e",
}
for _, tc := range cases {
f.Add(tc)
}
f.Fuzz(func(t *testing.T, arg string) {
// Get rid of outputs
args := []string{arg}
opts := new(util.Options)
opts.Logger = util.InitLogger(0)
//nolint:errcheck,gosec // Only make sure the program does not crash
cli.ParseMiscArgs(args, opts)
})
}