diff options
author | Pablo Mazzini <pmazzini@gmail.com> | 2018-04-06 21:46:03 +0100 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2018-04-06 21:46:03 +0100 |
commit | 4f3c3e7e1776c130121a89414ec1034908aad967 (patch) | |
tree | 7e38f9ceb01146494abcb7b56c815edcad0def63 | |
parent | f6a343e8c71a7dfe1f6a64d22cbd312db063d3f7 (diff) |
fix OptInterfaceId (#29)
-rw-r--r-- | dhcpv6/option_interfaceid.go | 6 | ||||
-rw-r--r-- | dhcpv6/option_interfaceid_test.go | 33 |
2 files changed, 36 insertions, 3 deletions
diff --git a/dhcpv6/option_interfaceid.go b/dhcpv6/option_interfaceid.go index 4ba83f5..2634d9f 100644 --- a/dhcpv6/option_interfaceid.go +++ b/dhcpv6/option_interfaceid.go @@ -19,7 +19,7 @@ func (op *OptInterfaceId) Code() OptionCode { func (op *OptInterfaceId) ToBytes() []byte { buf := make([]byte, 4) binary.BigEndian.PutUint16(buf[0:2], uint16(OPTION_INTERFACE_ID)) - binary.BigEndian.PutUint16(buf[2:4], 2) + binary.BigEndian.PutUint16(buf[2:4], uint16(len(op.interfaceId))) buf = append(buf, op.interfaceId...) return buf } @@ -33,7 +33,7 @@ func (op *OptInterfaceId) SetInterfaceID(interfaceId []byte) { } func (op *OptInterfaceId) Length() int { - return 4 + len(op.interfaceId) + return len(op.interfaceId) } func (op *OptInterfaceId) String() string { @@ -44,6 +44,6 @@ func (op *OptInterfaceId) String() string { // The input data does not include option code and length bytes. func ParseOptInterfaceId(data []byte) (*OptInterfaceId, error) { opt := OptInterfaceId{} - opt.interfaceId = append([]byte(nil), data[4:]...) + opt.interfaceId = append([]byte(nil), data...) return &opt, nil } diff --git a/dhcpv6/option_interfaceid_test.go b/dhcpv6/option_interfaceid_test.go new file mode 100644 index 0000000..21de822 --- /dev/null +++ b/dhcpv6/option_interfaceid_test.go @@ -0,0 +1,33 @@ +package dhcpv6 + +import ( + "bytes" + "testing" +) + +func TestOptInterfaceId(t *testing.T) { + expected := []byte("DSLAM01 eth2/1/01/21") + opt, err := ParseOptInterfaceId(expected) + if err != nil { + t.Fatal(err) + } + if optLen := opt.Length(); optLen != len(expected) { + t.Fatalf("Invalid length. Expected %v, got %v", len(expected), optLen) + } + if url := opt.InterfaceID(); !bytes.Equal(url, expected) { + t.Fatalf("Invalid Interface ID. Expected %v, got %v", expected, url) + } +} + +func TestOptInterfaceIdToBytes(t *testing.T) { + interfaceId := []byte("DSLAM01 eth2/1/01/21") + expected := []byte{00, 18, 00, byte(len(interfaceId))} + expected = append(expected, interfaceId...) + opt := OptInterfaceId{ + interfaceId: interfaceId, + } + toBytes := opt.ToBytes() + if !bytes.Equal(toBytes, expected) { + t.Fatalf("Invalid ToBytes result. Expected %v, got %v", expected, toBytes) + } +} |