From fb653292612eddca5b088ed88466c546d1597107 Mon Sep 17 00:00:00 2001 From: Elizabeth Alexander Hunt Date: Sat, 20 Jun 2026 09:40:07 -0700 Subject: Initial commit --- utils/net_test.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 utils/net_test.go (limited to 'utils/net_test.go') 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) + } + } +} -- cgit v1.3