diff options
author | Pablo Mazzini <pmazzini@gmail.com> | 2018-04-08 14:48:29 +0100 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2018-04-08 14:48:29 +0100 |
commit | b3906a775aa5fe1fd026c9bfe8600f70a15f6877 (patch) | |
tree | 751d99e3c1e5edaa7902f7709d1475f7a8b79056 | |
parent | 4f3c3e7e1776c130121a89414ec1034908aad967 (diff) |
fix OptRemoteId (#30)
-rw-r--r-- | dhcpv6/option_remoteid.go | 2 | ||||
-rw-r--r-- | dhcpv6/option_remoteid_test.go | 38 |
2 files changed, 39 insertions, 1 deletions
diff --git a/dhcpv6/option_remoteid.go b/dhcpv6/option_remoteid.go index 5d1b6db..210363d 100644 --- a/dhcpv6/option_remoteid.go +++ b/dhcpv6/option_remoteid.go @@ -20,7 +20,7 @@ func (op *OptRemoteId) Code() OptionCode { func (op *OptRemoteId) ToBytes() []byte { buf := make([]byte, 8) binary.BigEndian.PutUint16(buf[0:2], uint16(OPTION_REMOTE_ID)) - binary.BigEndian.PutUint16(buf[2:4], 2) + binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length())) binary.BigEndian.PutUint32(buf[4:8], uint32(op.enterpriseNumber)) buf = append(buf, op.remoteId...) return buf diff --git a/dhcpv6/option_remoteid_test.go b/dhcpv6/option_remoteid_test.go new file mode 100644 index 0000000..b2ef3cc --- /dev/null +++ b/dhcpv6/option_remoteid_test.go @@ -0,0 +1,38 @@ +package dhcpv6 + +import ( + "bytes" + "testing" +) + +func TestOptRemoteId(t *testing.T) { + expected := []byte{0xaa, 0xbb, 0xcc, 0xdd} + remoteId := []byte("DSLAM01 eth2/1/01/21") + expected = append(expected, remoteId...) + opt, err := ParseOptRemoteId(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 en := opt.EnterpriseNumber(); en != 0xaabbccdd { + t.Fatalf("Invalid Enterprise Number. Expected 0xaabbccdd, got %v", en) + } + if rid := opt.RemoteID(); !bytes.Equal(rid, remoteId) { + t.Fatalf("Invalid Remote ID. Expected %v, got %v", expected, rid) + } +} + +func TestOptRemoteIdToBytes(t *testing.T) { + remoteId := []byte("DSLAM01 eth2/1/01/21") + expected := []byte{00, 37, 00, byte(len(remoteId) + 4), 00, 00, 00, 00} + expected = append(expected, remoteId...) + opt := OptRemoteId{ + remoteId: remoteId, + } + toBytes := opt.ToBytes() + if !bytes.Equal(toBytes, expected) { + t.Fatalf("Invalid ToBytes result. Expected %v, got %v", expected, toBytes) + } +} |