More unit tests #19
5 changed files with 188 additions and 5 deletions
36
cli_test.go
36
cli_test.go
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
83
logawl/logging_test.go
Normal 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
|
||||
|
||||
}
|
65
query/query_test.go
Normal file
65
query/query_test.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
package query
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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)
|
||||
|
||||
}
|
Loading…
Reference in a new issue