[ruby/openssl] Add SSLSocket#readbyte

Companion to getbyte but raise EOFError
Similar to https://github.com/ruby/openssl/pull/438

https://github.com/ruby/openssl/commit/c40f70711a
This commit is contained in:
Grant Gardner 2024-07-01 00:25:45 +10:00 committed by git
parent 93b19d56de
commit 4d4ac00123
4 changed files with 44 additions and 0 deletions

View File

@ -107,6 +107,12 @@ module OpenSSL::Buffering
read(1)&.ord read(1)&.ord
end end
# Get the next 8bit byte. Raises EOFError on EOF
def readbyte
raise EOFError if eof?
getbyte
end
## ##
# Reads _size_ bytes from the stream. If _buf_ is provided it must # Reads _size_ bytes from the stream. If _buf_ is provided it must
# reference a string which will receive the data. # reference a string which will receive the data.

View File

@ -101,6 +101,27 @@ module OpenSSL::TestPairM
} }
end end
def test_getbyte
ssl_pair {|s1, s2|
s1 << "a"
assert_equal(97, s2.getbyte)
}
end
def test_readbyte
ssl_pair {|s1, s2|
s1 << "b"
assert_equal(98, s2.readbyte)
}
end
def test_readbyte_eof
ssl_pair {|s1, s2|
s2.close
assert_raise(EOFError) { s1.readbyte }
}
end
def test_gets def test_gets
ssl_pair {|s1, s2| ssl_pair {|s1, s2|
s1 << "abc\n\n$def123ghi" s1 << "abc\n\n$def123ghi"

View File

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

View File

@ -8,6 +8,10 @@ module OpenSSL::TestEOF
open_file("") {|f| assert_nil f.getbyte } open_file("") {|f| assert_nil f.getbyte }
end end
def test_readbyte_eof
open_file("") {|f| assert_raise(EOFError) { f.readbyte } }
end
def test_eof_0 def test_eof_0
open_file("") {|f| open_file("") {|f|
assert_equal("", f.read(0)) assert_equal("", f.read(0))