Permalink
Browse files

add failing tests for uploading and downloading files with awful file…

…names

Note that newlines are valid filename characters, and are even common with some
unsophisticated users. Mocha is failing at stubbing file objects with newlines,
so I left them out.
  • Loading branch information...
guns authored and net-ssh committed Aug 16, 2010
1 parent d3cc5c4 commit 62631983f3d2a4177b1d518c16e20b296d6fe404
Showing with 25 additions and 10 deletions.
  1. +15 −0 test/common.rb
  2. +5 −5 test/test_download.rb
  3. +5 −5 test/test_upload.rb
@@ -54,6 +54,21 @@ def prepare_directory(path, mode=0777, mtime=Time.now, atime=Time.now)
directory.stub!
end

# The POSIX spec unfortunately allows all characters in file names except
# ASCII 0x00(NUL) and 0x2F(/)
#
# Ideally, we should be testing filenames with newlines, but Mocha doesn't
# like this at all, so we leave them out. However, the Shellwords module
# handles newlines just fine, so we can be reasonably confident that they
# will work in practice
def awful_file_name
(((0x00..0x7f).to_a - [0x00, 0x0a, 0x2f]).map { |n| n.chr }).join + '.txt'
end

def escaped_file_name
"\\\001\\\002\\\003\\\004\\\005\\\006\\\a\\\b\\\t\\\v\\\f\\\r\\\016\\\017\\\020\\\021\\\022\\\023\\\024\\\025\\\026\\\027\\\030\\\031\\\032\\\e\\\034\\\035\\\036\\\037\\ \\!\\\"\\#\\$\\%\\&\\'\\(\\)\\*\\+,-.0123456789:\\;\\<\\=\\>\\?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\[\\\\\\]\\^_\\`abcdefghijklmnopqrstuvwxyz\\{\\|\\}\\~\\\177.txt"
end

class FileEntry
attr_reader :path, :contents, :mode, :mtime, :atime, :io

@@ -12,18 +12,18 @@ def test_download_file_should_transfer_file
assert_equal "a" * 1234, file.io.string
end

def test_download_file_with_spaces_in_name_should_escape_remote_file_name
file = prepare_file("/path/to/local file.txt", "")
def test_download_file_with_metacharacters_in_name_should_escape_remote_file_name
file = prepare_file("/path/to/local/#{awful_file_name}", "")

expect_scp_session "-f /path/to/remote\\ file.txt" do |channel|
expect_scp_session "-f /path/to/remote/#{escaped_file_name}" do |channel|
channel.sends_ok
channel.gets_data "C0666 0 local file.txt\n"
channel.gets_data "C0666 0 #{awful_file_name}\n"
channel.sends_ok
channel.gets_ok
channel.sends_ok
end

assert_scripted { scp.download!("/path/to/remote file.txt", "/path/to/local file.txt") }
assert_scripted { scp.download!("/path/to/remote/#{awful_file_name}", "/path/to/local/#{awful_file_name}") }
end

def test_download_with_preserve_should_send_times
@@ -16,18 +16,18 @@ def test_upload_file_should_transfer_file
assert_scripted { scp.upload!("/path/to/local.txt", "/path/to/remote.txt") }
end

def test_upload_file_with_spaces_in_name_should_escape_remote_file_name
prepare_file("/path/to/local file.txt", "")
def test_upload_file_with_metacharacters_in_name_should_escape_remote_file_name
prepare_file("/path/to/local/#{awful_file_name}", "")

expect_scp_session "-t /path/to/remote\\ file.txt" do |channel|
expect_scp_session "-t /path/to/remote/#{escaped_file_name}" do |channel|
channel.gets_ok
channel.sends_data "C0666 0 local file.txt\n"
channel.sends_data "C0666 0 #{awful_file_name}\n"
channel.gets_ok
channel.sends_ok
channel.gets_ok
end

assert_scripted { scp.upload!("/path/to/local file.txt", "/path/to/remote file.txt") }
assert_scripted { scp.upload!("/path/to/local/#{awful_file_name}", "/path/to/remote/#{awful_file_name}") }
end

def test_upload_file_with_preserve_should_send_times

0 comments on commit 6263198

Please sign in to comment.