diff options
author | thekuwayama <thekuwayama@gmail.com> | 2019-10-31 07:14:57 +0900 |
---|---|---|
committer | Samuel Williams <samuel.williams@oriontransfer.co.nz> | 2019-10-31 14:19:30 +1300 |
commit | dd87110a12dba028ab96ecd885970ba04094f9a4 (patch) | |
tree | 714078975d46523c0368d00754d377299e3fc3c4 | |
parent | b9e0c137508ad2f9da52f83db9e8fbbc0b7f4fbe (diff) | |
download | ruby-openssl-dd87110a12dba028ab96ecd885970ba04094f9a4.tar.gz |
add SSLSocket.open
-rw-r--r-- | lib/openssl/ssl.rb | 22 | ||||
-rw-r--r-- | test/test_ssl.rb | 26 |
2 files changed, 34 insertions, 14 deletions
diff --git a/lib/openssl/ssl.rb b/lib/openssl/ssl.rb index 8ffd8c74..9f3afc2f 100644 --- a/lib/openssl/ssl.rb +++ b/lib/openssl/ssl.rb @@ -442,9 +442,25 @@ YoaOffgTf5qxiwkjnlVZQc3whgnEt9FpVMvQ9eknyeGB5KHfayAc3+hUAvI3/Cr3 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 diff --git a/test/test_ssl.rb b/test/test_ssl.rb index 466996a6..cf1cb7e9 100644 --- a/test/test_ssl.rb +++ b/test/test_ssl.rb @@ -56,6 +56,21 @@ class OpenSSL::TestSSL < OpenSSL::SSLTestCase } 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 @@ -1592,17 +1607,6 @@ end 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, |