Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SSLSocket#readbyte #771

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/openssl/buffering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ def getbyte
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.
Expand Down
21 changes: 21 additions & 0 deletions test/openssl/test_pair.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,27 @@ def test_getc
}
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"
Expand Down
13 changes: 13 additions & 0 deletions test/openssl/test_ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,19 @@ def test_getbyte
}
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
Expand Down
4 changes: 4 additions & 0 deletions test/openssl/ut_eof.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def test_getbyte_eof
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))
Expand Down