From 82b6d0f716f1be6b1ce48c8e3039c90c688e4f4e Mon Sep 17 00:00:00 2001 From: Andrea Barberio Date: Fri, 22 Dec 2017 17:51:01 +0000 Subject: Fixed bootfile URL code and added tests --- dhcpv6/option_bootfileurl.go | 2 +- dhcpv6/option_bootfileurl_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 dhcpv6/option_bootfileurl_test.go diff --git a/dhcpv6/option_bootfileurl.go b/dhcpv6/option_bootfileurl.go index 4dd0af3..581ddd8 100644 --- a/dhcpv6/option_bootfileurl.go +++ b/dhcpv6/option_bootfileurl.go @@ -19,7 +19,7 @@ func (op *OptBootFileURL) Code() OptionCode { func (op *OptBootFileURL) ToBytes() []byte { buf := make([]byte, 4) binary.BigEndian.PutUint16(buf[0:2], uint16(OPT_BOOTFILE_URL)) - binary.BigEndian.PutUint16(buf[2:4], 2) + binary.BigEndian.PutUint16(buf[2:4], uint16(len(op.bootFileUrl))) buf = append(buf, op.bootFileUrl...) return buf } diff --git a/dhcpv6/option_bootfileurl_test.go b/dhcpv6/option_bootfileurl_test.go new file mode 100644 index 0000000..b42fd7c --- /dev/null +++ b/dhcpv6/option_bootfileurl_test.go @@ -0,0 +1,33 @@ +package dhcpv6 + +import ( + "bytes" + "testing" +) + +func TestOptBootFileURL(t *testing.T) { + expected := []byte("https://insomniac.slackware.it") + opt, err := ParseOptBootFileURL(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.BootFileURL(); !bytes.Equal(url, expected) { + t.Fatalf("Invalid boot file URL. Expected %v, got %v", expected, url) + } +} + +func TestOptBootFileURLToBytes(t *testing.T) { + urlString := []byte("https://insomniac.slackware.it") + expected := []byte{00, 59, 00, byte(len(urlString))} + expected = append(expected, urlString...) + opt := OptBootFileURL{ + bootFileUrl: urlString, + } + toBytes := opt.ToBytes() + if !bytes.Equal(toBytes, expected) { + t.Fatalf("Invalid ToBytes result. Expected %v, got %v", expected, toBytes) + } +} -- cgit v1.2.3