Skip to content

Commit

Permalink
miscoptions: add miscellaneous options file, add Preference type, upd…
Browse files Browse the repository at this point in the history
…ate tests
  • Loading branch information
mdlayher committed Jul 15, 2015
1 parent 4ef7397 commit a62f935
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
29 changes: 29 additions & 0 deletions miscoptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dhcp6

import (
"io"
)

// A Preference is a preference value, as defined in RFC 3315, Section 22.8.
//
// A preference value is sent by a server to a client to affect the selection
// of a server by the client.
type Preference uint8

// MarshalBinary allocates a byte slice containing the data from a Preference.
func (p Preference) MarshalBinary() ([]byte, error) {
return []byte{byte(p)}, nil
}

// UnmarshalBinary unmarshals a raw byte slice into a Preference.
//
// If the byte slice is not exactly 1 byte in length, io.ErrUnexpectedEOF is
// returned.
func (p *Preference) UnmarshalBinary(b []byte) error {
if len(b) != 1 {
return io.ErrUnexpectedEOF
}

*p = Preference(b[0])
return nil
}
15 changes: 4 additions & 11 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ var (
// from OptionOptionRequest, because an odd number of bytes are present.
errInvalidOptionRequest = errors.New("invalid option value for OptionRequestOption")

// errInvalidPreference is returned when a valid integer cannot be parsed
// from OptionPreference, because more or less than one byte are present.
errInvalidPreference = errors.New("invalid option value for OptionPreference")

// errInvalidRapidCommit is returned when OptionRapidCommit contains any
// amount of additional data, since it should be completely empty.
errInvalidRapidCommit = errors.New("invalid option value for OptionRapidCommit")
Expand Down Expand Up @@ -263,18 +259,15 @@ func (o Options) OptionRequest() ([]OptionCode, bool, error) {
// The boolean return value indicates if OptionPreference was present in the
// Options map. The error return value indicates if a valid integer value
// could not be parsed from the option.
func (o Options) Preference() (uint8, bool, error) {
func (o Options) Preference() (Preference, bool, error) {
v, ok := o.Get(OptionPreference)
if !ok {
return 0, false, nil
}

// Length must be exactly 1
if len(v) != 1 {
return 0, false, errInvalidPreference
}

return uint8(v[0]), true, nil
p := new(Preference)
err := p.UnmarshalBinary(v)
return *p, true, err
}

// ElapsedTime returns the Elapsed Time Option value, as described in RFC 3315,
Expand Down
15 changes: 12 additions & 3 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dhcp6
import (
"bytes"
"encoding"
"io"
"net"
"net/url"
"reflect"
Expand Down Expand Up @@ -135,6 +136,14 @@ func TestOptionsAddBinaryMarshaler(t *testing.T) {
}},
},
},
{
desc: "Preference",
code: OptionPreference,
bin: Preference(255),
options: Options{
OptionPreference: [][]byte{{255}},
},
},
{
desc: "StatusCode",
code: OptionStatusCode,
Expand Down Expand Up @@ -801,7 +810,7 @@ func TestOptionsPreference(t *testing.T) {
var tests = []struct {
desc string
options Options
preference uint8
preference Preference
ok bool
err error
}{
Expand All @@ -813,14 +822,14 @@ func TestOptionsPreference(t *testing.T) {
options: Options{
OptionPreference: [][]byte{{}},
},
err: errInvalidPreference,
err: io.ErrUnexpectedEOF,
},
{
desc: "OptionPreference present in Options map, but too long length",
options: Options{
OptionPreference: [][]byte{{0, 1}},
},
err: errInvalidPreference,
err: io.ErrUnexpectedEOF,
},
{
desc: "OptionPreference present in Options map",
Expand Down
2 changes: 1 addition & 1 deletion server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func TestServeOK(t *testing.T) {
r.b.Write(pb)

// Expect these option values set by server
var preference uint8 = 255
var preference Preference = 255
sCode := StatusSuccess
sMsg := "success"

Expand Down

0 comments on commit a62f935

Please sign in to comment.