Skip to content

Commit

Permalink
bsdp: simplify version type.
Browse files Browse the repository at this point in the history
  • Loading branch information
hugelgupf committed Jan 13, 2019
1 parent 74c7d1e commit a955d74
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 40 deletions.
4 changes: 2 additions & 2 deletions dhcpv4/bsdp/bsdp.go
Expand Up @@ -109,7 +109,7 @@ func NewInformList(hwaddr net.HardwareAddr, localIP net.IP, replyPort uint16) (*
// These are vendor-specific options used to pass along BSDP information.
vendorOpts := []dhcpv4.Option{
&OptMessageType{MessageTypeList},
&OptVersion{Version1_1},
Version1_1,
}
if needsReplyPort(replyPort) {
vendorOpts = append(vendorOpts, &OptReplyPort{replyPort})
Expand Down Expand Up @@ -156,7 +156,7 @@ func InformSelectForAck(ack dhcpv4.DHCPv4, replyPort uint16, selectedImage BootI
// Data for OptionSelectedBootImageID
vendorOpts := []dhcpv4.Option{
&OptMessageType{MessageTypeSelect},
&OptVersion{Version1_1},
Version1_1,
&OptSelectedBootImageID{selectedImage.ID},
}

Expand Down
30 changes: 14 additions & 16 deletions dhcpv4/bsdp/bsdp_option_version.go
Expand Up @@ -7,37 +7,35 @@ import (
"github.com/u-root/u-root/pkg/uio"
)

// Version is the BSDP protocol version. Can be one of 1.0 or 1.1.
type Version [2]byte

// Specific versions.
var (
Version1_0 = []byte{1, 0}
Version1_1 = []byte{1, 1}
Version1_0 = Version{1, 0}
Version1_1 = Version{1, 1}
)

// OptVersion represents a BSDP protocol version.
//
// Implements the BSDP option version. Can be one of 1.0 or 1.1
type OptVersion struct {
Version []byte
}

// ParseOptVersion constructs an OptVersion struct from a sequence of
// bytes and returns it, or an error.
func ParseOptVersion(data []byte) (*OptVersion, error) {
func ParseOptVersion(data []byte) (Version, error) {
buf := uio.NewBigEndianBuffer(data)
return &OptVersion{buf.CopyN(2)}, buf.FinError()
var v Version
buf.ReadBytes(v[:])
return v, buf.FinError()
}

// Code returns the option code.
func (o *OptVersion) Code() dhcpv4.OptionCode {
func (o Version) Code() dhcpv4.OptionCode {
return OptionVersion
}

// ToBytes returns a serialized stream of bytes for this option.
func (o *OptVersion) ToBytes() []byte {
return o.Version
func (o Version) ToBytes() []byte {
return o[:]
}

// String returns a human-readable string for this option.
func (o *OptVersion) String() string {
return fmt.Sprintf("BSDP Version -> %v.%v", o.Version[0], o.Version[1])
func (o Version) String() string {
return fmt.Sprintf("BSDP Version -> %d.%d", o[0], o[1])
}
8 changes: 3 additions & 5 deletions dhcpv4/bsdp/bsdp_option_version_test.go
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestOptVersionInterfaceMethods(t *testing.T) {
o := OptVersion{Version1_1}
o := Version1_1
require.Equal(t, OptionVersion, o.Code(), "Code")
require.Equal(t, []byte{1, 1}, o.ToBytes(), "ToBytes")
}
Expand All @@ -16,7 +16,7 @@ func TestParseOptVersion(t *testing.T) {
data := []byte{1, 1}
o, err := ParseOptVersion(data)
require.NoError(t, err)
require.Equal(t, &OptVersion{Version1_1}, o)
require.Equal(t, Version1_1, o)

// Short byte stream
data = []byte{2}
Expand All @@ -25,7 +25,5 @@ func TestParseOptVersion(t *testing.T) {
}

func TestOptVersionString(t *testing.T) {
// known
o := OptVersion{Version1_1}
require.Equal(t, "BSDP Version -> 1.1", o.String())
require.Equal(t, "BSDP Version -> 1.1", Version1_1.String())
}
4 changes: 2 additions & 2 deletions dhcpv4/bsdp/bsdp_test.go
Expand Up @@ -401,7 +401,7 @@ func TestMessageTypeForPacket(t *testing.T) {
&dhcpv4.OptHostName{HostName: "foobar1234"},
&OptVendorSpecificInformation{
Options: []dhcpv4.Option{
&OptVersion{Version: Version1_1},
Version1_1,
},
},
},
Expand All @@ -412,7 +412,7 @@ func TestMessageTypeForPacket(t *testing.T) {
&dhcpv4.OptHostName{HostName: "foobar1234"},
&OptVendorSpecificInformation{
Options: []dhcpv4.Option{
&OptVersion{Version: Version1_1},
Version1_1,
&OptMessageType{Type: MessageTypeList},
},
},
Expand Down
40 changes: 25 additions & 15 deletions dhcpv4/bsdp/option_vendor_specific_information_test.go
Expand Up @@ -9,7 +9,7 @@ import (

func TestOptVendorSpecificInformationInterfaceMethods(t *testing.T) {
messageTypeOpt := &OptMessageType{MessageTypeList}
versionOpt := &OptVersion{Version1_1}
versionOpt := Version1_1
o := &OptVendorSpecificInformation{[]dhcpv4.Option{messageTypeOpt, versionOpt}}
require.Equal(t, dhcpv4.OptionVendorSpecificInformation, o.Code(), "Code")

Expand All @@ -20,7 +20,7 @@ func TestOptVendorSpecificInformationInterfaceMethods(t *testing.T) {
o = &OptVendorSpecificInformation{
[]dhcpv4.Option{
&OptMessageType{MessageTypeList},
&OptVersion{Version1_1},
Version1_1,
},
}
require.Equal(t, expectedBytes, o.ToBytes(), "ToBytes")
Expand All @@ -44,7 +44,7 @@ func TestParseOptVendorSpecificInformation(t *testing.T) {
expected := &OptVendorSpecificInformation{
[]dhcpv4.Option{
&OptMessageType{MessageTypeList},
&OptVersion{Version1_1},
Version1_1,
},
}
require.Equal(t, 2, len(o.Options), "number of parsed suboptions")
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestOptVendorSpecificInformationString(t *testing.T) {
o := &OptVendorSpecificInformation{
[]dhcpv4.Option{
&OptMessageType{MessageTypeList},
&OptVersion{Version1_1},
Version1_1,
},
}
expectedString := "Vendor Specific Information ->\n BSDP Message Type -> LIST\n BSDP Version -> 1.1"
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestOptVendorSpecificInformationGetOptions(t *testing.T) {
o := &OptVendorSpecificInformation{
[]dhcpv4.Option{
&OptMessageType{MessageTypeList},
&OptVersion{Version1_1},
Version1_1,
},
}
foundOpts := o.GetOption(OptionBootImageList)
Expand All @@ -181,33 +181,43 @@ func TestOptVendorSpecificInformationGetOptions(t *testing.T) {
o = &OptVendorSpecificInformation{
[]dhcpv4.Option{
&OptMessageType{MessageTypeList},
&OptVersion{Version1_1},
Version1_1,
},
}
foundOpts = o.GetOption(OptionMessageType)
require.Equal(t, 1, len(foundOpts), "should only get one option")
require.Equal(t, MessageTypeList, foundOpts[0].(*OptMessageType).Type)

// Multiple options
//
// TODO: Remove this test when RFC 3396 is properly implemented. This
// isn't a valid packet. RFC 2131, Section 4.1: "Options may appear
// only once." RFC 3396 clarifies this to say that options will be
// concatenated. I.e., in this case, the bytes would be:
//
// <versioncode> 4 1 1 0 1
//
// Which would obviously not be parsed by the Version parser, as it
// only accepts two bytes.
o = &OptVendorSpecificInformation{
[]dhcpv4.Option{
&OptMessageType{MessageTypeList},
&OptVersion{Version1_1},
&OptVersion{Version1_0},
Version1_1,
Version1_0,
},
}
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)
require.Equal(t, Version1_1, foundOpts[0].(Version))
require.Equal(t, Version1_0, foundOpts[1].(Version))
}

func TestOptVendorSpecificInformationGetOneOption(t *testing.T) {
// No option
o := &OptVendorSpecificInformation{
[]dhcpv4.Option{
&OptMessageType{MessageTypeList},
&OptVersion{Version1_1},
Version1_1,
},
}
foundOpt := o.GetOneOption(OptionBootImageList)
Expand All @@ -217,7 +227,7 @@ func TestOptVendorSpecificInformationGetOneOption(t *testing.T) {
o = &OptVendorSpecificInformation{
[]dhcpv4.Option{
&OptMessageType{MessageTypeList},
&OptVersion{Version1_1},
Version1_1,
},
}
foundOpt = o.GetOneOption(OptionMessageType)
Expand All @@ -227,10 +237,10 @@ func TestOptVendorSpecificInformationGetOneOption(t *testing.T) {
o = &OptVendorSpecificInformation{
[]dhcpv4.Option{
&OptMessageType{MessageTypeList},
&OptVersion{Version1_1},
&OptVersion{Version1_0},
Version1_1,
Version1_0,
},
}
foundOpt = o.GetOneOption(OptionVersion)
require.Equal(t, Version1_1, foundOpt.(*OptVersion).Version)
require.Equal(t, Version1_1, foundOpt.(Version))
}

0 comments on commit a955d74

Please sign in to comment.