More unit tests (#19)
All checks were successful
continuous-integration/drone/push Build is passing

Co-authored-by: grumbulon <grumbulon@grumbulon.xyz>
Co-committed-by: grumbulon <grumbulon@grumbulon.xyz>
This commit is contained in:
grumbulon 2022-07-02 16:34:42 +00:00 committed by Sam
parent ffb714532b
commit 1ec4cb4d05
5 changed files with 235 additions and 5 deletions

View file

@ -3,6 +3,7 @@
package main
import (
"os"
"testing"
"git.froth.zone/sam/awl/util"
@ -41,3 +42,38 @@ func TestArgParse(t *testing.T) {
assert.Equal(t, test.want, act)
}
}
func TestQuery(t *testing.T) {
app := prepareCLI()
args := os.Args[0:1]
args = append(args, "--Treebug")
err := app.Run(args)
assert.NotNil(t, err)
}
func TestHTTPS(t *testing.T) {
app := prepareCLI()
args := os.Args[0:1]
args = append(args, "-H")
args = append(args, "@https://cloudflare-dns.com/dns-query")
args = append(args, "git.froth.zone")
err := app.Run(args)
assert.Nil(t, err)
}
func FuzzCli(f *testing.F) {
testcases := []string{"git.froth.zone", "", "!12345", "google.com.edu.org.fr"}
for _, tc := range testcases {
f.Add(tc)
}
f.Fuzz(func(t *testing.T, orig string) {
app := prepareCLI()
args := os.Args[0:1]
args = append(args, orig)
err := app.Run(args)
if err != nil {
assert.ErrorContains(t, err, "domain must be fully qualified")
}
assert.Nil(t, err)
})
}

View file

@ -46,7 +46,7 @@ func (l *Logger) UnMarshalLevel(lv Level) (string, error) {
case 3:
return "DEBUG ", nil
}
return "", fmt.Errorf("Invalid log level choice")
return "", fmt.Errorf("invalid log level choice")
}
func (l *Logger) IsLevel(level Level) bool {

View file

@ -44,7 +44,7 @@ func (l *Logger) Println(level Level, v ...any) {
}
// Formats the log header as such <LogLevel> YYYY/MM/DD HH:MM:SS (local time) <the message to log>
func (l *Logger) formatHeader(buf *[]byte, t time.Time, line int, level Level) {
func (l *Logger) formatHeader(buf *[]byte, t time.Time, line int, level Level) error {
if lvl, err := l.UnMarshalLevel(level); err == nil {
// This is ugly but functional
// maybe there can be an append func or something in the future
@ -67,10 +67,9 @@ func (l *Logger) formatHeader(buf *[]byte, t time.Time, line int, level Level) {
*buf = append(*buf, ':')
*buf = append(*buf, ' ')
} else {
fmt.Printf("Unable to unmarshal log level: %v", err)
os.Exit(2) //Fucking kill him
return fmt.Errorf("invalid log level choice")
}
return nil
}
// Printer prints the formatted message directly to stdErr

83
logawl/logging_test.go Normal file
View file

@ -0,0 +1,83 @@
package logawl
import (
"bytes"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var logger = New()
func TestLogawl(t *testing.T) {
assert.Equal(t, Level(2), logger.Level) //cast 2 (int) to 2 (level)
//Validate setting and getting levels from memory works
for i := range AllLevels {
logger.SetLevel(Level(i))
assert.Equal(t, Level(i), logger.GetLevel())
}
}
func TestUnmarshalLevels(t *testing.T) {
m := make(map[int]string)
var err error
//Fill map with unmarshalled level info
for i := range AllLevels {
m[i], err = logger.UnMarshalLevel(Level(i))
assert.Nil(t, err)
}
//iterate over map and assert equal
for i := range AllLevels {
lv, err := logger.UnMarshalLevel(Level(i))
assert.Nil(t, err)
assert.Equal(t, m[i], lv)
}
lv, err := logger.UnMarshalLevel(Level(9001))
assert.NotNil(t, err)
assert.Equal(t, "", lv)
assert.ErrorContains(t, err, "invalid log level choice")
}
func TestLogger(t *testing.T) {
for i := range AllLevels {
// only test non-exiting log levels
switch i {
case 1:
fn := func() {
logger.Info("")
}
var buffer bytes.Buffer
logger.Out = &buffer
fn()
case 2:
fn := func() {
logger.Info("Test")
}
var buffer bytes.Buffer
logger.Out = &buffer
fn()
case 3:
fn := func() {
logger.Debug("Test")
}
var buffer bytes.Buffer
logger.Out = &buffer
fn()
}
}
}
func TestFmt(t *testing.T) {
ti := time.Now()
test := []byte("test")
assert.NotNil(t, logger.formatHeader(&test, ti, 0, Level(9001))) //make sure error is error
}

112
query/query_test.go Normal file
View file

@ -0,0 +1,112 @@
package query
import (
"fmt"
"net"
"strconv"
"strings"
"testing"
"git.froth.zone/sam/awl/util"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)
func TestResolveHTTPS(t *testing.T) {
var err error
testCase := util.Answers{Server: "dns9.quad9.net/dns-query", Request: dns.TypeA, Name: "git.froth.zone"}
if !strings.HasPrefix(testCase.Server, "https://") {
testCase.Server = "https://" + testCase.Server
}
// if the domain is not canonical, make it canonical
if !strings.HasSuffix(testCase.Name, ".") {
testCase.Name = fmt.Sprintf("%s.", testCase.Name)
}
msg := new(dns.Msg)
msg.SetQuestion(testCase.Name, testCase.Request)
msg = msg.SetQuestion(testCase.Name, testCase.Request)
var in *dns.Msg
in, testCase.RTT, err = ResolveHTTPS(msg, testCase.Server)
assert.Nil(t, err)
assert.NotNil(t, in)
}
func Test2ResolveHTTPS(t *testing.T) {
var err error
testCase := util.Answers{Server: "dns9.quad9.net/dns-query", Request: dns.TypeA, Name: "git.froth.zone"}
msg := new(dns.Msg)
msg.SetQuestion(testCase.Name, testCase.Request)
msg = msg.SetQuestion(testCase.Name, testCase.Request)
var in *dns.Msg
in, testCase.RTT, err = ResolveHTTPS(msg, testCase.Server)
assert.NotNil(t, err)
assert.Nil(t, in)
}
func Test3ResolveHTTPS(t *testing.T) {
var err error
testCase := util.Answers{Server: "dns9..quad9.net/dns-query", Request: dns.TypeA, Name: "git.froth.zone."}
if !strings.HasPrefix(testCase.Server, "https://") {
testCase.Server = "https://" + testCase.Server
}
// if the domain is not canonical, make it canonical
if !strings.HasSuffix(testCase.Name, ".") {
testCase.Name = fmt.Sprintf("%s.", testCase.Name)
}
msg := new(dns.Msg)
msg.SetQuestion(testCase.Name, testCase.Request)
msg = msg.SetQuestion(testCase.Name, testCase.Request)
var in *dns.Msg
in, testCase.RTT, err = ResolveHTTPS(msg, testCase.Server)
assert.NotNil(t, err)
assert.Nil(t, in)
}
func TestQuic(t *testing.T) {
var err error
testCase := util.Answers{Server: "dns.adguard.com", Request: dns.TypeA, Name: "git.froth.zone"}
testCase2 := util.Answers{Server: "dns.adguard.com", Request: dns.TypeA, Name: "git.froth.zone"}
var testCases []util.Answers
testCases = append(testCases, testCase)
testCases = append(testCases, testCase2)
for i := range testCases {
switch i {
case 0:
port := 853
testCases[i].Server = net.JoinHostPort(testCases[i].Server, strconv.Itoa(port))
fmt.Println(testCases[i].Server)
// 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.Request)
msg = msg.SetQuestion(testCase.Name, testCase.Request)
var in *dns.Msg
in, testCase.RTT, err = ResolveQUIC(msg, testCase.Server)
assert.NotNil(t, err)
assert.Nil(t, in)
case 1:
port := 853
testCases[i].Server = net.JoinHostPort(testCases[i].Server, strconv.Itoa(port))
// 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(testCases[i].Name, testCases[i].Request)
msg = msg.SetQuestion(testCases[i].Name, testCases[i].Request)
var in *dns.Msg
in, testCase.RTT, err = ResolveQUIC(msg, testCases[i].Server)
assert.Nil(t, err)
assert.NotNil(t, in)
}
}
}