diff options
author | Pablo Mazzini <pmazzini@gmail.com> | 2018-08-12 12:50:24 +0200 |
---|---|---|
committer | Pablo Mazzini <pmazzini@gmail.com> | 2018-08-12 12:50:24 +0200 |
commit | 79d05d5ba9ac8fb488fc6aa1692366d933dee3e1 (patch) | |
tree | d1729404c5db5f79226588d96bd9361551ad65fc /dhcpv6/dhcpv6_test.go | |
parent | f55d67dc49bee6d6aa20913f070ed771b9ff725a (diff) |
IsOptionRequested method of DHCPv6Message
Diffstat (limited to 'dhcpv6/dhcpv6_test.go')
-rw-r--r-- | dhcpv6/dhcpv6_test.go | 104 |
1 files changed, 84 insertions, 20 deletions
diff --git a/dhcpv6/dhcpv6_test.go b/dhcpv6/dhcpv6_test.go index c48df24..812d284 100644 --- a/dhcpv6/dhcpv6_test.go +++ b/dhcpv6/dhcpv6_test.go @@ -4,6 +4,7 @@ import ( "net" "testing" + "github.com/insomniacslk/dhcp/iana" "github.com/stretchr/testify/require" ) @@ -125,33 +126,96 @@ func TestFromAndToBytes(t *testing.T) { require.Equal(t, expected, toBytes) } -func TestIsNetboot(t *testing.T) { - msg1 := DHCPv6Message{} - require.False(t, IsNetboot(&msg1)) +func TestNewAdvertiseFromSolicit(t *testing.T) { + s := DHCPv6Message{} + s.SetMessage(MessageTypeSolicit) + s.SetTransactionID(0xabcdef) + cid := OptClientId{} + s.AddOption(&cid) + duid := Duid{} - msg2 := DHCPv6Message{} - optro := OptRequestedOption{} - optro.AddRequestedOption(OptionBootfileURL) - msg2.AddOption(&optro) - require.True(t, IsNetboot(&msg2)) + a, err := NewAdvertiseFromSolicit(&s, WithServerID(duid)) + require.NoError(t, err) + require.Equal(t, a.(*DHCPv6Message).TransactionID(), s.TransactionID()) + require.Equal(t, a.Type(), MessageTypeAdvertise) +} + +func TestNewReplyFromDHCPv6Message(t *testing.T) { + msg := DHCPv6Message{} + msg.SetTransactionID(0xabcdef) + cid := OptClientId{} + msg.AddOption(&cid) + sid := OptServerId{} + duid := Duid{} + sid.Sid = duid + msg.AddOption(&sid) + + msg.SetMessage(MessageTypeConfirm) + rep, err := NewReplyFromDHCPv6Message(&msg, WithServerID(duid)) + require.NoError(t, err) + require.Equal(t, rep.(*DHCPv6Message).TransactionID(), msg.TransactionID()) + require.Equal(t, rep.Type(), MessageTypeReply) + + msg.SetMessage(MessageTypeRenew) + rep, err = NewReplyFromDHCPv6Message(&msg, WithServerID(duid)) + require.NoError(t, err) + require.Equal(t, rep.(*DHCPv6Message).TransactionID(), msg.TransactionID()) + require.Equal(t, rep.Type(), MessageTypeReply) + + msg.SetMessage(MessageTypeRebind) + rep, err = NewReplyFromDHCPv6Message(&msg, WithServerID(duid)) + require.NoError(t, err) + require.Equal(t, rep.(*DHCPv6Message).TransactionID(), msg.TransactionID()) + require.Equal(t, rep.Type(), MessageTypeReply) + + msg.SetMessage(MessageTypeRelease) + rep, err = NewReplyFromDHCPv6Message(&msg, WithServerID(duid)) + require.NoError(t, err) + require.Equal(t, rep.(*DHCPv6Message).TransactionID(), msg.TransactionID()) + require.Equal(t, rep.Type(), MessageTypeReply) - msg3 := DHCPv6Message{} - optbf := OptBootFileURL{} - msg3.AddOption(&optbf) - require.True(t, IsNetboot(&msg3)) + msg.SetMessage(MessageTypeSolicit) + rep, err = NewReplyFromDHCPv6Message(&msg) + require.Error(t, err) + + relay := DHCPv6Relay{} + rep, err = NewReplyFromDHCPv6Message(&relay) + require.Error(t, err) } -func TestIsOptionRequested(t *testing.T) { - msg1 := DHCPv6Message{} - require.False(t, IsOptionRequested(&msg1, OptionDNSRecursiveNameServer)) +func TestNewMessageTypeSolicitWithCID(t *testing.T) { + hwAddr, err := net.ParseMAC("24:0A:9E:9F:EB:2B") + require.NoError(t, err) + + duid := Duid{ + Type: DUID_LL, + HwType: iana.HwTypeEthernet, + LinkLayerAddr: hwAddr, + } - msg2 := DHCPv6Message{} - optro := OptRequestedOption{} - optro.AddRequestedOption(OptionDNSRecursiveNameServer) - msg2.AddOption(&optro) - require.True(t, IsOptionRequested(&msg2, OptionDNSRecursiveNameServer)) + s, err := NewSolicitWithCID(duid) + require.NoError(t, err) + + require.Equal(t, s.Type(), MessageTypeSolicit) + // Check CID + cidOption := s.GetOneOption(OptionClientID) + require.NotNil(t, cidOption) + cid, ok := cidOption.(*OptClientId) + require.True(t, ok) + require.Equal(t, cid.Cid, duid) + + // Check ORO + oroOption := s.GetOneOption(OptionORO) + require.NotNil(t, oroOption) + oro, ok := oroOption.(*OptRequestedOption) + require.True(t, ok) + opts := oro.RequestedOptions() + require.Contains(t, opts, OptionDNSRecursiveNameServer) + require.Contains(t, opts, OptionDomainSearchList) + require.Equal(t, len(opts), 2) } + func TestIsUsingUEFIArchTypeTrue(t *testing.T) { msg := DHCPv6Message{} opt := OptClientArchType{ArchType: EFI_BC} |