diff options
author | Pablo Mazzini <pmazzini@gmail.com> | 2018-04-08 23:46:07 +0100 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2018-04-09 00:46:07 +0200 |
commit | 05d4c67cbd22d98846d71524be2c9f9e18ea38fa (patch) | |
tree | 46cf88a4440d3be98d0803783f8dee5fa9779909 /dhcpv6 | |
parent | b3906a775aa5fe1fd026c9bfe8600f70a15f6877 (diff) |
fix OptIAPrefix (#31)
Diffstat (limited to 'dhcpv6')
-rw-r--r-- | dhcpv6/option_iaprefix.go | 8 | ||||
-rw-r--r-- | dhcpv6/option_iaprefix_test.go | 52 |
2 files changed, 56 insertions, 4 deletions
diff --git a/dhcpv6/option_iaprefix.go b/dhcpv6/option_iaprefix.go index b4adacb..57810b1 100644 --- a/dhcpv6/option_iaprefix.go +++ b/dhcpv6/option_iaprefix.go @@ -22,7 +22,7 @@ func (op *OptIAPrefix) Code() OptionCode { } func (op *OptIAPrefix) ToBytes() []byte { - buf := make([]byte, 25) + buf := make([]byte, 12) binary.BigEndian.PutUint16(buf[0:2], uint16(OPTION_IAPREFIX)) binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length())) binary.BigEndian.PutUint32(buf[4:8], op.preferredLifetime) @@ -91,8 +91,8 @@ func ParseOptIAPrefix(data []byte) (*OptIAPrefix, error) { } opt.preferredLifetime = binary.BigEndian.Uint32(data[:4]) opt.validLifetime = binary.BigEndian.Uint32(data[4:8]) - opt.prefixLength = data[9] - copy(opt.ipv6Prefix[:], data[9:17]) - copy(opt.options, data[17:]) + opt.prefixLength = data[8] + copy(opt.ipv6Prefix[:], data[9:25]) + copy(opt.options, data[25:]) return &opt, nil } diff --git a/dhcpv6/option_iaprefix_test.go b/dhcpv6/option_iaprefix_test.go new file mode 100644 index 0000000..caabc75 --- /dev/null +++ b/dhcpv6/option_iaprefix_test.go @@ -0,0 +1,52 @@ +package dhcpv6 + +import ( + "bytes" + "net" + "testing" +) + +func TestOptIAPrefix(t *testing.T) { + buf := []byte{ + 0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime + 0xee, 0xff, 0x00, 0x11, // validLifetime + 36, // prefixLength + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // ipv6Prefix + } + opt, err := ParseOptIAPrefix(buf) + if err != nil { + t.Fatal(err) + } + if pl := opt.PreferredLifetime(); pl != 0xaabbccdd { + t.Fatalf("Invalid Preferred Lifetime. Expected 0xaabbccdd, got %v", pl) + } + if vl := opt.ValidLifetime(); vl != 0xeeff0011 { + t.Fatalf("Invalid Valid Lifetime. Expected 0xeeff0011, got %v", vl) + } + if pr := opt.PrefixLength(); pr != 36 { + t.Fatalf("Invalid Prefix Length. Expected 36, got %v", pr) + } + if ip := opt.IPv6Prefix(); !bytes.Equal(ip, net.IPv6loopback) { + t.Fatalf("Invalid Prefix Length. Expected %v, got %v", net.IPv6loopback, ip) + } +} + +func TestOptIAPrefixToBytes(t *testing.T) { + buf := []byte{ + 0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime + 0xee, 0xff, 0x00, 0x11, // validLifetime + 36, // prefixLength + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ipv6Prefix + } + expected := []byte{00, 26, 00, byte(len(buf))} + expected = append(expected, buf...) + opt := OptIAPrefix{ + preferredLifetime: 0xaabbccdd, + validLifetime: 0xeeff0011, + prefixLength: 36, + } + toBytes := opt.ToBytes() + if !bytes.Equal(toBytes, expected) { + t.Fatalf("Invalid ToBytes result. Expected %v, got %v", expected, toBytes) + } +} |