summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/options_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'dhcpv4/options_test.go')
-rw-r--r--dhcpv4/options_test.go142
1 files changed, 100 insertions, 42 deletions
diff --git a/dhcpv4/options_test.go b/dhcpv4/options_test.go
index 9a2f1c0..02d4018 100644
--- a/dhcpv4/options_test.go
+++ b/dhcpv4/options_test.go
@@ -7,12 +7,13 @@ import (
"testing"
"github.com/stretchr/testify/require"
+ "github.com/u-root/u-root/pkg/uio"
)
func TestParseOption(t *testing.T) {
// Generic
- option := []byte{5, 4, 192, 168, 1, 254} // DNS option
- opt, err := ParseOption(OptionCode(option[0]), option[2:])
+ option := []byte{192, 168, 1, 254} // Name server option
+ opt, err := ParseOption(OptionNameServer, option)
require.NoError(t, err)
generic := opt.(*OptionGeneric)
require.Equal(t, OptionNameServer, generic.Code())
@@ -21,166 +22,223 @@ func TestParseOption(t *testing.T) {
require.Equal(t, "Name Server -> [192 168 1 254]", generic.String())
// Option subnet mask
- option = []byte{1, 4, 255, 255, 255, 0}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{255, 255, 255, 0}
+ opt, err = ParseOption(OptionSubnetMask, option)
require.NoError(t, err)
require.Equal(t, OptionSubnetMask, opt.Code(), "Code")
require.Equal(t, 4, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Option router
- option = []byte{3, 4, 192, 168, 1, 1}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{192, 168, 1, 1}
+ opt, err = ParseOption(OptionRouter, option)
require.NoError(t, err)
require.Equal(t, OptionRouter, opt.Code(), "Code")
require.Equal(t, 4, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Option domain name server
- option = []byte{6, 4, 192, 168, 1, 1}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{192, 168, 1, 1}
+ opt, err = ParseOption(OptionDomainNameServer, option)
require.NoError(t, err)
require.Equal(t, OptionDomainNameServer, opt.Code(), "Code")
require.Equal(t, 4, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Option host name
- option = []byte{12, 4, 't', 'e', 's', 't'}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{'t', 'e', 's', 't'}
+ opt, err = ParseOption(OptionHostName, option)
require.NoError(t, err)
require.Equal(t, OptionHostName, opt.Code(), "Code")
require.Equal(t, 4, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Option domain name
- option = []byte{15, 4, 't', 'e', 's', 't'}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{'t', 'e', 's', 't'}
+ opt, err = ParseOption(OptionDomainName, option)
require.NoError(t, err)
require.Equal(t, OptionDomainName, opt.Code(), "Code")
require.Equal(t, 4, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Option root path
- option = []byte{17, 4, '/', 'f', 'o', 'o'}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{'/', 'f', 'o', 'o'}
+ opt, err = ParseOption(OptionRootPath, option)
require.NoError(t, err)
require.Equal(t, OptionRootPath, opt.Code(), "Code")
require.Equal(t, 4, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Option broadcast address
- option = []byte{28, 4, 255, 255, 255, 255}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{255, 255, 255, 255}
+ opt, err = ParseOption(OptionBroadcastAddress, option)
require.NoError(t, err)
require.Equal(t, OptionBroadcastAddress, opt.Code(), "Code")
require.Equal(t, 4, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Option NTP servers
- option = []byte{42, 4, 10, 10, 10, 10}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{10, 10, 10, 10}
+ opt, err = ParseOption(OptionNTPServers, option)
require.NoError(t, err)
require.Equal(t, OptionNTPServers, opt.Code(), "Code")
require.Equal(t, 4, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Requested IP address
- option = []byte{50, 4, 1, 2, 3, 4}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{1, 2, 3, 4}
+ opt, err = ParseOption(OptionRequestedIPAddress, option)
require.NoError(t, err)
require.Equal(t, OptionRequestedIPAddress, opt.Code(), "Code")
require.Equal(t, 4, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Requested IP address lease time
- option = []byte{51, 4, 0, 0, 0, 0}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{0, 0, 0, 0}
+ opt, err = ParseOption(OptionIPAddressLeaseTime, option)
require.NoError(t, err)
require.Equal(t, OptionIPAddressLeaseTime, opt.Code(), "Code")
require.Equal(t, 4, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Message type
- option = []byte{53, 1, 1}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{1}
+ opt, err = ParseOption(OptionDHCPMessageType, option)
require.NoError(t, err)
require.Equal(t, OptionDHCPMessageType, opt.Code(), "Code")
require.Equal(t, 1, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Option server ID
- option = []byte{54, 4, 1, 2, 3, 4}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{1, 2, 3, 4}
+ opt, err = ParseOption(OptionServerIdentifier, option)
require.NoError(t, err)
require.Equal(t, OptionServerIdentifier, opt.Code(), "Code")
require.Equal(t, 4, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Parameter request list
- option = []byte{55, 3, 5, 53, 61}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{5, 53, 61}
+ opt, err = ParseOption(OptionParameterRequestList, option)
require.NoError(t, err)
require.Equal(t, OptionParameterRequestList, opt.Code(), "Code")
require.Equal(t, 3, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Option max message size
- option = []byte{57, 2, 1, 2}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{1, 2}
+ opt, err = ParseOption(OptionMaximumDHCPMessageSize, option)
require.NoError(t, err)
require.Equal(t, OptionMaximumDHCPMessageSize, opt.Code(), "Code")
require.Equal(t, 2, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Option class identifier
- option = []byte{60, 4, 't', 'e', 's', 't'}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{'t', 'e', 's', 't'}
+ opt, err = ParseOption(OptionClassIdentifier, option)
require.NoError(t, err)
require.Equal(t, OptionClassIdentifier, opt.Code(), "Code")
require.Equal(t, 4, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Option TFTP server name
- option = []byte{66, 4, 't', 'e', 's', 't'}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{'t', 'e', 's', 't'}
+ opt, err = ParseOption(OptionTFTPServerName, option)
require.NoError(t, err)
require.Equal(t, OptionTFTPServerName, opt.Code(), "Code")
require.Equal(t, 4, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Option Bootfile name
- option = []byte{67, 9, 'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't'}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{'l', 'i', 'n', 'u', 'x', 'b', 'o', 'o', 't'}
+ opt, err = ParseOption(OptionBootfileName, option)
require.NoError(t, err)
require.Equal(t, OptionBootfileName, opt.Code(), "Code")
require.Equal(t, 9, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Option user class information
- option = []byte{77, 5, 4, 't', 'e', 's', 't'}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{4, 't', 'e', 's', 't'}
+ opt, err = ParseOption(OptionUserClassInformation, option)
require.NoError(t, err)
require.Equal(t, OptionUserClassInformation, opt.Code(), "Code")
require.Equal(t, 5, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Option relay agent information
- option = []byte{82, 6, 1, 4, 129, 168, 0, 1}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{1, 4, 129, 168, 0, 1}
+ opt, err = ParseOption(OptionRelayAgentInformation, option)
require.NoError(t, err)
require.Equal(t, OptionRelayAgentInformation, opt.Code(), "Code")
require.Equal(t, 6, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
// Option client system architecture type option
- option = []byte{93, 4, 't', 'e', 's', 't'}
- opt, err = ParseOption(OptionCode(option[0]), option[2:])
+ option = []byte{'t', 'e', 's', 't'}
+ opt, err = ParseOption(OptionClientSystemArchitectureType, option)
require.NoError(t, err)
require.Equal(t, OptionClientSystemArchitectureType, opt.Code(), "Code")
require.Equal(t, 4, opt.Length(), "Length")
require.Equal(t, option, opt.ToBytes(), "ToBytes")
}
+func TestOptionsMarshal(t *testing.T) {
+ for i, tt := range []struct {
+ opts Options
+ want []byte
+ }{
+ {
+ opts: nil,
+ want: nil,
+ },
+ {
+ opts: Options{
+ &OptionGeneric{
+ OptionCode: 5,
+ Data: []byte{1, 2, 3, 4},
+ },
+ },
+ want: []byte{
+ 5 /* key */, 4 /* length */, 1, 2, 3, 4,
+ },
+ },
+ {
+ // Test sorted key order.
+ opts: Options{
+ &OptionGeneric{
+ OptionCode: 5,
+ Data: []byte{1, 2, 3},
+ },
+ &OptionGeneric{
+ OptionCode: 100,
+ Data: []byte{101, 102, 103},
+ },
+ },
+ want: []byte{
+ 5, 3, 1, 2, 3,
+ 100, 3, 101, 102, 103,
+ },
+ },
+ {
+ // Test RFC 3396.
+ opts: Options{
+ &OptionGeneric{
+ OptionCode: 5,
+ Data: bytes.Repeat([]byte{10}, math.MaxUint8+1),
+ },
+ },
+ want: append(append(
+ []byte{5, math.MaxUint8}, bytes.Repeat([]byte{10}, math.MaxUint8)...),
+ 5, 1, 10,
+ ),
+ },
+ } {
+ t.Run(fmt.Sprintf("Test %02d", i), func(t *testing.T) {
+ require.Equal(t, uio.ToBigEndian(tt.opts), tt.want)
+ })
+ }
+}
+
func TestOptionsUnmarshal(t *testing.T) {
for i, tt := range []struct {
input []byte