Skip to content

Commit

Permalink
add failing tests for uploading and downloading files with awful file…
Browse files Browse the repository at this point in the history
…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 Unknown committed Aug 16, 2010
1 parent d3cc5c4 commit 6263198
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
15 changes: 15 additions & 0 deletions test/common.rb
Expand Up @@ -54,6 +54,21 @@ def prepare_directory(path, mode=0777, mtime=Time.now, atime=Time.now)
directory.stub! directory.stub!
end 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 class FileEntry
attr_reader :path, :contents, :mode, :mtime, :atime, :io attr_reader :path, :contents, :mode, :mtime, :atime, :io


Expand Down
10 changes: 5 additions & 5 deletions test/test_download.rb
Expand Up @@ -12,18 +12,18 @@ def test_download_file_should_transfer_file
assert_equal "a" * 1234, file.io.string assert_equal "a" * 1234, file.io.string
end end


def test_download_file_with_spaces_in_name_should_escape_remote_file_name def test_download_file_with_metacharacters_in_name_should_escape_remote_file_name
file = prepare_file("/path/to/local file.txt", "") 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.sends_ok
channel.gets_data "C0666 0 local file.txt\n" channel.gets_data "C0666 0 #{awful_file_name}\n"
channel.sends_ok channel.sends_ok
channel.gets_ok channel.gets_ok
channel.sends_ok channel.sends_ok
end 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 end


def test_download_with_preserve_should_send_times def test_download_with_preserve_should_send_times
Expand Down
10 changes: 5 additions & 5 deletions test/test_upload.rb
Expand Up @@ -16,18 +16,18 @@ def test_upload_file_should_transfer_file
assert_scripted { scp.upload!("/path/to/local.txt", "/path/to/remote.txt") } assert_scripted { scp.upload!("/path/to/local.txt", "/path/to/remote.txt") }
end end


def test_upload_file_with_spaces_in_name_should_escape_remote_file_name def test_upload_file_with_metacharacters_in_name_should_escape_remote_file_name
prepare_file("/path/to/local file.txt", "") 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.gets_ok
channel.sends_data "C0666 0 local file.txt\n" channel.sends_data "C0666 0 #{awful_file_name}\n"
channel.gets_ok channel.gets_ok
channel.sends_ok channel.sends_ok
channel.gets_ok channel.gets_ok
end 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 end


def test_upload_file_with_preserve_should_send_times def test_upload_file_with_preserve_should_send_times
Expand Down

0 comments on commit 6263198

Please sign in to comment.