Skip to content

Commit

Permalink
add SSLSocket.open
Browse files Browse the repository at this point in the history
  • Loading branch information
thekuwayama authored and ioquatix committed Oct 31, 2019
1 parent b9e0c13 commit dd87110
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
22 changes: 19 additions & 3 deletions lib/openssl/ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,25 @@ def session_get_cb
end

class << self
##
# open is an alias to ::new
alias open new

# call-seq:
# open(remote_host, remote_port, context=nil, local_host=nil, local_port=nil)
#
# Creates a new instance of SSLSocket.
# _remote\_host_ and _remote_port_ are used to open TCPSocket.
# If _context_ is provided,
# the SSL Sockets initial params will be taken from the context.
# If _local\_host_ and _local\_port_ are specified,
# then those parameters are used on the local end to establish the connection.
#
# === Example
# ctx = OpenSSL::SSL::SSLContext.new
# sock = OpenSSL::SSL::SSLSocket.open('localhost', 443, ctx)
# sock.connect # Initiates a connection to localhost:443
def open(remote_host, remote_port, context=nil, local_host=nil, local_port=nil)
sock = ::TCPSocket.open(remote_host, remote_port, local_host, local_port)
OpenSSL::SSL::SSLSocket.new(sock, context)
end
end
end

Expand Down
26 changes: 15 additions & 11 deletions test/test_ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ def test_ssl_with_server_cert
}
end

def test_ssl_socket_open
start_server { |port|
begin
ctx = OpenSSL::SSL::SSLContext.new
ssl = OpenSSL::SSL::SSLSocket.open("127.0.0.1", port, ctx)
ssl.sync_close = true
ssl.connect

ssl.puts "abc"; assert_equal "abc\n", ssl.gets
ensure
ssl&.close
end
}
end

def test_add_certificate
ctx_proc = -> ctx {
# Unset values set by start_server
Expand Down Expand Up @@ -1592,17 +1607,6 @@ def test_fileno
sock2.close
end

def test_ssl_socket_new_alias
mn = OpenSSL::SSL::SSLSocket.method(:new)
mo = OpenSSL::SSL::SSLSocket.method(:open)

if mn.inspect == "#<Method: Class#new>"
assert_equal mn.hash, mo.hash
else
assert_equal mn, mo
end
end

private

def start_server_version(version, ctx_proc = nil,
Expand Down

0 comments on commit dd87110

Please sign in to comment.