From c472e5c7dee2e1cb16e297b23726a714521e8e0b Mon Sep 17 00:00:00 2001 From: xegineering Date: Tue, 1 Oct 2024 21:44:47 +0200 Subject: Add minimal soundbox Go package This only adds the `Soundbox` struct type which has a `net.HardwareAddr` to identify it together with a constructor and a unit test. --- soundbox/soundbox.go | 25 +++++++++++++++++++++++++ soundbox/soundbox_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 soundbox/soundbox.go create mode 100644 soundbox/soundbox_test.go diff --git a/soundbox/soundbox.go b/soundbox/soundbox.go new file mode 100644 index 0000000..fcabcdc --- /dev/null +++ b/soundbox/soundbox.go @@ -0,0 +1,25 @@ +package soundbox + +import ( + "fmt" + "net" +) + +type Soundbox struct { + HardwareAddr net.HardwareAddr +} + +func NewSoundbox(mac string) (Soundbox, error) { + hardwareAddr, err := net.ParseMAC(mac) + if err != nil { + return Soundbox{}, err + } + + if len(hardwareAddr) != 6 { + return Soundbox{}, fmt.Errorf("Only IEEE 802 MAC-48 addresses supported") + } + + return Soundbox{ + HardwareAddr: hardwareAddr, + }, nil +} diff --git a/soundbox/soundbox_test.go b/soundbox/soundbox_test.go new file mode 100644 index 0000000..6a5cde3 --- /dev/null +++ b/soundbox/soundbox_test.go @@ -0,0 +1,25 @@ +package soundbox + +import ( + "testing" +) + +func TestNewSoundbox(t *testing.T) { + // valid MAC should succeed + _, err := NewSoundbox("12:34:56:ab:cd:ef") + if err != nil { + t.Errorf("NewSoundbox() failed: %v", err) + } + + // invalid MAC should fail + _, err = NewSoundbox("12:34:56:ab:cd") + if err == nil { + t.Errorf("NewSoundbox() failed: %v", err) + } + + // No EUI-64 supported + _, err = NewSoundbox("12:34:56:ab:cd:ef:12:45") + if err == nil { + t.Errorf("NewSoundbox() failed: %v", err) + } +} -- cgit v1.2.3-70-g09d2