1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/openssl] Add SSLSocket#getbyte

Normal sockets respond to `getbyte`, so we should make SSLSocket respond
to `getbyte` as well.  This way we can substitute SSLSockets for regular
sockets.

https://github.com/ruby/openssl/commit/ac1490b7c9
This commit is contained in:
Aaron Patterson 2021-04-15 16:53:47 -07:00 committed by Kazuki Yamaguchi
parent 6d71918d94
commit 593164c2be
3 changed files with 36 additions and 0 deletions

View file

@ -99,8 +99,27 @@ module OpenSSL::Buffering
end
end
if "".respond_to?(:unpack1)
def unpack_byte(str)
str.unpack1("C")
end
else
def unpack_byte(str)
str.unpack("C").first
end
end
public
# call-seq:
# ssl.getbyte => 81
#
# Get the next 8bit byte from `ssl`. Returns `nil` on EOF
def getbyte
byte = read(1)
byte && unpack_byte(byte)
end
##
# Reads _size_ bytes from the stream. If _buf_ is provided it must
# reference a string which will receive the data.

View file

@ -185,6 +185,19 @@ class OpenSSL::TestSSL < OpenSSL::SSLTestCase
}
end
def test_getbyte
start_server { |port|
server_connect(port) { |ssl|
str = +("x" * 100 + "\n")
ssl.syswrite(str)
newstr = str.bytesize.times.map { |i|
ssl.getbyte
}.pack("C*")
assert_equal(str, newstr)
}
}
end
def test_sync_close
start_server do |port|
begin

View file

@ -4,6 +4,10 @@ require 'test/unit'
if defined?(OpenSSL)
module OpenSSL::TestEOF
def test_getbyte_eof
open_file("") {|f| assert_nil f.getbyte }
end
def test_eof_0
open_file("") {|f|
assert_equal("", f.read(0))