metasploit-framework /lib/gemcache/ruby/1.9.1/gems/state_machine-1.1.2/test/unit/node_collection_test.rb

Language Ruby Lines 363
MD5 Hash 2675c85de28e1f853b1eaf7761b69391 Estimated Cost $7,338 (why?)
Repository https://bitbucket.org/technopunk2099/metasploit-framework.git View Raw File View Project SPDX
  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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')

class Node < Struct.new(:name, :value, :machine)
  def context
    yield
  end
end

class NodeCollectionByDefaultTest < Test::Unit::TestCase
  def setup
    @machine = StateMachine::Machine.new(Class.new)
    @collection = StateMachine::NodeCollection.new(@machine)
  end
  
  def test_should_not_have_any_nodes
    assert_equal 0, @collection.length
  end
  
  def test_should_have_a_machine
    assert_equal @machine, @collection.machine
  end
  
  def test_should_index_by_name
    @collection << object = Node.new(:parked)
    assert_equal object, @collection[:parked]
  end
end

class NodeCollectionTest < Test::Unit::TestCase
  def setup
    @machine = StateMachine::Machine.new(Class.new)
    @collection = StateMachine::NodeCollection.new(@machine)
  end
  
  def test_should_raise_exception_if_invalid_option_specified
    exception = assert_raise(ArgumentError) { StateMachine::NodeCollection.new(@machine, :invalid => true) }
    assert_equal 'Invalid key(s): invalid', exception.message
  end
  
  def test_should_raise_exception_on_lookup_if_invalid_index_specified
    exception = assert_raise(ArgumentError) { @collection[:something, :invalid] }
    assert_equal 'Invalid index: :invalid', exception.message
  end
  
  def test_should_raise_exception_on_fetch_if_invalid_index_specified
    exception = assert_raise(ArgumentError) { @collection.fetch(:something, :invalid) }
    assert_equal 'Invalid index: :invalid', exception.message
  end
end

class NodeCollectionAfterBeingCopiedTest < Test::Unit::TestCase
  def setup
    machine = StateMachine::Machine.new(Class.new)
    @collection = StateMachine::NodeCollection.new(machine)
    @collection << @parked = Node.new(:parked)
    
    @contexts_run = contexts_run = []
    @collection.context([:parked]) {contexts_run << :parked}
    @contexts_run.clear
    
    @copied_collection = @collection.dup
    @copied_collection << @idling = Node.new(:idling)
    @copied_collection.context([:first_gear]) {contexts_run << :first_gear}
  end
  
  def test_should_not_modify_the_original_list
    assert_equal 1, @collection.length
    assert_equal 2, @copied_collection.length
  end
  
  def test_should_not_modify_the_indices
    assert_nil @collection[:idling]
    assert_equal @idling, @copied_collection[:idling]
  end
  
  def test_should_copy_each_node
    assert_not_same @parked, @copied_collection[:parked]
  end
  
  def test_should_not_run_contexts
    assert_equal [], @contexts_run
  end
  
  def test_should_not_modify_contexts
    @collection << Node.new(:first_gear)
    assert_equal [], @contexts_run
  end
  
  def test_should_copy_contexts
    @copied_collection << Node.new(:parked)
    assert !@contexts_run.empty?
  end
end

class NodeCollectionWithoutIndicesTest < Test::Unit::TestCase
  def setup
    machine = StateMachine::Machine.new(Class.new)
    @collection = StateMachine::NodeCollection.new(machine, :index => {})
  end
  
  def test_should_allow_adding_node
    @collection << Object.new
    assert_equal 1, @collection.length
  end
  
  def test_should_not_allow_keys_retrieval
    exception = assert_raise(ArgumentError) { @collection.keys }
    assert_equal 'No indices configured', exception.message
  end
  
  def test_should_not_allow_lookup
    @collection << object = Object.new
    exception = assert_raise(ArgumentError) { @collection[0] }
    assert_equal 'No indices configured', exception.message
  end
  
  def test_should_not_allow_fetching
    @collection << object = Object.new
    exception = assert_raise(ArgumentError) { @collection.fetch(0) }
    assert_equal 'No indices configured', exception.message
  end
end

class NodeCollectionWithIndicesTest < Test::Unit::TestCase
  def setup
    machine = StateMachine::Machine.new(Class.new)
    @collection = StateMachine::NodeCollection.new(machine, :index => [:name, :value])
    
    @object = Node.new(:parked, 1)
    @collection << @object
  end
  
  def test_should_use_first_index_by_default_on_key_retrieval
    assert_equal [:parked], @collection.keys
  end
  
  def test_should_allow_customizing_index_for_key_retrieval
    assert_equal [1], @collection.keys(:value)
  end
  
  def test_should_use_first_index_by_default_on_lookup
    assert_equal @object, @collection[:parked]
    assert_nil @collection[1]
  end
  
  def test_should_allow_customizing_index_on_lookup
    assert_equal @object, @collection[1, :value]
    assert_nil @collection[:parked, :value]
  end
  
  def test_should_use_first_index_by_default_on_fetch
    assert_equal @object, @collection.fetch(:parked)
    exception = assert_raise(IndexError) { @collection.fetch(1) }
    assert_equal '1 is an invalid name', exception.message
  end
  
  def test_should_allow_customizing_index_on_fetch
    assert_equal @object, @collection.fetch(1, :value)
    exception = assert_raise(IndexError) { @collection.fetch(:parked, :value) }
    assert_equal ':parked is an invalid value', exception.message
  end
end

class NodeCollectionWithNodesTest < Test::Unit::TestCase
  def setup
    @machine = StateMachine::Machine.new(Class.new)
    @collection = StateMachine::NodeCollection.new(@machine)
    
    @parked = Node.new(:parked, nil, @machine)
    @idling = Node.new(:idling, nil, @machine)
    
    @collection << @parked
    @collection << @idling
  end
  
  def test_should_be_able_to_enumerate
    order = []
    @collection.each {|object| order << object}
    
    assert_equal [@parked, @idling], order
  end
  
  def test_should_be_able_to_concatenate_multiple_nodes
    @first_gear = Node.new(:first_gear, nil, @machine)
    @second_gear = Node.new(:second_gear, nil, @machine)
    @collection.concat([@first_gear, @second_gear])
    
    order = []
    @collection.each {|object| order << object}
    assert_equal [@parked, @idling, @first_gear, @second_gear], order
  end
  
  def test_should_be_able_to_access_by_index
    assert_equal @parked, @collection.at(0)
    assert_equal @idling, @collection.at(1)
  end
  
  def test_should_deep_copy_machine_changes
    new_machine = StateMachine::Machine.new(Class.new)
    @collection.machine = new_machine
    
    assert_equal new_machine, @collection.machine
    assert_equal new_machine, @parked.machine
    assert_equal new_machine, @idling.machine
  end
end

class NodeCollectionAfterUpdateTest < Test::Unit::TestCase
  def setup
    machine = StateMachine::Machine.new(Class.new)
    @collection = StateMachine::NodeCollection.new(machine, :index => [:name, :value])
    
    @parked = Node.new(:parked, 1)
    @idling = Node.new(:idling, 2)
    
    @collection << @parked << @idling
    
    @parked.name = :parking
    @parked.value = 0
    @collection.update(@parked)
  end
  
  def test_should_not_change_the_index
    assert_equal @parked, @collection.at(0)
  end
  
  def test_should_not_duplicate_in_the_collection
    assert_equal 2, @collection.length
  end
  
  def test_should_add_each_indexed_key
    assert_equal @parked, @collection[:parking]
    assert_equal @parked, @collection[0, :value]
  end
  
  def test_should_remove_each_old_indexed_key
    assert_nil @collection[:parked]
    assert_nil @collection[1, :value]
  end
end

class NodeCollectionWithStringIndexTest < Test::Unit::TestCase
  def setup
    machine = StateMachine::Machine.new(Class.new)
    @collection = StateMachine::NodeCollection.new(machine, :index => [:name, :value])
    
    @parked = Node.new(:parked, 1)
    @collection << @parked
  end
  
  def test_should_index_by_name
    assert_equal @parked, @collection[:parked]
  end
  
  def test_should_index_by_string_name
    assert_equal @parked, @collection['parked']
  end
end

class NodeCollectionWithSymbolIndexTest < Test::Unit::TestCase
  def setup
    machine = StateMachine::Machine.new(Class.new)
    @collection = StateMachine::NodeCollection.new(machine, :index => [:name, :value])
    
    @parked = Node.new('parked', 1)
    @collection << @parked
  end
  
  def test_should_index_by_name
    assert_equal @parked, @collection['parked']
  end
  
  def test_should_index_by_symbol_name
    assert_equal @parked, @collection[:parked]
  end
end

class NodeCollectionWithNumericIndexTest < Test::Unit::TestCase
  def setup
    machine = StateMachine::Machine.new(Class.new)
    @collection = StateMachine::NodeCollection.new(machine, :index => [:name, :value])
    
    @parked = Node.new(10, 1)
    @collection << @parked
  end
  
  def test_should_index_by_name
    assert_equal @parked, @collection[10]
  end
  
  def test_should_index_by_string_name
    assert_equal @parked, @collection['10']
  end
  
  def test_should_index_by_symbol_name
    assert_equal @parked, @collection[:'10']
  end
end

class NodeCollectionWithPredefinedContextsTest < Test::Unit::TestCase
  def setup
    machine = StateMachine::Machine.new(Class.new)
    @collection = StateMachine::NodeCollection.new(machine)
    
    @contexts_run = contexts_run = []
    @collection.context([:parked]) { contexts_run << :parked }
    @collection.context([:parked]) { contexts_run << :second_parked }
  end
  
  def test_should_run_contexts_in_the_order_defined
    @collection << Node.new(:parked)
    assert_equal [:parked, :second_parked], @contexts_run
  end
  
  def test_should_not_run_contexts_if_not_matched
    @collection << Node.new(:idling)
    assert_equal [], @contexts_run
  end
end

class NodeCollectionWithPostdefinedContextsTest < Test::Unit::TestCase
  def setup
    machine = StateMachine::Machine.new(Class.new)
    @collection = StateMachine::NodeCollection.new(machine)
    @collection << Node.new(:parked)
  end
  
  def test_should_run_context_if_matched
    contexts_run = []
    @collection.context([:parked]) { contexts_run << :parked }
    assert_equal [:parked], contexts_run
  end
  
  def test_should_not_run_contexts_if_not_matched
    contexts_run = []
    @collection.context([:idling]) { contexts_run << :idling }
    assert_equal [], contexts_run
  end
end

class NodeCollectionWithMatcherContextsTest < Test::Unit::TestCase
  def setup
    machine = StateMachine::Machine.new(Class.new)
    @collection = StateMachine::NodeCollection.new(machine)
    @collection << Node.new(:parked)
  end
  
  def test_should_always_run_all_matcher_context
    contexts_run = []
    @collection.context([StateMachine::AllMatcher.instance]) { contexts_run << :all }
    assert_equal [:all], contexts_run
  end
  
  def test_should_only_run_blacklist_matcher_if_not_matched
    contexts_run = []
    @collection.context([StateMachine::BlacklistMatcher.new([:parked])]) { contexts_run << :blacklist }
    assert_equal [], contexts_run
    
    @collection << Node.new(:idling)
    assert_equal [:blacklist], contexts_run
  end
end
Back to Top