2014-01-21 05:31:06 +08:00
|
|
|
require 'simplecov'
|
|
|
|
require 'minitest/autorun'
|
|
|
|
require 'minitest/unit'
|
|
|
|
require 'fileutils'
|
2019-09-27 23:28:02 +08:00
|
|
|
require 'tmpdir'
|
2014-01-21 05:31:06 +08:00
|
|
|
require 'digest/sha1'
|
|
|
|
require 'zip'
|
|
|
|
require 'gentestfiles'
|
|
|
|
|
|
|
|
TestFiles.create_test_files
|
|
|
|
TestZipFile.create_test_zips
|
|
|
|
|
2015-11-21 18:07:30 +08:00
|
|
|
if defined? JRUBY_VERSION
|
|
|
|
require 'jruby'
|
|
|
|
JRuby.objectspace = true
|
|
|
|
end
|
|
|
|
|
2014-07-15 23:23:48 +08:00
|
|
|
::MiniTest.after_run do
|
2014-01-21 05:31:06 +08:00
|
|
|
FileUtils.rm_rf('test/data/generated')
|
|
|
|
end
|
|
|
|
|
|
|
|
module IOizeString
|
|
|
|
attr_reader :tell
|
|
|
|
|
|
|
|
def read(count = nil)
|
|
|
|
@tell ||= 0
|
2020-02-09 18:39:04 +08:00
|
|
|
count ||= size
|
2014-01-21 05:31:06 +08:00
|
|
|
retVal = slice(@tell, count)
|
|
|
|
@tell += count
|
2015-03-23 00:27:29 +08:00
|
|
|
retVal
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def seek(index, offset)
|
|
|
|
@tell ||= 0
|
|
|
|
case offset
|
|
|
|
when IO::SEEK_END
|
|
|
|
newPos = size + index
|
|
|
|
when IO::SEEK_SET
|
|
|
|
newPos = index
|
|
|
|
when IO::SEEK_CUR
|
|
|
|
newPos = @tell + index
|
|
|
|
else
|
2015-03-21 16:27:44 +08:00
|
|
|
raise 'Error in test method IOizeString::seek'
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
2015-03-23 00:25:35 +08:00
|
|
|
if newPos < 0 || newPos >= size
|
2014-01-21 05:31:06 +08:00
|
|
|
raise Errno::EINVAL
|
|
|
|
else
|
2015-03-23 01:03:50 +08:00
|
|
|
@tell = newPos
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset
|
|
|
|
@tell = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module DecompressorTests
|
|
|
|
# expects @refText, @refLines and @decompressor
|
|
|
|
|
2015-03-21 16:27:44 +08:00
|
|
|
TEST_FILE = 'test/data/file1.txt'
|
2014-01-21 05:31:06 +08:00
|
|
|
|
|
|
|
def setup
|
|
|
|
@refText = ''
|
|
|
|
File.open(TEST_FILE) { |f| @refText = f.read }
|
|
|
|
@refLines = @refText.split($/)
|
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_read_everything
|
2020-01-05 23:06:05 +08:00
|
|
|
assert_equal(@refText, @decompressor.read)
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_read_in_chunks
|
2014-01-21 05:31:06 +08:00
|
|
|
chunkSize = 5
|
2020-01-05 23:06:05 +08:00
|
|
|
while (decompressedChunk = @decompressor.read(chunkSize))
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_equal(@refText.slice!(0, chunkSize), decompressedChunk)
|
|
|
|
end
|
|
|
|
assert_equal(0, @refText.size)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module AssertEntry
|
|
|
|
def assert_next_entry(filename, zis)
|
|
|
|
assert_entry(filename, zis, zis.get_next_entry.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_entry(filename, zis, entryName)
|
|
|
|
assert_equal(filename, entryName)
|
2015-03-25 00:02:54 +08:00
|
|
|
assert_entry_contents_for_stream(filename, zis, entryName)
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def assert_entry_contents_for_stream(filename, zis, entryName)
|
2015-03-21 16:27:44 +08:00
|
|
|
File.open(filename, 'rb') do |file|
|
2014-01-21 05:31:06 +08:00
|
|
|
expected = file.read
|
|
|
|
actual = zis.read
|
2017-06-29 10:57:12 +08:00
|
|
|
if expected != actual
|
2015-03-23 00:25:35 +08:00
|
|
|
if (expected && actual) && (expected.length > 400 || actual.length > 400)
|
2015-03-23 01:03:50 +08:00
|
|
|
zipEntryFilename = entryName + '.zipEntry'
|
2015-03-21 16:27:44 +08:00
|
|
|
File.open(zipEntryFilename, 'wb') { |entryfile| entryfile << actual }
|
2014-01-21 05:31:06 +08:00
|
|
|
fail("File '#{filename}' is different from '#{zipEntryFilename}'")
|
|
|
|
else
|
|
|
|
assert_equal(expected, actual)
|
|
|
|
end
|
|
|
|
end
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2015-03-25 00:37:12 +08:00
|
|
|
def self.assert_contents(filename, aString)
|
2015-03-21 16:27:44 +08:00
|
|
|
fileContents = ''
|
|
|
|
File.open(filename, 'rb') { |f| fileContents = f.read }
|
2017-06-29 10:57:12 +08:00
|
|
|
if fileContents != aString
|
2015-03-23 00:25:35 +08:00
|
|
|
if fileContents.length > 400 || aString.length > 400
|
2015-03-21 16:27:44 +08:00
|
|
|
stringFile = filename + '.other'
|
|
|
|
File.open(stringFile, 'wb') { |f| f << aString }
|
2014-01-21 05:31:06 +08:00
|
|
|
fail("File '#{filename}' is different from contents of string stored in '#{stringFile}'")
|
|
|
|
else
|
|
|
|
assert_equal(fileContents, aString)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_stream_contents(zis, testZipFile)
|
2015-06-08 15:29:08 +08:00
|
|
|
assert(!zis.nil?)
|
2014-01-21 05:31:06 +08:00
|
|
|
testZipFile.entry_names.each do |entryName|
|
|
|
|
assert_next_entry(entryName, zis)
|
|
|
|
end
|
2017-01-08 14:31:51 +08:00
|
|
|
assert_nil(zis.get_next_entry)
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_test_zip_contents(testZipFile)
|
|
|
|
::Zip::InputStream.open(testZipFile.zip_name) do |zis|
|
|
|
|
assert_stream_contents(zis, testZipFile)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def assert_entry_contents(zipFile, entryName, filename = entryName.to_s)
|
2014-01-21 05:31:06 +08:00
|
|
|
zis = zipFile.get_input_stream(entryName)
|
2015-03-25 00:02:54 +08:00
|
|
|
assert_entry_contents_for_stream(filename, zis, entryName)
|
2014-01-21 05:31:06 +08:00
|
|
|
ensure
|
|
|
|
zis.close if zis
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module CrcTest
|
|
|
|
class TestOutputStream
|
|
|
|
include ::Zip::IOExtras::AbstractOutputStream
|
|
|
|
|
|
|
|
attr_accessor :buffer
|
|
|
|
|
|
|
|
def initialize
|
2015-03-21 16:27:44 +08:00
|
|
|
@buffer = ''
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2015-03-23 00:43:44 +08:00
|
|
|
def <<(data)
|
2014-01-21 05:31:06 +08:00
|
|
|
@buffer << data
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_crc_test(compressorClass)
|
|
|
|
str = "Here's a nice little text to compute the crc for! Ho hum, it is nice nice nice nice indeed."
|
|
|
|
fakeOut = TestOutputStream.new
|
|
|
|
|
|
|
|
deflater = compressorClass.new(fakeOut)
|
|
|
|
deflater << str
|
|
|
|
assert_equal(0x919920fc, deflater.crc)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Enumerable
|
|
|
|
def compare_enumerables(otherEnumerable)
|
|
|
|
otherAsArray = otherEnumerable.to_a
|
2015-03-21 16:10:37 +08:00
|
|
|
each_with_index do |element, index|
|
2014-01-21 05:31:06 +08:00
|
|
|
return false unless yield(element, otherAsArray[index])
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2015-03-23 00:30:24 +08:00
|
|
|
size == otherAsArray.size
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module CommonZipFileFixture
|
|
|
|
include AssertEntry
|
|
|
|
|
2015-03-21 16:27:44 +08:00
|
|
|
EMPTY_FILENAME = 'emptyZipFile.zip'
|
2014-01-21 05:31:06 +08:00
|
|
|
|
|
|
|
TEST_ZIP = TestZipFile::TEST_ZIP2.clone
|
2015-03-21 16:27:44 +08:00
|
|
|
TEST_ZIP.zip_name = 'test/data/generated/5entry_copy.zip'
|
2014-01-21 05:31:06 +08:00
|
|
|
|
|
|
|
def setup
|
2014-02-07 07:00:38 +08:00
|
|
|
File.delete(EMPTY_FILENAME) if File.exist?(EMPTY_FILENAME)
|
2014-01-21 05:31:06 +08:00
|
|
|
FileUtils.cp(TestZipFile::TEST_ZIP2.zip_name, TEST_ZIP.zip_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module ExtraAssertions
|
|
|
|
def assert_forwarded(anObject, method, retVal, *expectedArgs)
|
|
|
|
callArgs = nil
|
|
|
|
setCallArgsProc = proc { |args| callArgs = args }
|
|
|
|
anObject.instance_eval <<-"end_eval"
|
|
|
|
alias #{method}_org #{method}
|
|
|
|
def #{method}(*args)
|
|
|
|
ObjectSpace._id2ref(#{setCallArgsProc.object_id}).call(args)
|
|
|
|
ObjectSpace._id2ref(#{retVal.object_id})
|
|
|
|
end
|
|
|
|
end_eval
|
|
|
|
|
|
|
|
assert_equal(retVal, yield) # Invoke test
|
|
|
|
assert_equal(expectedArgs, callArgs)
|
|
|
|
ensure
|
|
|
|
anObject.instance_eval "undef #{method}; alias #{method} #{method}_org"
|
|
|
|
end
|
|
|
|
end
|
2016-10-09 06:20:45 +08:00
|
|
|
|
|
|
|
module ZipEntryData
|
|
|
|
TEST_ZIPFILE = 'someZipFile.zip'
|
|
|
|
TEST_COMMENT = 'a comment'
|
|
|
|
TEST_COMPRESSED_SIZE = 1234
|
|
|
|
TEST_CRC = 325_324
|
|
|
|
TEST_EXTRA = 'Some data here'
|
|
|
|
TEST_COMPRESSIONMETHOD = ::Zip::Entry::DEFLATED
|
|
|
|
TEST_NAME = 'entry name'
|
|
|
|
TEST_SIZE = 8432
|
|
|
|
TEST_ISDIRECTORY = false
|
|
|
|
TEST_TIME = Time.now
|
|
|
|
end
|