Skip to content

Commit fb6097a

Browse files
jplothainesr
andcommitted
Add tests for AES decryption.
Co-authored-by: Robert Haines <rhaines@manchester.ac.uk>
1 parent 059da9c commit fb6097a

File tree

6 files changed

+70
-0
lines changed

6 files changed

+70
-0
lines changed

test/crypto/aes_encryption_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
5+
class AESDecrypterTest < MiniTest::Test
6+
def setup
7+
@decrypter256 = ::Zip::AESDecrypter.new('password', ::Zip::AESEncryption::STRENGTH_256_BIT)
8+
@decrypter128 = ::Zip::AESDecrypter.new('password', ::Zip::AESEncryption::STRENGTH_128_BIT)
9+
end
10+
11+
def test_header_bytesize
12+
assert_equal 18, @decrypter256.header_bytesize
13+
end
14+
15+
def test_gp_flags
16+
assert_equal 1, @decrypter256.gp_flags
17+
end
18+
19+
def test_decrypt_aes256
20+
@decrypter256.reset!([125, 138, 163, 42, 19, 1, 155, 66, 203, 174, 183, 235, 197, 122, 232, 68, 252, 225].pack('C*'))
21+
assert_equal 'a', @decrypter256.decrypt([161].map(&:chr).join)
22+
end
23+
24+
def test_decrypt_aes128
25+
@decrypter128.reset!([127, 254, 117, 113, 255, 209, 171, 131, 179, 106].pack('C*'))
26+
assert_equal [75, 4, 0], @decrypter128.decrypt([34, 33, 106].map(&:chr).join).chars.map(&:ord)
27+
end
28+
29+
def test_reset!
30+
@decrypter256.reset!([125, 138, 163, 42, 19, 1, 155, 66, 203, 174, 183, 235, 197, 122, 232, 68, 252, 225].pack('C*'))
31+
assert_equal 'a', @decrypter256.decrypt([161].map(&:chr).join)
32+
33+
@decrypter256.reset!([118, 221, 166, 27, 165, 141, 24, 122, 227, 197, 52, 135, 222, 67, 221, 92, 231, 117].pack('C*'))
34+
assert_equal 'b', @decrypter256.decrypt([135].map(&:chr).join)
35+
end
36+
end

test/data/zip-aes-128.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a

test/data/zip-aes-128.zip

145 Bytes
Binary file not shown.

test/data/zip-aes-256.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
b

test/data/zip-aes-256.zip

281 Bytes
Binary file not shown.

test/encryption_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
class EncryptionTest < MiniTest::Test
66
ENCRYPT_ZIP_TEST_FILE = 'test/data/zipWithEncryption.zip'
7+
AES_128_ZIP_TEST_FILE = 'test/data/zip-aes-128.zip'
8+
AES_256_ZIP_TEST_FILE = 'test/data/zip-aes-256.zip'
79
INPUT_FILE1 = 'test/data/file1.txt'
810
INPUT_FILE2 = 'test/data/file2.txt'
11+
INPUT_FILE3 = 'test/data/zip-aes-128.txt'
12+
INPUT_FILE4 = 'test/data/zip-aes-256.txt'
913

1014
def setup
1115
Zip.default_compression = ::Zlib::DEFAULT_COMPRESSION
@@ -65,4 +69,32 @@ def test_decrypt
6569
assert_equal ::File.read(INPUT_FILE2), zis.read
6670
end
6771
end
72+
73+
def test_aes_128_decrypt
74+
Zip::InputStream.open(
75+
AES_128_ZIP_TEST_FILE,
76+
decrypter: Zip::AESDecrypter.new('password', Zip::AESEncryption::STRENGTH_128_BIT)
77+
) do |zis|
78+
entry = zis.get_next_entry
79+
assert_equal 'a', entry.name
80+
assert_equal 1, entry.size
81+
assert_equal ::File.read(INPUT_FILE3), zis.read
82+
end
83+
end
84+
85+
def test_aes_256_decrypt
86+
Zip::InputStream.open(
87+
AES_256_ZIP_TEST_FILE,
88+
decrypter: Zip::AESDecrypter.new('password', Zip::AESEncryption::STRENGTH_256_BIT)
89+
) do |zis|
90+
entry = zis.get_next_entry
91+
assert_equal 'a', entry.name
92+
assert_equal 0, entry.size
93+
assert_equal '', zis.read
94+
entry = zis.get_next_entry
95+
assert_equal 'b', entry.name
96+
assert_equal 1, entry.size
97+
assert_equal ::File.read(INPUT_FILE4), zis.read
98+
end
99+
end
68100
end

0 commit comments

Comments
 (0)