Change encryption_test less implementation specific

Seems like zlib deflate compress differently when Zlib::SYNC_FLUSH is
used.
This commit is contained in:
Joni Lahtinen 2020-04-29 16:41:21 +03:00
parent 5af76cecb5
commit 99d8f59eaf
1 changed files with 19 additions and 11 deletions

View File

@ -14,20 +14,28 @@ class EncryptionTest < MiniTest::Test
end end
def test_encrypt def test_encrypt
test_file = ::File.open(ENCRYPT_ZIP_TEST_FILE, 'rb').read content = File.open(INPUT_FILE1, 'r').read
test_filename = 'top_secret_file.txt'
@rand = [250, 143, 107, 13, 143, 22, 155, 75, 228, 150, 12] password = 'swordfish'
@output = ::Zip::DOSTime.stub(:now, ::Zip::DOSTime.new(2014, 12, 17, 15, 56, 24)) do
Random.stub(:rand, ->(_range) { @rand.shift }) do encrypted_zip = Zip::OutputStream.write_buffer(::StringIO.new(''), Zip::TraditionalEncrypter.new(password)) do |out|
Zip::OutputStream.write_buffer(::StringIO.new(''), Zip::TraditionalEncrypter.new('password')) do |zos| out.put_next_entry(test_filename)
zos.put_next_entry('file1.txt') out.write content
zos.write ::File.open(INPUT_FILE1).read
end.string
end
end end
@output.unpack('C*').each_with_index do |c, i| Zip::InputStream.open(encrypted_zip, 0, Zip::TraditionalDecrypter.new(password)) do |zis|
assert_equal test_file[i].ord, c entry = zis.get_next_entry
assert_equal test_filename, entry.name
assert_equal 1327, entry.size
assert_equal content, zis.read
end
assert_raises(Zip::DecompressionError) do
Zip::InputStream.open(encrypted_zip, 0, Zip::TraditionalDecrypter.new(password + 'wrong')) do |zis|
zis.get_next_entry
assert_equal content, zis.read
end
end end
end end