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

Language Ruby Lines 702
MD5 Hash 9572d17ff6461e8f56bbe974f1cb5106 Estimated Cost $14,532 (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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')

class CallbackTest < Test::Unit::TestCase
  def test_should_raise_exception_if_invalid_type_specified
    exception = assert_raise(ArgumentError) { StateMachine::Callback.new(:invalid) {} }
    assert_equal 'Type must be :before, :after, :around, or :failure', exception.message
  end
  
  def test_should_not_raise_exception_if_using_before_type
    assert_nothing_raised { StateMachine::Callback.new(:before) {} }
  end
  
  def test_should_not_raise_exception_if_using_after_type
    assert_nothing_raised { StateMachine::Callback.new(:after) {} }
  end
  
  def test_should_not_raise_exception_if_using_around_type
    assert_nothing_raised { StateMachine::Callback.new(:around) {} }
  end
  
  def test_should_not_raise_exception_if_using_failure_type
    assert_nothing_raised { StateMachine::Callback.new(:failure) {} }
  end
  
  def test_should_raise_exception_if_no_methods_specified
    exception = assert_raise(ArgumentError) { StateMachine::Callback.new(:before) }
    assert_equal 'Method(s) for callback must be specified', exception.message
  end
  
  def test_should_not_raise_exception_if_method_specified_in_do_option
    assert_nothing_raised { StateMachine::Callback.new(:before, :do => :run) }
  end
  
  def test_should_not_raise_exception_if_method_specified_as_argument
    assert_nothing_raised { StateMachine::Callback.new(:before, :run) }
  end
  
  def test_should_not_raise_exception_if_method_specified_as_block
    assert_nothing_raised { StateMachine::Callback.new(:before, :run) {} }
  end
  
  def test_should_not_raise_exception_if_implicit_option_specified
    assert_nothing_raised { StateMachine::Callback.new(:before, :do => :run, :invalid => :valid) }
  end
  
  def test_should_not_bind_to_objects
    assert !StateMachine::Callback.bind_to_object
  end
  
  def test_should_not_have_a_terminator
    assert_nil StateMachine::Callback.terminator
  end
end

class CallbackByDefaultTest < Test::Unit::TestCase
  def setup
    @callback = StateMachine::Callback.new(:before) {}
  end
  
  def test_should_have_type
    assert_equal :before, @callback.type
  end
  
  def test_should_not_have_a_terminator
    assert_nil @callback.terminator
  end
  
  def test_should_have_a_branch_with_all_matcher_requirements
    assert_equal StateMachine::AllMatcher.instance, @callback.branch.event_requirement
    assert_equal StateMachine::AllMatcher.instance, @callback.branch.state_requirements.first[:from]
    assert_equal StateMachine::AllMatcher.instance, @callback.branch.state_requirements.first[:to]
  end
  
  def test_should_not_have_any_known_states
    assert_equal [], @callback.known_states
  end
end

class CallbackWithMethodArgumentTest < Test::Unit::TestCase
  def setup
    @callback = StateMachine::Callback.new(:before, lambda {|*args| @args = args})
    
    @object = Object.new
    @result = @callback.call(@object)
  end
  
  def test_should_be_successful
    assert @result
  end
  
  def test_should_call_with_empty_context
    assert_equal [@object], @args
  end
end

class CallbackWithMultipleMethodArgumentsTest < Test::Unit::TestCase
  def setup
    @callback = StateMachine::Callback.new(:before, :run_1, :run_2)
    
    class << @object = Object.new
      attr_accessor :callbacks
      
      def run_1
        (@callbacks ||= []) << :run_1
      end
      
      def run_2
        (@callbacks ||= []) << :run_2
      end
    end
    
    @result = @callback.call(@object)
  end
  
  def test_should_be_successful
    assert @result
  end
  
  def test_should_call_each_callback_in_order
    assert_equal [:run_1, :run_2], @object.callbacks
  end
end

class CallbackWithDoMethodTest < Test::Unit::TestCase
  def setup
    @callback = StateMachine::Callback.new(:before, :do => lambda {|*args| @args = args})
    
    @object = Object.new
    @result = @callback.call(@object)
  end
  
  def test_should_be_successful
    assert @result
  end
  
  def test_should_call_with_empty_context
    assert_equal [@object], @args
  end
end

class CallbackWithMultipleDoMethodsTest < Test::Unit::TestCase
  def setup
    @callback = StateMachine::Callback.new(:before, :do => [:run_1, :run_2])
    
    class << @object = Object.new
      attr_accessor :callbacks
      
      def run_1
        (@callbacks ||= []) << :run_1
      end
      
      def run_2
        (@callbacks ||= []) << :run_2
      end
    end
    
    @result = @callback.call(@object)
  end
  
  def test_should_be_successful
    assert @result
  end
  
  def test_should_call_each_callback_in_order
    assert_equal [:run_1, :run_2], @object.callbacks
  end
end

class CallbackWithBlockTest < Test::Unit::TestCase
  def setup
    @callback = StateMachine::Callback.new(:before) do |*args|
      @args = args
    end
    
    @object = Object.new
    @result = @callback.call(@object)
  end
  
  def test_should_be_successful
    assert @result
  end
  
  def test_should_call_with_empty_context
    assert_equal [@object], @args
  end
end

class CallbackWithMixedMethodsTest < Test::Unit::TestCase
  def setup
    @callback = StateMachine::Callback.new(:before, :run_argument, :do => :run_do) do |object|
      object.callbacks << :block
    end
    
    class << @object = Object.new
      attr_accessor :callbacks
      
      def run_argument
        (@callbacks ||= []) << :argument
      end
      
      def run_do
        (@callbacks ||= []) << :do
      end
    end
    
    @result = @callback.call(@object)
  end
  
  def test_should_be_successful
    assert @result
  end
  
  def test_should_call_each_callback_in_order
    assert_equal [:argument, :do, :block], @object.callbacks
  end
end

class CallbackWithExplicitRequirementsTest < Test::Unit::TestCase
  def setup
    @object = Object.new
    @callback = StateMachine::Callback.new(:before, :from => :parked, :to => :idling, :on => :ignite, :do => lambda {})
  end
  
  def test_should_call_with_empty_context
    assert @callback.call(@object, {})
  end
  
  def test_should_not_call_if_from_not_included
    assert !@callback.call(@object, :from => :idling)
  end
  
  def test_should_not_call_if_to_not_included
    assert !@callback.call(@object, :to => :parked)
  end
  
  def test_should_not_call_if_on_not_included
    assert !@callback.call(@object, :on => :park)
  end
  
  def test_should_call_if_all_requirements_met
    assert @callback.call(@object, :from => :parked, :to => :idling, :on => :ignite)
  end
  
  def test_should_include_in_known_states
    assert_equal [:parked, :idling], @callback.known_states
  end
end

class CallbackWithImplicitRequirementsTest < Test::Unit::TestCase
  def setup
    @object = Object.new
    @callback = StateMachine::Callback.new(:before, :parked => :idling, :on => :ignite, :do => lambda {})
  end
  
  def test_should_call_with_empty_context
    assert @callback.call(@object, {})
  end
  
  def test_should_not_call_if_from_not_included
    assert !@callback.call(@object, :from => :idling)
  end
  
  def test_should_not_call_if_to_not_included
    assert !@callback.call(@object, :to => :parked)
  end
  
  def test_should_not_call_if_on_not_included
    assert !@callback.call(@object, :on => :park)
  end
  
  def test_should_call_if_all_requirements_met
    assert @callback.call(@object, :from => :parked, :to => :idling, :on => :ignite)
  end
  
  def test_should_include_in_known_states
    assert_equal [:parked, :idling], @callback.known_states
  end
end

class CallbackWithIfConditionTest < Test::Unit::TestCase
  def setup
    @object = Object.new
  end
  
  def test_should_call_if_true
    callback = StateMachine::Callback.new(:before, :if => lambda {true}, :do => lambda {})
    assert callback.call(@object)
  end
  
  def test_should_not_call_if_false
    callback = StateMachine::Callback.new(:before, :if => lambda {false}, :do => lambda {})
    assert !callback.call(@object)
  end
end

class CallbackWithUnlessConditionTest < Test::Unit::TestCase
  def setup
    @object = Object.new
  end
  
  def test_should_call_if_false
    callback = StateMachine::Callback.new(:before, :unless => lambda {false}, :do => lambda {})
    assert callback.call(@object)
  end
  
  def test_should_not_call_if_true
    callback = StateMachine::Callback.new(:before, :unless => lambda {true}, :do => lambda {})
    assert !callback.call(@object)
  end
end

class CallbackWithoutTerminatorTest < Test::Unit::TestCase
  def setup
    @object = Object.new
  end
  
  def test_should_not_halt_if_result_is_false
    callback = StateMachine::Callback.new(:before, :do => lambda {false}, :terminator => nil)
    assert_nothing_thrown { callback.call(@object) }
  end
end

class CallbackWithTerminatorTest < Test::Unit::TestCase
  def setup
    @object = Object.new
  end
  
  def test_should_not_halt_if_terminator_does_not_match
    callback = StateMachine::Callback.new(:before, :do => lambda {false}, :terminator => lambda {|result| result == true})
    assert_nothing_thrown { callback.call(@object) }
  end
  
  def test_should_halt_if_terminator_matches
    callback = StateMachine::Callback.new(:before, :do => lambda {false}, :terminator => lambda {|result| result == false})
    assert_throws(:halt) { callback.call(@object) }
  end
  
  def test_should_halt_if_terminator_matches_any_method
    callback = StateMachine::Callback.new(:before, :do => [lambda {true}, lambda {false}], :terminator => lambda {|result| result == false})
    assert_throws(:halt) { callback.call(@object) }
  end
end

class CallbackWithoutArgumentsTest < Test::Unit::TestCase
  def setup
    @callback = StateMachine::Callback.new(:before, :do => lambda {|object| @arg = object})
    
    @object = Object.new
    @callback.call(@object, {}, 1, 2, 3)
  end
  
  def test_should_call_method_with_object_as_argument
    assert_equal @object, @arg
  end
end

class CallbackWithArgumentsTest < Test::Unit::TestCase
  def setup
    @callback = StateMachine::Callback.new(:before, :do => lambda {|*args| @args = args})
    
    @object = Object.new
    @callback.call(@object, {}, 1, 2, 3)
  end
  
  def test_should_call_method_with_all_arguments
    assert_equal [@object, 1, 2, 3], @args
  end
end

class CallbackWithUnboundMethodTest < Test::Unit::TestCase
  def setup
    @callback = StateMachine::Callback.new(:before, :do => lambda {|*args| @context = args.unshift(self)})
    
    @object = Object.new
    @callback.call(@object, {}, 1, 2, 3)
  end
  
  def test_should_call_method_outside_the_context_of_the_object
    assert_equal [self, @object, 1, 2, 3], @context
  end
end

class CallbackWithBoundMethodTest < Test::Unit::TestCase
  def setup
    @object = Object.new
  end
  
  def test_should_call_method_within_the_context_of_the_object_for_block_methods
    context = nil
    callback = StateMachine::Callback.new(:before, :do => lambda {|*args| context = [self] + args}, :bind_to_object => true)
    callback.call(@object, {}, 1, 2, 3)
    
    assert_equal [@object, 1, 2, 3], context
  end
  
  def test_should_ignore_option_for_symbolic_methods
    class << @object
      attr_reader :context
      
      def after_ignite(*args)
        @context = args
      end
    end
    
    callback = StateMachine::Callback.new(:before, :do => :after_ignite, :bind_to_object => true)
    callback.call(@object)
    
    assert_equal [], @object.context
  end
  
  def test_should_ignore_option_for_string_methods
    callback = StateMachine::Callback.new(:before, :do => '[1, 2, 3]', :bind_to_object => true)
    assert callback.call(@object)
  end
end

class CallbackWithMultipleBoundMethodsTest < Test::Unit::TestCase
  def setup
    @object = Object.new
    
    first_context = nil
    second_context = nil
    
    @callback = StateMachine::Callback.new(:before, :do => [lambda {first_context = self}, lambda {second_context = self}], :bind_to_object => true)
    @callback.call(@object)
    
    @first_context = first_context
    @second_context = second_context
  end
  
  def test_should_call_each_method_within_the_context_of_the_object
    assert_equal @object, @first_context
    assert_equal @object, @second_context
  end
end

class CallbackWithApplicationBoundObjectTest < Test::Unit::TestCase
  def setup
    @original_bind_to_object = StateMachine::Callback.bind_to_object
    StateMachine::Callback.bind_to_object = true
    
    context = nil
    @callback = StateMachine::Callback.new(:before, :do => lambda {|*args| context = self})
    
    @object = Object.new
    @callback.call(@object)
    @context = context
  end
  
  def test_should_call_method_within_the_context_of_the_object
    assert_equal @object, @context
  end
  
  def teardown
    StateMachine::Callback.bind_to_object = @original_bind_to_object
  end
end

class CallbackWithBoundMethodAndArgumentsTest < Test::Unit::TestCase
  def setup
    @object = Object.new
  end
  
  def test_should_include_single_argument_if_specified
    context = nil
    callback = StateMachine::Callback.new(:before, :do => lambda {|arg1| context = [arg1]}, :bind_to_object => true)
    callback.call(@object, {}, 1)
    assert_equal [1], context
  end
  
  def test_should_include_multiple_arguments_if_specified
    context = nil
    callback = StateMachine::Callback.new(:before, :do => lambda {|arg1, arg2, arg3| context = [arg1, arg2, arg3]}, :bind_to_object => true)
    callback.call(@object, {}, 1, 2, 3)
    assert_equal [1, 2, 3], context
  end
  
  def test_should_include_arguments_if_splat_used
    context = nil
    callback = StateMachine::Callback.new(:before, :do => lambda {|*args| context = args}, :bind_to_object => true)
    callback.call(@object, {}, 1, 2, 3)
    assert_equal [1, 2, 3], context
  end
end

class CallbackWithApplicationTerminatorTest < Test::Unit::TestCase
  def setup
    @original_terminator = StateMachine::Callback.bind_to_object
    StateMachine::Callback.terminator = lambda {|result| result == false}
    
    @object = Object.new
  end
  
  def test_should_not_halt_if_terminator_does_not_match
    callback = StateMachine::Callback.new(:before, :do => lambda {true})
    assert_nothing_thrown { callback.call(@object) }
  end
  
  def test_should_halt_if_terminator_matches
    callback = StateMachine::Callback.new(:before, :do => lambda {false})
    assert_throws(:halt) { callback.call(@object) }
  end
  
  def teardown
    StateMachine::Callback.bind_to_object = @original_bind_to_object
  end
end

class CallbackWithAroundTypeAndBlockTest < Test::Unit::TestCase
  def setup
    @object = Object.new
    @callbacks = []
  end
  
  def test_should_evaluate_before_without_after
    callback = StateMachine::Callback.new(:around, lambda {|*args| block = args.pop; @args = args; block.call})
    assert callback.call(@object)
    assert_equal [@object], @args
  end
  
  def test_should_evaluate_after_without_before
    callback = StateMachine::Callback.new(:around, lambda {|*args| block = args.pop; block.call; @args = args})
    assert callback.call(@object)
    assert_equal [@object], @args
  end
  
  def test_should_halt_if_not_yielded
    callback = StateMachine::Callback.new(:around, lambda {|block|})
    assert_throws(:halt) { callback.call(@object) }
  end
  
  def test_should_call_block_after_before
    callback = StateMachine::Callback.new(:around, lambda {|block| @callbacks << :before; block.call})
    assert callback.call(@object) { @callbacks << :block }
    assert_equal [:before, :block], @callbacks
  end
  
  def test_should_call_block_before_after
    @callbacks = []
    callback = StateMachine::Callback.new(:around, lambda {|block| block.call; @callbacks << :after})
    assert callback.call(@object) { @callbacks << :block }
    assert_equal [:block, :after], @callbacks
  end
  
  def test_should_halt_if_block_halts
    callback = StateMachine::Callback.new(:around, lambda {|block| block.call; @callbacks << :after})
    assert_throws(:halt) { callback.call(@object) { throw :halt }  }
    assert_equal [], @callbacks
  end
end

class CallbackWithAroundTypeAndMultipleMethodsTest < Test::Unit::TestCase
  def setup
    @callback = StateMachine::Callback.new(:around, :run_1, :run_2)
    
    class << @object = Object.new
      attr_accessor :before_callbacks
      attr_accessor :after_callbacks
      
      def run_1
        (@before_callbacks ||= []) << :run_1
        yield
        (@after_callbacks ||= []) << :run_1
      end
      
      def run_2
        (@before_callbacks ||= []) << :run_2
        yield
        (@after_callbacks ||= []) << :run_2
      end
    end
  end
  
  def test_should_succeed
    assert @callback.call(@object)
  end
  
  def test_should_evaluate_before_callbacks_in_order
    @callback.call(@object)
    assert_equal [:run_1, :run_2], @object.before_callbacks
  end
  
  def test_should_evaluate_after_callbacks_in_reverse_order
    @callback.call(@object)
    assert_equal [:run_2, :run_1], @object.after_callbacks
  end
  
  def test_should_call_block_after_before_callbacks
    @callback.call(@object) { (@object.before_callbacks ||= []) << :block }
    assert_equal [:run_1, :run_2, :block], @object.before_callbacks
  end
  
  def test_should_call_block_before_after_callbacks
    @callback.call(@object) { (@object.after_callbacks ||= []) << :block }
    assert_equal [:block, :run_2, :run_1], @object.after_callbacks
  end
  
  def test_should_halt_if_first_doesnt_yield
    class << @object
      def run_1
        (@before_callbacks ||= []) << :run_1
      end
    end
    
    catch(:halt) do
      @callback.call(@object) { (@object.before_callbacks ||= []) << :block }
    end
    
    assert_equal [:run_1], @object.before_callbacks
    assert_nil @object.after_callbacks
  end
  
  def test_should_halt_if_last_doesnt_yield
    class << @object
      def run_2
        (@before_callbacks ||= []) << :run_2
      end
    end
    
    catch(:halt) { @callback.call(@object) }
    assert_equal [:run_1, :run_2], @object.before_callbacks
    assert_nil @object.after_callbacks
  end
  
  def test_should_not_evaluate_further_methods_if_after_halts
    class << @object
      def run_2
        (@before_callbacks ||= []) << :run_2
        yield
        (@after_callbacks ||= []) << :run_2
        throw :halt
      end
    end
    
    catch(:halt) { @callback.call(@object) }
    assert_equal [:run_1, :run_2], @object.before_callbacks
    assert_equal [:run_2], @object.after_callbacks
  end
end

class CallbackWithAroundTypeAndArgumentsTest < Test::Unit::TestCase
  def setup
    @object = Object.new
  end
  
  def test_should_include_object_if_specified
    callback = StateMachine::Callback.new(:around, lambda {|object, block| @args = [object]; block.call})
    callback.call(@object)
    assert_equal [@object], @args
  end
  
  def test_should_include_arguments_if_specified
    callback = StateMachine::Callback.new(:around, lambda {|object, arg1, arg2, arg3, block| @args = [object, arg1, arg2, arg3]; block.call})
    callback.call(@object, {}, 1, 2, 3)
    assert_equal [@object, 1, 2, 3], @args
  end
  
  def test_should_include_arguments_if_splat_used
    callback = StateMachine::Callback.new(:around, lambda {|*args| block = args.pop; @args = args; block.call})
    callback.call(@object, {}, 1, 2, 3)
    assert_equal [@object, 1, 2, 3], @args
  end
end

class CallbackWithAroundTypeAndTerminatorTest < Test::Unit::TestCase
  def setup
    @object = Object.new
  end
  
  def test_should_not_halt_if_terminator_does_not_match
    callback = StateMachine::Callback.new(:around, :do => lambda {|block| block.call(false); false}, :terminator => lambda {|result| result == true})
    assert_nothing_thrown { callback.call(@object) }
  end
  
  def test_should_not_halt_if_terminator_matches
    callback = StateMachine::Callback.new(:around, :do => lambda {|block| block.call(false); false}, :terminator => lambda {|result| result == false})
    assert_nothing_thrown { callback.call(@object) }
  end
end

class CallbackWithAroundTypeAndBoundMethodTest < Test::Unit::TestCase
  def setup
    @object = Object.new
  end
  
  def test_should_call_method_within_the_context_of_the_object
    context = nil
    callback = StateMachine::Callback.new(:around, :do => lambda {|block| context = self; block.call}, :bind_to_object => true)
    callback.call(@object, {}, 1, 2, 3)
    
    assert_equal @object, context
  end
  
  def test_should_include_arguments_if_specified
    context = nil
    callback = StateMachine::Callback.new(:around, :do => lambda {|*args| block = args.pop; context = args; block.call}, :bind_to_object => true)
    callback.call(@object, {}, 1, 2, 3)
    
    assert_equal [1, 2, 3], context
  end
end
Back to Top