summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/option_iaaddress_test.go
blob: d77d3ff19905816fe7b502ce3ba94fe901b84324 (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
package dhcpv6

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

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

func TestIAAddressParseAndGetter(t *testing.T) {
	for i, tt := range []struct {
		buf  []byte
		err  error
		want []*OptIAAddress
	}{
		{
			buf: []byte{
				0, 5, // IAAddr option
				0, 0x18, // length
				0x24, 1, 0xdb, 0, 0x30, 0x10, 0xc0, 0x8f, 0xfa, 0xce, 0, 0, 0, 0x44, 0, 0, // IPv6
				0, 0, 0, 2, // PreferredLifetime
				0, 0, 0, 4, // ValidLifetime
			},
			want: []*OptIAAddress{
				&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{}},
				},
			},
		},
		{
			buf: []byte{
				0, 5, // IAAddr option
				0, 32, // length
				0x24, 1, 0xdb, 0, 0x30, 0x10, 0xc0, 0x8f, 0xfa, 0xce, 0, 0, 0, 0x44, 0, 0, // IPv6
				0, 0, 0, 2, // PreferredLifetime
				0, 0, 0, 4, // ValidLifetime
				0, 13, // option status code
				0, 4, // length
				0, 0, // StatusSuccess,
				'O', 'K', // OK
			},
			want: []*OptIAAddress{
				&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{
						&OptStatusCode{StatusCode: iana.StatusSuccess, StatusMessage: "OK"},
					}},
				},
			},
		},
		{
			buf: []byte{
				0, 5, // IAAddr option
				0, 0x18, // length
				0x24, 1, 0xdb, 0, 0x30, 0x10, 0xc0, 0x8f, 0xfa, 0xce, 0, 0, 0, 0x44, 0, 0, // IPv6
				0, 0, 0, 2, // PreferredLifetime
				0, 0, 0, 4, // ValidLifetime

				0, 5, // IAAddr option
				0, 0x18, // length
				0x24, 1, 0xdb, 0, 0x30, 0x10, 0xc0, 0x8f, 0xfa, 0xce, 0, 0, 0, 0x44, 0, 0, // IPv6
				0, 0, 0, 2, // PreferredLifetime
				0, 0, 0, 4, // ValidLifetime
			},
			want: []*OptIAAddress{
				&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{}},
				},
				&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{}},
				},
			},
		},
		{
			buf:  []byte{0, 3, 0, 1, 0},
			want: nil,
			err:  uio.ErrUnreadBytes,
		},
		{
			buf: []byte{
				0, 5, // IAAddr option code
				0, 4, // length
				0, 0, 0, 1, // truncated IP
			},
			want: nil,
			err:  uio.ErrBufferTooShort,
		},
		{
			buf: []byte{
				0, 5, // IAAddr option
				0, 28, // length
				0x24, 1, 0xdb, 0, 0x30, 0x10, 0xc0, 0x8f, 0xfa, 0xce, 0, 0, 0, 0x44, 0, 0, // IPv6
				0, 0, 0, 2, // PreferredLifetime
				0, 0, 0, 4, // ValidLifetime
				0, 13, // option status code
				0, 1, // length
				// option too short
			},
			err: uio.ErrBufferTooShort,
		},
	} {
		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
			var io IdentityOptions
			if err := io.FromBytes(tt.buf); !errors.Is(err, tt.err) {
				t.Errorf("FromBytes = %v, want %v", err, tt.err)
			}
			if got := io.Addresses(); !reflect.DeepEqual(got, tt.want) {
				t.Errorf("Addresses = %v, want %v", got, tt.want)
			}

			var wantOneAddr *OptIAAddress
			if len(tt.want) >= 1 {
				wantOneAddr = tt.want[0]
			}
			if got := io.OneAddress(); !reflect.DeepEqual(got, wantOneAddr) {
				t.Errorf("OneAddress = %v, want %v", got, wantOneAddr)
			}

			if len(tt.want) >= 1 {
				var b IdentityOptions
				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 TestOptIAAddressString(t *testing.T) {
	ipaddr := []byte{0x24, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
	data := append(ipaddr, []byte{
		0x00, 0x00, 0x00, 70, // preferred lifetime
		0x00, 0x00, 0x00, 50, // valid lifetime
		0, 8, 0, 2, 0xaa, 0xbb, // options
	}...)
	var opt OptIAAddress
	err := opt.FromBytes(data)
	require.NoError(t, err)

	str := opt.String()
	require.Contains(
		t, str,
		"IP=2401:203:405:607:809:a0b:c0d:e0f",
		"String() should return the ipv6addr",
	)
	require.Contains(
		t, str,
		"PreferredLifetime=1m10s",
		"String() should return the preferredlifetime",
	)
	require.Contains(
		t, str,
		"ValidLifetime=50s",
		"String() should return the validlifetime",
	)
}