-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathmatch_test.go
More file actions
599 lines (561 loc) · 15.4 KB
/
Copy pathmatch_test.go
File metadata and controls
599 lines (561 loc) · 15.4 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
package ssh_config
import (
"strings"
"testing"
)
func TestMatchHostBasic(t *testing.T) {
us := &UserSettings{
userConfigFinder: testConfigFinder("testdata/match-host"),
}
val := us.Get("dev.example.com", "Port")
if val != "2222" {
t.Errorf("expected Port=2222 for dev.example.com, got %q", val)
}
val = us.Get("dev.example.com", "User")
if val != "admin" {
t.Errorf("expected User=admin for dev.example.com, got %q", val)
}
val = us.Get("dev.example.com", "IdentityFile")
if val != "~/.ssh/prod_key" {
t.Errorf("expected IdentityFile=~/.ssh/prod_key, got %q", val)
}
}
func TestMatchHostNoMatch(t *testing.T) {
us := &UserSettings{
userConfigFinder: testConfigFinder("testdata/match-host"),
systemConfigFinder: nullConfigFinder,
}
// "other.com" doesn't match *.example.com, should fall back to defaults
val := us.Get("other.com", "Port")
if val != "22" {
t.Errorf("expected default Port=22 for other.com, got %q", val)
}
val = us.Get("other.com", "User")
if val != "" {
t.Errorf("expected empty User for other.com, got %q", val)
}
}
func TestMatchHostNegation(t *testing.T) {
us := &UserSettings{
userConfigFinder: testConfigFinder("testdata/match-host-negation"),
}
// dev.example.com matches *.example.com and is not excluded by
// !*.test.example.com, so the Match block applies.
val := us.Get("dev.example.com", "Port")
if val != "2222" {
t.Errorf("expected Port=2222 for dev.example.com, got %q", val)
}
val = us.Get("dev.example.com", "User")
if val != "prod" {
t.Errorf("expected User=prod for dev.example.com, got %q", val)
}
// dev.test.example.com matches !*.test.example.com negation, so the
// Match block should NOT apply. The Host block should match instead.
val = us.Get("dev.test.example.com", "Port")
if val != "22" {
t.Errorf("expected Port=22 for dev.test.example.com (negated), got %q", val)
}
val = us.Get("dev.test.example.com", "User")
if val != "default" {
t.Errorf("expected User=default for dev.test.example.com (negated), got %q", val)
}
}
func TestMatchAll(t *testing.T) {
us := &UserSettings{
userConfigFinder: testConfigFinder("testdata/match-all"),
systemConfigFinder: nullConfigFinder,
}
// "special" matches the explicit Host block first
val := us.Get("special", "Port")
if val != "1111" {
t.Errorf("expected Port=1111 for special, got %q", val)
}
// "special" should also get User from the Match all block
val = us.Get("special", "User")
if val != "matchuser" {
t.Errorf("expected User=matchuser for special, got %q", val)
}
// An arbitrary host should match "Match all"
val = us.Get("anything.example.com", "Port")
if val != "4567" {
t.Errorf("expected Port=4567 for anything.example.com via Match all, got %q", val)
}
val = us.Get("anything.example.com", "User")
if val != "matchuser" {
t.Errorf("expected User=matchuser for anything.example.com via Match all, got %q", val)
}
}
func TestMatchMixed(t *testing.T) {
us := &UserSettings{
userConfigFinder: testConfigFinder("testdata/match-mixed"),
systemConfigFinder: nullConfigFinder,
}
// "bastion" matches the explicit Host block
val := us.Get("bastion", "Port")
if val != "22" {
t.Errorf("expected Port=22 for bastion, got %q", val)
}
val = us.Get("bastion", "User")
if val != "root" {
t.Errorf("expected User=root for bastion, got %q", val)
}
// app.prod.example.com matches "Match Host *.prod.example.com" and
// also "Host *.example.com". First match wins for each key.
val = us.Get("app.prod.example.com", "Port")
if val != "2222" {
t.Errorf("expected Port=2222 for app.prod.example.com, got %q", val)
}
val = us.Get("app.prod.example.com", "User")
if val != "deploy" {
t.Errorf("expected User=deploy for app.prod.example.com, got %q", val)
}
// app.staging.example.com matches "Host *.example.com" (Port 80)
// first, then "Match Host *.staging.example.com" (Port 3333).
// SSH semantics: first match wins per key.
val = us.Get("app.staging.example.com", "Port")
if val != "80" {
t.Errorf("expected Port=80 for app.staging.example.com (Host block first), got %q", val)
}
// plain.example.com matches "Host *.example.com" only
val = us.Get("plain.example.com", "Port")
if val != "80" {
t.Errorf("expected Port=80 for plain.example.com, got %q", val)
}
val = us.Get("plain.example.com", "User")
if val != "webuser" {
t.Errorf("expected User=webuser for plain.example.com, got %q", val)
}
// unknown host matches only "Match all"
val = us.Get("unknown.host", "User")
if val != "fallback" {
t.Errorf("expected User=fallback for unknown.host via Match all, got %q", val)
}
}
func TestMatchMixedGetAll(t *testing.T) {
us := &UserSettings{
userConfigFinder: testConfigFinder("testdata/match-mixed"),
systemConfigFinder: nullConfigFinder,
}
// app.prod.example.com should get both IdentityFiles from the Match Host
// block, plus the one from Match all.
vals := us.GetAll("app.prod.example.com", "IdentityFile")
want := []string{"~/.ssh/prod_key1", "~/.ssh/prod_key2", "~/.ssh/default_key"}
if len(vals) != len(want) {
t.Fatalf("GetAll IdentityFile for app.prod.example.com: got %d values %v, want %d values %v", len(vals), vals, len(want), want)
}
for i := range want {
if vals[i] != want[i] {
t.Errorf("GetAll IdentityFile[%d]: got %q, want %q", i, vals[i], want[i])
}
}
}
func TestMatchDirectiveInline(t *testing.T) {
tests := []struct {
name string
config string
alias string
key string
wantVal string
wantErr string
}{
{
name: "basic match host",
config: `Match Host *.example.com
Port 2222`,
alias: "test.example.com",
key: "Port",
wantVal: "2222",
},
{
name: "match host no match",
config: `Match Host *.example.com
Port 2222`,
alias: "test.other.com",
key: "Port",
wantVal: "",
},
{
name: "match all",
config: `Match all
Port 9999`,
alias: "anything",
key: "Port",
wantVal: "9999",
},
{
name: "match host multiple patterns",
config: `Match Host *.example.com *.example.org
Port 2222`,
alias: "test.example.org",
key: "Port",
wantVal: "2222",
},
{
name: "match host with comment",
config: `Match Host *.example.com # Production servers
Port 2222`,
alias: "test.example.com",
key: "Port",
wantVal: "2222",
},
{
name: "empty match should error",
config: `Match
Port 2222`,
wantErr: "Match directive requires",
},
{
name: "match host case insensitive",
config: `Match HOST *.example.com
Port 2222`,
alias: "test.example.com",
key: "Port",
wantVal: "2222",
},
{
name: "match host mixed case",
config: `Match HoSt *.example.com
Port 2222`,
alias: "test.example.com",
key: "Port",
wantVal: "2222",
},
{
name: "match all uppercase",
config: `Match ALL
Port 9999`,
alias: "anything",
key: "Port",
wantVal: "9999",
},
{
name: "match keyword itself case insensitive",
config: `MATCH Host *.example.com
Port 2222`,
alias: "test.example.com",
key: "Port",
wantVal: "2222",
},
{
name: "match host extra spaces between patterns",
config: `Match Host *.example.com *.example.org
Port 2222`,
alias: "test.example.org",
key: "Port",
wantVal: "2222",
},
{
name: "match host trailing spaces",
config: "Match Host *.example.com \n Port 2222",
alias: "test.example.com",
key: "Port",
wantVal: "2222",
},
{
name: "match host leading spaces on match line",
config: ` Match Host *.example.com
Port 2222`,
alias: "test.example.com",
key: "Port",
wantVal: "2222",
},
{
name: "host before match host, same pattern",
config: `Host *.example.com
Port 1111
Match Host *.example.com
Port 2222`,
alias: "test.example.com",
key: "Port",
wantVal: "1111", // Host block appears first, wins
},
{
name: "match host before host, same pattern",
config: `Match Host *.example.com
Port 2222
Host *.example.com
Port 1111`,
alias: "test.example.com",
key: "Port",
wantVal: "2222", // Match block appears first, wins
},
{
name: "match host provides key not in host",
config: `Host *.example.com
Port 1111
Match Host *.example.com
User admin`,
alias: "test.example.com",
key: "User",
wantVal: "admin", // Not set in Host, comes from Match
},
{
name: "match host negation excludes",
config: `Match Host *.example.com !staging.example.com
Port 2222`,
alias: "staging.example.com",
key: "Port",
wantVal: "",
},
{
name: "match host negation allows",
config: `Match Host *.example.com !staging.example.com
Port 2222`,
alias: "prod.example.com",
key: "Port",
wantVal: "2222",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg, err := Decode(strings.NewReader(tt.config))
if tt.wantErr != "" {
if err == nil {
t.Fatalf("expected error containing %q, got nil", tt.wantErr)
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("expected error containing %q, got %v", tt.wantErr, err)
}
return
}
if err != nil {
t.Fatalf("unexpected parse error: %v", err)
}
got, err := cfg.Get(tt.alias, tt.key)
if err != nil {
t.Fatalf("unexpected Get error: %v", err)
}
if got != tt.wantVal {
t.Errorf("Get(%q, %q) = %q, want %q", tt.alias, tt.key, got, tt.wantVal)
}
})
}
}
func TestMatchUnsupportedCriteria(t *testing.T) {
// Every Match criterion from the ssh_config manpage that we don't
// support, plus case variations and the special Exec case.
tests := []struct {
name string
config string
wantErr string
}{
// Exec gets its own error message because it's a security concern.
{
name: "exec lowercase",
config: "Match exec \"echo hello\"\n Port 22",
wantErr: "ssh_config: Match Exec is not supported",
},
{
name: "exec uppercase",
config: "Match EXEC \"echo hello\"\n Port 22",
wantErr: "ssh_config: Match Exec is not supported",
},
{
name: "exec mixed case",
config: "Match ExEc \"echo hello\"\n Port 22",
wantErr: "ssh_config: Match Exec is not supported",
},
{
name: "exec with complex command",
config: "Match Exec \"test -f /etc/ssh/flag\"\n Port 22",
wantErr: "ssh_config: Match Exec is not supported",
},
// All other unsupported criteria.
{
name: "user",
config: "Match User admin\n Port 22",
wantErr: "ssh_config: unsupported Match criterion",
},
{
name: "user uppercase",
config: "Match USER admin\n Port 22",
wantErr: "ssh_config: unsupported Match criterion",
},
{
name: "localuser",
config: "Match LocalUser kevin\n Port 22",
wantErr: "ssh_config: unsupported Match criterion",
},
{
name: "localuser uppercase",
config: "Match LOCALUSER kevin\n Port 22",
wantErr: "ssh_config: unsupported Match criterion",
},
{
name: "originalhost",
config: "Match OriginalHost *.example.com\n Port 22",
wantErr: "ssh_config: unsupported Match criterion",
},
{
name: "originalhost uppercase",
config: "Match ORIGINALHOST *.example.com\n Port 22",
wantErr: "ssh_config: unsupported Match criterion",
},
{
name: "canonical",
config: "Match canonical\n Port 22",
wantErr: "ssh_config: unsupported Match criterion",
},
{
name: "final",
config: "Match final\n Port 22",
wantErr: "ssh_config: unsupported Match criterion",
},
{
name: "tagged",
config: "Match Tagged mytag\n Port 22",
wantErr: "ssh_config: unsupported Match criterion",
},
{
name: "localnetwork",
config: "Match LocalNetwork 192.168.1.0/24\n Port 22",
wantErr: "ssh_config: unsupported Match criterion",
},
{
name: "completely bogus criterion",
config: "Match Bogus value\n Port 22",
wantErr: "ssh_config: unsupported Match criterion",
},
// Match Host with no patterns after it.
{
name: "match host with no patterns",
config: "Match Host\n Port 22",
wantErr: "ssh_config: Match Host requires at least one pattern",
},
// Match Host followed by only whitespace.
{
name: "match host only whitespace after",
config: "Match Host \n Port 22",
wantErr: "ssh_config: Match Host requires at least one pattern",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := Decode(strings.NewReader(tt.config))
if err == nil {
t.Fatalf("expected error containing %q, got nil", tt.wantErr)
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("expected error containing %q, got: %v", tt.wantErr, err)
}
})
}
}
func TestMatchDirectiveGetAll(t *testing.T) {
config := `Match Host *.prod.example.com
IdentityFile ~/.ssh/prod_key1
IdentityFile ~/.ssh/prod_key2
Match all
IdentityFile ~/.ssh/default_key`
cfg, err := Decode(strings.NewReader(config))
if err != nil {
t.Fatalf("unexpected parse error: %v", err)
}
vals, err := cfg.GetAll("app.prod.example.com", "IdentityFile")
if err != nil {
t.Fatalf("unexpected GetAll error: %v", err)
}
want := []string{"~/.ssh/prod_key1", "~/.ssh/prod_key2", "~/.ssh/default_key"}
if len(vals) != len(want) {
t.Fatalf("GetAll returned %d values %v, want %d values %v", len(vals), vals, len(want), want)
}
for i := range want {
if vals[i] != want[i] {
t.Errorf("GetAll[%d] = %q, want %q", i, vals[i], want[i])
}
}
}
func TestMatchStringRoundTrip(t *testing.T) {
tests := []struct {
name string
config string
}{
{
name: "match host",
config: `Match Host *.example.com
Port 2222
`,
},
{
name: "match all",
config: `Match all
Port 4567
`,
},
{
name: "match host with comment",
config: `Match Host *.example.com # production
Port 2222
`,
},
{
name: "match host multiple patterns",
config: `Match Host *.example.com *.example.org
Port 2222
`,
},
{
name: "match ALL uppercase round-trip",
config: `Match ALL
Port 4567
`,
},
{
name: "match All mixed case round-trip",
config: `Match All
Port 4567
`,
},
{
name: "mixed host and match",
config: `Host bastion
Port 22
Match Host *.example.com
Port 2222
Match all
User fallback
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg, err := Decode(strings.NewReader(tt.config))
if err != nil {
t.Fatalf("unexpected parse error: %v", err)
}
got := cfg.String()
if got != tt.config {
t.Errorf("round-trip mismatch:\ngot:\n%s\nwant:\n%s", got, tt.config)
}
})
}
}
func TestMatchFileRoundTrip(t *testing.T) {
for _, filename := range []string{
"testdata/match-host",
"testdata/match-all",
"testdata/match-mixed",
"testdata/match-host-negation",
} {
data := loadFile(t, filename)
cfg, err := Decode(strings.NewReader(string(data)))
if err != nil {
t.Fatalf("%s: unexpected parse error: %v", filename, err)
}
got := cfg.String()
if got != string(data) {
t.Errorf("%s: round-trip mismatch:\ngot:\n%s\nwant:\n%s", filename, got, string(data))
}
}
}
// TestMatchExistingDirectiveFile tests that the existing testdata/match-directive
// file (which contains "Match all") now parses successfully.
func TestMatchExistingDirectiveFile(t *testing.T) {
us := &UserSettings{
userConfigFinder: testConfigFinder("testdata/match-directive"),
}
val := us.Get("anyhost", "Port")
if val != "4567" {
t.Errorf("expected Port=4567 via Match all, got %q", val)
}
}