File: test_net_http_persistent_ssl_reuse.rb

package info (click to toggle)
ruby-net-http-persistent 2.9.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 260 kB
  • ctags: 411
  • sloc: ruby: 1,954; makefile: 2
file content (111 lines) | stat: -rw-r--r-- 2,419 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
require 'rubygems'
require 'minitest/autorun'
require 'net/http/persistent'
have_ssl =
  begin
    require 'openssl'
    require 'webrick'
    require 'webrick/ssl'
    true
  rescue LoadError
    false
  end

##
# This test is based on (and contains verbatim code from) the Net::HTTP tests
# in ruby

class TestNetHttpPersistentSSLReuse < Minitest::Test

  class NullWriter
    def <<(s) end
    def puts(*args) end
    def print(*args) end
    def printf(*args) end
  end

  def setup
    @name = OpenSSL::X509::Name.parse 'CN=localhost/DC=localdomain'

    @key = OpenSSL::PKey::RSA.new 1024

    @cert = OpenSSL::X509::Certificate.new
    @cert.version = 2
    @cert.serial = 0
    @cert.not_before = Time.now
    @cert.not_after = Time.now + 300
    @cert.public_key = @key.public_key
    @cert.subject = @name
    @cert.issuer = @name

    @cert.sign @key, OpenSSL::Digest::SHA1.new

    @host = 'localhost'
    @port = 10082

    config = {
      :BindAddress                => @host,
      :Port                       => @port,
      :Logger                     => WEBrick::Log.new(NullWriter.new),
      :AccessLog                  => [],
      :ShutDownSocketWithoutClose => true,
      :ServerType                 => Thread,
      :SSLEnable                  => true,
      :SSLCertificate             => @cert,
      :SSLPrivateKey              => @key,
      :SSLStartImmediately        => true,
    }

    @server = WEBrick::HTTPServer.new config

    @server.mount_proc '/' do |req, res|
      res.body = "ok"
    end

    @server.start

    begin
      TCPSocket.open(@host, @port).close
    rescue Errno::ECONNREFUSED
      sleep 0.2
      n_try_max -= 1
      raise 'cannot spawn server; give up' if n_try_max < 0
      retry
    end
  end

  def teardown
    if @server then
      @server.shutdown
      sleep 0.01 until @server.status == :Stop
    end
  end

  def test_ssl_connection_reuse
    store = OpenSSL::X509::Store.new
    store.add_cert @cert

    @http = Net::HTTP::Persistent::SSLReuse.new @host, @port
    @http.cert_store = store
    @http.use_ssl = true
    @http.verify_mode = OpenSSL::SSL::VERIFY_PEER

    @http.start
    @http.get '/'
    @http.finish

    @http.start
    @http.get '/'
    @http.finish

    @http.start
    @http.get '/'

    socket = @http.instance_variable_get :@socket
    ssl_socket = socket.io

    assert ssl_socket.session_reused?
  end

end if have_ssl