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 ZipInputStreamTest < MiniTest::Test
|
2014-01-21 05:31:06 +08:00
|
|
|
include AssertEntry
|
|
|
|
|
2015-09-03 21:16:32 +08:00
|
|
|
class IOLike
|
|
|
|
extend Forwardable
|
|
|
|
|
|
|
|
def initialize(path, mode)
|
|
|
|
@file = File.new(path, mode)
|
|
|
|
end
|
|
|
|
|
|
|
|
delegate ::Zip::File::IO_METHODS => :@file
|
|
|
|
end
|
|
|
|
|
2014-01-21 05:31:06 +08:00
|
|
|
def test_new
|
|
|
|
zis = ::Zip::InputStream.new(TestZipFile::TEST_ZIP2.zip_name)
|
|
|
|
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
|
|
|
|
assert_equal(true, zis.eof?)
|
|
|
|
zis.close
|
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_open_with_block
|
2015-03-24 00:08:12 +08:00
|
|
|
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
|
|
|
|
assert_equal(true, zis.eof?)
|
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_open_without_block
|
2015-03-21 16:27:44 +08:00
|
|
|
zis = ::Zip::InputStream.open(File.new(TestZipFile::TEST_ZIP2.zip_name, 'rb'))
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
|
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_open_buffer_with_block
|
2015-03-21 16:27:44 +08:00
|
|
|
::Zip::InputStream.open(File.new(TestZipFile::TEST_ZIP2.zip_name, 'rb')) do |zis|
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
|
|
|
|
assert_equal(true, zis.eof?)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_open_string_io_without_block
|
2021-05-30 14:04:56 +08:00
|
|
|
string_io = ::StringIO.new(::File.read(TestZipFile::TEST_ZIP2.zip_name, mode: 'rb'))
|
2014-01-21 05:31:06 +08:00
|
|
|
zis = ::Zip::InputStream.open(string_io)
|
|
|
|
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_open_string_io_with_block
|
2021-05-30 14:04:56 +08:00
|
|
|
string_io = ::StringIO.new(::File.read(TestZipFile::TEST_ZIP2.zip_name, mode: 'rb'))
|
2014-01-21 05:31:06 +08:00
|
|
|
::Zip::InputStream.open(string_io) do |zis|
|
|
|
|
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
|
|
|
|
assert_equal(true, zis.eof?)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_open_buffer_without_block
|
2014-01-21 05:31:06 +08:00
|
|
|
zis = ::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name)
|
|
|
|
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
|
|
|
|
end
|
|
|
|
|
2015-09-03 21:16:32 +08:00
|
|
|
def test_open_io_like_with_block
|
|
|
|
::Zip::InputStream.open(IOLike.new(TestZipFile::TEST_ZIP2.zip_name, 'rb')) do |zis|
|
|
|
|
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
|
|
|
|
assert_equal(true, zis.eof?)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-26 00:40:46 +08:00
|
|
|
def test_open_file_with_gp3bit_set
|
|
|
|
::Zip::InputStream.open('test/data/gpbit3stored.zip') do |zis|
|
2021-11-29 00:51:06 +08:00
|
|
|
error = assert_raises(::Zip::StreamingError) do
|
2021-06-26 00:40:46 +08:00
|
|
|
zis.get_next_entry
|
|
|
|
end
|
2021-11-29 00:51:06 +08:00
|
|
|
assert_match(/file1\.txt/, error.message)
|
|
|
|
assert_equal('file1.txt', error.entry.name)
|
2021-06-26 00:40:46 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-26 00:51:54 +08:00
|
|
|
def test_open_file_with_gp3bit_set_created_by_osx_archive
|
|
|
|
::Zip::InputStream.open('test/data/osx-archive.zip') do |zis|
|
2021-11-29 00:51:06 +08:00
|
|
|
error = assert_raises(::Zip::StreamingError) do
|
2021-06-26 00:51:54 +08:00
|
|
|
zis.get_next_entry
|
|
|
|
end
|
2021-11-29 00:51:06 +08:00
|
|
|
assert_match(/1\.txt/, error.message)
|
|
|
|
assert_equal('1.txt', error.entry.name)
|
2021-06-26 00:51:54 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-26 18:26:06 +08:00
|
|
|
def test_open_split_archive_raises_error
|
|
|
|
::Zip::InputStream.open('test/data/invalid-split.zip') do |zis|
|
2022-08-14 18:30:43 +08:00
|
|
|
error = assert_raises(::Zip::SplitArchiveError) do
|
2021-06-26 18:26:06 +08:00
|
|
|
zis.get_next_entry
|
|
|
|
end
|
2022-08-14 18:30:43 +08:00
|
|
|
refute(error.message.empty?)
|
2021-06-26 18:26:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-05 02:53:33 +08:00
|
|
|
def test_open_encrypted_archive_raises_error
|
|
|
|
::Zip::InputStream.open('test/data/zipWithEncryption.zip') do |zis|
|
|
|
|
assert_raises(::Zip::Error) do
|
|
|
|
zis.get_next_entry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-26 20:35:16 +08:00
|
|
|
def test_size_no_entry
|
|
|
|
zis = ::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name)
|
|
|
|
assert_nil(zis.size)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_size_with_entry
|
|
|
|
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
|
|
|
|
zis.get_next_entry
|
|
|
|
assert_equal(123_702, zis.size)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-14 05:09:55 +08:00
|
|
|
def test_get_entry_ftypes
|
|
|
|
::Zip::InputStream.open(TestZipFile::TEST_ZIP4.zip_name) do |zis|
|
|
|
|
entry = zis.get_next_entry
|
|
|
|
assert_equal(:file, entry.ftype)
|
|
|
|
|
|
|
|
entry = zis.get_next_entry
|
|
|
|
assert_equal(:directory, entry.ftype)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_incomplete_reads
|
2015-03-24 00:08:12 +08:00
|
|
|
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
|
2014-01-21 05:31:06 +08:00
|
|
|
entry = zis.get_next_entry # longAscii.txt
|
|
|
|
assert_equal(false, zis.eof?)
|
|
|
|
assert_equal(TestZipFile::TEST_ZIP2.entry_names[0], entry.name)
|
2017-06-29 10:57:12 +08:00
|
|
|
assert !zis.gets.empty?
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_equal(false, zis.eof?)
|
|
|
|
entry = zis.get_next_entry # empty.txt
|
|
|
|
assert_equal(TestZipFile::TEST_ZIP2.entry_names[1], entry.name)
|
|
|
|
assert_equal(0, entry.size)
|
2017-01-08 14:31:51 +08:00
|
|
|
assert_nil(zis.gets)
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_equal(true, zis.eof?)
|
|
|
|
entry = zis.get_next_entry # empty_chmod640.txt
|
|
|
|
assert_equal(TestZipFile::TEST_ZIP2.entry_names[2], entry.name)
|
|
|
|
assert_equal(0, entry.size)
|
2017-01-08 14:31:51 +08:00
|
|
|
assert_nil(zis.gets)
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_equal(true, zis.eof?)
|
|
|
|
entry = zis.get_next_entry # short.txt
|
|
|
|
assert_equal(TestZipFile::TEST_ZIP2.entry_names[3], entry.name)
|
2017-06-29 10:57:12 +08:00
|
|
|
assert !zis.gets.empty?
|
2014-01-21 05:31:06 +08:00
|
|
|
entry = zis.get_next_entry # longBinary.bin
|
|
|
|
assert_equal(TestZipFile::TEST_ZIP2.entry_names[4], entry.name)
|
2017-06-29 10:57:12 +08:00
|
|
|
assert !zis.gets.empty?
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_incomplete_reads_from_string_io
|
2021-05-30 14:04:56 +08:00
|
|
|
string_io = ::StringIO.new(::File.read(TestZipFile::TEST_ZIP2.zip_name, mode: 'rb'))
|
2014-01-21 05:31:06 +08:00
|
|
|
::Zip::InputStream.open(string_io) do |zis|
|
|
|
|
entry = zis.get_next_entry # longAscii.txt
|
|
|
|
assert_equal(false, zis.eof?)
|
|
|
|
assert_equal(TestZipFile::TEST_ZIP2.entry_names[0], entry.name)
|
2017-06-29 10:57:12 +08:00
|
|
|
assert !zis.gets.empty?
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_equal(false, zis.eof?)
|
|
|
|
entry = zis.get_next_entry # empty.txt
|
|
|
|
assert_equal(TestZipFile::TEST_ZIP2.entry_names[1], entry.name)
|
|
|
|
assert_equal(0, entry.size)
|
2017-01-08 14:31:51 +08:00
|
|
|
assert_nil(zis.gets)
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_equal(true, zis.eof?)
|
|
|
|
entry = zis.get_next_entry # empty_chmod640.txt
|
|
|
|
assert_equal(TestZipFile::TEST_ZIP2.entry_names[2], entry.name)
|
|
|
|
assert_equal(0, entry.size)
|
2017-01-08 14:31:51 +08:00
|
|
|
assert_nil(zis.gets)
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_equal(true, zis.eof?)
|
|
|
|
entry = zis.get_next_entry # short.txt
|
|
|
|
assert_equal(TestZipFile::TEST_ZIP2.entry_names[3], entry.name)
|
2017-06-29 10:57:12 +08:00
|
|
|
assert !zis.gets.empty?
|
2014-01-21 05:31:06 +08:00
|
|
|
entry = zis.get_next_entry # longBinary.bin
|
|
|
|
assert_equal(TestZipFile::TEST_ZIP2.entry_names[4], entry.name)
|
2017-06-29 10:57:12 +08:00
|
|
|
assert !zis.gets.empty?
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_read_with_number_of_bytes_returns_nil_at_eof
|
|
|
|
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
|
|
|
|
entry = zis.get_next_entry # longAscii.txt
|
|
|
|
zis.read(entry.size)
|
|
|
|
assert_equal(true, zis.eof?)
|
|
|
|
assert_nil(zis.read(1))
|
|
|
|
assert_nil(zis.read(1))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-16 10:06:59 +08:00
|
|
|
def test_read_with_zero_returns_empty_string
|
|
|
|
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
|
|
|
|
assert_equal('', zis.read(0))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-21 05:31:06 +08:00
|
|
|
def test_rewind
|
2015-03-24 00:08:12 +08:00
|
|
|
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
|
2014-01-21 05:31:06 +08:00
|
|
|
e = zis.get_next_entry
|
|
|
|
assert_equal(TestZipFile::TEST_ZIP2.entry_names[0], e.name)
|
|
|
|
|
|
|
|
# Do a little reading
|
2021-03-06 08:36:09 +08:00
|
|
|
buf = +''
|
2014-01-21 05:31:06 +08:00
|
|
|
buf << zis.read(100)
|
|
|
|
assert_equal(100, zis.pos)
|
2015-03-21 16:27:44 +08:00
|
|
|
buf << (zis.gets || '')
|
|
|
|
buf << (zis.gets || '')
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_equal(false, zis.eof?)
|
|
|
|
|
|
|
|
zis.rewind
|
|
|
|
|
2021-03-06 08:36:09 +08:00
|
|
|
buf2 = +''
|
2014-01-21 05:31:06 +08:00
|
|
|
buf2 << zis.read(100)
|
2015-03-21 16:27:44 +08:00
|
|
|
buf2 << (zis.gets || '')
|
|
|
|
buf2 << (zis.gets || '')
|
2014-01-21 05:31:06 +08:00
|
|
|
|
|
|
|
assert_equal(buf, buf2)
|
|
|
|
|
|
|
|
zis.rewind
|
|
|
|
assert_equal(false, zis.eof?)
|
|
|
|
assert_equal(0, zis.pos)
|
|
|
|
|
|
|
|
assert_entry(e.name, zis, e.name)
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mix_read_and_gets
|
2015-03-24 00:08:12 +08:00
|
|
|
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
|
2014-01-21 05:31:06 +08:00
|
|
|
zis.get_next_entry
|
2015-03-21 16:27:44 +08:00
|
|
|
assert_equal('#!/usr/bin/env ruby', zis.gets.chomp)
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_equal(false, zis.eof?)
|
2015-03-21 16:27:44 +08:00
|
|
|
assert_equal('', zis.gets.chomp)
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_equal(false, zis.eof?)
|
2015-03-21 16:27:44 +08:00
|
|
|
assert_equal('$VERBOSE =', zis.read(10))
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_equal(false, zis.eof?)
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2014-03-12 09:19:24 +08:00
|
|
|
def test_ungetc
|
|
|
|
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
|
|
|
|
zis.get_next_entry
|
|
|
|
first_line = zis.gets.chomp
|
2014-03-12 23:28:02 +08:00
|
|
|
first_line.reverse.bytes.each { |b| zis.ungetc(b) }
|
2014-03-12 09:19:24 +08:00
|
|
|
assert_equal('#!/usr/bin/env ruby', zis.gets.chomp)
|
2015-03-21 16:27:44 +08:00
|
|
|
assert_equal('$VERBOSE =', zis.read(10))
|
2014-03-12 09:19:24 +08:00
|
|
|
end
|
|
|
|
end
|
2020-11-09 01:19:49 +08:00
|
|
|
|
|
|
|
def test_readline_then_read
|
|
|
|
::Zip::InputStream.open(TestZipFile::TEST_ZIP2.zip_name) do |zis|
|
|
|
|
zis.get_next_entry
|
|
|
|
assert_equal("#!/usr/bin/env ruby\n", zis.readline)
|
|
|
|
refute(zis.eof?)
|
|
|
|
refute_empty(zis.read) # Also should not raise an error.
|
|
|
|
assert(zis.eof?)
|
|
|
|
end
|
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|