aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2024-07-03 17:54:14 +0900
committerGitHub <noreply@github.com>2024-07-03 17:54:14 +0900
commitc959729bd2d8b1d1a9da116b84ec590913bf56bf (patch)
tree8c76c7a2479baa502b6a3cfa31640efb7e6a2331
parentbb455270ccabcb33d98cb9796411652185de6af4 (diff)
parentc40f70711ae121894ce0f3e657bf1b7d25a0cb4a (diff)
downloadruby-openssl-c959729bd2d8b1d1a9da116b84ec590913bf56bf.tar.gz
Merge pull request #771 from lwoggardner/readbyte
Add SSLSocket#readbyte
-rw-r--r--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/lib/openssl/buffering.rb b/lib/openssl/buffering.rb
index d0b4b180..85f593af 100644
--- a/lib/openssl/buffering.rb
+++ b/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 66e36a7a..10942191 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 1471b0cb..f011e881 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 7b18f43a..06aa632a 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))