summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/option_ip_address_lease_time_test.go
blob: 70c40473a05a070b31a6d5c1fd9408960cd210de (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
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) {
	o := Options{OptionIPAddressLeaseTime.Code(): []byte{0, 0, 168, 192}}
	leaseTime := GetIPAddressLeaseTime(o, 0)
	require.Equal(t, 43200*time.Second, leaseTime)

	// Too short.
	o = Options{OptionIPAddressLeaseTime.Code(): []byte{168, 192}}
	leaseTime = GetIPAddressLeaseTime(o, 0)
	require.Equal(t, time.Duration(0), leaseTime)

	// Too long.
	o = Options{OptionIPAddressLeaseTime.Code(): []byte{1, 1, 1, 1, 1}}
	leaseTime = GetIPAddressLeaseTime(o, 0)
	require.Equal(t, time.Duration(0), leaseTime)

	// Empty.
	require.Equal(t, time.Duration(10), GetIPAddressLeaseTime(Options{}, 10))
}