Fix testing on windows, add better Plan 9 support
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

Signed-off-by: Sam Therapy <sam@samtherapy.net>
This commit is contained in:
Sam Therapy 2022-06-30 22:26:40 +02:00 committed by Gitea
parent ce2688cf1a
commit 2e2d5187d1
7 changed files with 88 additions and 46 deletions

1
cli.go
View file

@ -1,4 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
package main
import (

View file

@ -1,4 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
package main
import (

View file

@ -7,7 +7,6 @@ package conf
import (
"os"
"runtime"
"strings"
"github.com/miekg/dns"
)
@ -24,29 +23,3 @@ func GetDNSConfig() (*dns.ClientConfig, error) {
return dns.ClientConfigFromFile("/etc/resolv.conf")
}
}
// Plan 9 stores its network data in /net/ndb, which seems to be formatted a specific way
func getPlan9Config(str string) (*dns.ClientConfig, error) {
str = strings.ReplaceAll(str, "\n", "")
spl := strings.FieldsFunc(str, split)
servers := []string{}
for _, option := range spl {
if strings.HasPrefix(option, "dns=") {
servers = append(servers, strings.TrimPrefix(option, "dns="))
}
}
// TODO: read more about how customizable Plan 9 is
return &dns.ClientConfig{
Servers: servers,
Search: []string{},
Port: "53",
Ndots: 1,
Timeout: 5,
Attempts: 1,
}, nil
}
func split(r rune) bool {
return r == ' ' || r == '\t'
}

View file

@ -1,18 +0,0 @@
// SPDX-License-Identifier: BSD-3-Clause
package conf
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetPlan9Config(t *testing.T) {
ndb := `ip=192.168.122.45 ipmask=255.255.255.0 ipgw=192.168.122.1
sys=chog9
dns=192.168.122.1`
act, err := getPlan9Config(ndb)
assert.Nil(t, err)
assert.Equal(t, act.Servers[0], "192.168.122.1")
}

40
conf/plan9.go Normal file
View file

@ -0,0 +1,40 @@
// SPDX-License-Identifier: BSD-3-Clause
package conf
import (
"fmt"
"strings"
"github.com/miekg/dns"
)
// Plan 9 stores its network data in /net/ndb, which seems to be formatted a specific way
// Yoink it and use it.
//
// See ndb(7).
func getPlan9Config(str string) (*dns.ClientConfig, error) {
str = strings.ReplaceAll(str, "\n", "")
spl := strings.FieldsFunc(str, splitChars)
var servers []string
for _, option := range spl {
if strings.HasPrefix(option, "dns=") {
servers = append(servers, strings.TrimPrefix(option, "dns="))
}
}
if len(servers) == 0 {
return nil, fmt.Errorf("plan9: no DNS servers found")
}
// TODO: read more about how customizable Plan 9 is
return &dns.ClientConfig{
Servers: servers,
Search: []string{},
Port: "53",
}, nil
}
// Split the string at either space or tabs
func splitChars(r rune) bool {
return r == ' ' || r == '\t'
}

45
conf/plan9_test.go Normal file
View file

@ -0,0 +1,45 @@
// SPDX-License-Identifier: BSD-3-Clause
package conf
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetPlan9Config(t *testing.T) {
ndbs := []struct {
in string
want string
}{
{`ip=192.168.122.45 ipmask=255.255.255.0 ipgw=192.168.122.1
sys=chog9
dns=192.168.122.1`, "192.168.122.1"},
{`ipnet=murray-hill ip=135.104.0.0 ipmask=255.255.0.0
dns=135.104.10.1
ntp=ntp.cs.bell-labs.com
ipnet=plan9 ip=135.104.9.0 ipmask=255.255.255.0
ntp=oncore.cs.bell-labs.com
smtp=smtp1.cs.bell-labs.com
ip=135.104.9.6 sys=anna dom=anna.cs.bell-labs.com
smtp=smtp2.cs.bell-labs.com`, "135.104.10.1"},
}
for _, ndb := range ndbs {
act, err := getPlan9Config(ndb.in)
assert.Nil(t, err)
assert.Equal(t, ndb.want, act.Servers[0])
}
invalid := `sys = spindle
dom=spindle.research.bell-labs.com
bootf=/mips/9powerboot
ip=135.104.117.32 ether=080069020677
proto=il`
act, err := getPlan9Config(invalid)
assert.ErrorContains(t, err, "no DNS servers found")
assert.Nil(t, act)
}

2
go.mod
View file

@ -30,7 +30,7 @@ require (
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.11 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect