summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrant Gardner <[email protected]>2024-07-01 00:25:45 +1000
committergit <[email protected]>2024-07-03 08:54:18 +0000
commit4d4ac00123aa21d3027bcd0aa0242c1bc129837e (patch)
treee8aad397dc7c684d16af3f1461eebdb5dc7e889b
parent93b19d56de64fdee790a96ddf96fcd08d889ac93 (diff)
[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
-rw-r--r--ext/openssl/lib/openssl/buffering.rb6
-rw-r--r--test/openssl/test_pair.rb21
-rw-r--r--test/openssl/test_ssl.rb13
-rw-r--r--test/openssl/ut_eof.rb4
4 files changed, 44 insertions, 0 deletions
diff --git a/ext/openssl/lib/openssl/buffering.rb b/ext/openssl/lib/openssl/buffering.rb
index d0b4b18038..85f593af0f 100644
--- a/ext/openssl/lib/openssl/buffering.rb
+++ b/ext/openssl/lib/openssl/buffering.rb
@@ -107,6 +107,12 @@ module OpenSSL::Buffering
read(1)&.ord
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
# reference a string which will receive the data.
diff --git a/test/openssl/test_pair.rb b/test/openssl/test_pair.rb
index 66e36a7ab4..10942191dd 100644
--- a/test/openssl/test_pair.rb
+++ b/test/openssl/test_pair.rb
@@ -101,6 +101,27 @@ module OpenSSL::TestPairM
}
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
ssl_pair {|s1, s2|
s1 << "abc\n\n$def123ghi"
diff --git a/test/openssl/test_ssl.rb b/test/openssl/test_ssl.rb
index 1471b0cb36..f011e881e9 100644
--- a/test/openssl/test_ssl.rb
+++ b/test/openssl/test_ssl.rb
@@ -248,6 +248,19 @@ class OpenSSL::TestSSL < OpenSSL::SSLTestCase
}
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
start_server do |port|
begin
diff --git a/test/openssl/ut_eof.rb b/test/openssl/ut_eof.rb
index 7b18f43a79..06aa632a65 100644
--- a/test/openssl/ut_eof.rb
+++ b/test/openssl/ut_eof.rb
@@ -8,6 +8,10 @@ module OpenSSL::TestEOF
open_file("") {|f| assert_nil f.getbyte }
end
+ def test_readbyte_eof
+ open_file("") {|f| assert_raise(EOFError) { f.readbyte } }
+ end
+
def test_eof_0
open_file("") {|f|
assert_equal("", f.read(0))