Browse Source

Remove Statused class

new-scoring
aeris 2 years ago
parent
commit
5e1715b0fc

+ 2
- 3
lib/cryptcheck.rb View File

@@ -30,8 +30,7 @@ module CryptCheck
30 30
 		end
31 31
 	end
32 32
 
33
-	autoload :Status, 'cryptcheck/status'
34
-	autoload :Statused, 'cryptcheck/statused'
33
+	autoload :State, 'cryptcheck/state'
35 34
 	autoload :Logger, 'cryptcheck/logger'
36 35
 	autoload :Tls, 'cryptcheck/tls'
37 36
 	module Tls
@@ -96,7 +95,7 @@ module CryptCheck
96 95
 						else
97 96
 							server.new *a, **kargs
98 97
 						end
99
-					ap s.status
98
+					ap s.states
100 99
 					exit
101 100
 					if grade
102 101
 						g = grade.new s

+ 0
- 51
lib/cryptcheck/status.rb View File

@@ -1,51 +0,0 @@
1
-module CryptCheck
2
-	class Status
3
-		LEVELS   = %i(best perfect good warning error critical).freeze
4
-		PROBLEMS = %i(warning error critical).freeze
5
-
6
-		extend Enumerable
7
-
8
-		def self.each(&block)
9
-			LEVELS.each &block
10
-		end
11
-
12
-		def self.empty
13
-			self.collect { |s| [s, []] }.to_h
14
-		end
15
-
16
-		def self.status(statuses)
17
-			statuses = self.convert statuses
18
-			self.min LEVELS, statuses
19
-		end
20
-
21
-		class << self
22
-			alias_method :'[]', :status
23
-		end
24
-
25
-		def self.problem(statuses)
26
-			statuses = self.convert statuses
27
-			self.min PROBLEMS, statuses
28
-		end
29
-
30
-		def self.sort(statuses)
31
-			statuses.sort { |a, b| self.compare a, b }
32
-		end
33
-
34
-		def self.compare(a, b)
35
-			LEVELS.find_index(a.status) <=> LEVELS.find_index(b.status)
36
-		end
37
-
38
-		private
39
-		def self.convert(statuses)
40
-			statuses = [statuses] unless statuses.respond_to? :first
41
-			first    = statuses.first
42
-			statuses = statuses.collect &:status if first.respond_to? :status
43
-			statuses
44
-		end
45
-
46
-		def self.min(levels, statuses)
47
-			return nil if statuses.empty?
48
-			(levels & statuses).last
49
-		end
50
-	end
51
-end

+ 0
- 48
lib/cryptcheck/statused.rb View File

@@ -1,48 +0,0 @@
1
-module CryptCheck
2
-	module Statused
3
-		def status
4
-			@status ||= calculate_status
5
-		end
6
-
7
-		private
8
-		def merge(statuses)
9
-			Status.collect do |s|
10
-				status = statuses.collect { |ss| ss[s] }
11
-				status = status.inject &:+
12
-				[s, status.uniq]
13
-			end.to_h
14
-		end
15
-
16
-		def checks
17
-			[]
18
-		end
19
-
20
-		def children
21
-			[]
22
-		end
23
-
24
-		def perform_check(check)
25
-			name, check, level = check
26
-			result             = check.call self
27
-			return nil unless result
28
-			level ||= result
29
-			[level, name]
30
-		end
31
-
32
-		def personal_status
33
-			states = Status.empty
34
-			checks.each do |check|
35
-				level, name = perform_check check
36
-				next unless level
37
-				states[level] << name
38
-			end
39
-			states
40
-		end
41
-
42
-		def calculate_status
43
-			children_statuses = children.collect(&:status)
44
-			statuses = [personal_status] + children_statuses
45
-			merge statuses
46
-		end
47
-	end
48
-end

+ 1
- 1
lib/cryptcheck/tls/cert.rb View File

@@ -107,7 +107,7 @@ module CryptCheck
107 107
 				@cert.issuer
108 108
 			end
109 109
 
110
-			include ::CryptCheck::Statused
110
+			include ::CryptCheck::State
111 111
 
112 112
 			CHECKS = [:weak_sign, -> (s) do
113 113
 				not (SIGNATURE_ALGORITHMS_X509[s.signature_algorithm] & WEAK_SIGN).empty?

+ 4
- 4
lib/cryptcheck/tls/fixture.rb View File

@@ -17,7 +17,7 @@ class ::OpenSSL::PKey::EC
17 17
 		"ECC #{self.size} bits"
18 18
 	end
19 19
 
20
-	include ::CryptCheck::Statused
20
+	include ::CryptCheck::State
21 21
 
22 22
 	CHECKS = [
23 23
 			[:weak_key, -> (s) do
@@ -50,7 +50,7 @@ class ::OpenSSL::PKey::RSA
50 50
 		"RSA #{self.size} bits"
51 51
 	end
52 52
 
53
-	include ::CryptCheck::Statused
53
+	include ::CryptCheck::State
54 54
 
55 55
 	CHECKS = [
56 56
 			[:weak_key, -> (s) do
@@ -81,7 +81,7 @@ class ::OpenSSL::PKey::DSA
81 81
 		"DSA #{self.size} bits"
82 82
 	end
83 83
 
84
-	include ::CryptCheck::Statused
84
+	include ::CryptCheck::State
85 85
 
86 86
 	CHECKS = [
87 87
 			[:weak_key, -> (_) { :critical }]
@@ -105,7 +105,7 @@ class ::OpenSSL::PKey::DH
105 105
 		"DH #{self.size} bits"
106 106
 	end
107 107
 
108
-	include ::CryptCheck::Statused
108
+	include ::CryptCheck::State
109 109
 
110 110
 	CHECKS = [
111 111
 			[:weak_dh, -> (s) do

+ 1
- 1
lib/cryptcheck/tls/server.rb View File

@@ -69,7 +69,7 @@ module CryptCheck
69 69
 				@cert.extensions.any? { |e| e.oid == '1.3.6.1.5.5.7.1.24' }
70 70
 			end
71 71
 
72
-			include Statused
72
+			include State
73 73
 
74 74
 			CHECKS = [
75 75
 					# Protocols

+ 136
- 6
spec/cryptcheck/status_spec.rb View File

@@ -1,9 +1,9 @@
1 1
 require 'ostruct'
2 2
 
3
-describe CryptCheck::Status do
3
+describe CryptCheck::State do
4 4
 	describe '::status' do
5 5
 		it 'must handle empty list' do
6
-			expect(CryptCheck::Status.status []).to be_nil
6
+			expect(CryptCheck::State.status []).to be_nil
7 7
 		end
8 8
 
9 9
 		it 'must answer correctly' do
@@ -56,7 +56,7 @@ describe CryptCheck::Status do
56 56
 					[:best, :perfect]      => :perfect,
57 57
 					[:best, :best]         => :best
58 58
 			}.each do |levels, result|
59
-				got = CryptCheck::Status.status levels
59
+				got = CryptCheck::State.status levels
60 60
 				expect(got).to be(result), "#{levels} : expected #{result.inspect}, got #{got.inspect}"
61 61
 			end
62 62
 		end
@@ -64,7 +64,7 @@ describe CryptCheck::Status do
64 64
 		it 'must handle object list' do
65 65
 			critical = OpenStruct.new status: :critical
66 66
 			warning  = OpenStruct.new status: :warning
67
-			expect(CryptCheck::Status.status [critical, warning]).to be :critical
67
+			expect(CryptCheck::State.status [critical, warning]).to be :critical
68 68
 		end
69 69
 	end
70 70
 
@@ -119,7 +119,7 @@ describe CryptCheck::Status do
119 119
 					[:best, :perfect]      => nil,
120 120
 					[:best, :best]         => nil
121 121
 			}.each do |levels, result|
122
-				got = CryptCheck::Status.problem levels
122
+				got = CryptCheck::State.problem levels
123 123
 				expect(got).to be(result), "#{levels} : expected #{result.inspect}, got #{got.inspect}"
124 124
 			end
125 125
 		end
@@ -127,7 +127,137 @@ describe CryptCheck::Status do
127 127
 		it 'must handle object list' do
128 128
 			critical = OpenStruct.new status: :critical
129 129
 			warning  = OpenStruct.new status: :warning
130
-			expect(CryptCheck::Status.problem [critical, warning]).to be :critical
130
+			expect(CryptCheck::State.problem [critical, warning]).to be :critical
131
+		end
132
+	end
133
+
134
+	describe '#states' do
135
+		def match_states(actual, **expected)
136
+			expected = ::CryptCheck::State.empty.merge expected
137
+			expect(actual.states).to eq expected
138
+		end
139
+
140
+		let(:empty) do
141
+			Class.new do
142
+				include ::CryptCheck::State
143
+
144
+				def checks
145
+					[]
146
+				end
147
+			end.new
148
+		end
149
+		let(:childless) do
150
+			Class.new do
151
+				include ::CryptCheck::State
152
+
153
+				def checks
154
+					[
155
+							[:foo, -> (_) { true }, :critical],
156
+							[:bar, -> (_) { :error }],
157
+							[:baz, -> (_) { false }]
158
+					]
159
+				end
160
+			end.new
161
+		end
162
+		let(:parent) do
163
+			child = Class.new do
164
+				include ::CryptCheck::State
165
+
166
+				def checks
167
+					[[:bar, -> (_) { :error }]]
168
+				end
169
+			end.new
170
+			Class.new do
171
+				include ::CryptCheck::State
172
+
173
+				def initialize(child)
174
+					@child = child
175
+				end
176
+
177
+				def checks
178
+					[[:foo, -> (_) { :critical }]]
179
+				end
180
+
181
+				def children
182
+					[@child]
183
+				end
184
+			end.new(child)
185
+		end
186
+		let(:duplicated) do
187
+			child = Class.new do
188
+				include ::CryptCheck::State
189
+
190
+				def checks
191
+					[[:foo, -> (_) { :critical }]]
192
+				end
193
+			end.new
194
+			Class.new do
195
+				include ::CryptCheck::State
196
+
197
+				def initialize(child)
198
+					@child = child
199
+				end
200
+
201
+				def checks
202
+					[[:foo, -> (_) { :critical }]]
203
+				end
204
+
205
+				def children
206
+					[@child]
207
+				end
208
+			end.new(child)
209
+		end
210
+
211
+		it 'must return empty if no check nor child' do
212
+			match_states empty
213
+		end
214
+
215
+		it 'must return personal status if no child' do
216
+			match_states childless, critical: %i(foo), error: %i(bar)
217
+		end
218
+
219
+		it 'must return personal and children statuses' do
220
+			match_states parent, critical: %i(foo), error: %i(bar)
221
+		end
222
+
223
+		it 'must return remove duplicated status' do
224
+			match_states duplicated, critical: %i(foo)
225
+		end
226
+	end
227
+
228
+	describe '#status' do
229
+		it 'must return nil if nothing special' do
230
+			empty = Class.new do
231
+				include ::CryptCheck::State
232
+
233
+				def checks
234
+					[]
235
+				end
236
+			end.new
237
+			expect(empty.status).to be_nil
238
+		end
239
+
240
+		it 'must return the status if only one' do
241
+			empty = Class.new do
242
+				include ::CryptCheck::State
243
+
244
+				def checks
245
+					[[:foo, -> (_) { :critical }]]
246
+				end
247
+			end.new
248
+			expect(empty.status).to be :critical
249
+		end
250
+
251
+		it 'must return the worst status if multiple' do
252
+			empty = Class.new do
253
+				include ::CryptCheck::State
254
+
255
+				def checks
256
+					[[:foo, -> (_) { :critical }],
257
+					 [:bar, -> (_) { :error }]]
258
+				end
259
+			end.new
260
+			expect(empty.status).to be :critical
131 261
 		end
132 262
 	end
133 263
 end

+ 0
- 82
spec/cryptcheck/statused_spec.rb View File

@@ -1,82 +0,0 @@
1
-describe CryptCheck::Statused do
2
-	def match_status(actual, **expected)
3
-		expected = ::CryptCheck::Status.empty.merge expected
4
-		expect(actual.status).to eq expected
5
-	end
6
-
7
-	describe '::status' do
8
-		it 'must return empty if no check nor child' do
9
-			statused = Class.new do
10
-				include ::CryptCheck::Statused
11
-			end.new
12
-			match_status statused
13
-		end
14
-
15
-		it 'must return personal status if no child' do
16
-			statused = Class.new do
17
-				include ::CryptCheck::Statused
18
-
19
-				def checks
20
-					[
21
-							[:foo, -> (_) { true }, :critical],
22
-							[:bar, -> (_) { :error }],
23
-							[:baz, -> (_) { false }]
24
-					]
25
-				end
26
-			end.new
27
-			match_status statused, critical: %i(foo), error: %i(bar)
28
-		end
29
-
30
-		it 'must return personal and children statuses' do
31
-			child  = Class.new do
32
-				include ::CryptCheck::Statused
33
-
34
-				def checks
35
-					[[:bar, -> (_) { :error }]]
36
-				end
37
-			end.new
38
-			parent = Class.new do
39
-				include ::CryptCheck::Statused
40
-
41
-				def initialize(child)
42
-					@child = child
43
-				end
44
-
45
-				def checks
46
-					[[:foo, -> (_) { :critical }]]
47
-				end
48
-
49
-				def children
50
-					[@child]
51
-				end
52
-			end.new(child)
53
-			match_status parent, critical: %i(foo), error: %i(bar)
54
-		end
55
-
56
-		it 'must return remove duplicated status' do
57
-			child  = Class.new do
58
-				include ::CryptCheck::Statused
59
-
60
-				def checks
61
-					[[:foo, -> (_) { :critical }]]
62
-				end
63
-			end.new
64
-			parent = Class.new do
65
-				include ::CryptCheck::Statused
66
-
67
-				def initialize(child)
68
-					@child = child
69
-				end
70
-
71
-				def checks
72
-					[[:foo, -> (_) { :critical }]]
73
-				end
74
-
75
-				def children
76
-					[@child]
77
-				end
78
-			end.new(child)
79
-			match_status parent, critical: %i(foo)
80
-		end
81
-	end
82
-end

Loading…
Cancel
Save