Skip to content

Commit

Permalink
added test cases for encryption support
Browse files Browse the repository at this point in the history
  • Loading branch information
matsu911 committed Jan 5, 2015
1 parent fd26052 commit 87d1a5a
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/zip/crypto/traditional_encryption.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TraditionalEncrypter < Encrypter
def header(crc32)
[].tap do |header|
(header_bytesize - 1).times do
header << rand(0..255)
header << Random.rand(0..255)
end
header << (crc32 >> 24)
end.map{|x| encode x}.pack("C*")
Expand Down
55 changes: 55 additions & 0 deletions test/crypto/null_encryption_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'test_helper'

class NullEncrypterTest < MiniTest::Test
def setup
@encrypter = ::Zip::NullEncrypter.new
end

def test_header_bytesize
assert_equal 0, @encrypter.header_bytesize
end

def test_gp_flags
assert_equal 0, @encrypter.gp_flags
end

def test_header
[nil, '', 'a' * 10, 0xffffffff].each do |arg|
assert_empty @encrypter.header(arg)
end
end

def test_encrypt
[nil, '', 'a' * 10, 0xffffffff].each do |data|
assert_equal data, @encrypter.encrypt(data)
end
end

def test_reset!
assert_respond_to @encrypter, :reset!
end
end

class NullDecrypterTest < MiniTest::Test
def setup
@decrypter = ::Zip::NullDecrypter.new
end

def test_header_bytesize
assert_equal 0, @decrypter.header_bytesize
end

def test_gp_flags
assert_equal 0, @decrypter.gp_flags
end

def test_decrypt
[nil, '', 'a' * 10, 0xffffffff].each do |data|
assert_equal data, @decrypter.decrypt(data)
end
end

def test_reset!
assert_respond_to @decrypter, :reset!
end
end
79 changes: 79 additions & 0 deletions test/crypto/traditional_encryption_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
require 'test_helper'

class TraditionalEncrypterTest < MiniTest::Test
def setup
@encrypter = ::Zip::TraditionalEncrypter.new('password')
end

def test_header_bytesize
assert_equal 12, @encrypter.header_bytesize
end

def test_gp_flags
assert_equal 1, @encrypter.gp_flags
end

def test_header
@encrypter.reset!
exepected = [239, 57, 234, 154, 246, 80, 83, 221, 74, 200, 116, 154].pack("C*")
Random.stub(:rand, 1) do
assert_equal exepected, @encrypter.header(0xffffffff)
end
end

def test_encrypt
@encrypter.reset!
Random.stub(:rand, 1) { @encrypter.header(0xffffffff) }
assert_raises(NoMethodError) { @encrypter.encrypt(nil) }
assert_raises(NoMethodError) { @encrypter.encrypt(1) }
assert_equal '', @encrypter.encrypt('')
assert_equal [2, 25, 13, 222, 17, 190, 250, 133, 133, 166].pack("C*"), @encrypter.encrypt('a' * 10)
end

def test_reset!
@encrypter.reset!
Random.stub(:rand, 1) { @encrypter.header(0xffffffff) }
[2, 25, 13, 222, 17, 190, 250, 133, 133, 166].map(&:chr).each do |c|
assert_equal c, @encrypter.encrypt('a')
end
assert_equal 134.chr, @encrypter.encrypt('a')
@encrypter.reset!
Random.stub(:rand, 1) { @encrypter.header(0xffffffff) }
[2, 25, 13, 222, 17, 190, 250, 133, 133, 166].map(&:chr).each do |c|
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
assert_equal 1, @decrypter.gp_flags
end

def test_decrypt
@decrypter.reset!([239, 57, 234, 154, 246, 80, 83, 221, 74, 200, 116, 154].pack("C*"))
[2, 25, 13, 222, 17, 190, 250, 133, 133, 166].map(&:chr).each do |c|
assert_equal 'a', @decrypter.decrypt(c)
end
end

def test_reset!
@decrypter.reset!([239, 57, 234, 154, 246, 80, 83, 221, 74, 200, 116, 154].pack("C*"))
[2, 25, 13, 222, 17, 190, 250, 133, 133, 166].map(&:chr).each do |c|
assert_equal 'a', @decrypter.decrypt(c)
end
assert_equal 229.chr, @decrypter.decrypt(2.chr)
@decrypter.reset!([239, 57, 234, 154, 246, 80, 83, 221, 74, 200, 116, 154].pack("C*"))
[2, 25, 13, 222, 17, 190, 250, 133, 133, 166].map(&:chr).each do |c|
assert_equal 'a', @decrypter.decrypt(c)
end
end
end

0 comments on commit 87d1a5a

Please sign in to comment.