aboutsummaryrefslogtreecommitdiff
path: root/lib/ssh
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-12-18 23:52:07 +0700
committerShulhan <ms@kilabit.info>2023-12-19 00:11:01 +0700
commit7a2a2949f6787d5301bfb9cd6e592658c7515818 (patch)
tree95dff759c1ba7333c559af481023dde4e5928eca /lib/ssh
parentbea2e36962bc30cc7b8ca396665d2bd716b8d7e2 (diff)
downloadpakakeh.go-7a2a2949f6787d5301bfb9cd6e592658c7515818.tar.xz
ssh/config: rewrite unit tests for [Config.Get] using [lib.test.Data]
Using test.Data make the output easy to audit and modified.
Diffstat (limited to 'lib/ssh')
-rw-r--r--lib/ssh/config/config_test.go106
-rw-r--r--lib/ssh/config/testdata/config_get_test.txt43
2 files changed, 73 insertions, 76 deletions
diff --git a/lib/ssh/config/config_test.go b/lib/ssh/config/config_test.go
index 51f4c032..30e0ccdc 100644
--- a/lib/ssh/config/config_test.go
+++ b/lib/ssh/config/config_test.go
@@ -5,9 +5,9 @@
package config
import (
+ "bytes"
"log"
"os"
- "path/filepath"
"testing"
"github.com/shuLhan/share/lib/test"
@@ -54,99 +54,53 @@ func TestPatternToRegex(t *testing.T) {
}
}
-func TestConfig_Get(t *testing.T) {
+func TestConfigGet(t *testing.T) {
type testCase struct {
- exp func() Section
- s string
+ name string
+ exp string
}
var (
- cfg *Config
- err error
+ tdata *test.Data
+ err error
)
+ tdata, err = test.LoadData(`testdata/config_get_test.txt`)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ var cfg *Config
+
cfg, err = Load(`./testdata/config`)
if err != nil {
t.Fatal(err)
}
- var listTestCase = []testCase{{
- s: ``,
- exp: func() Section {
- var sec = *testDefaultSection
- return sec
- },
+ var cases = []testCase{{
+ name: ``,
+ exp: string(tdata.Output[`empty`]),
}, {
- s: `example.local`,
- exp: func() Section {
- var sec = *testDefaultSection
- sec.name = `example.local`
- sec.IdentityFile = []string{
- filepath.Join(testDefaultSection.homeDir, `.ssh`, `notexist`),
- }
- sec.Field = map[string]string{
- KeyChallengeResponseAuthentication: ValueYes,
- KeyCheckHostIP: ValueYes,
- KeyConnectionAttempts: DefConnectionAttempts,
- KeyHostname: `127.0.0.1`,
- KeyIdentityFile: `~/.ssh/notexist`,
- KeyPort: DefPort,
- KeyUser: `test`,
- KeyXAuthLocation: DefXAuthLocation,
- }
- return sec
- },
+ name: `example.local`,
+ exp: string(tdata.Output[`example.local`]),
}, {
- s: `my.example.local`,
- exp: func() Section {
- var sec = *testDefaultSection
- sec.name = `my.example.local`
- sec.IdentityFile = []string{
- filepath.Join(testDefaultSection.homeDir, `.ssh`, `notexist`),
- }
- sec.Field = map[string]string{
- KeyChallengeResponseAuthentication: ValueYes,
- KeyCheckHostIP: ValueYes,
- KeyConnectionAttempts: DefConnectionAttempts,
- KeyHostname: `127.0.0.2`,
- KeyIdentityFile: `~/.ssh/notexist`,
- KeyPort: DefPort,
- KeyUser: `wildcard`,
- KeyXAuthLocation: DefXAuthLocation,
- }
- return sec
- },
+ name: `my.example.local`,
+ exp: string(tdata.Output[`my.example.local`]),
}, {
- s: `foo.local`,
- exp: func() Section {
- var sec = *testDefaultSection
- sec.name = `foo.local`
- sec.IdentityFile = []string{
- filepath.Join(testDefaultSection.homeDir, `.ssh`, `foo`),
- filepath.Join(testDefaultSection.homeDir, `.ssh`, `allfoo`),
- }
- sec.Field = map[string]string{
- KeyChallengeResponseAuthentication: ValueYes,
- KeyCheckHostIP: ValueYes,
- KeyConnectionAttempts: DefConnectionAttempts,
- KeyHostname: `127.0.0.3`,
- KeyPort: DefPort,
- KeyUser: `allfoo`,
- KeyIdentityFile: `~/.ssh/allfoo`,
- KeyXAuthLocation: DefXAuthLocation,
- }
- return sec
- },
+ name: `foo.local`,
+ exp: string(tdata.Output[`foo.local`]),
}}
var (
- c testCase
- got *Section
+ section *Section
+ buf bytes.Buffer
+ c testCase
)
-
- for _, c = range listTestCase {
- got = cfg.Get(c.s)
- test.Assert(t, c.s, c.exp(), *got)
+ for _, c = range cases {
+ section = cfg.Get(c.name)
+ buf.Reset()
+ section.WriteTo(&buf)
+ test.Assert(t, c.name, c.exp, buf.String())
}
}
diff --git a/lib/ssh/config/testdata/config_get_test.txt b/lib/ssh/config/testdata/config_get_test.txt
new file mode 100644
index 00000000..21735853
--- /dev/null
+++ b/lib/ssh/config/testdata/config_get_test.txt
@@ -0,0 +1,43 @@
+Test output for [Section.Get].
+
+<<< empty
+Host *
+ challengeresponseauthentication yes
+ checkhostip yes
+ connectionattempts 1
+ port 22
+ xauthlocation /usr/X11R6/bin/xauth
+
+<<< example.local
+Host example.local
+ challengeresponseauthentication yes
+ checkhostip yes
+ connectionattempts 1
+ hostname 127.0.0.1
+ identityfile ~/.ssh/notexist
+ port 22
+ user test
+ xauthlocation /usr/X11R6/bin/xauth
+
+<<< my.example.local
+Host my.example.local
+ challengeresponseauthentication yes
+ checkhostip yes
+ connectionattempts 1
+ hostname 127.0.0.2
+ identityfile ~/.ssh/notexist
+ port 22
+ user wildcard
+ xauthlocation /usr/X11R6/bin/xauth
+
+<<< foo.local
+Host foo.local
+ challengeresponseauthentication yes
+ checkhostip yes
+ connectionattempts 1
+ hostname 127.0.0.3
+ identityfile ~/.ssh/foo
+ identityfile ~/.ssh/allfoo
+ port 22
+ user allfoo
+ xauthlocation /usr/X11R6/bin/xauth