robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 8c77b5be50c50ab119

master
2.1 KiB91 linesraw
1local t = require('test.testutil')
2local parser = require('vim.net._ssh')
3local describe, it = t.describe, t.it
4local eq = t.eq
5
6describe('SSH parser', function()
7  it('parses SSH configuration strings', function()
8    local config = [[
9      Host *
10        ConnectTimeout 10
11        ServerAliveInterval 60
12        ServerAliveCountMax 3
13        # Use a specific key for any host not otherwise specified
14        # IdentityFile ~/.ssh/id_rsa
15
16      Host=dev
17        HostName=dev.example.com
18        User=devuser
19        Port=2222
20        IdentityFile=~/.ssh/id_rsa_dev
21
22      Host prod test
23        HostName 198.51.100.10
24        User admin
25        Port 22
26        IdentityFile ~/.ssh/id_rsa_prod
27        ForwardAgent yes
28
29      Host test
30        IdentitiesOnly yes
31
32      Host "quoted string"
33        User quote
34        Port 22
35
36      Match host foo host gh
37        HostName github.com
38        User git
39        IdentityFile ~/.ssh/id_rsa_github
40        IdentitiesOnly yes
41    ]]
42
43    eq({
44      'dev',
45      'prod',
46      'test',
47      'quoted string',
48      'gh',
49    }, parser.parse_ssh_config(config))
50  end)
51
52  it('ignores repeated and trailing separators', function()
53    -- Runs of separators between values, and trailing whitespace before the
54    -- line break, must not yield empty hostnames.
55    local config = table.concat({
56      'Host  alpha   beta ',
57      '  HostName example.com',
58      '',
59    }, '\n')
60
61    eq({ 'alpha', 'beta' }, parser.parse_ssh_config(config))
62  end)
63
64  it('fails when a quote is not closed', function()
65    local config = [[
66      Host prod dev "test prod my
67        HostName 198.51.100.10
68        User admin
69        Port 22
70        IdentityFile ~/.ssh/id_rsa_prod
71        ForwardAgent yes
72    ]]
73
74    local ok, _ = pcall(parser.parse_ssh_config, config)
75    eq(false, ok)
76  end)
77
78  it('fails when the line ends with a single backslash', function()
79    local config = [[
80      Host prod test
81        HostName 198.51.100.10
82        User admin\
83        Port 22
84        IdentityFile ~/.ssh/id_rsa_prod
85        ForwardAgent yes
86    ]]
87
88    local ok, _ = pcall(parser.parse_ssh_config, config)
89    eq(false, ok)
90  end)
91end)