From 5edf6e397b1f3ad5fff322bd59364d2a1a992d6d Mon Sep 17 00:00:00 2001 From: insomniac Date: Sun, 6 May 2018 14:32:46 +0200 Subject: Added tests for OptServerId (#59) --- dhcpv6/option_serverid.go | 3 +- dhcpv6/option_serverid_test.go | 67 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 dhcpv6/option_serverid_test.go diff --git a/dhcpv6/option_serverid.go b/dhcpv6/option_serverid.go index 298b095..ffc1b69 100644 --- a/dhcpv6/option_serverid.go +++ b/dhcpv6/option_serverid.go @@ -8,6 +8,7 @@ import ( "fmt" ) +// OptServerId represents a Client ID option type OptServerId struct { Sid Duid } @@ -32,7 +33,7 @@ func (op *OptServerId) String() string { return fmt.Sprintf("OptServerId{sid=%v}", op.Sid.String()) } -// build an OptServerId structure from a sequence of bytes. +// ParseOptServerId builds an OptServerId structure from a sequence of bytes. // The input data does not include option code and length bytes. func ParseOptServerId(data []byte) (*OptServerId, error) { if len(data) < 2 { diff --git a/dhcpv6/option_serverid_test.go b/dhcpv6/option_serverid_test.go new file mode 100644 index 0000000..f420b78 --- /dev/null +++ b/dhcpv6/option_serverid_test.go @@ -0,0 +1,67 @@ +package dhcpv6 + +import ( + "net" + "testing" + + "github.com/insomniacslk/dhcp/iana" + "github.com/stretchr/testify/require" +) + +func TestParseOptServerId(t *testing.T) { + data := []byte{ + 0, 3, // DUID_LL + 0, 1, // hwtype ethernet + 0, 1, 2, 3, 4, 5, // hw addr + } + opt, err := ParseOptServerId(data) + require.NoError(t, err) + require.Equal(t, opt.Sid.Type, DUID_LL) + require.Equal(t, opt.Sid.HwType, iana.HwTypeEthernet) + require.Equal(t, opt.Sid.LinkLayerAddr, net.HardwareAddr([]byte{0, 1, 2, 3, 4, 5})) +} + +func TestOptServerIdToBytes(t *testing.T) { + opt := OptServerId{ + Sid: Duid{ + Type: DUID_LL, + HwType: iana.HwTypeEthernet, + LinkLayerAddr: net.HardwareAddr([]byte{5, 4, 3, 2, 1, 0}), + }, + } + expected := []byte{ + 0, 2, // OPTION_SERVERID + 0, 10, // length + 0, 3, // DUID_LL + 0, 1, // hwtype ethernet + 5, 4, 3, 2, 1, 0, // hw addr + } + require.Equal(t, expected, opt.ToBytes()) +} + +func TestOptServerIdDecodeEncode(t *testing.T) { + data := []byte{ + 0, 3, // DUID_LL + 0, 1, // hwtype ethernet + 5, 4, 3, 2, 1, 0, // hw addr + } + expected := append([]byte{ + 0, 2, // OPTION_SERVERID + 0, 10, // length + }, data...) + opt, err := ParseOptServerId(data) + require.NoError(t, err) + require.Equal(t, expected, opt.ToBytes()) +} + +func TestOptionServerId(t *testing.T) { + opt := OptServerId{ + Sid: Duid{ + Type: DUID_LL, + HwType: iana.HwTypeEthernet, + LinkLayerAddr: net.HardwareAddr([]byte{0xde, 0xad, 0, 0, 0xbe, 0xef}), + }, + } + require.Equal(t, opt.Length(), 10) + require.Equal(t, opt.Code(), OPTION_SERVERID) +} -- cgit v1.2.3