summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_informationrefreshtime_test.go
blob: 1345c2891d43726543632ffd163e2073cf77f3c4 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package dhcpv6

import (
	"bytes"
	"errors"
	"fmt"
	"reflect"
	"testing"
	"time"

	"github.com/google/go-cmp/cmp"
	"github.com/u-root/uio/uio"
)

func TestInformationRefreshTimeParseAndGetter(t *testing.T) {
	for i, tt := range []struct {
		buf  []byte
		err  error
		want time.Duration
	}{
		{
			buf: []byte{
				0, 32, // IRT option
				0, 4, // length
				0, 0, 0, 3,
			},
			want: 3 * time.Second,
		},
		{
			buf: []byte{
				0, 32, // IRT option
				0, 6, // length
				0, 0, 0, 3, 0, 0,
			},
			err: uio.ErrUnreadBytes,
		},
		{
			buf: []byte{0, 32, 0, 1, 0},
			err: uio.ErrBufferTooShort,
		},
		{
			buf: []byte{0, 32, 0},
			err: uio.ErrUnreadBytes,
		},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			var mo MessageOptions
			if err := mo.FromBytes(tt.buf); !errors.Is(err, tt.err) {
				t.Errorf("FromBytes = %v, want %v", err, tt.err)
			}
			if got := mo.InformationRefreshTime(0); !reflect.DeepEqual(got, tt.want) {
				t.Errorf("InformationRefreshTime = %v, want %v", got, tt.want)
			}

			if tt.err == nil {
				var m MessageOptions
				m.Add(OptInformationRefreshTime(tt.want))
				got := m.ToBytes()
				if diff := cmp.Diff(tt.buf, got); diff != "" {
					t.Errorf("ToBytes mismatch (-want, +got): %s", diff)
				}
			}
		})
	}
}

func TestOptInformationRefreshTime(t *testing.T) {
	var opt optInformationRefreshTime
	err := opt.FromBytes([]byte{0xaa, 0xbb, 0xcc, 0xdd})
	if err != nil {
		t.Fatal(err)
	}
	if informationRefreshTime := opt.InformationRefreshtime; informationRefreshTime != time.Duration(0xaabbccdd)*time.Second {
		t.Fatalf("Invalid information refresh time. Expected 0xaabb, got %v", informationRefreshTime)
	}
}

func TestOptInformationRefreshTimeToBytes(t *testing.T) {
	opt := OptInformationRefreshTime(0)
	expected := []byte{0, 0, 0, 0}
	if toBytes := opt.ToBytes(); !bytes.Equal(expected, toBytes) {
		t.Fatalf("Invalid ToBytes output. Expected %v, got %v", expected, toBytes)
	}
}

func TestOptInformationRefreshTimeString(t *testing.T) {
	opt := OptInformationRefreshTime(3600 * time.Second)
	expected := "Information Refresh Time: 1h0m0s"
	if optString := opt.String(); optString != expected {
		t.Fatalf("Invalid elapsed time string. Expected %v, got %v", expected, optString)
	}
}