From f160f8e781bff0abab609b357d38555d85fa2d55 Mon Sep 17 00:00:00 2001 From: Chris Koch Date: Sat, 25 Feb 2023 23:48:22 -0800 Subject: ClientLinkLayerAddress: tests for FromBytes, ToBytes, and Getter Signed-off-by: Chris Koch --- dhcpv6/option_clientlinklayeraddress_test.go | 77 ++++++++++++++++++---------- 1 file changed, 51 insertions(+), 26 deletions(-) diff --git a/dhcpv6/option_clientlinklayeraddress_test.go b/dhcpv6/option_clientlinklayeraddress_test.go index 1ef40c5..bd603e7 100644 --- a/dhcpv6/option_clientlinklayeraddress_test.go +++ b/dhcpv6/option_clientlinklayeraddress_test.go @@ -2,40 +2,65 @@ package dhcpv6 import ( "bytes" + "errors" + "fmt" "net" "testing" + "github.com/google/go-cmp/cmp" "github.com/insomniacslk/dhcp/iana" "github.com/stretchr/testify/require" + "github.com/u-root/uio/uio" ) -func TestParseOptClientLinkLayerAddress(t *testing.T) { - data := []byte{ - 0, 1, // LinkLayerType - 164, 131, 231, 227, 223, 136, - } - var opt optClientLinkLayerAddress - err := opt.FromBytes(data) +func TestClientLinkLayerAddressParseAndGetter(t *testing.T) { + for i, tt := range []struct { + buf []byte + err error + wantHWType iana.HWType + wantHWAddr net.HardwareAddr + }{ + { + buf: []byte{ + 0, 79, // Client Link Layer Address option + 0, 8, // length + 0, 1, // Ethernet + 1, 2, 3, 4, 5, 6, + }, + wantHWType: iana.HWTypeEthernet, + wantHWAddr: net.HardwareAddr{1, 2, 3, 4, 5, 6}, + }, + { + buf: []byte{0, 79, 0, 1, 0}, + err: uio.ErrBufferTooShort, + }, + { + buf: []byte{0, 79, 0}, + err: uio.ErrUnreadBytes, + }, + } { + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { + var ro RelayOptions + if err := ro.FromBytes(tt.buf); !errors.Is(err, tt.err) { + t.Errorf("FromBytes = %v, want %v", err, tt.err) + } + if gotHWType, gotHWAddr := ro.ClientLinkLayerAddress(); gotHWType != tt.wantHWType || !bytes.Equal(gotHWAddr, tt.wantHWAddr) { + t.Errorf("ClientLinkLayerAddress = (%s, %v), want (%s, %v)", gotHWType, tt.wantHWType, gotHWAddr, tt.wantHWAddr) + } - require.NoError(t, err) - require.Equal(t, OptionClientLinkLayerAddr, opt.Code()) - require.Equal(t, iana.HWTypeEthernet, opt.LinkLayerType) - require.Equal(t, net.HardwareAddr(data[2:]), opt.LinkLayerAddress) - require.Equal(t, "Client Link-Layer Address: Type=Ethernet LinkLayerAddress=a4:83:e7:e3:df:88", opt.String()) + if tt.err == nil { + var m MessageOptions + m.Add(OptClientLinkLayerAddress(tt.wantHWType, tt.wantHWAddr)) + got := m.ToBytes() + if diff := cmp.Diff(tt.buf, got); diff != "" { + t.Errorf("ToBytes mismatch (-want, +got): %s", diff) + } + } + }) + } } -func TestOptClientLinkLayerAddressToBytes(t *testing.T) { - mac, _ := net.ParseMAC("a4:83:e7:e3:df:88") - opt := optClientLinkLayerAddress{ - LinkLayerType: iana.HWTypeEthernet, - LinkLayerAddress: mac, - } - want := []byte{ - 0, 1, // LinkLayerType - 164, 131, 231, 227, 223, 136, - } - b := opt.ToBytes() - if !bytes.Equal(b, want) { - t.Fatalf("opt.ToBytes()=%v, want %v", b, want) - } +func TestOptClientLinkLayerAddressString(t *testing.T) { + opt := OptClientLinkLayerAddress(iana.HWTypeEthernet, net.HardwareAddr{0xa4, 0x83, 0xe7, 0xe3, 0xdf, 0x88}) + require.Equal(t, "Client Link-Layer Address: Type=Ethernet LinkLayerAddress=a4:83:e7:e3:df:88", opt.String()) } -- cgit v1.2.3