summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-07-23 23:18:33 +0700
committerShulhan <ms@kilabit.info>2023-07-24 12:59:28 +0700
commit075e8cd898f8d3fbd9e5ded6b2109214868b7de8 (patch)
treedb8c6027df8ee94082a3d9c299b2ca06a81b3409
parent19189b86ac0b00250456a23990584377f2b1f40e (diff)
downloadpakakeh.go-075e8cd898f8d3fbd9e5ded6b2109214868b7de8.tar.xz
ssh/config: store the section name
The section name is the string after "Host" or "Match" keyword. By storing the section name, we can tell which host or pattern the Section belong.
-rw-r--r--lib/ssh/config/config_test.go4
-rw-r--r--lib/ssh/config/section.go8
-rw-r--r--lib/ssh/config/section_match.go2
-rw-r--r--lib/ssh/config/section_match_test.go27
-rw-r--r--lib/ssh/config/section_test.go22
5 files changed, 33 insertions, 30 deletions
diff --git a/lib/ssh/config/config_test.go b/lib/ssh/config/config_test.go
index c832591c..790bb96e 100644
--- a/lib/ssh/config/config_test.go
+++ b/lib/ssh/config/config_test.go
@@ -14,7 +14,7 @@ import (
)
var (
- testDefaultSection = newSection()
+ testDefaultSection = newSection(``)
testParser *parser
)
@@ -71,6 +71,7 @@ func TestConfig_Get(t *testing.T) {
}, {
s: "example.local",
exp: func(def Section) *Section {
+ def.name = `example.local`
def.Hostname = "127.0.0.1"
def.User = "test"
def.PrivateKeyFile = ""
@@ -88,6 +89,7 @@ func TestConfig_Get(t *testing.T) {
}, {
s: "my.example.local",
exp: func(def Section) *Section {
+ def.name = `*.example.local`
def.Hostname = "127.0.0.2"
def.User = "wildcard"
def.PrivateKeyFile = ""
diff --git a/lib/ssh/config/section.go b/lib/ssh/config/section.go
index 13c2ea00..ab08b23b 100644
--- a/lib/ssh/config/section.go
+++ b/lib/ssh/config/section.go
@@ -155,6 +155,9 @@ type Section struct {
CanonicalizePermittedCNAMEs *PermittedCNAMEs
+ // name contains the raw value after Host or Match.
+ name string
+
AddKeysToAgent string
AddressFamily string
BindAddress string
@@ -212,11 +215,12 @@ type Section struct {
}
// newSection create new Host or Match with default values.
-func newSection() *Section {
+func newSection(name string) *Section {
return &Section{
Environments: map[string]string{},
Field: map[string]string{},
+ name: name,
AddKeysToAgent: valueNo,
AddressFamily: valueAny,
Port: defPort,
@@ -248,7 +252,7 @@ func newSection() *Section {
func newSectionHost(rawPattern string) (host *Section) {
patterns := strings.Fields(rawPattern)
- host = newSection()
+ host = newSection(rawPattern)
host.patterns = make([]*pattern, 0, len(patterns))
for _, pattern := range patterns {
diff --git a/lib/ssh/config/section_match.go b/lib/ssh/config/section_match.go
index acee3bc6..700e6f60 100644
--- a/lib/ssh/config/section_match.go
+++ b/lib/ssh/config/section_match.go
@@ -29,7 +29,7 @@ func newSectionMatch(rawPattern string) (match *Section, err error) {
criteria *matchCriteria
)
- match = newSection()
+ match = newSection(rawPattern)
match.criteria = make([]*matchCriteria, 0)
match.useCriteria = true
diff --git a/lib/ssh/config/section_match_test.go b/lib/ssh/config/section_match_test.go
index f6d8e704..f976afd7 100644
--- a/lib/ssh/config/section_match_test.go
+++ b/lib/ssh/config/section_match_test.go
@@ -5,8 +5,9 @@
package config
import (
- "reflect"
"testing"
+
+ "github.com/shuLhan/share/lib/test"
)
func TestNewSectionMatch(t *testing.T) {
@@ -30,9 +31,7 @@ func TestNewSectionMatch(t *testing.T) {
}
got.init(testParser.workDir, testParser.homeDir)
- if !reflect.DeepEqual(*c.exp, *got) {
- t.Fatalf("newSectionMatch: expecting %v, got %v", c.exp, got)
- }
+ test.Assert(t, c.raw, *c.exp, *got)
}
}
@@ -44,6 +43,7 @@ func TestParseCriteriaAll(t *testing.T) {
}{{
raw: "all ",
exp: func(exp Section) *Section {
+ exp.name = `all `
exp.criteria = []*matchCriteria{{
name: criteriaAll,
}}
@@ -54,6 +54,7 @@ func TestParseCriteriaAll(t *testing.T) {
raw: "canonical all",
exp: func(exp Section) *Section {
+ exp.name = `canonical all`
exp.criteria = []*matchCriteria{{
name: criteriaCanonical,
}, {
@@ -65,6 +66,7 @@ func TestParseCriteriaAll(t *testing.T) {
}, {
raw: "final all",
exp: func(exp Section) *Section {
+ exp.name = `final all`
exp.criteria = []*matchCriteria{{
name: criteriaFinal,
}, {
@@ -93,9 +95,7 @@ func TestParseCriteriaAll(t *testing.T) {
got.init(testParser.workDir, testParser.homeDir)
exp := c.exp(*testDefaultSection)
- if !reflect.DeepEqual(*exp, *got) {
- t.Fatalf("parseCriteriaAll: expecting %v, got %v", exp, got)
- }
+ test.Assert(t, c.raw, *exp, *got)
}
}
@@ -107,6 +107,7 @@ func TestNewSectionMatch_ParseCriteriaExec(t *testing.T) {
}{{
raw: `exec "echo true"`,
exp: func(exp Section) *Section {
+ exp.name = `exec "echo true"`
exp.criteria = []*matchCriteria{{
name: criteriaExec,
arg: `echo true`,
@@ -117,6 +118,7 @@ func TestNewSectionMatch_ParseCriteriaExec(t *testing.T) {
}, {
raw: `exec "echo true`,
exp: func(exp Section) *Section {
+ exp.name = `exec "echo true`
exp.criteria = []*matchCriteria{{
name: criteriaExec,
arg: `echo true`,
@@ -138,9 +140,7 @@ func TestNewSectionMatch_ParseCriteriaExec(t *testing.T) {
got.init(testParser.workDir, testParser.homeDir)
exp := c.exp(*testDefaultSection)
- if !reflect.DeepEqual(*exp, *got) {
- t.Fatalf("parseCriteriaExec: expecting %v, got %v", exp, got)
- }
+ test.Assert(t, c.raw, *exp, *got)
}
}
@@ -152,6 +152,7 @@ func TestParseCriteriaWithArg(t *testing.T) {
}{{
raw: `user name*`,
exp: func(exp Section) *Section {
+ exp.name = `user name*`
exp.criteria = []*matchCriteria{{
name: criteriaUser,
arg: `name*`,
@@ -165,6 +166,7 @@ func TestParseCriteriaWithArg(t *testing.T) {
}, {
raw: `user "a*,b*"`,
exp: func(exp Section) *Section {
+ exp.name = `user "a*,b*"`
exp.criteria = []*matchCriteria{{
name: criteriaUser,
arg: `a*,b*`,
@@ -180,6 +182,7 @@ func TestParseCriteriaWithArg(t *testing.T) {
}, {
raw: `user "a*,b*`,
exp: func(exp Section) *Section {
+ exp.name = `user "a*,b*`
exp.criteria = []*matchCriteria{{
name: criteriaUser,
arg: `a*,b*`,
@@ -206,8 +209,6 @@ func TestParseCriteriaWithArg(t *testing.T) {
got.init(testParser.workDir, testParser.homeDir)
exp := c.exp(*testDefaultSection)
- if !reflect.DeepEqual(*exp, *got) {
- t.Fatalf("parseCriteriaWithArg: expecting %v, got %v", exp, got)
- }
+ test.Assert(t, c.raw, *exp, *got)
}
}
diff --git a/lib/ssh/config/section_test.go b/lib/ssh/config/section_test.go
index ac022905..0f969f2d 100644
--- a/lib/ssh/config/section_test.go
+++ b/lib/ssh/config/section_test.go
@@ -5,8 +5,9 @@
package config
import (
- "reflect"
"testing"
+
+ "github.com/shuLhan/share/lib/test"
)
func TestNewSectionHost(t *testing.T) {
@@ -22,6 +23,7 @@ func TestNewSectionHost(t *testing.T) {
}, {
rawPattern: "*",
exp: func(exp Section) *Section {
+ exp.name = `*`
exp.patterns = []*pattern{{
value: "*",
}}
@@ -30,6 +32,7 @@ func TestNewSectionHost(t *testing.T) {
}, {
rawPattern: "192.168.1.?",
exp: func(exp Section) *Section {
+ exp.name = `192.168.1.?`
exp.patterns = []*pattern{{
value: `192.168.1.?`,
}}
@@ -38,6 +41,7 @@ func TestNewSectionHost(t *testing.T) {
}, {
rawPattern: "!*.co.uk *",
exp: func(exp Section) *Section {
+ exp.name = `!*.co.uk *`
exp.patterns = []*pattern{{
value: `*.co.uk`,
isNegate: true,
@@ -53,9 +57,7 @@ func TestNewSectionHost(t *testing.T) {
got.init(testParser.workDir, testParser.homeDir)
exp := c.exp(*testDefaultSection)
- if !reflect.DeepEqual(*exp, *got) {
- t.Fatalf("newSectionHost: expecting %v, got %v", exp, got)
- }
+ test.Assert(t, c.rawPattern, *exp, *got)
}
}
@@ -82,9 +84,7 @@ func TestSection_init(t *testing.T) {
got.init(testParser.workDir, testParser.homeDir)
exp := c.exp(*testDefaultSection)
- if !reflect.DeepEqual(exp.IdentityFile, got.IdentityFile) {
- t.Fatalf("init: expecting %v, got %v", exp, got)
- }
+ test.Assert(t, `init`, exp.IdentityFile, got.IdentityFile)
}
}
@@ -108,9 +108,7 @@ func TestSection_setEnv(t *testing.T) {
for _, c := range cases {
cfg.setEnv(c.value)
- if !reflect.DeepEqual(c.exp, cfg.Environments) {
- t.Fatalf("setEnv: expecting %v, got %v", c.exp, cfg.Environments)
- }
+ test.Assert(t, c.value, c.exp, cfg.Environments)
}
}
@@ -143,8 +141,6 @@ func TestSection_setSendEnv(t *testing.T) {
for _, c := range cases {
cfg.setSendEnv(envs, c.pattern)
- if !reflect.DeepEqual(c.exp, cfg.Environments) {
- t.Fatalf("setSendEnv: %s: expecting %v, got %v", c.pattern, c.exp, cfg.Environments)
- }
+ test.Assert(t, c.pattern, c.exp, cfg.Environments)
}
}