/
deprecated_open_ssl_constant_spec.rb
102 lines (83 loc) · 3.32 KB
/
deprecated_open_ssl_constant_spec.rb
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::DeprecatedOpenSSLConstant do
subject(:cop) { described_class.new(config) }
let(:config) { RuboCop::Config.new }
it 'registers an offense with cipher constant and two arguments and corrects' do
expect_offense(<<~RUBY)
OpenSSL::Cipher::AES.new(128, :GCM)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Cipher.new('AES-128-GCM')` instead of `OpenSSL::Cipher::AES.new(128, :GCM)`.
RUBY
expect_correction(<<~RUBY)
OpenSSL::Cipher.new('AES-128-GCM')
RUBY
end
it 'registers an offense with cipher constant and one argument and corrects' do
expect_offense(<<~RUBY)
OpenSSL::Cipher::AES.new('128-GCM')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Cipher.new('AES-128-GCM')` instead of `OpenSSL::Cipher::AES.new('128-GCM')`.
RUBY
expect_correction(<<~RUBY)
OpenSSL::Cipher.new('AES-128-GCM')
RUBY
end
it 'registers an offense with AES + blocksize constant and mode argument and corrects' do
expect_offense(<<~RUBY)
OpenSSL::Cipher::AES128.new(:GCM)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Cipher.new('AES-128-GCM')` instead of `OpenSSL::Cipher::AES128.new(:GCM)`.
RUBY
expect_correction(<<~RUBY)
OpenSSL::Cipher.new('AES-128-GCM')
RUBY
end
it 'registers an offense with AES + blocksize constant and corrects' do
expect_offense(<<~RUBY)
OpenSSL::Cipher::AES128.new
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Cipher.new('AES-128-CBC')` instead of `OpenSSL::Cipher::AES128.new`.
RUBY
expect_correction(<<~RUBY)
OpenSSL::Cipher.new('AES-128-CBC')
RUBY
end
it 'does not register an offense when using cipher with a string' do
expect_no_offenses(<<~RUBY)
OpenSSL::Cipher.new('AES-128-GCM')
RUBY
end
it 'registers an offense when building an instance using an digest constant and corrects' do
expect_offense(<<~RUBY)
OpenSSL::Digest::SHA256.new
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Digest.new('SHA256')` instead of `OpenSSL::Digest::SHA256.new`.
RUBY
expect_correction(<<~RUBY)
OpenSSL::Digest.new('SHA256')
RUBY
end
it 'registers an offense when using ::Digest class methods on an algorithm constant and corrects' do
expect_offense(<<~RUBY)
OpenSSL::Digest::SHA256.digest('foo')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Digest.digest('SHA256', 'foo')` instead of `OpenSSL::Digest::SHA256.digest('foo')`.
RUBY
expect_correction(<<~RUBY)
OpenSSL::Digest.digest('SHA256', 'foo')
RUBY
end
it 'registers an offense when using an digest constant with chained methods and corrects' do
expect_offense(<<~RUBY)
OpenSSL::Digest::SHA256.new.digest('foo')
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Digest.new('SHA256')` instead of `OpenSSL::Digest::SHA256.new`.
RUBY
expect_correction(<<~RUBY)
OpenSSL::Digest.new('SHA256').digest('foo')
RUBY
end
it 'does not register an offense when building digest using an algorithm string' do
expect_no_offenses(<<~RUBY)
OpenSSL::Digest.new('SHA256')
RUBY
end
it 'does not register an offense when using ::Digest class methods with an algorithm string and value' do
expect_no_offenses(<<~RUBY)
OpenSSL::Digest.digest('SHA256', 'foo')
RUBY
end
end