From d7cd0594274352de80d86a3bf32b487dfde63629 Mon Sep 17 00:00:00 2001 From: Brandon Bennett Date: Fri, 11 May 2018 14:09:24 -0600 Subject: add dhcpv4.GetOption and dhcpv4.GetSingleOption methods (#63) GetOption and GetOneOption are convenience methods for getting a DHCPv4 option from its Option Code. GetOption returns a list of options that match since the DHCP RFC allows for an option to be present multiple times with its values appended together. (Note: I am not sure I've ever seen this done and it's not clear if how option parsing would work for more complex values. Is appending done at the byte level? ) GetOneOption will return the first found option that matches a code. --- dhcpv4/dhcpv4_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'dhcpv4/dhcpv4_test.go') diff --git a/dhcpv4/dhcpv4_test.go b/dhcpv4/dhcpv4_test.go index 065b6e0..4e4f7b5 100644 --- a/dhcpv4/dhcpv4_test.go +++ b/dhcpv4/dhcpv4_test.go @@ -300,6 +300,28 @@ func TestNewToBytes(t *testing.T) { require.Equal(t, expected, got) } +func TestGetOption(t *testing.T) { + d, err := New() + if err != nil { + t.Fatal(err) + } + + hostnameOpt := &OptionGeneric{OptionCode: OptionHostName, Data: []byte("darkstar")} + bootFileOpt1 := &OptionGeneric{OptionCode: OptionBootfileName, Data: []byte("boot.img")} + bootFileOpt2 := &OptionGeneric{OptionCode: OptionBootfileName, Data: []byte("boot2.img")} + d.AddOption(hostnameOpt) + d.AddOption(bootFileOpt1) + d.AddOption(bootFileOpt2) + + require.Equal(t, d.GetOption(OptionHostName), []Option{hostnameOpt}) + require.Equal(t, d.GetOption(OptionBootfileName), []Option{bootFileOpt1, bootFileOpt2}) + require.Equal(t, d.GetOption(OptionRouter), []Option{}) + + require.Equal(t, d.GetOneOption(OptionHostName), hostnameOpt) + require.Equal(t, d.GetOneOption(OptionBootfileName), bootFileOpt1) + require.Equal(t, d.GetOneOption(OptionRouter), nil) +} + // TODO // test broadcast/unicast flags // test Options setter/getter -- cgit v1.2.3