From a0dfd597ad9b693e7aef0a11c1dae182fc3e40f9 Mon Sep 17 00:00:00 2001
From: George <zhoreeq@users.noreply.github.com>
Date: Tue, 22 Sep 2020 16:40:00 -0400
Subject: [PATCH] Update domain.go and tests

---
 Makefile                    |  5 +++-
 go.sum                      |  1 +
 pkg/meshname/domain.go      |  8 ++++---
 pkg/meshname/domain_test.go | 47 +++++++++++++++++++++++++++++++++++++
 pkg/meshname/server_test.go | 31 ------------------------
 5 files changed, 57 insertions(+), 35 deletions(-)
 create mode 100644 pkg/meshname/domain_test.go
 delete mode 100644 pkg/meshname/server_test.go

diff --git a/Makefile b/Makefile
index 2cd568a..ba7d316 100644
--- a/Makefile
+++ b/Makefile
@@ -8,4 +8,7 @@ all:
 clean:
 	$(RM) meshnamed meshnamed.exe
 
-.PHONY: all clean
+test:
+	go test pkg/meshname/*_test.go
+
+.PHONY: all clean test
diff --git a/go.sum b/go.sum
index 8e8d3bb..dc3697b 100644
--- a/go.sum
+++ b/go.sum
@@ -10,6 +10,7 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
 golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
 golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g=
 golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
 golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
diff --git a/pkg/meshname/domain.go b/pkg/meshname/domain.go
index 936b22c..44c2862 100644
--- a/pkg/meshname/domain.go
+++ b/pkg/meshname/domain.go
@@ -7,22 +7,24 @@ import (
 	"strings"
 )
 
+// DomainFromIP derives a meshname subdomain for the authoritative DNS server address
 func DomainFromIP(target *net.IP) string {
 	return strings.ToLower(base32.StdEncoding.EncodeToString(*target)[0:26])
 }
 
+// IPFromDomain derives authoritative DNS server address from the meshname subdomain
 func IPFromDomain(domain *string) (net.IP, error) {
 	name := strings.ToUpper(*domain) + "======"
 	data, err := base32.StdEncoding.DecodeString(name)
 	if err != nil {
-		return net.IP{}, err
+		return nil, err
 	}
 	if len(data) != 16 {
-		return net.IP{}, errors.New("Invalid subdomain")
+		return nil, errors.New("can't decode IP address, invalid subdomain")
 	}
 	ipAddr := net.IP(data)
 	if ipAddr == nil {
-		return net.IP{}, errors.New("Invalid IP address")
+		return nil, errors.New("can't decode IP address, invalid data")
 	}
 	return ipAddr, nil
 }
diff --git a/pkg/meshname/domain_test.go b/pkg/meshname/domain_test.go
new file mode 100644
index 0000000..26512ef
--- /dev/null
+++ b/pkg/meshname/domain_test.go
@@ -0,0 +1,47 @@
+package meshname
+
+import  (
+	"bytes"
+	"net"
+	"testing"
+	"fmt"
+
+	"github.com/zhoreeq/meshname/pkg/meshname"
+)
+
+func TestIPFromDomain(t *testing.T) {
+	test_subdomain := "aib7cwwdeob2vtnqf2cfnm7ilq"
+	test_ip := net.ParseIP("203:f15a:c323:83aa:cdb0:2e84:56b3:e85c")
+
+	if ip, err := meshname.IPFromDomain(&test_subdomain); err != nil {
+		t.Fatal(err)
+	} else if bytes.Compare(ip, test_ip) != 0 {
+	   t.Fatalf("Decoding IP error %s != %s", ip.String(), test_ip.String())
+	}
+}
+
+func TestDomainFromIP(t *testing.T) {
+	test_subdomain := "aib7cwwdeob2vtnqf2cfnm7ilq"
+	test_ip := net.ParseIP("203:f15a:c323:83aa:cdb0:2e84:56b3:e85c")
+
+	subdomain := meshname.DomainFromIP(&test_ip)
+	if subdomain != test_subdomain {
+		t.Fatalf("Encoding domain error: %s != %s", subdomain, test_subdomain)
+	}
+}
+
+func ExampleIPFromDomain() {
+	test_subdomain := "aib7cwwdeob2vtnqf2cfnm7ilq"
+
+	if ip, err := meshname.IPFromDomain(&test_subdomain); err == nil {
+		fmt.Println(ip)
+	}
+	// Output: 203:f15a:c323:83aa:cdb0:2e84:56b3:e85c
+}
+
+func ExampleDomainFromIP() {
+	test_ip := net.ParseIP("203:f15a:c323:83aa:cdb0:2e84:56b3:e85c")
+
+	fmt.Println(meshname.DomainFromIP(&test_ip))
+	// Output: aib7cwwdeob2vtnqf2cfnm7ilq
+}
diff --git a/pkg/meshname/server_test.go b/pkg/meshname/server_test.go
deleted file mode 100644
index 69c15f7..0000000
--- a/pkg/meshname/server_test.go
+++ /dev/null
@@ -1,31 +0,0 @@
-package meshname
-
-import  (
-	"bytes"
-	"net"
-	"testing"
-
-	"github.com/zhoreeq/meshname/pkg/meshname"
-)
-
-func TestIPFromDomain(t *testing.T) {
-	test_subdomain := "aib7cwwdeob2vtnqf2cfnm7ilq"
-	test_ip := net.ParseIP("203:f15a:c323:83aa:cdb0:2e84:56b3:e85c")
-
-	ip, err := meshname.IPFromDomain(&test_subdomain)
-	if err != nil {
-		t.Errorf("Decoding IP from domain failed %s", err)
-	} else if bytes.Compare(ip, test_ip) != 0 {
-	   t.Errorf("Decoding IP error %s != %s", ip.String(), test_ip.String())
-	}
-}
-
-func TestDomainFromIP(t *testing.T) {
-	test_subdomain := "aib7cwwdeob2vtnqf2cfnm7ilq"
-	test_ip := net.ParseIP("203:f15a:c323:83aa:cdb0:2e84:56b3:e85c")
-
-	subdomain := meshname.DomainFromIP(&test_ip)
-	if subdomain != test_subdomain {
-		t.Errorf("Encoding domain error: %s != %s", subdomain, test_subdomain)
-	}
-}