2015-01-05 22:20:48 +08:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class TraditionalEncrypterTest < MiniTest::Test
|
|
|
|
def setup
|
2015-01-08 17:30:32 +08:00
|
|
|
@mtime = ::Zip::DOSTime.new(2014, 12, 17, 15, 56, 24)
|
2015-01-05 22:20:48 +08:00
|
|
|
@encrypter = ::Zip::TraditionalEncrypter.new('password')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_header_bytesize
|
|
|
|
assert_equal 12, @encrypter.header_bytesize
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_gp_flags
|
2015-01-08 17:30:32 +08:00
|
|
|
assert_equal 9, @encrypter.gp_flags
|
2015-01-05 22:20:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_header
|
|
|
|
@encrypter.reset!
|
2015-03-21 16:27:44 +08:00
|
|
|
exepected = [239, 57, 234, 154, 246, 80, 83, 221, 74, 200, 121, 91].pack('C*')
|
2015-01-05 22:20:48 +08:00
|
|
|
Random.stub(:rand, 1) do
|
2015-01-08 17:30:32 +08:00
|
|
|
assert_equal exepected, @encrypter.header(@mtime)
|
2015-01-05 22:20:48 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_encrypt
|
|
|
|
@encrypter.reset!
|
2015-01-08 17:30:32 +08:00
|
|
|
Random.stub(:rand, 1) { @encrypter.header(@mtime) }
|
2015-01-05 22:20:48 +08:00
|
|
|
assert_raises(NoMethodError) { @encrypter.encrypt(nil) }
|
|
|
|
assert_raises(NoMethodError) { @encrypter.encrypt(1) }
|
|
|
|
assert_equal '', @encrypter.encrypt('')
|
2015-03-21 16:27:44 +08:00
|
|
|
assert_equal [100, 218, 7, 114, 226, 82, 62, 93, 224, 62].pack('C*'), @encrypter.encrypt('a' * 10)
|
2015-01-05 22:20:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_reset!
|
|
|
|
@encrypter.reset!
|
2015-01-08 17:30:32 +08:00
|
|
|
Random.stub(:rand, 1) { @encrypter.header(@mtime) }
|
|
|
|
[100, 218, 7, 114, 226, 82, 62, 93, 224, 62].map(&:chr).each do |c|
|
2015-01-05 22:20:48 +08:00
|
|
|
assert_equal c, @encrypter.encrypt('a')
|
|
|
|
end
|
2015-01-08 17:30:32 +08:00
|
|
|
assert_equal 56.chr, @encrypter.encrypt('a')
|
2015-01-05 22:20:48 +08:00
|
|
|
@encrypter.reset!
|
2015-01-08 17:30:32 +08:00
|
|
|
Random.stub(:rand, 1) { @encrypter.header(@mtime) }
|
|
|
|
[100, 218, 7, 114, 226, 82, 62, 93, 224, 62].map(&:chr).each do |c|
|
2015-01-05 22:20:48 +08:00
|
|
|
assert_equal c, @encrypter.encrypt('a')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TraditionalDecrypterTest < MiniTest::Test
|
|
|
|
def setup
|
|
|
|
@decrypter = ::Zip::TraditionalDecrypter.new('password')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_header_bytesize
|
|
|
|
assert_equal 12, @decrypter.header_bytesize
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_gp_flags
|
2015-01-08 17:30:32 +08:00
|
|
|
assert_equal 9, @decrypter.gp_flags
|
2015-01-05 22:20:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_decrypt
|
2015-03-21 16:27:44 +08:00
|
|
|
@decrypter.reset!([239, 57, 234, 154, 246, 80, 83, 221, 74, 200, 121, 91].pack('C*'))
|
2015-01-08 17:30:32 +08:00
|
|
|
[100, 218, 7, 114, 226, 82, 62, 93, 224, 62].map(&:chr).each do |c|
|
2015-01-05 22:20:48 +08:00
|
|
|
assert_equal 'a', @decrypter.decrypt(c)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_reset!
|
2015-03-21 16:27:44 +08:00
|
|
|
@decrypter.reset!([239, 57, 234, 154, 246, 80, 83, 221, 74, 200, 121, 91].pack('C*'))
|
2015-01-08 17:30:32 +08:00
|
|
|
[100, 218, 7, 114, 226, 82, 62, 93, 224, 62].map(&:chr).each do |c|
|
2015-01-05 22:20:48 +08:00
|
|
|
assert_equal 'a', @decrypter.decrypt(c)
|
|
|
|
end
|
2015-01-08 17:30:32 +08:00
|
|
|
assert_equal 91.chr, @decrypter.decrypt(2.chr)
|
2015-03-21 16:27:44 +08:00
|
|
|
@decrypter.reset!([239, 57, 234, 154, 246, 80, 83, 221, 74, 200, 121, 91].pack('C*'))
|
2015-01-08 17:30:32 +08:00
|
|
|
[100, 218, 7, 114, 226, 82, 62, 93, 224, 62].map(&:chr).each do |c|
|
2015-01-05 22:20:48 +08:00
|
|
|
assert_equal 'a', @decrypter.decrypt(c)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|