Skip to content

Commit

Permalink
options{,_test}: add Options.BootFileURL and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlayher committed Jul 10, 2015
1 parent 12e02a9 commit f31d32b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
20 changes: 20 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/binary"
"errors"
"net"
"net/url"
"sort"
"time"
)
Expand Down Expand Up @@ -467,6 +468,25 @@ func (o Options) IAPrefix() ([]*IAPrefix, bool, error) {
return iaprefix, true, nil
}

// BootFileURL returns the Boot File URL Option value, described in RFC 5970,
// Section 3.1.
//
// The URL return value contains a URL which may be used by clients to obtain
// a boot file for PXE.
//
// The boolean return value indicates if OptionBootFileURL was present in the
// Options map. The error return value indicates if a valid boot file URL
// could not be parsed from the option.
func (o Options) BootFileURL() (*url.URL, bool, error) {
v, ok := o.Get(OptionBootFileURL)
if !ok {
return nil, false, nil
}

u, err := url.Parse(string(v))
return u, true, err
}

// parseClasses parses multiple contiguous byte slices contained in
// OptionUserClass or OptionVendorClass, of the form:
// - 2 bytes: length
Expand Down
65 changes: 65 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package dhcp6
import (
"bytes"
"encoding"
"errors"
"net"
"net/url"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -1476,6 +1478,69 @@ func TestOptionsIAPrefix(t *testing.T) {
}
}

// TestOptionsBootFileURL verifies that Options.BootFileURL properly parses
// and returns a URL, if it is available with OptionBootFileURL.
func TestOptionsBootFileURL(t *testing.T) {
var tests = []struct {
description string
options Options
u *url.URL
ok bool
err error
}{
{
description: "OptionBootFileURL not present in Options map",
},
{
description: "OptionBootFileURL present in Options map, but invalid URL",
options: Options{
OptionBootFileURL: [][]byte{[]byte("http://www.%a0.com/foo")},
},
err: &url.Error{
Op: "parse",
URL: "http://www.%a0.com/foo",
Err: errors.New("hexadecimal escape in host"),
},
},
{
description: "OptionBootFileURL present in Options map",
options: Options{
OptionBootFileURL: [][]byte{[]byte("tftp://192.168.1.1:69")},
},
u: &url.URL{
Scheme: "tftp",
Host: "192.168.1.1:69",
},
ok: true,
},
}

for i, tt := range tests {
u, ok, err := tt.options.BootFileURL()
if err != nil {
if want, got := tt.err.Error(), err.Error(); want != got {
t.Fatalf("[%02d] test %q, unexpected error for Options.BootFileURL(): %v != %v",
i, tt.description, want, got)
}

continue
}

if want, got := tt.ok, ok; want != got {
t.Fatalf("[%02d] test %q, unexpected ok for Options.BootFileURL(): %v != %v",
i, tt.description, want, got)
}
if !ok {
continue
}

if want, got := tt.u.String(), u.String(); want != got {
t.Fatalf("[%02d] test %q, unexpected value for Options.BootFileURL(): %v != %v",
i, tt.description, want, got)
}
}
}

// TestOptions_enumerate verifies that Options.enumerate correctly enumerates
// and sorts an Options map into key/value option pairs.
func TestOptions_enumerate(t *testing.T) {
Expand Down

0 comments on commit f31d32b

Please sign in to comment.