summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_ip_address_lease_time_test.go
blob: edd709f3a1e632dd9c3f76155512aeaed67caa32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package dhcpv4

import (
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func TestOptIPAddressLeaseTime(t *testing.T) {
	o := OptIPAddressLeaseTime(43200 * time.Second)
	require.Equal(t, OptionIPAddressLeaseTime, o.Code, "Code")
	require.Equal(t, []byte{0, 0, 168, 192}, o.Value.ToBytes(), "ToBytes")
	require.Equal(t, "IP Addresses Lease Time: 12h0m0s", o.String(), "String")
}

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

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

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

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