Fix a mis-firing CentralDirectory test.

`test_read_from_truncated_zip_file` was not testing what it thought it
was. It was testing whether we caught an out-of-bounds cdir offset, not
whether we caught a corrupted cdir entry.

This commit embraces the actual behaviour and tests that we catch an
out-of-bounds error for both standard `IO`s and `StringIO`s.
This commit is contained in:
Robert Haines 2021-06-18 11:44:58 +01:00
parent bf3ae2ad76
commit afe1892208
2 changed files with 22 additions and 10 deletions

View File

@ -149,11 +149,16 @@ module Zip
end
def read_central_directory_entries(io) #:nodoc:
# `StringIO` doesn't raise `EINVAL` if you seek beyond the current end,
# so we need to catch that *and* query `io#eof?` here.
eof = false
begin
io.seek(@cdir_offset, IO::SEEK_SET)
rescue Errno::EINVAL
raise Error, 'Zip consistency problem while reading central directory entry'
eof = true
end
raise Error, 'Zip consistency problem while reading central directory entry' if eof || io.eof?
@entry_set = EntrySet.new
@size.times do
entry = Entry.read_c_dir_entry(io)

View File

@ -26,15 +26,22 @@ class ZipCentralDirectoryTest < MiniTest::Test
rescue ::Zip::Error
end
def test_read_from_truncated_zip_file
fragment = ''
File.open('test/data/testDirectory.bin', 'rb') { |f| fragment = f.read }
fragment.slice!(12) # removed part of first cdir entry. eocd structure still complete
fragment.extend(IOizeString)
entry = ::Zip::CentralDirectory.new
entry.read_from_stream(fragment)
raise 'ZipError expected'
rescue ::Zip::Error
def test_read_eocd_with_wrong_cdir_offset_from_file
::File.open('test/data/testDirectory.bin', 'rb') do |f|
assert_raises(::Zip::Error) do
cdir = ::Zip::CentralDirectory.new
cdir.read_from_stream(f)
end
end
end
def test_read_eocd_with_wrong_cdir_offset_from_buffer
::File.open('test/data/testDirectory.bin', 'rb') do |f|
assert_raises(::Zip::Error) do
cdir = ::Zip::CentralDirectory.new
cdir.read_from_stream(StringIO.new(f.read))
end
end
end
def test_write_to_stream