diff options
author | Sean Karlage <skarlage@fb.com> | 2018-10-01 09:21:23 -0700 |
---|---|---|
committer | Sean Karlage <skarlage@fb.com> | 2018-10-01 09:21:23 -0700 |
commit | fafee2841d9aa4218f5de9506d45b1255ded393b (patch) | |
tree | 76587965376d7d32e2665ed24b82c158573a79db /dhcpv4/bsdp/bsdp_test.go | |
parent | 884667ec282b43650c4d3a50530e2753f3919448 (diff) |
BSDP: Add MessageTypeFromPacket helper
This is a helper function to extract the BSDP message type from a given
`dhcpv4.DHCPv4` packet structure. It's useful in helping to identify
what kind of BSDP packet you're dealing with (since the distinction is
only made in one of the vendor-specific options encapsulated in Option
43).
Diffstat (limited to 'dhcpv4/bsdp/bsdp_test.go')
-rw-r--r-- | dhcpv4/bsdp/bsdp_test.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/dhcpv4/bsdp/bsdp_test.go b/dhcpv4/bsdp/bsdp_test.go index 7da4dfa..c9c868a 100644 --- a/dhcpv4/bsdp/bsdp_test.go +++ b/dhcpv4/bsdp/bsdp_test.go @@ -371,3 +371,72 @@ func TestNewReplyForInformSelect(t *testing.T) { RequireHasOption(t, vendorOpts, &OptMessageType{Type: MessageTypeSelect}) RequireHasOption(t, vendorOpts, &OptSelectedBootImageID{ID: images[0].ID}) } + +func TestMessageTypeForPacket(t *testing.T) { + var ( + pkt *dhcpv4.DHCPv4 + gotMessageType MessageType + gotOK bool + ) + + testcases := []struct { + tcName string + opts []dhcpv4.Option + wantOK bool + wantMessageType MessageType + }{ + { + tcName: "No options", + opts: []dhcpv4.Option{}, + wantOK: false, + }, + { + tcName: "Some options, no vendor opts", + opts: []dhcpv4.Option{ + &dhcpv4.OptHostName{HostName: "foobar1234"}, + }, + wantOK: false, + }, + { + tcName: "Vendor opts, no message type", + opts: []dhcpv4.Option{ + &dhcpv4.OptHostName{HostName: "foobar1234"}, + &OptVendorSpecificInformation{ + Options: []dhcpv4.Option{ + &OptVersion{Version: Version1_1}, + }, + }, + }, + wantOK: false, + }, + { + tcName: "Vendor opts, with message type", + opts: []dhcpv4.Option{ + &dhcpv4.OptHostName{HostName: "foobar1234"}, + &OptVendorSpecificInformation{ + Options: []dhcpv4.Option{ + &OptVersion{Version: Version1_1}, + &OptMessageType{Type: MessageTypeList}, + }, + }, + }, + wantOK: true, + wantMessageType: MessageTypeList, + }, + } + for _, tt := range testcases { + t.Run(tt.tcName, func(t *testing.T) { + pkt, _ = dhcpv4.New() + for _, opt := range tt.opts { + pkt.AddOption(opt) + } + gotMessageType, gotOK = MessageTypeFromPacket(pkt) + if tt.wantOK { + require.True(t, gotOK) + require.Equal(t, tt.wantMessageType, gotMessageType) + } else { + require.False(t, gotOK) + } + }) + } +} |