File: config_test.go

package info (click to toggle)
golang-github-katalix-go-l2tp 0.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 632 kB
  • sloc: ansic: 127; sh: 10; makefile: 4
file content (303 lines) | stat: -rw-r--r-- 7,140 bytes parent folder | download
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package config

import (
	"fmt"
	"reflect"
	"strings"
	"testing"
	"time"

	"github.com/katalix/go-l2tp/l2tp"
)

func TestGetTunnels(t *testing.T) {
	cases := []struct {
		in   string
		want []NamedTunnel
	}{
		{
			in: `[tunnel.t1]
				 encap = "ip"
				 version = "l2tpv3"
				 peer = "82.9.90.101:1701"
				 tid = 412
				 ptid = 8192
				 framing_caps = ["sync"]
				 host_name = "blackhole.local"

				 [tunnel.t2]
				 encap = "udp"
				 version = "l2tpv2"
				 peer = "[2001:0000:1234:0000:0000:C1C0:ABCD:0876]:6543"
				 hello_timeout = 250
				 window_size = 10
				 retry_timeout = 250
				 max_retries = 2
				 framing_caps = ["sync","async"]
				 `,
			want: []NamedTunnel{
				{
					Name: "t1",
					Config: &l2tp.TunnelConfig{
						Encap:        l2tp.EncapTypeIP,
						Version:      l2tp.ProtocolVersion3,
						Peer:         "82.9.90.101:1701",
						TunnelID:     412,
						PeerTunnelID: 8192,
						FramingCaps:  l2tp.FramingCapSync,
						HostName:     "blackhole.local",
					},
				},
				{
					Name: "t2",
					Config: &l2tp.TunnelConfig{
						Encap:        l2tp.EncapTypeUDP,
						Version:      l2tp.ProtocolVersion2,
						Peer:         "[2001:0000:1234:0000:0000:C1C0:ABCD:0876]:6543",
						HelloTimeout: 250 * time.Millisecond,
						WindowSize:   10,
						RetryTimeout: 250 * time.Millisecond,
						MaxRetries:   2,
						FramingCaps:  l2tp.FramingCapSync | l2tp.FramingCapAsync,
					},
				},
			},
		},
		{
			in: `[tunnel.t1]
				 encap = "ip"
				 version = "l2tpv3"
				 peer = "127.0.0.1:5001"

				 [tunnel.t1.session.s1]
				 pseudowire = "eth"
				 cookie = [ 0x34, 0x04, 0xa9, 0xbe ]
				 peer_cookie = [ 0x80, 0x12, 0xff, 0x5b ]
				 seqnum = true
				 reorder_timeout = 1500
				 l2spec_type = "none"

				 [tunnel.t1.session.s2]
				 pseudowire = "ppp"
				 sid = 90210
				 psid = 1237812
				 interface_name = "becky"
				 l2spec_type = "default"

				 [tunnel.t1.session.s3]
				 pseudowire = "pppac"
				 pppoe_session_id = 5612
				 pppoe_peer_mac = [ 0xca, 0x6b, 0x7e, 0x93, 0xc4, 0xc3 ]
				`,
			want: []NamedTunnel{
				{
					Name: "t1",
					Config: &l2tp.TunnelConfig{
						Encap:       l2tp.EncapTypeIP,
						Version:     l2tp.ProtocolVersion3,
						Peer:        "127.0.0.1:5001",
						FramingCaps: l2tp.FramingCapSync | l2tp.FramingCapAsync,
					},
					Sessions: []NamedSession{
						{
							Name: "s1",
							Config: &l2tp.SessionConfig{
								Pseudowire:     l2tp.PseudowireTypeEth,
								Cookie:         []byte{0x34, 0x04, 0xa9, 0xbe},
								PeerCookie:     []byte{0x80, 0x12, 0xff, 0x5b},
								SeqNum:         true,
								ReorderTimeout: time.Millisecond * 1500,
								L2SpecType:     l2tp.L2SpecTypeNone,
							},
						},
						{
							Name: "s2",
							Config: &l2tp.SessionConfig{
								Pseudowire:    l2tp.PseudowireTypePPP,
								SessionID:     90210,
								PeerSessionID: 1237812,
								InterfaceName: "becky",
								L2SpecType:    l2tp.L2SpecTypeDefault,
							},
						},
						{
							Name: "s3",
							Config: &l2tp.SessionConfig{
								Pseudowire:     l2tp.PseudowireTypePPPAC,
								PPPoESessionId: 5612,
								PPPoEPeerMac:   [6]byte{0xca, 0x6b, 0x7e, 0x93, 0xc4, 0xc3},
							},
						},
					},
				},
			},
		},
	}
	for _, c := range cases {
		cfg, err := LoadString(c.in)
		if err != nil {
			t.Fatalf("LoadString(%v): %v", c.in, err)
		}
		for _, want_t := range c.want {
			got_t, err := cfg.findTunnelByName(want_t.Name)
			if err != nil {
				t.Fatalf("missing tunnel: %v", err)
			}
			for _, want_s := range want_t.Sessions {
				got_s, err := got_t.findSessionByName(want_s.Name)
				if err != nil {
					t.Fatalf("missing session: %v", err)
				}
				if !reflect.DeepEqual(got_s, &want_s) {
					t.Fatalf("got %v, want %v", got_s, want_s)
				}
			}
		}
	}
}

func (c *Config) findTunnelByName(name string) (*NamedTunnel, error) {
	for _, t := range c.Tunnels {
		if t.Name == name {
			return &t, nil
		}
	}
	return nil, fmt.Errorf("no tunnel of name %s", name)
}

func (t *NamedTunnel) findSessionByName(name string) (*NamedSession, error) {
	for _, s := range t.Sessions {
		if s.Name == name {
			return &s, nil
		}
	}
	return nil, fmt.Errorf("no session of name %s", name)
}

func TestBadConfig(t *testing.T) {
	cases := []struct {
		name string
		in   string
		estr string
	}{
		{
			name: "Bad type (int not string)",
			in: `[tunnel.t1]
				 encap = 42`,
			estr: "could not be parsed as a string",
		},
		{
			name: "Bad type (float not string)",
			in: `[tunnel.t1]
				 encap = 42.21`,
			estr: "could not be parsed as a string",
		},
		{
			name: "Bad type (array not string)",
			in: `[tunnel.t1]
				 version = [0x12, 0x34]`,
			estr: "could not be parsed as a string",
		},
		{
			name: "Bad type (bool not string)",
			in: `[tunnel.t1]
				 encap = false`,
			estr: "could not be parsed as a string",
		},
		{
			name: "Bad value (unrecognised encap)",
			in: `[tunnel.t1]
				 encap = "sausage"`,
			estr: "expect 'udp' or 'ip'",
		},
		{
			name: "Bad value (unrecognised version)",
			in: `[tunnel.t1]
				 version = "2001"`,
			estr: "expect 'l2tpv2' or 'l2tpv3'",
		},
		{
			name: "Bad value (unrecognised pseudowire)",
			in: `[tunnel.t1]
				 [tunnel.t1.session.s1]
				 pseudowire = "monkey"`,
			estr: "expect 'ppp', 'eth', or 'pppac'",
		},
		{
			name: "Bad value (unrecognised L2SpecType)",
			in: `[tunnel.t1]
				 [tunnel.t1.session.s1]
				 l2spec_type = "whizzoo"`,
			estr: "expect 'none' or 'default'",
		},
		{
			name: "Bad value (unrecognised FramingCap)",
			in: `[tunnel.t1]
				 framing_caps = [ "bizzle" ]`,
			estr: "expect 'sync' or 'async'",
		},
		{
			name: "Bad value (range exceeded)",
			in: `[tunnel.t1]
				 tid = 4294967297`,
			estr: "out of range",
		},
		{
			name: "Bad value (range exceeded)",
			in: `[tunnel.t1]
				 [tunnel.t1.session.s1]
				 cookie = [ 0x1e, 0xf0, 0x1fe, 0x24 ]`,
			estr: "out of range",
		},
		{
			name: "Malformed (no tunnel name)",
			in:   `[tunnel]`,
			estr: "tunnel instances must be named",
		},
		{
			name: "Malformed (no tunnel name 2)",
			in:   `tunnel = "t1"`,
			estr: "tunnel instances must be named",
		},
		{
			name: "Malformed (no session name)",
			in: `[tunnel.t1]
				 version = "l2tpv3"
				 [tunnel.t1.session]
				 pseudowire = "udp"`,
			estr: "session instances must be named",
		},
		{
			name: "Malformed (no session name)",
			in: `[tunnel.t1]
				 version = "l2tpv3"
				 session = 42`,
			estr: "session instances must be named",
		},
		{
			name: "Malformed (bad tunnel parameter)",
			in: `[tunnel.t1]
				 monkey = "banana"`,
			estr: "unrecognised parameter",
		},
		{
			name: "Malformed (bad session parameter)",
			in: `[tunnel.t1]
				 [tunnel.t1.session.s1]
				 whizz = 42`,
			estr: "unrecognised parameter",
		},
	}

	for _, tt := range cases {
		t.Run(tt.name, func(t *testing.T) {
			_, err := LoadString(tt.in)
			if err == nil {
				t.Fatalf("LoadString(%v) succeeded when we expected an error", tt.in)
			}
			if !strings.Contains(err.Error(), tt.estr) {
				t.Fatalf("LoadString(%v): error %q doesn't contain expected substring %q", tt.in, err, tt.estr)
			}
		})
	}
}