Skip to content

Commit

Permalink
Improve support for renewal and rebinding options
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelharo committed May 29, 2024
1 parent 6ac7581 commit 90f0a31
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
10 changes: 10 additions & 0 deletions dhcpv4/option_duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ func OptIPAddressLeaseTime(d time.Duration) Option {
return Option{Code: OptionIPAddressLeaseTime, Value: Duration(d)}
}

// The IP address renew time option as described by RFC 2132, Section 9.11.
func OptRenewTimeValue(d time.Duration) Option {
return Option{Code: OptionRenewTimeValue, Value: Duration(d)}
}

// The IP address rebinding time option as described by RFC 2132, Section 9.12.
func OptRebindingTimeValue(d time.Duration) Option {
return Option{Code: OptionRebindingTimeValue, Value: Duration(d)}
}

// The IPv6-Only Preferred option is described by RFC 8925, Section 3.1
func OptIPv6OnlyPreferred(d time.Duration) Option {
return Option{Code: OptionIPv6OnlyPreferred, Value: Duration(d)}
Expand Down
54 changes: 54 additions & 0 deletions dhcpv4/option_duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,60 @@ func TestGetIPAddressLeaseTime(t *testing.T) {
require.Equal(t, time.Duration(10), m.IPAddressLeaseTime(10))
}

func TestOptRenewTimeValue(t *testing.T) {
o := OptRenewTimeValue(43200 * time.Second)
require.Equal(t, OptionRenewTimeValue, o.Code, "Code")
require.Equal(t, []byte{0, 0, 168, 192}, o.Value.ToBytes(), "ToBytes")
require.Equal(t, "Renew Time Value: 12h0m0s", o.String(), "String")
}

func TestGetRenewTimeValue(t *testing.T) {
m, _ := New(WithGeneric(OptionRenewTimeValue, []byte{0, 0, 168, 192}))
leaseTime := m.IPAddressRenewalTime(0)
require.Equal(t, 43200*time.Second, leaseTime)

// Too short.
m, _ = New(WithGeneric(OptionRenewTimeValue, []byte{168, 192}))
leaseTime = m.IPAddressRenewalTime(0)
require.Equal(t, time.Duration(0), leaseTime)

// Too long.
m, _ = New(WithGeneric(OptionRenewTimeValue, []byte{1, 1, 1, 1, 1}))
leaseTime = m.IPAddressRenewalTime(0)
require.Equal(t, time.Duration(0), leaseTime)

// Empty.
m, _ = New()
require.Equal(t, time.Duration(10), m.IPAddressRenewalTime(10))
}

func TestOptRebindingTimeValue(t *testing.T) {
o := OptRebindingTimeValue(43200 * time.Second)
require.Equal(t, OptionRebindingTimeValue, o.Code, "Code")
require.Equal(t, []byte{0, 0, 168, 192}, o.Value.ToBytes(), "ToBytes")
require.Equal(t, "Rebinding Time Value: 12h0m0s", o.String(), "String")
}

func TestGetRebindingTimeValue(t *testing.T) {
m, _ := New(WithGeneric(OptionRebindingTimeValue, []byte{0, 0, 168, 192}))
leaseTime := m.IPAddressRebindingTime(0)
require.Equal(t, 43200*time.Second, leaseTime)

// Too short.
m, _ = New(WithGeneric(OptionRebindingTimeValue, []byte{168, 192}))
leaseTime = m.IPAddressRebindingTime(0)
require.Equal(t, time.Duration(0), leaseTime)

// Too long.
m, _ = New(WithGeneric(OptionRebindingTimeValue, []byte{1, 1, 1, 1, 1}))
leaseTime = m.IPAddressRebindingTime(0)
require.Equal(t, time.Duration(0), leaseTime)

// Empty.
m, _ = New()
require.Equal(t, time.Duration(10), m.IPAddressRebindingTime(10))
}

func TestOptIPv6OnlyPreferred(t *testing.T) {
o := OptIPv6OnlyPreferred(43200 * time.Second)
require.Equal(t, OptionIPv6OnlyPreferred, o.Code, "Code")
Expand Down

0 comments on commit 90f0a31

Please sign in to comment.