about summary refs log tree commit homepage
path: root/test/test_ssl.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_ssl.rb')
-rw-r--r--test/test_ssl.rb72
1 files changed, 72 insertions, 0 deletions
diff --git a/test/test_ssl.rb b/test/test_ssl.rb
index 13c14f0..8f01ef7 100644
--- a/test/test_ssl.rb
+++ b/test/test_ssl.rb
@@ -71,6 +71,15 @@ AQjjxMXhwULlmuR/K+WwlaZPiLIBYalLAZQ7ZbOPeVkJ8ePao0eLAgEC
       end
     end
     client = ssl_client(host, port)
+    client.write("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
+    buf = ''
+    Timeout.timeout(60) do
+      buf << client.readpartial(111) until buf =~ /HI\z/
+    end
+    head, body = buf.split("\r\n\r\n", 2)
+    assert_equal "HI", body
+    assert_match %r{\AHTTP/1\.\d 200 OK\r\n}, head
+
     client.write("GET / HTTP/1.0\r\n\r\n")
     head, body = client.read.split("\r\n\r\n", 2)
     assert_equal "HI", body
@@ -79,4 +88,67 @@ AQjjxMXhwULlmuR/K+WwlaZPiLIBYalLAZQ7ZbOPeVkJ8ePao0eLAgEC
     client.close if client
     quit_wait(pid)
   end
+
+  def test_ssl_hijack
+    err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1]
+    ctx = srv_ctx
+    pid = mkserver(cfg) do
+      cfg.instance_eval do
+        ru = lambda do |env|
+          io = env['rack.hijack'].call
+          Thread.new(io) do |s|
+            s.write "HTTP/1.1 201 Switching Protocols\r\n\r\n"
+            case req = s.gets
+            when "inspect\n"
+              s.puts(s.instance_variable_get(:@ssl).inspect)
+            when "remote_address\n"
+              s.puts(s.remote_address.inspect)
+            when "each\n"
+              line = ''
+              s.each do |l|
+                l.strip!
+                line << l
+                break if l == 'd'
+              end
+              s.puts line
+            when "missing\n"
+              begin
+                s.any_old_invalid_test_method
+                s.puts "FAIL"
+              rescue => e
+                s.puts "#{e.class}: #{e.message}"
+              end
+            when nil
+              s.close
+            else
+              p [ :ERR, req ]
+            end until s.closed?
+          end
+          [ 200, DieIfUsed, DieIfUsed ]
+        end
+        app(:rack, ru) { listen "#{host}:#{port}", ssl_ctx: ctx }
+        logger(Logger.new(err.path))
+      end
+    end
+    client = ssl_client(host, port)
+    client.write("GET / HTTP/1.0\r\n\r\n")
+
+    Timeout.timeout(60) do
+      assert_equal "HTTP/1.1 201 Switching Protocols\r\n", client.gets
+      assert_equal "\r\n", client.gets
+      client.puts "inspect"
+      assert_match %r{SSLSocket}, client.gets
+      client.puts "remote_address"
+      assert_equal client.to_io.local_address.inspect, client.gets.strip
+      client.puts "missing"
+      assert_match %r{NoMethodError}, client.gets
+
+      client.puts "each"
+      %w(a b c d).each { |x| client.puts(x) }
+      assert_equal "abcd", client.gets.strip
+    end
+  ensure
+    client.close if client
+    quit_wait(pid)
+  end
 end if defined?(OpenSSL)