Skip to content

Commit

Permalink
[rubygems/rubygems] Test to show non-standard behavior of zero byte f…
Browse files Browse the repository at this point in the history
…iles in archive

Added more tests for some of the other behavior as well.
Tests were missing for readpartial with a buffer, and reading
remaining bytes after a partial read, using StringIO as reference.

rubygems/rubygems@d600a657b1
  • Loading branch information
martinemde authored and matzbot committed Feb 2, 2023
1 parent 188688a commit 0853703
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions test/rubygems/test_gem_package_tar_reader_entry.rb
Expand Up @@ -125,6 +125,18 @@ def test_read
assert_equal @contents, @entry.read
end

def test_consecutive_read
expected = StringIO.new(@contents)
assert_equal expected.read, @entry.read
assert_equal expected.read, @entry.read
end

def test_consecutive_read_bytes_past_eof
expected = StringIO.new(@contents)
assert_equal expected.read, @entry.read
assert_equal expected.read(1), @entry.read(1)
end

def test_read_big
assert_equal @contents, @entry.read(@contents.size * 2)
end
Expand All @@ -133,9 +145,24 @@ def test_read_small
assert_equal @contents[0...100], @entry.read(100)
end

def test_readpartial
def test_read_remaining
@entry.read(100)
assert_equal @contents[100..-1], @entry.read
end

def test_read_partial
assert_equal @contents[0...100], @entry.readpartial(100)
end

def test_read_partial_buffer
buffer = "".b
@entry.readpartial(100, buffer)
assert_equal @contents[0...100], buffer
end

def test_readpartial_past_eof
@entry.readpartial(@contents.size)
assert_raise(EOFError) do
@entry.read(@contents.size)
@entry.readpartial(1)
end
end
Expand All @@ -149,4 +176,35 @@ def test_rewind

assert_equal char, @entry.getc
end

def test_read_zero
expected = StringIO.new("")
assert_equal expected.read(0), @entry.read(0)
end

def test_readpartial_zero
expected = StringIO.new("")
assert_equal expected.readpartial(0), @entry.readpartial(0)
end

def util_zero_byte_entry
tar = String.new
tar << tar_file_header("lib/empty", "", 0, 0, Time.now)
tar << "\0" * (512 - (tar.size % 512))
util_entry tar
end

def test_zero_byte_file_read
zero_entry = util_zero_byte_entry
expected = StringIO.new("")

assert_equal expected.read, zero_entry.read
end

def test_zero_byte_file_readpartial
zero_entry = util_zero_byte_entry
expected = StringIO.new("")

assert_equal expected.readpartial(0), zero_entry.readpartial(0)
end
end

0 comments on commit 0853703

Please sign in to comment.