| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Mongrel
Revision: 1033
Author: luislavena
Date: 27 Jul 2008 08:47:46
Changes:Fixed tests to work under Windows.
- Backported #process_based_port functionality form 1.2 to allow concurrency during CI.
- Make /tmp/testfile work with Windows.
| ... | ...@@ -64,3 +64,16 @@ | |
| 64 | 64 | |
| 65 | 65 | return results |
| 66 | 66 | end |
| 67 | ||
| 68 | # process_based_port provides a port number, usable for TCP and UDP | |
| 69 | # connections based on $$ and with a 6000 as base. | |
| 70 | # this is required if you perform several builds of mongrel in parallel | |
| 71 | # (like continuous integration systems) | |
| 72 | def process_based_port | |
| 73 | 6000 + $$ % 1000 | |
| 74 | end | |
| 75 | ||
| 76 | # Platform check helper ;-) | |
| 77 | def windows? | |
| 78 | result = RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ | |
| 79 | end |
| ... | ...@@ -34,9 +34,10 @@ | |
| 34 | 34 | class HandlersTest < Test::Unit::TestCase |
| 35 | 35 | |
| 36 | 36 | def setup |
| 37 | @port = process_based_port | |
| 37 | 38 | stats = Mongrel::StatisticsFilter.new(:sample_rate => 1) |
| 38 | 39 | |
| 39 | @config = Mongrel::Configurator.new :host => '127.0.0.1', :port => 9998 do | |
| 40 | @config = Mongrel::Configurator.new :host => '127.0.0.1', :port => @port do | |
| 40 | 41 | listener do |
| 41 | 42 | uri "/", :handler => SimpleHandler.new |
| 42 | 43 | uri "/", :handler => stats |
| ... | ...@@ -49,50 +50,52 @@ | |
| 49 | 50 | uri "/relative", :handler => Mongrel::DirHandler.new(nil, listing_allowed=false, index_html="none") |
| 50 | 51 | end |
| 51 | 52 | end |
| 52 | ||
| 53 | File.open("/tmp/testfile", 'w') do | |
| 53 | ||
| 54 | @test_file = windows? ? "testfile" : "tmp/testfile" | |
| 55 | ||
| 56 | File.open("/#{@test_file}", 'w') do | |
| 54 | 57 | # Do nothing |
| 55 | 58 | end |
| 56 | ||
| 59 | ||
| 57 | 60 | @config.run |
| 58 | 61 | end |
| 59 | 62 | |
| 60 | 63 | def teardown |
| 61 | 64 | @config.stop(false, true) |
| 62 | File.delete "/tmp/testfile" | |
| 65 | File.delete "/#{@test_file}" | |
| 63 | 66 | end |
| 64 | 67 | |
| 65 | 68 | def test_more_web_server |
| 66 | res = hit([ "http://localhost:9998/test", | |
| 67 | "http://localhost:9998/dumb", | |
| 68 | "http://localhost:9998/404", | |
| 69 | "http://localhost:9998/files/rdoc/index.html", | |
| 70 | "http://localhost:9998/files/rdoc/nothere.html", | |
| 71 | "http://localhost:9998/files/rdoc/", | |
| 72 | "http://localhost:9998/files_nodir/rdoc/", | |
| 73 | "http://localhost:9998/status", | |
| 69 | res = hit([ "http://localhost:#{@port}/test", | |
| 70 | "http://localhost:#{@port}/dumb", | |
| 71 | "http://localhost:#{@port}/404", | |
| 72 | "http://localhost:#{@port}/files/rdoc/index.html", | |
| 73 | "http://localhost:#{@port}/files/rdoc/nothere.html", | |
| 74 | "http://localhost:#{@port}/files/rdoc/", | |
| 75 | "http://localhost:#{@port}/files_nodir/rdoc/", | |
| 76 | "http://localhost:#{@port}/status", | |
| 74 | 77 | ]) |
| 75 | 78 | check_status res, String |
| 76 | 79 | end |
| 77 | ||
| 80 | ||
| 78 | 81 | def test_nil_dirhandler |
| 79 | 82 | # Camping uses this internally |
| 80 | handler = Mongrel::DirHandler.new(nil, false) | |
| 81 | assert handler.can_serve("/tmp/testfile") | |
| 83 | handler = Mongrel::DirHandler.new(nil, false) | |
| 84 | assert handler.can_serve("/#{@test_file}") | |
| 82 | 85 | # Not a bug! A nil @file parameter is the only circumstance under which |
| 83 | 86 | # we are allowed to serve any existing file |
| 84 | assert handler.can_serve("../../../../../../../../../../tmp/testfile") | |
| 87 | assert handler.can_serve("../../../../../../../../../../#{@test_file}") | |
| 85 | 88 | end |
| 86 | 89 | |
| 87 | 90 | def test_non_nil_dirhandler_is_not_vulnerable_to_path_traversal |
| 88 | 91 | # The famous security bug of Mongrel 1.1.2 |
| 89 | 92 | handler = Mongrel::DirHandler.new("/doc", false) |
| 90 | assert_nil handler.can_serve("/tmp/testfile") | |
| 91 | assert_nil handler.can_serve("../../../../../../../../../../tmp/testfile") | |
| 93 | assert_nil handler.can_serve("/#{@test_file}") | |
| 94 | assert_nil handler.can_serve("../../../../../../../../../../#{@test_file}") | |
| 92 | 95 | end |
| 93 | 96 | |
| 94 | 97 | def test_deflate |
| 95 | Net::HTTP.start("localhost", 9998) do |h| | |
| 98 | Net::HTTP.start("localhost", @port) do |h| | |
| 96 | 99 | # Test that no accept-encoding returns a non-deflated response |
| 97 | 100 | req = h.get("/dumb") |
| 98 | 101 | assert( |
| ... | ...@@ -110,14 +113,14 @@ | |
| 110 | 113 | |
| 111 | 114 | # TODO: find out why this fails on win32 but nowhere else |
| 112 | 115 | #def test_posting_fails_dirhandler |
| 113 | # req = Net::HTTP::Post.new("http://localhost:9998/files/rdoc/") | |
| 116 | # req = Net::HTTP::Post.new("http://localhost:#{@port}/files/rdoc/") | |
| 114 | 117 | # req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';') |
| 115 | # res = hit [["http://localhost:9998/files/rdoc/",req]] | |
| 118 | # res = hit [["http://localhost:#{@port}/files/rdoc/",req]] | |
| 116 | 119 | # check_status res, Net::HTTPNotFound |
| 117 | 120 | #end |
| 118 | 121 | |
| 119 | 122 | def test_unregister |
| 120 | @config.listeners["127.0.0.1:9998"].unregister("/") | |
| 123 | @config.listeners["127.0.0.1:#{@port}"].unregister("/") | |
| 121 | 124 | end |
| 122 | 125 | end |
| 123 | 126 |