From 1c4ddb97d7751cb75e2bd2254423be27aeff4de3 Mon Sep 17 00:00:00 2001 From: Kevin Burke Date: Wed, 24 May 2017 21:30:13 -0700 Subject: [PATCH] use Default() and validate() --- config.go | 25 ++++++++++++++++++------ config_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++ testdata/invalid-port | 2 ++ 3 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 testdata/invalid-port diff --git a/config.go b/config.go index c7120b9..b4f467e 100644 --- a/config.go +++ b/config.go @@ -82,7 +82,14 @@ func findVal(c *Config, alias, key string) (string, error) { if c == nil { return "", nil } - return c.Get(alias, key) + val, err := c.Get(alias, key) + if err != nil || val == "" { + return "", err + } + if err := validate(key, val); err != nil { + return "", err + } + return val, nil } // Get finds the first value for key within a declaration that matches the @@ -98,8 +105,9 @@ func Get(alias, key string) string { } // GetStrict finds the first value for key within a declaration that matches the -// alias. For more information on how patterns are matched, see the manpage for -// ssh_config. +// alias. If key has a default value and no matching configuration is found, the +// default will be returned. For more information on default values and the way +// patterns are matched, see the manpage for ssh_config. // // error will be non-nil if and only if a user's configuration file or the // system configuration file could not be parsed, and u.IgnoreErrors is false. @@ -124,8 +132,9 @@ func (u *UserSettings) Get(alias, key string) string { } // GetStrict finds the first value for key within a declaration that matches the -// alias. For more information on how patterns are matched, see the manpage for -// ssh_config. +// alias. If key has a default value and no matching configuration is found, the +// default will be returned. For more information on default values and the way +// patterns are matched, see the manpage for ssh_config. // // error will be non-nil if and only if a user's configuration file or the // system configuration file could not be parsed, and u.IgnoreErrors is false. @@ -162,7 +171,11 @@ func (u *UserSettings) GetStrict(alias, key string) (string, error) { if err != nil || val != "" { return val, err } - return findVal(u.systemConfig, alias, key) + val2, err2 := findVal(u.systemConfig, alias, key) + if err2 != nil || val2 != "" { + return val2, err2 + } + return Default(key), nil } func parseFile(filename string) (*Config, error) { diff --git a/config_test.go b/config_test.go index 0726004..66762c7 100644 --- a/config_test.go +++ b/config_test.go @@ -52,6 +52,51 @@ func TestGet(t *testing.T) { } } +func TestGetWithDefault(t *testing.T) { + us := &UserSettings{ + userConfigFinder: testConfigFinder("testdata/config1"), + } + + val, err := us.GetStrict("wap", "PasswordAuthentication") + if err != nil { + t.Fatalf("expected nil err, got %v", err) + } + if val != "yes" { + t.Errorf("expected to get PasswordAuthentication yes, got %q", val) + } +} + +func TestGetInvalidPort(t *testing.T) { + us := &UserSettings{ + userConfigFinder: testConfigFinder("testdata/invalid-port"), + } + + val, err := us.GetStrict("test.test", "Port") + if err == nil { + t.Fatalf("expected non-nil err, got nil") + } + if val != "" { + t.Errorf("expected to get '' for val, got %q", val) + } + if err.Error() != `ssh_config: strconv.ParseUint: parsing "notanumber": invalid syntax` { + t.Errorf("wrong error: got %v", err) + } +} + +func TestGetNotFoundNoDefault(t *testing.T) { + us := &UserSettings{ + userConfigFinder: testConfigFinder("testdata/config1"), + } + + val, err := us.GetStrict("wap", "CanonicalDomains") + if err != nil { + t.Fatalf("expected nil err, got %v", err) + } + if val != "" { + t.Errorf("expected to get CanonicalDomains '', got %q", val) + } +} + func TestGetWildcard(t *testing.T) { us := &UserSettings{ userConfigFinder: testConfigFinder("testdata/config3"), diff --git a/testdata/invalid-port b/testdata/invalid-port new file mode 100644 index 0000000..845b918 --- /dev/null +++ b/testdata/invalid-port @@ -0,0 +1,2 @@ +Host test.test + Port notanumber