2021-05-24 01:24:22 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-01-21 05:31:06 +08:00
|
|
|
require 'test_helper'
|
|
|
|
|
2014-07-15 23:23:48 +08:00
|
|
|
class ZipCentralDirectoryTest < MiniTest::Test
|
2014-07-25 02:22:52 +08:00
|
|
|
def teardown
|
|
|
|
::Zip.reset!
|
|
|
|
end
|
|
|
|
|
2014-01-21 05:31:06 +08:00
|
|
|
def test_read_from_stream
|
2020-02-18 06:35:08 +08:00
|
|
|
::File.open(TestZipFile::TEST_ZIP2.zip_name, 'rb') do |zip_file|
|
|
|
|
cdir = ::Zip::CentralDirectory.read_from_stream(zip_file)
|
2014-01-21 05:31:06 +08:00
|
|
|
|
|
|
|
assert_equal(TestZipFile::TEST_ZIP2.entry_names.size, cdir.size)
|
2021-01-26 21:15:14 +08:00
|
|
|
assert_equal(cdir.entries.map(&:name).sort, TestZipFile::TEST_ZIP2.entry_names.sort)
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_equal(TestZipFile::TEST_ZIP2.comment, cdir.comment)
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_read_from_invalid_stream
|
2020-02-18 06:35:08 +08:00
|
|
|
File.open('test/data/file2.txt', 'rb') do |zip_file|
|
2014-01-21 05:31:06 +08:00
|
|
|
cdir = ::Zip::CentralDirectory.new
|
2020-02-18 06:35:08 +08:00
|
|
|
cdir.read_from_stream(zip_file)
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2019-09-26 20:58:43 +08:00
|
|
|
raise 'ZipError expected!'
|
2014-01-24 17:37:38 +08:00
|
|
|
rescue ::Zip::Error
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2021-06-18 18:44:58 +08:00
|
|
|
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
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2021-11-20 18:50:55 +08:00
|
|
|
def test_count_entries
|
|
|
|
[
|
|
|
|
['test/data/osx-archive.zip', 4],
|
|
|
|
['test/data/zip64-sample.zip', 2],
|
2021-11-21 04:02:47 +08:00
|
|
|
['test/data/max_length_file_comment.zip', 1],
|
|
|
|
['test/data/100000-files.zip', 100_000]
|
2021-11-20 18:50:55 +08:00
|
|
|
].each do |filename, num_entries|
|
|
|
|
cdir = ::Zip::CentralDirectory.new
|
|
|
|
|
|
|
|
::File.open(filename, 'rb') do |f|
|
|
|
|
assert_equal(num_entries, cdir.count_entries(f))
|
|
|
|
|
|
|
|
f.seek(0)
|
|
|
|
s = StringIO.new(f.read)
|
|
|
|
assert_equal(num_entries, cdir.count_entries(s))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-21 05:31:06 +08:00
|
|
|
def test_write_to_stream
|
2020-08-03 00:51:08 +08:00
|
|
|
entries = [
|
|
|
|
::Zip::Entry.new(
|
|
|
|
'file.zip', 'flimse',
|
|
|
|
comment: 'myComment', extra: 'somethingExtra'
|
|
|
|
),
|
|
|
|
::Zip::Entry.new('file.zip', 'secondEntryName'),
|
|
|
|
::Zip::Entry.new('file.zip', 'lastEntry.txt', comment: 'Has a comment')
|
|
|
|
]
|
2020-02-19 18:52:49 +08:00
|
|
|
|
2015-03-21 16:27:44 +08:00
|
|
|
cdir = ::Zip::CentralDirectory.new(entries, 'my zip comment')
|
2020-02-19 18:52:49 +08:00
|
|
|
File.open('test/data/generated/cdirtest.bin', 'wb') do |f|
|
|
|
|
cdir.write_to_stream(f)
|
|
|
|
end
|
|
|
|
|
|
|
|
cdir_readback = ::Zip::CentralDirectory.new
|
|
|
|
File.open('test/data/generated/cdirtest.bin', 'rb') do |f|
|
|
|
|
cdir_readback.read_from_stream(f)
|
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
|
2020-02-19 18:52:49 +08:00
|
|
|
assert_equal(cdir.entries.sort, cdir_readback.entries.sort)
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_write64_to_stream
|
2014-01-24 17:37:38 +08:00
|
|
|
::Zip.write_zip64_support = true
|
2020-08-03 00:51:08 +08:00
|
|
|
entries = [
|
|
|
|
::Zip::Entry.new(
|
|
|
|
'file.zip', 'file1-little', comment: 'comment1', size: 200,
|
|
|
|
compressed_size: 200, crc: 101,
|
|
|
|
compression_method: ::Zip::Entry::STORED
|
|
|
|
),
|
|
|
|
::Zip::Entry.new(
|
|
|
|
'file.zip', 'file2-big', comment: 'comment2',
|
|
|
|
size: 20_000_000_000, compressed_size: 18_000_000_000, crc: 102
|
|
|
|
),
|
|
|
|
::Zip::Entry.new(
|
|
|
|
'file.zip', 'file3-alsobig', comment: 'comment3',
|
|
|
|
size: 21_000_000_000, compressed_size: 15_000_000_000, crc: 103
|
|
|
|
),
|
|
|
|
::Zip::Entry.new(
|
|
|
|
'file.zip', 'file4-little', comment: 'comment4',
|
|
|
|
size: 121, compressed_size: 100, crc: 104
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2015-03-24 00:23:04 +08:00
|
|
|
[0, 250, 18_000_000_300, 33_000_000_350].each_with_index do |offset, index|
|
2014-01-21 05:31:06 +08:00
|
|
|
entries[index].local_header_offset = offset
|
|
|
|
end
|
2020-02-19 18:52:49 +08:00
|
|
|
|
2015-03-21 16:27:44 +08:00
|
|
|
cdir = ::Zip::CentralDirectory.new(entries, 'zip comment')
|
2020-02-19 18:52:49 +08:00
|
|
|
File.open('test/data/generated/cdir64test.bin', 'wb') do |f|
|
|
|
|
cdir.write_to_stream(f)
|
|
|
|
end
|
|
|
|
|
|
|
|
cdir_readback = ::Zip::CentralDirectory.new
|
|
|
|
File.open('test/data/generated/cdir64test.bin', 'rb') do |f|
|
|
|
|
cdir_readback.read_from_stream(f)
|
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
|
2020-02-19 18:52:49 +08:00
|
|
|
assert_equal(cdir.entries.sort, cdir_readback.entries.sort)
|
|
|
|
assert_equal(::Zip::VERSION_NEEDED_TO_EXTRACT_ZIP64, cdir_readback.instance_variable_get(:@version_needed_for_extract))
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_equality
|
2020-08-03 00:51:08 +08:00
|
|
|
cdir1 = ::Zip::CentralDirectory.new(
|
|
|
|
[
|
|
|
|
::Zip::Entry.new('file.zip', 'flimse', extra: 'somethingExtra'),
|
|
|
|
::Zip::Entry.new('file.zip', 'secondEntryName'),
|
|
|
|
::Zip::Entry.new('file.zip', 'lastEntry.txt')
|
|
|
|
],
|
|
|
|
'my zip comment'
|
|
|
|
)
|
|
|
|
cdir2 = ::Zip::CentralDirectory.new(
|
|
|
|
[
|
|
|
|
::Zip::Entry.new('file.zip', 'flimse', extra: 'somethingExtra'),
|
|
|
|
::Zip::Entry.new('file.zip', 'secondEntryName'),
|
|
|
|
::Zip::Entry.new('file.zip', 'lastEntry.txt')
|
|
|
|
],
|
|
|
|
'my zip comment'
|
|
|
|
)
|
|
|
|
cdir3 = ::Zip::CentralDirectory.new(
|
|
|
|
[
|
|
|
|
::Zip::Entry.new('file.zip', 'flimse', extra: 'somethingExtra'),
|
|
|
|
::Zip::Entry.new('file.zip', 'secondEntryName'),
|
|
|
|
::Zip::Entry.new('file.zip', 'lastEntry.txt')
|
|
|
|
],
|
|
|
|
'comment?'
|
|
|
|
)
|
|
|
|
cdir4 = ::Zip::CentralDirectory.new(
|
|
|
|
[
|
|
|
|
::Zip::Entry.new('file.zip', 'flimse', extra: 'somethingExtra'),
|
|
|
|
::Zip::Entry.new('file.zip', 'lastEntry.txt')
|
|
|
|
],
|
|
|
|
'comment?'
|
|
|
|
)
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_equal(cdir1, cdir1)
|
|
|
|
assert_equal(cdir1, cdir2)
|
|
|
|
|
|
|
|
assert(cdir1 != cdir3)
|
|
|
|
assert(cdir2 != cdir3)
|
|
|
|
assert(cdir2 != cdir3)
|
|
|
|
assert(cdir3 != cdir4)
|
|
|
|
|
2015-03-21 16:27:44 +08:00
|
|
|
assert(cdir3 != 'hello')
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
end
|