Skip to content

Commit

Permalink
Merge pull request #17 from jerome-laforge/FixInterfaceId
Browse files Browse the repository at this point in the history
Fix InterfaceID. InterfaceID is just opaque value.
  • Loading branch information
mdlayher committed Oct 6, 2016
2 parents bef566a + affe76b commit a8b0ea6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 50 deletions.
17 changes: 17 additions & 0 deletions miscoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,20 @@ func (r *RelayMessageOption) RelayMessage() (*RelayMessage, error) {

return rm, nil
}

// An InterfaceID is an opaque value of arbitrary length generated
// by the relay agent to identify one of the
// relay agent's interfaces.
type InterfaceID []byte

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

// UnmarshalBinary unmarshals a raw byte slice into a InterfaceID.
func (i *InterfaceID) UnmarshalBinary(b []byte) error {
*i = make([]byte, len(b))
copy(*i, b)
return nil
}
10 changes: 5 additions & 5 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,21 +429,21 @@ func (o Options) VendorOpts() (*VendorOpts, bool, error) {
// InterfaceID returns the Interface-Id Option value, described in RFC 3315,
// Section 22.18.
//
// The Data structure returned contains any raw class data present in
// The InterfaceID structure returned contains any raw class data present in
// the option.
//
// The boolean return value indicates if InterfaceID was present in the
// Options map. The error return value indicates if any errors were present
// in the interface-id data.
func (o Options) InterfaceID() (Data, bool, error) {
func (o Options) InterfaceID() (InterfaceID, bool, error) {
v, ok := o.Get(OptionInterfaceID)
if !ok {
return nil, false, nil
}

var d Data
err := d.UnmarshalBinary(v)
return d, true, err
var i InterfaceID
err := i.UnmarshalBinary(v)
return i, true, err
}

// IAPD returns the Identity Association for Prefix Delegation Option value,
Expand Down
52 changes: 7 additions & 45 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1493,59 +1493,29 @@ func TestInterfaceID(t *testing.T) {
var tests = []struct {
desc string
options Options
interfaceID [][]byte
interfaceID InterfaceID
ok bool
err error
}{
{
desc: "InterfaceID not present in Options map",
},
{
desc: "InterfaceID present in Options map, but empty",
options: Options{
OptionInterfaceID: [][]byte{{}},
},
err: io.ErrUnexpectedEOF,
},
{
desc: "InterfaceID present in Options map, one item, zero length",
options: Options{
OptionInterfaceID: [][]byte{{
0, 0,
}},
},
interfaceID: [][]byte{{}},
ok: true,
},
{
desc: "InterfaceID present in Options map, one item, extra byte",
options: Options{
OptionInterfaceID: [][]byte{{
0, 1, 1, 255,
}},
},
err: io.ErrUnexpectedEOF,
},
{
desc: "InterfaceID present in Options map, one item",
options: Options{
OptionInterfaceID: [][]byte{{
0, 1, 1,
}},
},
interfaceID: [][]byte{{1}},
interfaceID: []byte{0, 1, 1},
ok: true,
},
{
desc: "InterfaceID present in Options map, three items",
desc: "InterfaceID present in Options map with no interface-id data",
options: Options{
OptionInterfaceID: [][]byte{{
0, 1, 1,
0, 2, 2, 2,
0, 3, 3, 3, 3,
}},
OptionInterfaceID: [][]byte{{}},
},
interfaceID: [][]byte{{1}, {2, 2}, {3, 3, 3}},
interfaceID: []byte{},
ok: true,
},
}
Expand All @@ -1561,17 +1531,9 @@ func TestInterfaceID(t *testing.T) {
continue
}

if want, got := len(tt.interfaceID), len(interfaceID); want != got {
t.Fatalf("[%02d] test %q, unexpected interface-id slice length: %v != %v",
if want, got := tt.interfaceID, interfaceID; !bytes.Equal(want, got) {
t.Fatalf("[%02d] test %q, unexpected value for Options.InterfaceID()\n- want: %v\n- got: %v",
i, tt.desc, want, got)

}

for j := range interfaceID {
if want, got := tt.interfaceID[j], interfaceID[j]; !bytes.Equal(want, got) {
t.Fatalf("[%02d:%02d] test %q, unexpected value for Options.InterfaceID()\n- want: %v\n- got: %v",
i, j, tt.desc, want, got)
}
}

if want, got := tt.ok, ok; want != got {
Expand Down

0 comments on commit a8b0ea6

Please sign in to comment.