From 2373b663049df69a168f1e876e817a49d8ed8a7b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 18 Mar 2009 01:01:25 -0700 Subject: tests: do not trust (our correct use of) 1.9 encodings Despite reading numerous articles and inspecting the 1.9.1-p0 C source, I will never trust that we're always handling encoding-aware IO objects correctly. Thus this new test uses UNIX shell utilities that should always operate on files/sockets on a byte-level. Signed-off-by: Eric Wong --- test/test_helper.rb | 13 ++++++++++++- test/unit/test_upload.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/test_helper.rb b/test/test_helper.rb index f809af3..4243606 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -41,7 +41,18 @@ def redirect_test_io STDOUT.reopen(orig_out) end end - + +# which(1) exit codes cannot be trusted on some systems +# We use UNIX shell utilities in some tests because we don't trust +# ourselves to write Ruby 100% correctly :) +def which(bin) + ex = ENV['PATH'].split(/:/).detect do |x| + x << "/#{bin}" + File.executable?(x) + end or warn "`#{bin}' not found in PATH=#{ENV['PATH']}" + ex +end + # Either takes a string to do a get request against, or a tuple of [URI, HTTP] where # HTTP is some kind of Net::HTTP request object (POST, HEAD, etc.) def hit(uris) diff --git a/test/unit/test_upload.rb b/test/unit/test_upload.rb index edc94da..41fc473 100644 --- a/test/unit/test_upload.rb +++ b/test/unit/test_upload.rb @@ -135,6 +135,34 @@ class UploadTest < Test::Unit::TestCase assert_equal resp[:size], new_tmp.stat.size end + # Despite reading numerous articles and inspecting the 1.9.1-p0 C + # source, Eric Wong will never trust that we're always handling + # encoding-aware IO objects correctly. Thus this test uses shell + # utilities that should always operate on files/sockets on a + # byte-level. + def test_uncomfortable_with_onenine_encodings + # POSIX doesn't require all of these to be present on a system + which('curl') or return + which('sha1sum') or return + which('dd') or return + + start_server(@sha1_app) + + tmp = Tempfile.new('dd_dest') + assert(system("dd", "if=#{@random.path}", "of=#{tmp.path}", + "bs=#{@bs}", "count=#{@count}"), + "dd #@random to #{tmp}") + sha1_re = %r!\b([a-f0-9]{40})\b! + sha1_out = `sha1sum #{tmp.path}` + assert $?.success?, 'sha1sum ran OK' + + assert_match(sha1_re, sha1_out) + sha1 = sha1_re.match(sha1_out)[1] + resp = `curl -isSfN -T#{tmp.path} http://#@addr:#@port/` + assert $?.success?, 'curl ran OK' + assert_match(%r!\b#{sha1}\b!, resp) + end + private def length -- cgit v1.2.3-24-ge0c7