| 1 | package ssh_config |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "log" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func loadFile(t *testing.T, filename string) []byte { |
| 13 | t.Helper() |
| 14 | data, err := os.ReadFile(filename) |
| 15 | if err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | return data |
| 19 | } |
| 20 | |
| 21 | var files = []string{ |
| 22 | "testdata/config1", |
| 23 | "testdata/config2", |
| 24 | "testdata/eol-comments", |
| 25 | } |
| 26 | |
| 27 | func TestDecode(t *testing.T) { |
| 28 | for _, filename := range files { |
| 29 | data := loadFile(t, filename) |
| 30 | cfg, err := Decode(bytes.NewReader(data), false) |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | out := cfg.String() |
| 35 | if out != string(data) { |
| 36 | t.Errorf("%s out != data: got:\n%s\nwant:\n%s\n", filename, out, string(data)) |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func testConfigFinder(filename string) func() string { |
| 42 | return func() string { return filename } |
| 43 | } |
| 44 | |
| 45 | func nullConfigFinder() string { |
| 46 | return "" |
| 47 | } |
| 48 | |
| 49 | func TestGet(t *testing.T) { |
| 50 | us := &UserSettings{ |
| 51 | userConfigFinder: testConfigFinder("testdata/config1"), |
| 52 | } |
| 53 | |
| 54 | val := us.Get("wap", "User") |
| 55 | if val != "root" { |
| 56 | t.Errorf("expected to find User root, got %q", val) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestGetWithDefault(t *testing.T) { |
| 61 | us := &UserSettings{ |
| 62 | userConfigFinder: testConfigFinder("testdata/config1"), |
| 63 | } |
| 64 | |
| 65 | val, err := us.GetStrict("wap", "PasswordAuthentication") |
| 66 | if err != nil { |
| 67 | t.Fatalf("expected nil err, got %v", err) |
| 68 | } |
| 69 | if val != "yes" { |
| 70 | t.Errorf("expected to get PasswordAuthentication yes, got %q", val) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestGetAllWithDefault(t *testing.T) { |
| 75 | us := &UserSettings{ |
| 76 | userConfigFinder: testConfigFinder("testdata/config1"), |
| 77 | } |
| 78 | |
| 79 | val, err := us.GetAllStrict("wap", "PasswordAuthentication") |
| 80 | if err != nil { |
| 81 | t.Fatalf("expected nil err, got %v", err) |
| 82 | } |
| 83 | if len(val) != 1 || val[0] != "yes" { |
| 84 | t.Errorf("expected to get PasswordAuthentication yes, got %q", val) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestGetIdentities(t *testing.T) { |
| 89 | us := &UserSettings{ |
| 90 | userConfigFinder: testConfigFinder("testdata/identities"), |
| 91 | } |
| 92 | |
| 93 | val, err := us.GetAllStrict("hasidentity", "IdentityFile") |
| 94 | if err != nil { |
| 95 | t.Errorf("expected nil err, got %v", err) |
| 96 | } |
| 97 | if len(val) != 1 || val[0] != "file1" { |
| 98 | t.Errorf(`expected ["file1"], got %v`, val) |
| 99 | } |
| 100 | |
| 101 | val, err = us.GetAllStrict("has2identity", "IdentityFile") |
| 102 | if err != nil { |
| 103 | t.Errorf("expected nil err, got %v", err) |
| 104 | } |
| 105 | if len(val) != 2 || val[0] != "f1" || val[1] != "f2" { |
| 106 | t.Errorf(`expected [\"f1\", \"f2\"], got %v`, val) |
| 107 | } |
| 108 | |
| 109 | val, err = us.GetAllStrict("randomhost", "IdentityFile") |
| 110 | if err != nil { |
| 111 | t.Errorf("expected nil err, got %v", err) |
| 112 | } |
| 113 | if len(val) != len(defaultProtocol2Identities) { |
| 114 | // TODO: return the right values here. |
| 115 | log.Printf("expected defaults, got %v", val) |
| 116 | } else { |
| 117 | for i, v := range defaultProtocol2Identities { |
| 118 | if val[i] != v { |
| 119 | t.Errorf("invalid %d in val, expected %s got %s", i, v, val[i]) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | val, err = us.GetAllStrict("protocol1", "IdentityFile") |
| 125 | if err != nil { |
| 126 | t.Errorf("expected nil err, got %v", err) |
| 127 | } |
| 128 | if len(val) != 1 || val[0] != "~/.ssh/identity" { |
| 129 | t.Errorf("expected [\"~/.ssh/identity\"], got %v", val) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | func TestGetInvalidPort(t *testing.T) { |
| 134 | us := &UserSettings{ |
| 135 | userConfigFinder: testConfigFinder("testdata/invalid-port"), |
| 136 | } |
| 137 | |
| 138 | val, err := us.GetStrict("test.test", "Port") |
| 139 | if err == nil { |
| 140 | t.Fatalf("expected non-nil err, got nil") |
| 141 | } |
| 142 | if val != "" { |
| 143 | t.Errorf("expected to get '' for val, got %q", val) |
| 144 | } |
| 145 | if err.Error() != `ssh_config: strconv.ParseUint: parsing "notanumber": invalid syntax` { |
| 146 | t.Errorf("wrong error: got %v", err) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func TestGetNotFoundNoDefault(t *testing.T) { |
| 151 | us := &UserSettings{ |
| 152 | userConfigFinder: testConfigFinder("testdata/config1"), |
| 153 | } |
| 154 | |
| 155 | val, err := us.GetStrict("wap", "CanonicalDomains") |
| 156 | if err != nil { |
| 157 | t.Fatalf("expected nil err, got %v", err) |
| 158 | } |
| 159 | if val != "" { |
| 160 | t.Errorf("expected to get CanonicalDomains '', got %q", val) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | func TestGetAllNotFoundNoDefault(t *testing.T) { |
| 165 | us := &UserSettings{ |
| 166 | userConfigFinder: testConfigFinder("testdata/config1"), |
| 167 | } |
| 168 | |
| 169 | val, err := us.GetAllStrict("wap", "CanonicalDomains") |
| 170 | if err != nil { |
| 171 | t.Fatalf("expected nil err, got %v", err) |
| 172 | } |
| 173 | if len(val) != 0 { |
| 174 | t.Errorf("expected to get CanonicalDomains '', got %q", val) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | func TestGetWildcard(t *testing.T) { |
| 179 | us := &UserSettings{ |
| 180 | userConfigFinder: testConfigFinder("testdata/config3"), |
| 181 | } |
| 182 | |
| 183 | val := us.Get("bastion.stage.i.us.example.net", "Port") |
| 184 | if val != "22" { |
| 185 | t.Errorf("expected to find Port 22, got %q", val) |
| 186 | } |
| 187 | |
| 188 | val = us.Get("bastion.net", "Port") |
| 189 | if val != "25" { |
| 190 | t.Errorf("expected to find Port 24, got %q", val) |
| 191 | } |
| 192 | |
| 193 | val = us.Get("10.2.3.4", "Port") |
| 194 | if val != "23" { |
| 195 | t.Errorf("expected to find Port 23, got %q", val) |
| 196 | } |
| 197 | val = us.Get("101.2.3.4", "Port") |
| 198 | if val != "25" { |
| 199 | t.Errorf("expected to find Port 24, got %q", val) |
| 200 | } |
| 201 | val = us.Get("20.20.20.4", "Port") |
| 202 | if val != "24" { |
| 203 | t.Errorf("expected to find Port 24, got %q", val) |
| 204 | } |
| 205 | val = us.Get("20.20.20.20", "Port") |
| 206 | if val != "25" { |
| 207 | t.Errorf("expected to find Port 25, got %q", val) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | func TestGetExtraSpaces(t *testing.T) { |
| 212 | us := &UserSettings{ |
| 213 | userConfigFinder: testConfigFinder("testdata/extraspace"), |
| 214 | } |
| 215 | |
| 216 | val := us.Get("test.test", "Port") |
| 217 | if val != "1234" { |
| 218 | t.Errorf("expected to find Port 1234, got %q", val) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | func TestGetCaseInsensitive(t *testing.T) { |
| 223 | us := &UserSettings{ |
| 224 | userConfigFinder: testConfigFinder("testdata/config1"), |
| 225 | } |
| 226 | |
| 227 | val := us.Get("wap", "uSER") |
| 228 | if val != "root" { |
| 229 | t.Errorf("expected to find User root, got %q", val) |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | func TestGetEmpty(t *testing.T) { |
| 234 | us := &UserSettings{ |
| 235 | userConfigFinder: nullConfigFinder, |
| 236 | systemConfigFinder: nullConfigFinder, |
| 237 | } |
| 238 | val, err := us.GetStrict("wap", "User") |
| 239 | if err != nil { |
| 240 | t.Errorf("expected nil error, got %v", err) |
| 241 | } |
| 242 | if val != "" { |
| 243 | t.Errorf("expected to get empty string, got %q", val) |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | func TestGetEqsign(t *testing.T) { |
| 248 | us := &UserSettings{ |
| 249 | userConfigFinder: testConfigFinder("testdata/eqsign"), |
| 250 | } |
| 251 | |
| 252 | val := us.Get("test.test", "Port") |
| 253 | if val != "1234" { |
| 254 | t.Errorf("expected to find Port 1234, got %q", val) |
| 255 | } |
| 256 | val = us.Get("test.test", "Port2") |
| 257 | if val != "5678" { |
| 258 | t.Errorf("expected to find Port2 5678, got %q", val) |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | var includeFile = []byte(` |
| 263 | # This host should not exist, so we can use it for test purposes / it won't |
| 264 | # interfere with any other configurations. |
| 265 | Host kevinburke.ssh_config.test.example.com |
| 266 | Port 4567 |
| 267 | `) |
| 268 | |
| 269 | func TestInclude(t *testing.T) { |
| 270 | if testing.Short() { |
| 271 | t.Skip("skipping fs write in short mode") |
| 272 | } |
| 273 | testPath := filepath.Join(homedir(), ".ssh", "kevinburke-ssh-config-test-file") |
| 274 | err := os.WriteFile(testPath, includeFile, 0644) |
| 275 | if err != nil { |
| 276 | t.Skipf("couldn't write SSH config file: %v", err.Error()) |
| 277 | } |
| 278 | defer os.Remove(testPath) |
| 279 | us := &UserSettings{ |
| 280 | userConfigFinder: testConfigFinder("testdata/include"), |
| 281 | } |
| 282 | val := us.Get("kevinburke.ssh_config.test.example.com", "Port") |
| 283 | if val != "4567" { |
| 284 | t.Errorf("expected to find Port=4567 in included file, got %q", val) |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | func TestIncludeSystem(t *testing.T) { |
| 289 | if testing.Short() { |
| 290 | t.Skip("skipping fs write in short mode") |
| 291 | } |
| 292 | testPath := filepath.Join("/", "etc", "ssh", "kevinburke-ssh-config-test-file") |
| 293 | err := os.WriteFile(testPath, includeFile, 0644) |
| 294 | if err != nil { |
| 295 | t.Skipf("couldn't write SSH config file: %v", err.Error()) |
| 296 | } |
| 297 | defer os.Remove(testPath) |
| 298 | us := &UserSettings{ |
| 299 | systemConfigFinder: testConfigFinder("testdata/include"), |
| 300 | } |
| 301 | val := us.Get("kevinburke.ssh_config.test.example.com", "Port") |
| 302 | if val != "4567" { |
| 303 | t.Errorf("expected to find Port=4567 in included file, got %q", val) |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | var recursiveIncludeFile = []byte(` |
| 308 | Host kevinburke.ssh_config.test.example.com |
| 309 | Include kevinburke-ssh-config-recursive-include |
| 310 | `) |
| 311 | |
| 312 | func TestIncludeRecursive(t *testing.T) { |
| 313 | if testing.Short() { |
| 314 | t.Skip("skipping fs write in short mode") |
| 315 | } |
| 316 | testPath := filepath.Join(homedir(), ".ssh", "kevinburke-ssh-config-recursive-include") |
| 317 | err := os.WriteFile(testPath, recursiveIncludeFile, 0644) |
| 318 | if err != nil { |
| 319 | t.Skipf("couldn't write SSH config file: %v", err.Error()) |
| 320 | } |
| 321 | defer os.Remove(testPath) |
| 322 | us := &UserSettings{ |
| 323 | userConfigFinder: testConfigFinder("testdata/include-recursive"), |
| 324 | } |
| 325 | val, err := us.GetStrict("kevinburke.ssh_config.test.example.com", "Port") |
| 326 | if err != ErrDepthExceeded { |
| 327 | t.Errorf("Recursive include: expected ErrDepthExceeded, got %v", err) |
| 328 | } |
| 329 | if val != "" { |
| 330 | t.Errorf("non-empty string value %s", val) |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | func TestIncludeString(t *testing.T) { |
| 335 | if testing.Short() { |
| 336 | t.Skip("skipping fs write in short mode") |
| 337 | } |
| 338 | data, err := os.ReadFile("testdata/include") |
| 339 | if err != nil { |
| 340 | log.Fatal(err) |
| 341 | } |
| 342 | c, err := Decode(bytes.NewReader(data), false) |
| 343 | if err != nil { |
| 344 | t.Fatal(err) |
| 345 | } |
| 346 | s := c.String() |
| 347 | if s != string(data) { |
| 348 | t.Errorf("mismatch: got %q\nwant %q", s, string(data)) |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | var matchTests = []struct { |
| 353 | in []string |
| 354 | alias string |
| 355 | want bool |
| 356 | }{ |
| 357 | {[]string{"*"}, "any.test", true}, |
| 358 | {[]string{"a", "b", "*", "c"}, "any.test", true}, |
| 359 | {[]string{"a", "b", "c"}, "any.test", false}, |
| 360 | {[]string{"any.test"}, "any1test", false}, |
| 361 | {[]string{"192.168.0.?"}, "192.168.0.1", true}, |
| 362 | {[]string{"192.168.0.?"}, "192.168.0.10", false}, |
| 363 | {[]string{"*.co.uk"}, "bbc.co.uk", true}, |
| 364 | {[]string{"*.co.uk"}, "subdomain.bbc.co.uk", true}, |
| 365 | {[]string{"*.*.co.uk"}, "bbc.co.uk", false}, |
| 366 | {[]string{"*.*.co.uk"}, "subdomain.bbc.co.uk", true}, |
| 367 | {[]string{"*.example.com", "!*.dialup.example.com", "foo.dialup.example.com"}, "foo.dialup.example.com", false}, |
| 368 | {[]string{"test.*", "!test.host"}, "test.host", false}, |
| 369 | } |
| 370 | |
| 371 | func TestMatches(t *testing.T) { |
| 372 | for _, tt := range matchTests { |
| 373 | patterns := make([]*Pattern, len(tt.in)) |
| 374 | for i := range tt.in { |
| 375 | pat, err := NewPattern(tt.in[i]) |
| 376 | if err != nil { |
| 377 | t.Fatalf("error compiling pattern %s: %v", tt.in[i], err) |
| 378 | } |
| 379 | patterns[i] = pat |
| 380 | } |
| 381 | host := &Host{ |
| 382 | Patterns: patterns, |
| 383 | } |
| 384 | got := host.Matches(tt.alias) |
| 385 | if got != tt.want { |
| 386 | t.Errorf("host(%q).Matches(%q): got %v, want %v", tt.in, tt.alias, got, tt.want) |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | func TestMatchUnsupported(t *testing.T) { |
| 392 | us := &UserSettings{ |
| 393 | userConfigFinder: testConfigFinder("testdata/match-directive"), |
| 394 | } |
| 395 | |
| 396 | _, err := us.GetStrict("test.test", "Port") |
| 397 | if err == nil { |
| 398 | t.Fatal("expected Match directive to error, didn't") |
| 399 | } |
| 400 | if !strings.Contains(err.Error(), "ssh_config: Match directive parsing is unsupported") { |
| 401 | t.Errorf("wrong error: %v", err) |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | func TestIndexInRange(t *testing.T) { |
| 406 | us := &UserSettings{ |
| 407 | userConfigFinder: testConfigFinder("testdata/config4"), |
| 408 | } |
| 409 | |
| 410 | user, err := us.GetStrict("wap", "User") |
| 411 | if err != nil { |
| 412 | t.Fatal(err) |
| 413 | } |
| 414 | if user != "root" { |
| 415 | t.Errorf("expected User to be %q, got %q", "root", user) |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | func TestDosLinesEndingsDecode(t *testing.T) { |
| 420 | us := &UserSettings{ |
| 421 | userConfigFinder: testConfigFinder("testdata/dos-lines"), |
| 422 | } |
| 423 | |
| 424 | user, err := us.GetStrict("wap", "User") |
| 425 | if err != nil { |
| 426 | t.Fatal(err) |
| 427 | } |
| 428 | |
| 429 | if user != "root" { |
| 430 | t.Errorf("expected User to be %q, got %q", "root", user) |
| 431 | } |
| 432 | |
| 433 | host, err := us.GetStrict("wap2", "HostName") |
| 434 | if err != nil { |
| 435 | t.Fatal(err) |
| 436 | } |
| 437 | |
| 438 | if host != "8.8.8.8" { |
| 439 | t.Errorf("expected HostName to be %q, got %q", "8.8.8.8", host) |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | func TestNoTrailingNewline(t *testing.T) { |
| 444 | us := &UserSettings{ |
| 445 | userConfigFinder: testConfigFinder("testdata/config-no-ending-newline"), |
| 446 | systemConfigFinder: nullConfigFinder, |
| 447 | } |
| 448 | |
| 449 | port, err := us.GetStrict("example", "Port") |
| 450 | if err != nil { |
| 451 | t.Fatal(err) |
| 452 | } |
| 453 | |
| 454 | if port != "4242" { |
| 455 | t.Errorf("wrong port: got %q want 4242", port) |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | func TestCustomFinder(t *testing.T) { |
| 460 | us := &UserSettings{} |
| 461 | us.ConfigFinder(func() string { |
| 462 | return "testdata/config1" |
| 463 | }) |
| 464 | |
| 465 | val := us.Get("wap", "User") |
| 466 | if val != "root" { |
| 467 | t.Errorf("expected to find User root, got %q", val) |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | func TestCustomFinderWhenIgnoringMatchDirective(t *testing.T) { |
| 472 | us := &UserSettings{ |
| 473 | IgnoreMatchDirective: true, |
| 474 | } |
| 475 | us.ConfigFinder(func() string { |
| 476 | return "testdata/config1-with-match-directive" |
| 477 | }) |
| 478 | |
| 479 | val := us.Get("git.yahoo.com", "HostName") |
| 480 | if val != "git.proxy.com" { |
| 481 | t.Errorf("expected to find Hostname git.proxy.com, got %q", val) |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | func TestCustomFinderWhenNotIgnoringMatchDirective(t *testing.T) { |
| 486 | us := &UserSettings{} |
| 487 | us.ConfigFinder(func() string { |
| 488 | return "testdata/config1-with-match-directive" |
| 489 | }) |
| 490 | |
| 491 | val := us.Get("git.yahoo.com", "HostName") |
| 492 | if val != "" { |
| 493 | t.Errorf("expected to find Hostname empty %q", val) |
| 494 | } |
| 495 | } |