awl/cli/misc_test.go
Sam Therapy 92812c337f
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
Another day's work
Signed-off-by: Sam Therapy <sam@samtherapy.net>
2022-07-21 19:06:46 -05:00

112 lines
2.3 KiB
Go

// SPDX-License-Identifier: BSD-3-Clause
package cli_test
import (
"testing"
"git.froth.zone/sam/awl/cli"
"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(cli.Options)
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(cli.Options)
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(cli.Options)
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(cli.Options)
opts.Reverse = true
err := cli.ParseMiscArgs(args, opts)
assert.NilError(t, err)
assert.Equal(t, opts.Request.Type, dns.StringToType["PTR"])
}
func TestDefaultServer(t *testing.T) {
t.Parallel()
tests := []struct {
in string
want string
}{
{"TLS", "dns.google"},
{"HTTPS", "https://dns.cloudflare.com/dns-query"},
{"QUIC", "dns.adguard.com"},
}
for _, test := range tests {
test := test
t.Run(test.in, func(t *testing.T) {
t.Parallel()
args := []string{}
opts := new(cli.Options)
switch test.in {
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 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) {
args := []string{arg}
opts := new(cli.Options)
//nolint:errcheck // Only make sure the program does not crash
cli.ParseMiscArgs(args, opts)
})
}