Skip to content

Commit

Permalink
Adds tests on aes decryption
Browse files Browse the repository at this point in the history
  • Loading branch information
jplot committed Jun 12, 2024
1 parent aa95161 commit dbc35d0
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/crypto/aes_encryption_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require 'test_helper'

class AESDecrypterTest < MiniTest::Test
def setup
@decrypter_256 = ::Zip::AESDecrypter.new('password', ::Zip::AESEncryption::STRENGTH_256_BIT)
@decrypter_128 = ::Zip::AESDecrypter.new('password', ::Zip::AESEncryption::STRENGTH_128_BIT)
end

def test_header_bytesize
assert_equal 18, @decrypter_256.header_bytesize
end

def test_gp_flags
assert_equal 1, @decrypter_256.gp_flags
end

def test_decrypt_aes_256
@decrypter_256.reset!([125, 138, 163, 42, 19, 1, 155, 66, 203, 174, 183, 235, 197, 122, 232, 68, 252, 225].pack('C*'))
assert_equal 'a', @decrypter_256.decrypt([161].map(&:chr).join)
end

def test_decrypt_aes_128
@decrypter_128.reset!([127, 254, 117, 113, 255, 209, 171, 131, 179, 106].pack('C*'))
assert_equal [75, 4, 0], @decrypter_128.decrypt([34, 33, 106].map(&:chr).join).chars.map(&:ord)
end

def test_reset!
@decrypter_256.reset!([125, 138, 163, 42, 19, 1, 155, 66, 203, 174, 183, 235, 197, 122, 232, 68, 252, 225].pack('C*'))
assert_equal 'a', @decrypter_256.decrypt([161].map(&:chr).join)

@decrypter_256.reset!([118, 221, 166, 27, 165, 141, 24, 122, 227, 197, 52, 135, 222, 67, 221, 92, 231, 117].pack('C*'))
assert_equal 'b', @decrypter_256.decrypt([135].map(&:chr).join)
end
end
1 change: 1 addition & 0 deletions test/data/zip-aes-128.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
Binary file added test/data/zip-aes-128.zip
Binary file not shown.
1 change: 1 addition & 0 deletions test/data/zip-aes-256.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b
Binary file added test/data/zip-aes-256.zip
Binary file not shown.
32 changes: 32 additions & 0 deletions test/encryption_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

class EncryptionTest < MiniTest::Test
ENCRYPT_ZIP_TEST_FILE = 'test/data/zipWithEncryption.zip'
AES_128_ZIP_TEST_FILE = 'test/data/zip-aes-128.zip'
AES_256_ZIP_TEST_FILE = 'test/data/zip-aes-256.zip'
INPUT_FILE1 = 'test/data/file1.txt'
INPUT_FILE2 = 'test/data/file2.txt'
INPUT_FILE3 = 'test/data/zip-aes-128.txt'
INPUT_FILE4 = 'test/data/zip-aes-256.txt'

def setup
Zip.default_compression = ::Zlib::DEFAULT_COMPRESSION
Expand Down Expand Up @@ -65,4 +69,32 @@ def test_decrypt
assert_equal ::File.read(INPUT_FILE2), zis.read
end
end

def test_aes_128_decrypt
Zip::InputStream.open(
AES_128_ZIP_TEST_FILE,
decrypter: Zip::AESDecrypter.new('password', Zip::AESEncryption::STRENGTH_128_BIT)
) do |zis|
entry = zis.get_next_entry
assert_equal 'a', entry.name
assert_equal 1, entry.size
assert_equal ::File.read(INPUT_FILE3), zis.read
end
end

def test_aes_256_decrypt
Zip::InputStream.open(
AES_256_ZIP_TEST_FILE,
decrypter: Zip::AESDecrypter.new('password', Zip::AESEncryption::STRENGTH_256_BIT)
) do |zis|
entry = zis.get_next_entry
assert_equal 'a', entry.name
assert_equal 0, entry.size
assert_equal '', zis.read
entry = zis.get_next_entry
assert_equal 'b', entry.name
assert_equal 1, entry.size
assert_equal ::File.read(INPUT_FILE4), zis.read
end
end
end

0 comments on commit dbc35d0

Please sign in to comment.