Compare commits

..

3 commits

Author SHA1 Message Date
grumbulon 3d1f63144c yea 2022-06-30 08:43:06 -04:00
grumbulon 3467eabfe7 More tests 2022-06-29 18:21:43 -04:00
grumbulon c2eab0212a Test coverage 2022-06-29 14:28:00 -04:00
5 changed files with 158 additions and 3 deletions

View file

@ -2,6 +2,7 @@
package main
import (
"os"
"testing"
"git.froth.zone/sam/awl/util"
@ -40,3 +41,21 @@ 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, "--debug")
err := app.Run(args)
assert.Nil(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)
}

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

@ -37,8 +37,6 @@ func (l *Logger) Println(level Level, v ...any) {
l.Printer(2, fmt.Sprintln(v...)) //Info level
case 3:
l.Printer(3, fmt.Sprintln(v...)) //Debug level
default:
break
}
}
}

82
logawl/logging_test.go Normal file
View file

@ -0,0 +1,82 @@
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")
logger.formatHeader(&test, ti, 0, Level(9001))
}

56
query/query_test.go Normal file
View file

@ -0,0 +1,56 @@
package query
import (
"testing"
"time"
"git.froth.zone/sam/awl/util"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)
func TestHTTPS(t *testing.T) {
for i := range queries {
switch i {
case 1:
msg := new(dns.Msg)
testCase := util.Answers{Server: UnmarshallHTTPS(i), Request: dns.TypeAAAA, Name: "localhost"}
msg = msg.SetQuestion(testCase.Name, testCase.Request)
m, rtt, err := ResolveHTTPS(msg, testCase.Name)
assert.Error(t, err)
assert.Nil(t, m)
assert.Equal(t, rtt, time.Duration(0))
case 2:
msg := new(dns.Msg)
testCase := util.Answers{Server: UnmarshallHTTPS(i), Request: dns.TypeAAAA, Name: "localhost"}
msg = msg.SetQuestion(testCase.Name, testCase.Request)
m, rtt, err := ResolveHTTPS(msg, testCase.Name)
assert.Nil(t, err)
assert.Nil(t, m)
assert.Equal(t, rtt, time.Duration(0))
}
}
}
const (
one int = iota
cloudflare
)
var queries = []int{
one,
cloudflare,
}
func UnmarshallHTTPS(i int) string {
switch i {
case 0:
return "::1"
case 1:
return "https://cloudflare-dns.com/dns-query"
}
return ""
}