File: traditional_encryption_test.rb

package info (click to toggle)
ruby-zip 3.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,120 kB
  • sloc: ruby: 9,958; makefile: 23
file content (82 lines) | stat: -rw-r--r-- 2,511 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true

require_relative '../test_helper'

class TraditionalEncrypterTest < Minitest::Test
  def setup
    @mtime = ::Zip::DOSTime.new(2014, 12, 17, 15, 56, 24)
    @encrypter = ::Zip::TraditionalEncrypter.new('password')
  end

  def test_header_bytesize
    assert_equal 12, @encrypter.header_bytesize
  end

  def test_gp_flags
    assert_equal 9, @encrypter.gp_flags
  end

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

  def test_encrypt
    @encrypter.reset!
    Random.stub(:rand, 1) { @encrypter.header(@mtime) }
    assert_raises(NoMethodError) { @encrypter.encrypt(nil) }
    assert_raises(NoMethodError) { @encrypter.encrypt(1) }
    assert_equal '', @encrypter.encrypt('')
    assert_equal [100, 218, 7, 114, 226, 82, 62, 93, 224, 62].pack('C*'), @encrypter.encrypt('a' * 10)
  end

  def test_reset!
    @encrypter.reset!
    Random.stub(:rand, 1) { @encrypter.header(@mtime) }
    [100, 218, 7, 114, 226, 82, 62, 93, 224, 62].map(&:chr).each do |c|
      assert_equal c, @encrypter.encrypt('a')
    end
    assert_equal 56.chr, @encrypter.encrypt('a')
    @encrypter.reset!
    Random.stub(:rand, 1) { @encrypter.header(@mtime) }
    [100, 218, 7, 114, 226, 82, 62, 93, 224, 62].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 9, @decrypter.gp_flags
  end

  def test_decrypt
    @decrypter.reset!([239, 57, 234, 154, 246, 80, 83, 221, 74, 200, 121, 91].pack('C*'))
    [100, 218, 7, 114, 226, 82, 62, 93, 224, 62].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, 121, 91].pack('C*'))
    [100, 218, 7, 114, 226, 82, 62, 93, 224, 62].map(&:chr).each do |c|
      assert_equal 'a', @decrypter.decrypt(c)
    end
    assert_equal 91.chr, @decrypter.decrypt(2.chr)
    @decrypter.reset!([239, 57, 234, 154, 246, 80, 83, 221, 74, 200, 121, 91].pack('C*'))
    [100, 218, 7, 114, 226, 82, 62, 93, 224, 62].map(&:chr).each do |c|
      assert_equal 'a', @decrypter.decrypt(c)
    end
  end
end