summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_iapd_test.go
blob: da72f0cacd75f361ad86803142190c6efb9ff956 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package dhcpv6

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

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

func TestIAPDParseAndGetter(t *testing.T) {
	for i, tt := range []struct {
		buf  []byte
		err  error
		want []*OptIAPD
	}{
		{
			buf: []byte{
				0, 25, // IAPD option code
				0, 41, // length
				1, 0, 0, 0, // IAID
				0, 0, 0, 1, // T1
				0, 0, 0, 2, // T2
				0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length
				0, 0, 0, 2, // IAPrefix preferredLifetime
				0, 0, 0, 4, // IAPrefix validLifetime
				36,                                             // IAPrefix prefixLength
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix
			},
			want: []*OptIAPD{
				&OptIAPD{
					IaId: [4]byte{1, 0, 0, 0},
					T1:   1 * time.Second,
					T2:   2 * time.Second,
					Options: PDOptions{Options: Options{&OptIAPrefix{
						PreferredLifetime: 2 * time.Second,
						ValidLifetime:     4 * time.Second,
						Prefix: &net.IPNet{
							Mask: net.CIDRMask(36, 128),
							IP:   net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
						},
						Options: PrefixOptions{Options: Options{}},
					}}},
				},
			},
		},
		{
			buf: []byte{
				0, 25, // IAPD option code
				0, 41, // length
				1, 0, 0, 0, // IAID
				0, 0, 0, 1, // T1
				0, 0, 0, 2, // T2
				0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length
				0, 0, 0, 2, // IAPrefix preferredLifetime
				0, 0, 0, 4, // IAPrefix validLifetime
				36,                                             // IAPrefix prefixLength
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix

				0, 25, // IAPD option code
				0, 41, // length
				1, 2, 3, 4, // IAID
				0, 0, 0, 5, // T1
				0, 0, 0, 6, // T2
				0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length
				0, 0, 0, 2, // IAPrefix preferredLifetime
				0, 0, 0, 4, // IAPrefix validLifetime
				36,                                             // IAPrefix prefixLength
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix
			},
			want: []*OptIAPD{
				&OptIAPD{
					IaId: [4]byte{1, 0, 0, 0},
					T1:   1 * time.Second,
					T2:   2 * time.Second,
					Options: PDOptions{Options: Options{&OptIAPrefix{
						PreferredLifetime: 2 * time.Second,
						ValidLifetime:     4 * time.Second,
						Prefix: &net.IPNet{
							Mask: net.CIDRMask(36, 128),
							IP:   net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
						},
						Options: PrefixOptions{Options: Options{}},
					}}},
				},
				&OptIAPD{
					IaId: [4]byte{1, 2, 3, 4},
					T1:   5 * time.Second,
					T2:   6 * time.Second,
					Options: PDOptions{Options: Options{&OptIAPrefix{
						PreferredLifetime: 2 * time.Second,
						ValidLifetime:     4 * time.Second,
						Prefix: &net.IPNet{
							Mask: net.CIDRMask(36, 128),
							IP:   net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
						},
						Options: PrefixOptions{Options: Options{}},
					}}},
				},
			},
		},
		{
			buf:  nil,
			want: nil,
		},
		{
			buf:  []byte{0, 25, 0, 1, 0},
			want: nil,
			err:  uio.ErrBufferTooShort,
		},
		{
			buf: []byte{
				0, 25, // IAPD option code
				0, 8, // length
				1, 0, 0, 0, // IAID
				0, 0, 0, 1, // T1
				// truncated from here
			},
			want: nil,
			err:  uio.ErrBufferTooShort,
		},
		{
			buf: []byte{
				0, 25, // IAPD option code
				0, 36, // length
				1, 0, 0, 0, // IAID
				0, 0, 0, 1, // T1
				0, 0, 0, 2, // T2
				0, 26, 0, 4, // 26 = IAPrefix Option, 4 = length
				0, 0, 0, 2, // IAPrefix preferredLifetime
				// Missing stuff
			},
			want: nil,
			err:  uio.ErrBufferTooShort,
		},
	} {
		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.IAPD(); !reflect.DeepEqual(got, tt.want) {
				t.Errorf("IAPD = %v, want %v", got, tt.want)
			}
			var wantOne *OptIAPD
			if len(tt.want) >= 1 {
				wantOne = tt.want[0]
			}
			if got := mo.OneIAPD(); !reflect.DeepEqual(got, wantOne) {
				t.Errorf("OneIAPD = %v, want %v", got, wantOne)
			}

			if len(tt.want) >= 1 {
				var b MessageOptions
				for _, iana := range tt.want {
					b.Add(iana)
				}
				got := b.ToBytes()
				if diff := cmp.Diff(tt.buf, got); diff != "" {
					t.Errorf("ToBytes mismatch (-want, +got): %s", diff)
				}
			}
		})
	}
}

func TestOptIAPDString(t *testing.T) {
	data := []byte{
		1, 0, 0, 0, // IAID
		0, 0, 0, 1, // T1
		0, 0, 0, 2, // T2
		0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length
		0xaa, 0xbb, 0xcc, 0xdd, // IAPrefix preferredLifetime
		0xee, 0xff, 0x00, 0x11, // IAPrefix validLifetime
		36,                                             // IAPrefix prefixLength
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix
	}
	var opt OptIAPD
	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,
		"T1=1s T2=2s",
		"String() should return the T1/T2 options",
	)
	require.Contains(
		t, str,
		"Options={",
		"String() should return a list of options",
	)
}