Skip to content

Commit

Permalink
fix ParseMACAddressFields and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mercimat committed Nov 22, 2023
1 parent d5a9c1f commit 0d634cb
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ie/mac-address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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])
}

Expand Down
117 changes: 117 additions & 0 deletions ie/mac_address_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 0d634cb

Please sign in to comment.