diff options
author | insomniac <insomniacslk@users.noreply.github.com> | 2018-05-06 14:01:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-06 14:01:59 +0200 |
commit | 10ecb5882ae87357b7e5495d63726c5ad11f53cf (patch) | |
tree | 7cae40164c72ae4e45d72bdca7528f55f50d4d3c | |
parent | f39ab2956da14cd15d0f64391758bcd273c78175 (diff) |
Added tests for OptClientId and using net.HardwareAddr for DUID (#58)
-rw-r--r-- | dhcpv6/duid.go | 3 | ||||
-rw-r--r-- | dhcpv6/option_clientid.go | 6 | ||||
-rw-r--r-- | dhcpv6/option_clientid_test.go | 67 |
3 files changed, 73 insertions, 3 deletions
diff --git a/dhcpv6/duid.go b/dhcpv6/duid.go index 8572349..22529c2 100644 --- a/dhcpv6/duid.go +++ b/dhcpv6/duid.go @@ -3,6 +3,7 @@ package dhcpv6 import ( "encoding/binary" "fmt" + "net" "github.com/insomniacslk/dhcp/iana" ) @@ -28,7 +29,7 @@ type Duid struct { Type DuidType HwType iana.HwTypeType // for DUID-LLT and DUID-LL. Ignored otherwise. RFC 826 Time uint32 // for DUID-LLT. Ignored otherwise - LinkLayerAddr []byte + LinkLayerAddr net.HardwareAddr EnterpriseNumber uint32 // for DUID-EN. Ignored otherwise EnterpriseIdentifier []byte // for DUID-EN. Ignored otherwise Uuid []byte // for DUID-UUID. Ignored otherwise diff --git a/dhcpv6/option_clientid.go b/dhcpv6/option_clientid.go index 2e24abb..3c01d8c 100644 --- a/dhcpv6/option_clientid.go +++ b/dhcpv6/option_clientid.go @@ -8,6 +8,7 @@ import ( "fmt" ) +// OptClientId represents a Client ID option type OptClientId struct { Cid Duid } @@ -32,8 +33,9 @@ func (op *OptClientId) String() string { return fmt.Sprintf("OptClientId{cid=%v}", op.Cid.String()) } -// build an OptClientId structure from a sequence of bytes. -// The input data does not include option code and length bytes. +// ParseOptClientId builds an OptClientId structure from a sequence +// of bytes. The input data does not include option code and length +// bytes. func ParseOptClientId(data []byte) (*OptClientId, error) { if len(data) < 2 { // at least the DUID type is necessary to continue diff --git a/dhcpv6/option_clientid_test.go b/dhcpv6/option_clientid_test.go new file mode 100644 index 0000000..dcaffca --- /dev/null +++ b/dhcpv6/option_clientid_test.go @@ -0,0 +1,67 @@ +package dhcpv6 + +import ( + "net" + "testing" + + "github.com/insomniacslk/dhcp/iana" + "github.com/stretchr/testify/require" +) + +func TestParseOptClientId(t *testing.T) { + data := []byte{ + 0, 3, // DUID_LL + 0, 1, // hwtype ethernet + 0, 1, 2, 3, 4, 5, // hw addr + } + opt, err := ParseOptClientId(data) + require.NoError(t, err) + require.Equal(t, opt.Cid.Type, DUID_LL) + require.Equal(t, opt.Cid.HwType, iana.HwTypeEthernet) + require.Equal(t, opt.Cid.LinkLayerAddr, net.HardwareAddr([]byte{0, 1, 2, 3, 4, 5})) +} + +func TestOptClientIdToBytes(t *testing.T) { + opt := OptClientId{ + Cid: Duid{ + Type: DUID_LL, + HwType: iana.HwTypeEthernet, + LinkLayerAddr: net.HardwareAddr([]byte{5, 4, 3, 2, 1, 0}), + }, + } + expected := []byte{ + 0, 1, // OPTION_CLIENTID + 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 TestOptClientIdDecodeEncode(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, 1, // OPTION_CLIENTID + 0, 10, // length + }, data...) + opt, err := ParseOptClientId(data) + require.NoError(t, err) + require.Equal(t, expected, opt.ToBytes()) +} + +func TestOptionClientId(t *testing.T) { + opt := OptClientId{ + Cid: 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_CLIENTID) +} |