Fix for known_host files containing * character

Quote sed output ("\1") retrieving "GlobalKnownHostsFile/UserKnownHostsFile" to
prevent bash globbing of special characters, e.g. '*'.

Added unit test "Config file containing star (*) should work".
Added test library function `assert_bash_list()'.
To run unit tests:

    cd test && ./runUnit _known_hosts_real.exp
This commit is contained in:
Freddy Vulto 2009-08-23 09:38:19 +02:00
parent 9af88e6bd4
commit 31f03cfb82
6 changed files with 42 additions and 4 deletions

View File

@ -1110,12 +1110,12 @@ _known_hosts_real()
if [ ${#config[@]} -gt 0 ]; then
local OIFS=$IFS IFS=$'\n'
# expand path (if present) to global known hosts file
global_kh=($( sed -ne 's/^[ \t]*[Gg][Ll][Oo][Bb][Aa][Ll][Kk][Nn][Oo][Ww][Nn][Hh][Oo][Ss][Tt][Ss][Ff][Ii][Ll][Ee]['"$'\t '"']*\(.*\)$/\1/p' "${config[@]}" ))
global_kh=($( sed -ne 's/^[ \t]*[Gg][Ll][Oo][Bb][Aa][Ll][Kk][Nn][Oo][Ww][Nn][Hh][Oo][Ss][Tt][Ss][Ff][Ii][Ll][Ee]['"$'\t '"']*\(.*\)$/"\1"/p' "${config[@]}" ))
for (( i=0; i < ${#global_kh[@]}; i++ )); do
global_kh[i]=$(echo "${global_kh[i]//\"/}")
done
# expand path (if present) to user known hosts file
user_kh=($( sed -ne 's/^[ \t]*[Uu][Ss][Ee][Rr][Kk][Nn][Oo][Ww][Nn][Hh][Oo][Ss][Tt][Ss][Ff][Ii][Ll][Ee]['"$'\t '"']*\(.*\)$/\1/p' "${config[@]}" ))
user_kh=($( sed -ne 's/^[ \t]*[Uu][Ss][Ee][Rr][Kk][Nn][Oo][Ww][Nn][Hh][Oo][Ss][Tt][Ss][Ff][Ii][Ll][Ee]['"$'\t '"']*\(.*\)$/"\1"/p' "${config[@]}" ))
for (( i=0; i < ${#user_kh[@]}; i++ )); do
user_kh[i]=$(echo "${user_kh[i]//\"/}")
done

View File

@ -0,0 +1,2 @@
star*d
tra,10.0.0.0 key

View File

@ -0,0 +1 @@
star*d2

View File

@ -0,0 +1,2 @@
UserKnownHostsFile fixtures/_known_hosts_real/known*hosts
UserKnownHostsFile "fixtures/_known_hosts_real/known*hosts2"

View File

@ -52,6 +52,29 @@ proc assert_bash_type {command} {
}; # assert_bash_type()
# Make sure the expected list is returned by executing the specified command.
# @param list $expected
# @param string $cmd Command given to generate items
# @param string $test (optional) Test titel. Default is "$cmd<TAB> should show completions"
# @param string $prompt (optional) Bash prompt. Default is "/@"
# @param integer $size (optional) Chunk size. Default is 20.
# @result boolean True if successful, False if not
proc assert_bash_list {expected cmd {test ""} {prompt /@} {size 20}} {
if {$test == ""} {set test "$cmd should show expected output"}
send "$cmd\r\n"
expect -ex "$cmd\r\n"
if {[match_items $expected $test]} {
expect {
-re $prompt { pass "$test" }
-re eof { unresolved "eof" }
}; # expect
} else {
fail "$test"
}; # if
}; # assert_bash_list()
# Make sure the expected items are returned by TAB-completing the specified
# command.
# @param list $expected
@ -328,7 +351,7 @@ proc match_items {items test {size 20}} {
for {set j 0} {$j < $size && $i + $j < [llength $items]} {incr j} {
set item "[lindex $items [expr {$i + $j}]]"
# Escape special regexp characters
regsub -all {([\[\]\(\)\.\\\+])} $item {\\\1} item
regsub -all {([\[\]\(\)\.\\\+\*])} $item {\\\1} item
append expected $item
if {[llength $items] > 1} {append expected {\s+}};
}; # for

View File

@ -57,7 +57,6 @@ expect {
sync_after_int
set test "Config file containing space should work"
set hosts [get_hosts]
# Hosts `gee' and `hus' are defined in ./fixtures/_known_hosts_real/spaced conf
@ -101,6 +100,17 @@ sync_after_int
assert_bash_exec "unset -v COMP_KNOWN_HOSTS_WITH_HOSTFILE"
sync_after_int
set test "Config file containing star (*) should work"
set hosts [get_hosts]
# Hosts `star*d', `tra' and `10.0.0.0' are defined in ./fixtures/_known_hosts_real/known*hosts
# Hosts `star*d2' is defined in ./fixtures/_known_hosts_real/known*hosts2
lappend hosts star*d star*d2 tra 10.0.0.0
set cmd {_known_hosts_real -aF 'fixtures/_known_hosts_real/star*conf' ''; echo_array COMPREPLY}
assert_bash_list $hosts $cmd $test
sync_after_int