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:
parent
bf3ae2ad76
commit
afe1892208
|
@ -149,11 +149,16 @@ module Zip
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_central_directory_entries(io) #:nodoc:
|
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
|
begin
|
||||||
io.seek(@cdir_offset, IO::SEEK_SET)
|
io.seek(@cdir_offset, IO::SEEK_SET)
|
||||||
rescue Errno::EINVAL
|
rescue Errno::EINVAL
|
||||||
raise Error, 'Zip consistency problem while reading central directory entry'
|
eof = true
|
||||||
end
|
end
|
||||||
|
raise Error, 'Zip consistency problem while reading central directory entry' if eof || io.eof?
|
||||||
|
|
||||||
@entry_set = EntrySet.new
|
@entry_set = EntrySet.new
|
||||||
@size.times do
|
@size.times do
|
||||||
entry = Entry.read_c_dir_entry(io)
|
entry = Entry.read_c_dir_entry(io)
|
||||||
|
|
|
@ -26,15 +26,22 @@ class ZipCentralDirectoryTest < MiniTest::Test
|
||||||
rescue ::Zip::Error
|
rescue ::Zip::Error
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_read_from_truncated_zip_file
|
def test_read_eocd_with_wrong_cdir_offset_from_file
|
||||||
fragment = ''
|
::File.open('test/data/testDirectory.bin', 'rb') do |f|
|
||||||
File.open('test/data/testDirectory.bin', 'rb') { |f| fragment = f.read }
|
assert_raises(::Zip::Error) do
|
||||||
fragment.slice!(12) # removed part of first cdir entry. eocd structure still complete
|
cdir = ::Zip::CentralDirectory.new
|
||||||
fragment.extend(IOizeString)
|
cdir.read_from_stream(f)
|
||||||
entry = ::Zip::CentralDirectory.new
|
end
|
||||||
entry.read_from_stream(fragment)
|
end
|
||||||
raise 'ZipError expected'
|
end
|
||||||
rescue ::Zip::Error
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
def test_write_to_stream
|
def test_write_to_stream
|
||||||
|
|
Loading…
Reference in New Issue