Compare commits
16 commits
Author | SHA1 | Date | |
---|---|---|---|
|
3d1f63144c | ||
|
3467eabfe7 | ||
|
c2eab0212a | ||
|
4353aa9559 | ||
|
f7e5a7adee | ||
be3b81029e | |||
dfea8060dd | |||
|
6d30a5ad9b | ||
|
a36f1b8685 | ||
d4b8d1469f | |||
f50b793931 | |||
|
68e6e54741 | ||
|
8b1a5f9147 | ||
|
6da6a55f6e | ||
8c61f9f409 | |||
|
182ec20469 |
7 changed files with 193 additions and 3 deletions
5
cli.go
5
cli.go
|
@ -134,6 +134,11 @@ func prepareCLI() *cli.App {
|
|||
Aliases: []string{"x"},
|
||||
Usage: "do a reverse lookup",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "debug",
|
||||
Usage: "enable debug logging",
|
||||
Value: false,
|
||||
},
|
||||
},
|
||||
Action: doQuery,
|
||||
}
|
||||
|
|
19
cli_test.go
19
cli_test.go
|
@ -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)
|
||||
}
|
||||
|
|
30
logawl/doc.go
Normal file
30
logawl/doc.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
LogAwl is a package for custom logging needs
|
||||
|
||||
LogAwl extends the standard log library with support for log levels
|
||||
This is _different_ from the syslog package in the standard library because you do not define a file
|
||||
because awl is a cli utility it writes directly to std err.
|
||||
*/
|
||||
// Use the New() function to init logawl
|
||||
//
|
||||
// logger := logawl.New()
|
||||
//
|
||||
// You can call specific logging levels from your new logger using
|
||||
//
|
||||
// logger.Debug("Message to log")
|
||||
// logger.Fatal("Message to log")
|
||||
// logger.Info("Message to log")
|
||||
// logger.Error("Message to log")
|
||||
//
|
||||
// You may also set the log level on the fly with
|
||||
//
|
||||
// Logger.SetLevel(3)
|
||||
// This allows you to change the default level (Info) and prevent log messages from being posted at higher verbosity levels
|
||||
//for example if
|
||||
// Logger.SetLevel(3)
|
||||
// is not called and you call
|
||||
// Logger.Debug()
|
||||
// this runs through
|
||||
// IsLevel(level)
|
||||
// to verify if the debug log should be sent to std.Err or not based on the current expected log level
|
||||
package logawl
|
|
@ -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 {
|
||||
|
|
|
@ -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
82
logawl/logging_test.go
Normal 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
56
query/query_test.go
Normal 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 ""
|
||||
}
|
Loading…
Reference in a new issue