summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/bsdp
diff options
context:
space:
mode:
authorSean Karlage <skarlage@fb.com>2018-07-29 11:09:58 -0700
committerSean Karlage <skarlage@fb.com>2018-07-29 14:18:44 -0700
commit2b05c7d03724d31529886d98f738499ac06ead7e (patch)
tree87fa5a1e94f981aa93d6783ac0d1a0ce4bf997eb /dhcpv4/bsdp
parent76761c89adf7dfd77abc1b601f3a6bac56724686 (diff)
Add some helper tools for tests
Adds new `OptionGetter` interface that helps when asserting that certain packets/vendor-specific opts contain specific options.
Diffstat (limited to 'dhcpv4/bsdp')
-rw-r--r--dhcpv4/bsdp/bsdp.go2
-rw-r--r--dhcpv4/bsdp/bsdp_test.go53
-rw-r--r--dhcpv4/bsdp/option_vendor_specific_information.go6
-rw-r--r--dhcpv4/bsdp/option_vendor_specific_information_test.go6
4 files changed, 26 insertions, 41 deletions
diff --git a/dhcpv4/bsdp/bsdp.go b/dhcpv4/bsdp/bsdp.go
index 22360ef..42edf7f 100644
--- a/dhcpv4/bsdp/bsdp.go
+++ b/dhcpv4/bsdp/bsdp.go
@@ -26,7 +26,7 @@ func ParseBootImageListFromAck(ack dhcpv4.DHCPv4) ([]BootImage, error) {
if err != nil {
return nil, err
}
- bootImageOpts := vendorOpt.GetOptions(OptionBootImageList)
+ bootImageOpts := vendorOpt.GetOption(OptionBootImageList)
for _, opt := range bootImageOpts {
images = append(images, opt.(*OptBootImageList).Images...)
}
diff --git a/dhcpv4/bsdp/bsdp_test.go b/dhcpv4/bsdp/bsdp_test.go
index da82d43..4cc55a6 100644
--- a/dhcpv4/bsdp/bsdp_test.go
+++ b/dhcpv4/bsdp/bsdp_test.go
@@ -9,21 +9,6 @@ import (
"github.com/stretchr/testify/require"
)
-func RequireEqualIPAddr(t *testing.T, a, b net.IP, msg ...interface{}) {
- if !net.IP.Equal(a, b) {
- t.Fatalf("Invalid %s. %v != %v", msg, a, b)
- }
-}
-
-func RequireHasOption(t *testing.T, opts []dhcpv4.Option, opcode dhcpv4.OptionCode) {
- for _, opt := range opts {
- if opt.Code() == opcode {
- return
- }
- }
- require.FailNow(t, "option not present in opts", dhcpv4.OptionCodeToString[opcode])
-}
-
func TestParseBootImageListFromAck(t *testing.T) {
expectedBootImages := []BootImage{
BootImage{
@@ -73,16 +58,16 @@ func TestNewInformList_NoReplyPort(t *testing.T) {
m, err := NewInformList(hwAddr, localIP, 0)
require.NoError(t, err)
- RequireHasOption(t, m.Options(), dhcpv4.OptionVendorSpecificInformation)
- RequireHasOption(t, m.Options(), dhcpv4.OptionParameterRequestList)
- RequireHasOption(t, m.Options(), dhcpv4.OptionMaximumDHCPMessageSize)
- RequireHasOption(t, m.Options(), dhcpv4.OptionEnd)
+ require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionVendorSpecificInformation))
+ require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionParameterRequestList))
+ require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionMaximumDHCPMessageSize))
+ require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionEnd))
opt := m.GetOneOption(dhcpv4.OptionVendorSpecificInformation)
require.NotNil(t, opt, "vendor opts not present")
vendorInfo := opt.(*OptVendorSpecificInformation)
- RequireHasOption(t, vendorInfo.Options, OptionMessageType)
- RequireHasOption(t, vendorInfo.Options, OptionVersion)
+ require.True(t, dhcpv4.HasOption(vendorInfo, OptionMessageType))
+ require.True(t, dhcpv4.HasOption(vendorInfo, OptionVersion))
opt = vendorInfo.GetOneOption(OptionMessageType)
require.Equal(t, MessageTypeList, opt.(*OptMessageType).Type)
@@ -104,7 +89,7 @@ func TestNewInformList_ReplyPort(t *testing.T) {
opt := m.GetOneOption(dhcpv4.OptionVendorSpecificInformation)
vendorInfo := opt.(*OptVendorSpecificInformation)
- RequireHasOption(t, vendorInfo.Options, OptionReplyPort)
+ require.True(t, dhcpv4.HasOption(vendorInfo, OptionReplyPort))
opt = vendorInfo.GetOneOption(OptionReplyPort)
require.Equal(t, replyPort, opt.(*OptReplyPort).Port)
@@ -146,27 +131,27 @@ func TestInformSelectForAck_Broadcast(t *testing.T) {
require.True(t, m.IsBroadcast())
// Validate options.
- RequireHasOption(t, m.Options(), dhcpv4.OptionClassIdentifier)
- RequireHasOption(t, m.Options(), dhcpv4.OptionParameterRequestList)
- RequireHasOption(t, m.Options(), dhcpv4.OptionDHCPMessageType)
+ require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionClassIdentifier))
+ require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionParameterRequestList))
+ require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionDHCPMessageType))
opt := m.GetOneOption(dhcpv4.OptionDHCPMessageType)
require.Equal(t, dhcpv4.MessageTypeInform, opt.(*dhcpv4.OptMessageType).MessageType)
- RequireHasOption(t, m.Options(), dhcpv4.OptionEnd)
+ require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionEnd))
// Validate vendor opts.
- RequireHasOption(t, m.Options(), dhcpv4.OptionVendorSpecificInformation)
+ require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionVendorSpecificInformation))
opt = m.GetOneOption(dhcpv4.OptionVendorSpecificInformation)
vendorInfo := opt.(*OptVendorSpecificInformation)
- RequireHasOption(t, vendorInfo.Options, OptionMessageType)
+ require.True(t, dhcpv4.HasOption(vendorInfo, OptionMessageType))
opt = vendorInfo.GetOneOption(OptionMessageType)
require.Equal(t, MessageTypeSelect, opt.(*OptMessageType).Type)
- RequireHasOption(t, vendorInfo.Options, OptionVersion)
- RequireHasOption(t, vendorInfo.Options, OptionSelectedBootImageID)
+ require.True(t, dhcpv4.HasOption(vendorInfo, OptionVersion))
+ require.True(t, dhcpv4.HasOption(vendorInfo, OptionSelectedBootImageID))
opt = vendorInfo.GetOneOption(OptionSelectedBootImageID)
require.Equal(t, bootImage.ID, opt.(*OptSelectedBootImageID).ID)
- RequireHasOption(t, vendorInfo.Options, OptionServerIdentifier)
+ require.True(t, dhcpv4.HasOption(vendorInfo, OptionServerIdentifier))
opt = vendorInfo.GetOneOption(OptionServerIdentifier)
- RequireEqualIPAddr(t, serverID, opt.(*OptServerIdentifier).ServerID)
+ require.True(t, serverID.Equal(opt.(*OptServerIdentifier).ServerID))
}
func TestInformSelectForAck_NoServerID(t *testing.T) {
@@ -226,10 +211,10 @@ func TestInformSelectForAck_ReplyPort(t *testing.T) {
m, err := InformSelectForAck(*ack, replyPort, bootImage)
require.NoError(t, err)
- RequireHasOption(t, m.Options(), dhcpv4.OptionVendorSpecificInformation)
+ require.True(t, dhcpv4.HasOption(m, dhcpv4.OptionVendorSpecificInformation))
opt := m.GetOneOption(dhcpv4.OptionVendorSpecificInformation)
vendorInfo := opt.(*OptVendorSpecificInformation)
- RequireHasOption(t, vendorInfo.Options, OptionReplyPort)
+ require.True(t, dhcpv4.HasOption(vendorInfo, OptionReplyPort))
opt = vendorInfo.GetOneOption(OptionReplyPort)
require.Equal(t, replyPort, opt.(*OptReplyPort).Port)
}
diff --git a/dhcpv4/bsdp/option_vendor_specific_information.go b/dhcpv4/bsdp/option_vendor_specific_information.go
index 5c8533e..e735b57 100644
--- a/dhcpv4/bsdp/option_vendor_specific_information.go
+++ b/dhcpv4/bsdp/option_vendor_specific_information.go
@@ -131,8 +131,8 @@ func (o *OptVendorSpecificInformation) Length() int {
return length
}
-// GetOptions returns all suboptions that match the given OptionCode code.
-func (o *OptVendorSpecificInformation) GetOptions(code dhcpv4.OptionCode) []dhcpv4.Option {
+// GetOption returns all suboptions that match the given OptionCode code.
+func (o *OptVendorSpecificInformation) GetOption(code dhcpv4.OptionCode) []dhcpv4.Option {
var opts []dhcpv4.Option
for _, opt := range o.Options {
if opt.Code() == code {
@@ -144,7 +144,7 @@ func (o *OptVendorSpecificInformation) GetOptions(code dhcpv4.OptionCode) []dhcp
// GetOneOption returns the first suboption that matches the OptionCode code.
func (o *OptVendorSpecificInformation) GetOneOption(code dhcpv4.OptionCode) dhcpv4.Option {
- opts := o.GetOptions(code)
+ opts := o.GetOption(code)
if len(opts) == 0 {
return nil
}
diff --git a/dhcpv4/bsdp/option_vendor_specific_information_test.go b/dhcpv4/bsdp/option_vendor_specific_information_test.go
index 9827618..5e7689d 100644
--- a/dhcpv4/bsdp/option_vendor_specific_information_test.go
+++ b/dhcpv4/bsdp/option_vendor_specific_information_test.go
@@ -136,7 +136,7 @@ func TestOptVendorSpecificInformationGetOptions(t *testing.T) {
&OptVersion{Version1_1},
},
}
- foundOpts := o.GetOptions(OptionBootImageList)
+ foundOpts := o.GetOption(OptionBootImageList)
require.Empty(t, foundOpts, "should not get any options")
// One option
@@ -146,7 +146,7 @@ func TestOptVendorSpecificInformationGetOptions(t *testing.T) {
&OptVersion{Version1_1},
},
}
- foundOpts = o.GetOptions(OptionMessageType)
+ foundOpts = o.GetOption(OptionMessageType)
require.Equal(t, 1, len(foundOpts), "should only get one option")
require.Equal(t, MessageTypeList, foundOpts[0].(*OptMessageType).Type)
@@ -158,7 +158,7 @@ func TestOptVendorSpecificInformationGetOptions(t *testing.T) {
&OptVersion{Version1_0},
},
}
- foundOpts = o.GetOptions(OptionVersion)
+ foundOpts = o.GetOption(OptionVersion)
require.Equal(t, 2, len(foundOpts), "should get two options")
require.Equal(t, Version1_1, foundOpts[0].(*OptVersion).Version)
require.Equal(t, Version1_0, foundOpts[1].(*OptVersion).Version)