diff options
author | Andrea Barberio <insomniac@slackware.it> | 2017-12-22 17:51:01 +0000 |
---|---|---|
committer | Andrea Barberio <insomniac@slackware.it> | 2017-12-22 17:51:01 +0000 |
commit | 82b6d0f716f1be6b1ce48c8e3039c90c688e4f4e (patch) | |
tree | 9520c2bcdd852a93c0f28c6969ca5c58701c1533 | |
parent | b4b606fb5c667a16bd60a5ab36244b6bd717f0a3 (diff) |
Fixed bootfile URL code and added tests
-rw-r--r-- | dhcpv6/option_bootfileurl.go | 2 | ||||
-rw-r--r-- | dhcpv6/option_bootfileurl_test.go | 33 |
2 files changed, 34 insertions, 1 deletions
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) + } +} |