Skip to content

Commit

Permalink
Merge pull request #12 from jerome-laforge/master
Browse files Browse the repository at this point in the history
Add more helper functions for RelayMessageOption.
  • Loading branch information
mdlayher committed Oct 6, 2016
2 parents 88ba6cf + 8e5c5a0 commit 74e4e92
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
24 changes: 24 additions & 0 deletions miscoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,27 @@ func (r *RelayMessageOption) SetRelayMessage(p *RelayMessage) error {
*r = b
return nil
}

// ClientServerMessage gets the client server message (e.g. Solicit,
// Advertise ...) into this option (when hopcount = 0 of outer RelayMessage).
func (r *RelayMessageOption) ClientServerMessage() (*Packet, error) {
p := new(Packet)
err := p.UnmarshalBinary(*r)
if err != nil {
return nil, err
}

return p, nil
}

// RelayMessage gets the relay message (e.g. Relay Forward, Relay Reply) into
// this option (when hopcount > 0 of outer RelayMessage).
func (r *RelayMessageOption) RelayMessage() (*RelayMessage, error) {
rm := new(RelayMessage)
err := rm.UnmarshalBinary(*r)
if err != nil {
return nil, err
}

return rm, nil
}
20 changes: 20 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,26 @@ func (o Options) ElapsedTime() (ElapsedTime, bool, error) {
return *t, true, err
}

// RelayMessageOption returns the Relay Message Option value, as described in RFC 3315,
// Section 22.10.
//
// The RelayMessage option carries a DHCP message in a Relay-forward or
// Relay-reply message.
//
// The boolean return value indicates if OptionRelayMsg was present in the
// Options map. The error return value indicates if a valid OptionRelayMsg could be
// parsed from the option.
func (o Options) RelayMessageOption() (RelayMessageOption, bool, error) {
v, ok := o.Get(OptionRelayMsg)
if !ok {
return nil, false, nil
}

r := new(RelayMessageOption)
err := r.UnmarshalBinary(v)
return *r, true, err
}

// Authentication returns the Authentication Option value, as described in RFC 3315,
// Section 22.11.
//
Expand Down
43 changes: 43 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,49 @@ func TestOptionsElapsedTime(t *testing.T) {
}
}

// TestOptionsRelayMessage verifies that Options.RelayMessageOption properly parses and
// returns an relay message option value, if one is available with RelayMessageOption.
func TestOptionsRelayMessage(t *testing.T) {
var tests = []struct {
desc string
options Options
authentication RelayMessageOption
ok bool
err error
}{
{
desc: "RelayMessageOption not present in Options map",
},
{
desc: "RelayMessageOption present in Options map",
options: Options{
OptionRelayMsg: [][]byte{{1, 1, 2, 3}},
},
authentication: []byte{1, 1, 2, 3},
ok: true,
},
}

for i, tt := range tests {
relayMsg, ok, err := tt.options.RelayMessageOption()
if want, got := tt.err, err; want != got {
t.Fatalf("[%02d] test %q, unexpected error for Options.RelayMessageOption\n- want: %v\n- got: %v", i, tt.desc, want, got)
}

if tt.err != nil {
continue
}

if want, got := tt.authentication, relayMsg; !reflect.DeepEqual(want, got) {
t.Fatalf("[%02d] test %q, unexpected value for Options.RelayMessageOption()\n- want: %v\n- got: %v", i, tt.desc, want, got)
}

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

// TestAuthentication verifies that Options.Authentication properly parses and
// returns an authentication value, if one is available with Authentication.
func TestAuthentication(t *testing.T) {
Expand Down

0 comments on commit 74e4e92

Please sign in to comment.