summaryrefslogtreecommitdiff
path: root/utils/net_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'utils/net_test.go')
-rw-r--r--utils/net_test.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/utils/net_test.go b/utils/net_test.go
new file mode 100644
index 0000000..27d991d
--- /dev/null
+++ b/utils/net_test.go
@@ -0,0 +1,63 @@
+package utils
+
+import "testing"
+
+func TestParseMacAndFormat(t *testing.T) {
+ // each input should parse and round-trip to the canonical form
+ canonical := map[string]string{
+ "aa:bb:cc:dd:ee:ff": "aa:bb:cc:dd:ee:ff", // already canonical
+ "AA:BB:CC:DD:EE:FF": "aa:bb:cc:dd:ee:ff", // uppercase
+ "8:0:27:ab:cd:ef": "08:00:27:ab:cd:ef", // macOS zero-stripped octets
+ " de:ad:be:ef:00:01 ": "de:ad:be:ef:00:01", // surrounding space
+ }
+ for in, want := range canonical {
+ mac, err := ParseMac(in)
+ if err != nil {
+ t.Errorf("ParseMac(%q) unexpected error: %v", in, err)
+ continue
+ }
+ if got := mac.Format(); got != want {
+ t.Errorf("ParseMac(%q).Format() = %q, want %q", in, got, want)
+ }
+ }
+}
+
+func TestParseMacInvalid(t *testing.T) {
+ for _, in := range []string{
+ "", // empty
+ "aa:bb:cc:dd:ee", // too few octets
+ "hello", // not a mac
+ "zz:zz:zz:zz:zz:zz", // non-hex digits
+ } {
+ if _, err := ParseMac(in); err == nil {
+ t.Errorf("ParseMac(%q) = nil error, want error", in)
+ }
+ }
+}
+
+func TestParseINet4AndFormat(t *testing.T) {
+ for _, ip := range []string{"192.168.1.5", "10.0.0.2", "0.0.0.0", "255.255.255.255"} {
+ addr, err := ParseINet4(ip)
+ if err != nil {
+ t.Errorf("ParseINet4(%q) unexpected error: %v", ip, err)
+ continue
+ }
+ if got := addr.Format(); got != ip {
+ t.Errorf("ParseINet4(%q).Format() = %q, want %q", ip, got, ip)
+ }
+ }
+}
+
+func TestParseINet4Invalid(t *testing.T) {
+ for _, in := range []string{
+ "", // empty
+ "1.2.3", // too few octets
+ "192.168.1.256", // octet overflows a byte
+ "::1", // IPv6, not dotted-decimal
+ "hello", // garbage
+ } {
+ if _, err := ParseINet4(in); err == nil {
+ t.Errorf("ParseINet4(%q) = nil error, want error", in)
+ }
+ }
+}