forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deprecated_open_ssl_constant_spec.rb
169 lines (141 loc) · 5.57 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::DeprecatedOpenSSLConstant, :config do
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 cipher constant and double quoted string 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 cipher constant and `cbc` argument and corrects' do
expect_offense(<<~RUBY)
OpenSSL::Cipher::DES.new('cbc')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Cipher.new('des-cbc')` instead of `OpenSSL::Cipher::DES.new('cbc')`.
RUBY
expect_correction(<<~RUBY)
OpenSSL::Cipher.new('des-cbc')
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
RuboCop::Cop::Lint::DeprecatedOpenSSLConstant::NO_ARG_ALGORITHM.each do |algorithm_name|
it 'registers an offense with cipher constant and no arguments and corrects' do
expect_offense(<<~RUBY, algorithm_name: algorithm_name)
OpenSSL::Cipher::#{algorithm_name}.new
^^^^^^^^^^^^^^^^^^{algorithm_name}^^^^ Use `OpenSSL::Cipher.new('#{algorithm_name.downcase}')` instead of `OpenSSL::Cipher::#{algorithm_name}.new`.
RUBY
expect_correction(<<~RUBY)
OpenSSL::Cipher.new('#{algorithm_name.downcase}')
RUBY
end
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 'does not register an offense with cipher constant and argument is a variable' do
expect_no_offenses(<<~RUBY)
mode = "cbc"
OpenSSL::Cipher::AES128.new(mode)
RUBY
end
it 'does not register an offense with cipher constant and send argument is a method' do
expect_no_offenses(<<~RUBY)
OpenSSL::Cipher::AES128.new(do_something)
RUBY
end
it 'does not register an offense with cipher constant and argument is a constant' do
expect_no_offenses(<<~RUBY)
OpenSSL::Cipher::AES128.new(MODE)
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
context 'when used in a block' do
it 'registers an offense when using ::Digest class methods on an algorithm constant and corrects' do
expect_offense(<<~RUBY)
do_something do
OpenSSL::Digest::SHA1.new
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Digest.new('SHA1')` instead of `OpenSSL::Digest::SHA1.new`.
end
RUBY
expect_correction(<<~RUBY)
do_something do
OpenSSL::Digest.new('SHA1')
end
RUBY
end
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