summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_temporaryaddress_test.go
blob: 6d94f8faf473615cea5fd2fba4ed878c7bce420f (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package dhcpv6

import (
	"net"
	"reflect"
	"testing"
	"time"

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

func TestParseMessageWithIATA(t *testing.T) {
	data := []byte{
		0, 4, // IATA option code
		0, 32, // length
		1, 0, 0, 0, // IAID
		// IATA Options
		0, 5, 0, 0x18, 0x24, 1, 0xdb, 0, 0x30, 0x10, 0xc0, 0x8f, 0xfa, 0xce, 0, 0, 0, 0x44, 0, 0, // IP
		0, 0, 0, 2, // PreferredLifetime
		0, 0, 0, 4, // ValidLifetime
	}
	var got MessageOptions
	if err := got.FromBytes(data); err != nil {
		t.Errorf("FromBytes = %v", err)
	}

	want := &OptIATA{
		IaId: [4]byte{1, 0, 0, 0},
		Options: IdentityOptions{Options: Options{&OptIAAddress{
			IPv6Addr:          net.IP{0x24, 1, 0xdb, 0, 0x30, 0x10, 0xc0, 0x8f, 0xfa, 0xce, 0, 0, 0, 0x44, 0, 0},
			PreferredLifetime: 2 * time.Second,
			ValidLifetime:     4 * time.Second,
			Options:           AddressOptions{Options: Options{}},
		}}},
	}
	if gotIATA := got.OneIATA(); !reflect.DeepEqual(gotIATA, want) {
		t.Errorf("OneIATA = %v, want %v", gotIATA, want)
	}
}

func TestOptIATAParseOptIATA(t *testing.T) {
	data := []byte{
		1, 0, 0, 0, // IAID
		0, 5, 0, 0x18, 0x24, 1, 0xdb, 0, 0x30, 0x10, 0xc0, 0x8f, 0xfa, 0xce, 0, 0, 0, 0x44, 0, 0, 0, 0, 0xb2, 0x7a, 0, 0, 0xc0, 0x8a, // options
	}
	var opt OptIATA
	err := opt.FromBytes(data)
	require.NoError(t, err)
	require.Equal(t, OptionIATA, opt.Code())
}

func TestOptIATAParseOptIATAInvalidLength(t *testing.T) {
	data := []byte{
		1, 0, 0, // truncated IAID
	}
	var opt OptIATA
	err := opt.FromBytes(data)
	require.Error(t, err)
}

func TestOptIATAParseOptIATAInvalidOptions(t *testing.T) {
	data := []byte{
		1, 0, 0, 0, // IAID
		0, 5, 0, 0x18, 0x24, 1, 0xdb, 0, 0x30, 0x10, 0xc0, 0x8f, 0xfa, 0xce, 0, 0, 0, 0x44, 0, 0, 0, 0, 0xb2, 0x7a, // truncated options
	}
	var opt OptIATA
	err := opt.FromBytes(data)
	require.Error(t, err)
}

func TestOptIATAGetOneOption(t *testing.T) {
	oaddr := &OptIAAddress{
		IPv6Addr: net.ParseIP("::1"),
	}
	opt := OptIATA{
		Options: IdentityOptions{[]Option{&OptStatusCode{}, oaddr}},
	}
	require.Equal(t, oaddr, opt.Options.OneAddress())
}

func TestOptIATAAddOption(t *testing.T) {
	opt := OptIATA{}
	opt.Options.Add(OptElapsedTime(0))
	require.Equal(t, 1, len(opt.Options.Options))
	require.Equal(t, OptionElapsedTime, opt.Options.Options[0].Code())
}

func TestOptIATAGetOneOptionMissingOpt(t *testing.T) {
	oaddr := &OptIAAddress{
		IPv6Addr: net.ParseIP("::1"),
	}
	opt := OptIATA{
		Options: IdentityOptions{[]Option{&OptStatusCode{}, oaddr}},
	}
	require.Equal(t, nil, opt.Options.GetOne(OptionDNSRecursiveNameServer))
}

func TestOptIATADelOption(t *testing.T) {
	optiaaddr := OptIAAddress{}
	optsc := OptStatusCode{}

	iana1 := OptIATA{
		Options: IdentityOptions{[]Option{
			&optsc,
			&optiaaddr,
			&optiaaddr,
		}},
	}
	iana1.Options.Del(OptionIAAddr)
	require.Equal(t, iana1.Options.Options, Options{&optsc})

	iana2 := OptIATA{
		Options: IdentityOptions{[]Option{
			&optiaaddr,
			&optsc,
			&optiaaddr,
		}},
	}
	iana2.Options.Del(OptionIAAddr)
	require.Equal(t, iana2.Options.Options, Options{&optsc})
}

func TestOptIATAToBytes(t *testing.T) {
	opt := OptIATA{
		IaId: [4]byte{1, 2, 3, 4},
		Options: IdentityOptions{[]Option{
			OptElapsedTime(10 * time.Millisecond),
		}},
	}
	expected := []byte{
		1, 2, 3, 4, // IA ID
		0, 8, 0, 2, 0x00, 0x01,
	}
	require.Equal(t, expected, opt.ToBytes())
}

func TestOptIATAString(t *testing.T) {
	data := []byte{
		1, 0, 0, 0, // IAID
		0, 5, 0, 0x18, 0x24, 1, 0xdb, 0, 0x30, 0x10, 0xc0, 0x8f, 0xfa, 0xce, 0, 0, 0, 0x44, 0, 0, 0, 0, 0xb2, 0x7a, 0, 0, 0xc0, 0x8a, // options
	}
	var opt OptIATA
	err := opt.FromBytes(data)
	require.NoError(t, err)

	str := opt.String()
	require.Contains(
		t, str,
		"IAID=0x01000000",
		"String() should return the IAID",
	)
	require.Contains(
		t, str,
		"Options={",
		"String() should return a list of options",
	)
}