diff options
| author | Paul Buetow <paul@buetow.org> | 2023-05-19 01:09:47 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-05-19 01:09:47 +0300 |
| commit | f03889b92628d1589d2e161c56510646398686a1 (patch) | |
| tree | 31a685293dfe170e7e056add43ad99bb558abf53 /internal | |
| parent | e881e64406bc8f2d0d5d3099a48d3cf4774e7bd8 (diff) | |
add config unit tests
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/config.go | 6 | ||||
| -rw-r--r-- | internal/config_test.go | 38 |
2 files changed, 43 insertions, 1 deletions
diff --git a/internal/config.go b/internal/config.go index f8f6b10..93549e2 100644 --- a/internal/config.go +++ b/internal/config.go @@ -38,10 +38,14 @@ func newConfig(configFile string) (config, error) { } func (c config) isParticipant(remoteAddr string) bool { + return c.isParticipantWithLookup(remoteAddr, net.LookupIP) +} + +func (c config) isParticipantWithLookup(remoteAddr string, lookupIP func(string) ([]net.IP, error)) bool { remoteIP := stripPort(remoteAddr) for _, participant := range c.Participants { - ips, err := net.LookupIP(stripPort(participant)) + ips, err := lookupIP(stripPort(participant)) if err != nil { log.Println(err) continue diff --git a/internal/config_test.go b/internal/config_test.go new file mode 100644 index 0000000..1a9ed69 --- /dev/null +++ b/internal/config_test.go @@ -0,0 +1,38 @@ +package internal + +import ( + "fmt" + "net" + "testing" +) + +func TestStripPort(t *testing.T) { + if "localhost" != stripPort("localhost:1234") { + t.Errorf("Unable to split port from \"localhost:1234\"") + } +} + +func TestIsParticipant(t *testing.T) { + config := config{Participants: []string{"localhost:1234", "hamburger:4321"}} + + lookupIP := func(addr string) ([]net.IP, error) { + switch addr { + case "localhost": + return []net.IP{{127, 0, 0, 1}}, nil + case "hamburger": + return []net.IP{{8, 8, 8, 8}}, nil + default: + return []net.IP{}, fmt.Errorf("Can't resolve %s", addr) + } + } + + remoteAddr := "127.0.0.1:323232" + if !config.isParticipantWithLookup(remoteAddr, lookupIP) { + t.Errorf("%s should be participant of %v", remoteAddr, config.Participants) + } + + remoteAddr = "9.9.9.9:2345" + if config.isParticipantWithLookup(remoteAddr, lookupIP) { + t.Errorf("%s should not be participant of %v", remoteAddr, config.Participants) + } +} |
