From 0d634cbdef0e39195cfad142d6cc617bb8ef48e4 Mon Sep 17 00:00:00 2001 From: mercimat <11376662+mercimat@users.noreply.github.com> Date: Wed, 22 Nov 2023 22:16:25 +0100 Subject: [PATCH] fix ParseMACAddressFields and add unit tests --- ie/mac-address.go | 4 ++ ie/mac_address_test.go | 117 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 ie/mac_address_test.go diff --git a/ie/mac-address.go b/ie/mac-address.go index 2a15691..527d774 100644 --- a/ie/mac-address.go +++ b/ie/mac-address.go @@ -157,6 +157,7 @@ func (f *MACAddressFields) UnmarshalBinary(b []byte) error { if l < offset+6 { return io.ErrUnexpectedEOF } + f.SourceMACAddress = make(net.HardwareAddr, 6) copy(f.SourceMACAddress, b[offset:offset+6]) offset += 6 } @@ -165,6 +166,7 @@ func (f *MACAddressFields) UnmarshalBinary(b []byte) error { if l < offset+6 { return io.ErrUnexpectedEOF } + f.DestinationMACAddress = make(net.HardwareAddr, 6) copy(f.DestinationMACAddress, b[offset:offset+6]) offset += 6 } @@ -173,6 +175,7 @@ func (f *MACAddressFields) UnmarshalBinary(b []byte) error { if l < offset+6 { return io.ErrUnexpectedEOF } + f.UpperSourceMACAddress = make(net.HardwareAddr, 6) copy(f.UpperSourceMACAddress, b[offset:offset+6]) offset += 6 } @@ -181,6 +184,7 @@ func (f *MACAddressFields) UnmarshalBinary(b []byte) error { if l < offset+6 { return io.ErrUnexpectedEOF } + f.UpperDestinationMACAddress = make(net.HardwareAddr, 6) copy(f.UpperDestinationMACAddress, b[offset:offset+6]) } diff --git a/ie/mac_address_test.go b/ie/mac_address_test.go new file mode 100644 index 0000000..017114c --- /dev/null +++ b/ie/mac_address_test.go @@ -0,0 +1,117 @@ +package ie_test + +import ( + "io" + "net" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/wmnsk/go-pfcp/ie" +) + +func TestParseMACAddressFields(t *testing.T) { + cases := []struct { + description string + serialized []byte + structured *ie.MACAddressFields + err error + }{ + { + description: "EmptyPayload", + serialized: []byte{}, + structured: nil, + err: io.ErrUnexpectedEOF, + }, + { + description: "TooSmallPayload", + serialized: []byte{1}, + structured: nil, + err: io.ErrUnexpectedEOF, + }, + { + description: "SmallestValidPayload", + serialized: []byte{0, 0}, + structured: &ie.MACAddressFields{}, + }, + { + description: "SourceMACAddress", + serialized: []byte{1, 0, 0, 0, 0, 0, 1}, + structured: &ie.MACAddressFields{ + Flags: 0x1, + SourceMACAddress: net.HardwareAddr{0, 0, 0, 0, 0, 1}, + }, + }, + { + description: "SourceMACAddressTooShort", + serialized: []byte{1, 0}, + structured: nil, + err: io.ErrUnexpectedEOF, + }, + { + description: "DestinationMACAddress", + serialized: []byte{2, 0, 0, 0, 0, 0, 1}, + structured: &ie.MACAddressFields{ + Flags: 0x2, + DestinationMACAddress: net.HardwareAddr{0, 0, 0, 0, 0, 1}, + }, + }, + { + description: "DestinationMACAddressTooShort", + serialized: []byte{2, 0}, + structured: nil, + err: io.ErrUnexpectedEOF, + }, + { + description: "UpperSourceMACAddress", + serialized: []byte{4, 0, 0, 0, 0, 0, 1}, + structured: &ie.MACAddressFields{ + Flags: 0x4, + UpperSourceMACAddress: net.HardwareAddr{0, 0, 0, 0, 0, 1}, + }, + }, + { + description: "UpperSourceMACAddressTooShort", + serialized: []byte{4, 0}, + structured: nil, + err: io.ErrUnexpectedEOF, + }, + { + description: "UpperDestinationMACAddress", + serialized: []byte{8, 0, 0, 0, 0, 0, 1}, + structured: &ie.MACAddressFields{ + Flags: 0x8, + UpperDestinationMACAddress: net.HardwareAddr{0, 0, 0, 0, 0, 1}, + }, + }, + { + description: "UpperDestinationMACAddressTooShort", + serialized: []byte{4, 0}, + structured: nil, + err: io.ErrUnexpectedEOF, + }, + { + description: "AllCombined", + serialized: []byte{15, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 4}, + structured: &ie.MACAddressFields{ + Flags: 0x0f, + SourceMACAddress: net.HardwareAddr{0, 0, 0, 0, 0, 1}, + DestinationMACAddress: net.HardwareAddr{0, 0, 0, 0, 0, 2}, + UpperSourceMACAddress: net.HardwareAddr{0, 0, 0, 0, 0, 3}, + UpperDestinationMACAddress: net.HardwareAddr{0, 0, 0, 0, 0, 4}, + }, + }, + } + for _, c := range cases { + t.Run(c.description, func(t *testing.T) { + got, err := ie.ParseMACAddressFields(c.serialized) + if err != c.err { + t.Errorf("expected error %v but got %v", c.err, err) + t.Fatal(err) + } + + if diff := cmp.Diff(got, c.structured); diff != "" { + t.Error(diff) + } + }) + } +}