Skip to content

Commit

Permalink
Merge pull request #771 from lwoggardner/readbyte
Browse files Browse the repository at this point in the history
Add SSLSocket#readbyte
  • Loading branch information
rhenium authored Jul 3, 2024
2 parents bb45527 + c40f707 commit c959729
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
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

0 comments on commit c959729

Please sign in to comment.