summaryrefslogtreecommitdiff
path: root/test/unit/_known_hosts_real.exp
blob: 01b09b6b9bc2510c2e54e7d2bb9c31c81e61e1ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
proc setup {} {
    # NOTE: Changing dir to $SRCDIR is necessary because file locations in the
    #       ssh config files (e.g. UserKnownHostsFile) are relative to $SRCDIR.
    assert_bash_exec {cd $SRCDIR}
    save_env
}


proc teardown {} {
    assert_env_unmodified {
        /COMPREPLY=/d
        /OLDHOME=/d
    }
    assert_bash_exec {cd $TESTDIR}
}


setup


set test "Hosts should be put in COMPREPLY"
set hosts [get_hosts -unsorted]
# Hosts `gee', `hus' and `jar' are defined in
# ./fixtures/_known_hosts_real/config
# doo, ike, jub, 10.0.0.1, kyl, 100.0.0.2, 10.10.0.3, blah, and bunch of IPv6
# test cases in ./fixtures/_known_hosts_real/known_hosts
lappend hosts blah doo gee hus ike jar jub kyl 10.0.0.1 100.0.0.2 10.10.0.3 fd00:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:5555 fe80::123:0xff:dead:beef%eth0 1111:2222:3333:4444:5555:6666:xxxx:abab 11xx:2222:3333:4444:5555:6666:xxxx:abab ::42
set cmd {unset COMPREPLY; _known_hosts_real -aF fixtures/_known_hosts_real/config ''; echo_array COMPREPLY}
assert_bash_list $hosts $cmd $test -sort
sync_after_int

set test "Hosts should have username prefix and colon suffix"
set hosts [get_hosts -unsorted]
# Hosts `gee', `hus' and `jar' are defined in
# ./fixtures/_known_hosts_real/config
# doo, ike, jub, 10.0.0.1, kyl, 100.0.0.2, 10.10.0.3, blah, and bunch of IPv6
# test cases in ./fixtures/_known_hosts_real/known_hosts
lappend hosts blah doo gee hus ike jar jub kyl 10.0.0.1 100.0.0.2 10.10.0.3 fd00:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:5555 fe80::123:0xff:dead:beef%eth0 1111:2222:3333:4444:5555:6666:xxxx:abab 11xx:2222:3333:4444:5555:6666:xxxx:abab ::42
set hosts [lsort -ascii $hosts]
set expected {}
foreach host $hosts {
    lappend expected "user@$host:"
}
# Call _known_hosts
set cmd {unset COMPREPLY; _known_hosts_real -acF fixtures/_known_hosts_real/config 'user@'; echo_array COMPREPLY}
assert_bash_list $expected $cmd $test -sort
sync_after_int

set test "Files containing consecutive spaces should work"
set hosts [get_hosts -unsorted]
set hosts_orig $hosts
# Hosts `gee' and `hus' are defined in
#`./fixtures/_known_hosts_real/spaced  conf'
# Hosts `two*' are defined in ./fixtures/_known_hosts_real/known_hosts2
lappend hosts gee hus two two2 two3 two4
set hosts_config $hosts
# Hosts `doo' and `ike' are defined in
# `./fixtures/_known_hosts_/spaced  known_hosts'
lappend hosts doo ike
set hosts        [join [bash_sort $hosts       ] "\\s+"]
set hosts_orig   [join [bash_sort $hosts_orig  ] "\\s+"]
set hosts_config [join [bash_sort $hosts_config] "\\s+"]
# Call _known_hosts
set cmd {unset COMPREPLY; _known_hosts_real -aF 'fixtures/_known_hosts_real/spaced  conf' ''; echo_array COMPREPLY}
send "$cmd\r"
expect -ex "$cmd\r\n"
expect {
    -re "^$hosts\r\n/@$"  { pass "$test" }
    -re "^$hosts_orig\r\n/@$"  { fail "$test (config file)" }
    -re "^$hosts_config\r\n/@$"  { fail "$test (known hosts file)" }
    -re /@ { unresolved "$test at prompt" }
    default { unresolved "$test" }
}
sync_after_int

set test "Files starting with tilde (~) should work"
set hosts [get_hosts -unsorted]
# Hosts `two*' are defined in ./fixtures/_known_hosts_real/known_hosts2
# Host `three' is defined in ./fixtures/_known_hosts_real/known_hosts3
# Host `four' is defined in ./fixtures/_known_hosts_real/known_hosts4
lappend hosts two two2 two3 two4 three four
set hosts [join [bash_sort $hosts] "\\s+"]
# Setup environment
set cmd {OLDHOME=$HOME; HOME=$SRCDIRABS}
send "$cmd\r"
expect -ex "$cmd\r\n/@"
# Call _known_hosts
set cmd {unset COMPREPLY; _known_hosts_real -aF fixtures/_known_hosts_real/config_tilde ''; echo_array COMPREPLY}
send "$cmd\r"
expect -ex "$cmd\r\n"
expect {
    -re "^$hosts\r\n/@$"  { pass "$test" }
    default { unresolved "$test" }
}
# Teardown environment
set cmd {HOME=$OLDHOME}
send "$cmd\r"
expect -ex "$cmd\r\n/@"
sync_after_int

set test "Empty COMP_KNOWN_HOSTS_WITH_HOSTFILE should omit HOSTFILE"
assert_bash_exec "COMP_KNOWN_HOSTS_WITH_HOSTFILE="
set hosts [get_hosts_avahi]
# Hosts `gee', `hus' and `jar' are defined in
# ./fixtures/_known_hosts_real/config
# doo, ike, jub, 10.0.0.1, kyl, 100.0.0.2, 10.10.0.3, blah, and bunch of IPv6
# test cases in ./fixtures/_known_hosts_real/known_hosts
lappend hosts blah doo gee hus ike jar jub kyl 10.0.0.1 100.0.0.2 10.10.0.3 fd00:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:5555 fe80::123:0xff:dead:beef%eth0 1111:2222:3333:4444:5555:6666:xxxx:abab 11xx:2222:3333:4444:5555:6666:xxxx:abab ::42
# Call _known_hosts
set cmd {unset COMPREPLY; _known_hosts_real -aF fixtures/_known_hosts_real/config ''; echo_array COMPREPLY}
assert_bash_list $hosts $cmd $test -sort
sync_after_int
assert_bash_exec "unset -v COMP_KNOWN_HOSTS_WITH_HOSTFILE"
sync_after_int

set test "Included config files should work"
set hosts [get_hosts -unsorted]
# Host 'recursion' is defined in
# ./fixtures/_known_hosts_real/config_include_recursion
# Host 'relative_path' is defined in
# ./fixtures/_known_hosts_real/.ssh/config_relative_path
lappend hosts recursion relative_path
set hosts [join [bash_sort $hosts] "\\s+"]
# Setup environment
# Redefined HOME to handle relative path inclusions on $HOME/.ssh
set cmd {OLDHOME=$HOME; HOME="$SRCDIRABS/fixtures/_known_hosts_real"}
send "$cmd\r"
expect -ex "$cmd\r\n/@"
# Call _known_hosts
set cmd {unset COMPREPLY; _known_hosts_real -aF fixtures/_known_hosts_real/config_include ''; echo_array COMPREPLY}
send "$cmd\r"
expect -ex "$cmd\r\n"
expect {
    -re "^$hosts\r\n/@$"  { pass "$test" }
    default { unresolved "$test" }
}
# Teardown environment
set cmd {HOME=$OLDHOME}
send "$cmd\r"
expect -ex "$cmd\r\n/@"
sync_after_int

set test "IPv6 filtering should work"
set hosts "127.0.0.1 localhost"
assert_bash_exec "COMP_KNOWN_HOSTS_WITH_HOSTFILE="
set cmd {unset COMPREPLY; _known_hosts_real -4F fixtures/_known_hosts_real/localhost_config ''; echo_array COMPREPLY}
assert_bash_list $hosts $cmd $test -sort
sync_after_int
assert_bash_exec "unset -v COMP_KNOWN_HOSTS_WITH_HOSTFILE"
sync_after_int

set test "IPv4 filtering should work"
set hosts "::1 localhost"
assert_bash_exec "COMP_KNOWN_HOSTS_WITH_HOSTFILE="
set cmd {unset COMPREPLY; _known_hosts_real -6F fixtures/_known_hosts_real/localhost_config ''; echo_array COMPREPLY}
assert_bash_list $hosts $cmd $test -sort
sync_after_int
assert_bash_exec "unset -v COMP_KNOWN_HOSTS_WITH_HOSTFILE"
sync_after_int

set test "IPv4+IPv6 filtering should work"
set hosts "localhost"
assert_bash_exec "COMP_KNOWN_HOSTS_WITH_HOSTFILE="
set cmd {unset COMPREPLY; _known_hosts_real -46F fixtures/_known_hosts_real/localhost_config ''; echo_array COMPREPLY}
assert_bash_list $hosts $cmd $test -sort
sync_after_int
assert_bash_exec "unset -v COMP_KNOWN_HOSTS_WITH_HOSTFILE"
sync_after_int


teardown