summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_dhcpv4_msg_test.go
diff options
context:
space:
mode:
authorMikael Magnusson <mikma@users.sourceforge.net>2020-03-04 21:43:00 +0100
committerMikael Magnusson <mikma@users.sourceforge.net>2020-03-06 21:46:38 +0100
commit19ee83379cf483ab151dd1ad4e36d09483d6a223 (patch)
tree8f818ae12245130a63aee17c49050062db9aa8db /dhcpv6/option_dhcpv4_msg_test.go
parent7ea59fc95373dc2c34b2a81c0917618402affe0f (diff)
dhcpv6: add DHCPv4-over-DHCPv6 support
Add message types, options, and modifier for handling DHCPv4-over-DHCPv6. Refer to RFC 7341 Signed-off-by: Mikael Magnusson <mikma@users.sourceforge.net>
Diffstat (limited to 'dhcpv6/option_dhcpv4_msg_test.go')
-rw-r--r--dhcpv6/option_dhcpv4_msg_test.go105
1 files changed, 105 insertions, 0 deletions
diff --git a/dhcpv6/option_dhcpv4_msg_test.go b/dhcpv6/option_dhcpv4_msg_test.go
new file mode 100644
index 0000000..1ffa17a
--- /dev/null
+++ b/dhcpv6/option_dhcpv4_msg_test.go
@@ -0,0 +1,105 @@
+package dhcpv6
+
+import (
+ "bytes"
+ "net"
+ "testing"
+
+ "github.com/insomniacslk/dhcp/dhcpv4"
+ "github.com/insomniacslk/dhcp/iana"
+ "github.com/stretchr/testify/require"
+)
+
+var magicCookie = [4]byte{99, 130, 83, 99}
+
+func TestParseOptDHCPv4Msg(t *testing.T) {
+ data := []byte{
+ 1, // dhcp request
+ 1, // ethernet hw type
+ 6, // hw addr length
+ 3, // hop count
+ 0xaa, 0xbb, 0xcc, 0xdd, // transaction ID, big endian (network)
+ 0, 3, // number of seconds
+ 0, 1, // broadcast
+ 0, 0, 0, 0, // client IP address
+ 0, 0, 0, 0, // your IP address
+ 0, 0, 0, 0, // server IP address
+ 0, 0, 0, 0, // gateway IP address
+ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // client MAC address + padding
+ }
+
+ // server host name
+ expectedHostname := []byte{}
+ for i := 0; i < 64; i++ {
+ expectedHostname = append(expectedHostname, 0)
+ }
+ data = append(data, expectedHostname...)
+ // boot file name
+ expectedBootfilename := []byte{}
+ for i := 0; i < 128; i++ {
+ expectedBootfilename = append(expectedBootfilename, 0)
+ }
+ data = append(data, expectedBootfilename...)
+ // magic cookie, then no options
+ data = append(data, magicCookie[:]...)
+
+ opt, err := ParseOptDHCPv4Msg(data)
+ d := opt.Msg
+ require.NoError(t, err)
+ require.Equal(t, d.OpCode, dhcpv4.OpcodeBootRequest)
+ require.Equal(t, d.HWType, iana.HWTypeEthernet)
+ require.Equal(t, d.HopCount, byte(3))
+ require.Equal(t, d.TransactionID, dhcpv4.TransactionID{0xaa, 0xbb, 0xcc, 0xdd})
+ require.Equal(t, d.NumSeconds, uint16(3))
+ require.Equal(t, d.Flags, uint16(1))
+ require.True(t, d.ClientIPAddr.Equal(net.IPv4zero))
+ require.True(t, d.YourIPAddr.Equal(net.IPv4zero))
+ require.True(t, d.GatewayIPAddr.Equal(net.IPv4zero))
+ require.Equal(t, d.ClientHWAddr, net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff})
+ require.Equal(t, d.ServerHostName, "")
+ require.Equal(t, d.BootFileName, "")
+ // no need to check Magic Cookie as it is already validated in FromBytes
+ // above
+}
+
+func TestOptDHCPv4MsgToBytes(t *testing.T) {
+ // the following bytes match what dhcpv4.New would create. Keep them in
+ // sync!
+ expected := []byte{
+ 1, // Opcode BootRequest
+ 1, // HwType Ethernet
+ 6, // HwAddrLen
+ 0, // HopCount
+ 0x11, 0x22, 0x33, 0x44, // TransactionID
+ 0, 0, // NumSeconds
+ 0, 0, // Flags
+ 0, 0, 0, 0, // ClientIPAddr
+ 0, 0, 0, 0, // YourIPAddr
+ 0, 0, 0, 0, // ServerIPAddr
+ 0, 0, 0, 0, // GatewayIPAddr
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ClientHwAddr
+ }
+ // ServerHostName
+ expected = append(expected, bytes.Repeat([]byte{0}, 64)...)
+ // BootFileName
+ expected = append(expected, bytes.Repeat([]byte{0}, 128)...)
+
+ // Magic Cookie
+ expected = append(expected, magicCookie[:]...)
+
+ // Minimum message length padding.
+ //
+ // 236 + 4 byte cookie + 59 bytes padding + 1 byte end.
+ expected = append(expected, bytes.Repeat([]byte{0}, 59)...)
+
+ // End
+ expected = append(expected, 0xff)
+
+ d, err := dhcpv4.New()
+ require.NoError(t, err)
+ // fix TransactionID to match the expected one, since it's randomly
+ // generated in New()
+ d.TransactionID = dhcpv4.TransactionID{0x11, 0x22, 0x33, 0x44}
+ opt := OptDHCPv4Msg{Msg: d}
+ require.Equal(t, expected, opt.ToBytes())
+}