Module: Ronin::Script::Testable

Includes:
UI::Output::Helpers
Included in:
Buildable, Deployable
Defined in:
lib/ronin/script/testable.rb

Overview

Adds testing methods to an Ronin::Script.

Since:

  • 1.1.0

Instance Method Summary collapse

Instance Method Details

#flunk(message) ⇒ Object (protected)

Flunks the testing process.

Parameters:

  • message (String)

    The message on why the testing failed.

Raises:

Since:

  • 1.1.0



85
86
87
# File 'lib/ronin/script/testable.rb', line 85

def flunk(message)
  raise(TestFailed,message)
end

#initialize(attributes = {}) ⇒ Object

Initializes the testable script.

Parameters:

  • attributes (Hash) (defaults to: {})

    Additional attributes for the script.

Since:

  • 1.1.0



43
44
45
46
47
# File 'lib/ronin/script/testable.rb', line 43

def initialize(attributes={})
  super(attributes)

  @test_blocks = []
end

#test { ... } ⇒ Script (protected)

Registers a given block to be called when the script is tested.

Yields:

  • [] The given block will be called when the script is being tested.

Returns:

Since:

  • 1.1.0



102
103
104
105
# File 'lib/ronin/script/testable.rb', line 102

def test(&block)
  @test_blocks << block
  return self
end

#test!true

Tests that the script is properly configured.

Returns:

  • (true)

    The exploit is built and ready for deployment.

See Also:

Since:

  • 1.1.0



61
62
63
64
65
66
67
68
# File 'lib/ronin/script/testable.rb', line 61

def test!
  print_info "Testing #{script_type} ..."

  @test_blocks.each { |block| block.call() }

  print_info "#{script_type} tested!"
  return true
end

#test?(message) { ... } ⇒ true (protected)

Tests whether an expression is true.

Parameters:

  • message (String)

    The failure message if the expression was not true.

Yields:

  • [] The given block will contain the expression to evaluate.

Returns:

  • (true)

    The expression was true.

Raises:

Since:

  • 1.1.0



126
127
128
# File 'lib/ronin/script/testable.rb', line 126

def test?(message,&block)
  test { flunk(message) unless block.call() }
end

#test_equal(name, expected_value, message = nil) ⇒ true (protected)

Tests whether a method has the expected value.

Parameters:

  • name (Symbol)

    The method to call.

  • expected_value (Object)

    The expected value.

  • message (String) (defaults to: nil)

    Optional failure message.

Returns:

  • (true)

    The method returned the expected value.

Raises:

  • (TestFailed)

    The method did not return the expected value.

Since:

  • 1.1.0



152
153
154
155
156
157
158
159
160
161
# File 'lib/ronin/script/testable.rb', line 152

def test_equal(name,expected_value,message=nil)
  name = name.to_sym

  test do
    actual_value = self.send(name)
    message ||= "#{name} (#{actual_value.inspect}) must equal #{expected_value.inspect}"

    flunk(message) unless actual_value == expected_value
  end
end

#test_in(name, expected_values, message = nil) ⇒ true (protected)

Tests a method has a value in the expected values.

Parameters:

  • name (Symbol)

    The method name.

  • expected_values (#include?)

    The expected values.

  • message (String) (defaults to: nil)

    Optional failure message.

Returns:

  • (true)

    The method returned one of the expected values.

Raises:

  • (TestFailed)

    The method did not return one of the expected values.

Since:

  • 1.1.0



320
321
322
323
324
325
326
327
328
329
# File 'lib/ronin/script/testable.rb', line 320

def test_in(name,expected_values,message=nil)
  name = name.to_sym

  test do
    actual_value = self.send(name)
    message ||= "#{name} (#{actual_value.inspect}) must be one of #{expected_values.inspect}"

    flunk(message) unless expected_values.include?(actual_value)
  end
end

#test_match(name, pattern, message = nil) ⇒ true (protected)

Tests whether a method matches the pattern.

Parameters:

  • name (Symbol)

    The method to call.

  • pattern (Regexp, String)

    The pattern to match against.

  • message (String) (defaults to: nil)

    Optional failure message.

Returns:

  • (true)

    The method matched the pattern.

Raises:

  • (TestFailed)

    The method did not match the pattern.

Since:

  • 1.1.0



254
255
256
257
258
259
260
261
262
263
# File 'lib/ronin/script/testable.rb', line 254

def test_match(name,pattern,message=nil)
  name = name.to_sym

  test do
    actual_value = self.send(name)
    message ||= "#{name} (#{actual_value.inspect}) must match #{pattern.inspect}"

    flunk(message) unless actual_value.match(pattern)
  end
end

#test_no_match(name, pattern, message = nil) ⇒ true (protected)

Tests whether a method does not matches the pattern.

Parameters:

  • name (Symbol)

    The method to call.

  • pattern (Regexp, String)

    The pattern to match against.

  • message (String) (defaults to: nil)

    Optional failure message.

Returns:

  • (true)

    The method matched the pattern.

Raises:

  • (TestFailed)

    The method did not match the pattern.

Since:

  • 1.1.0



287
288
289
290
291
292
293
294
295
296
# File 'lib/ronin/script/testable.rb', line 287

def test_no_match(name,pattern,message=nil)
  name = name.to_sym

  test do
    actual_value = self.send(name)
    message ||= "#{name} (#{actual_value.inspect}) cannot match #{pattern.inspect}"

    flunk(message) unless !actual_value.match(pattern)
  end
end

#test_not_equal(name, unexpected_value, message = nil) ⇒ true (protected)

Tests whether a method does not have the unexpected value.

Parameters:

  • name (Symbol)

    The method to call.

  • unexpected_value (Object)

    The unexpected value.

  • message (String) (defaults to: nil)

    Optional failure message.

Returns:

  • (true)

    The method did not return the unexpected value.

Raises:

  • (TestFailed)

    The method did return the unexpected value.

Since:

  • 1.1.0



185
186
187
188
189
190
191
192
193
194
# File 'lib/ronin/script/testable.rb', line 185

def test_not_equal(name,unexpected_value,message=nil)
  name = name.to_sym

  test do
    actual_value = self.send(name)
    message ||= "#{name} (#{actual_value.inspect}) cannot equal #{unexpected_value.inspect}"

    flunk(message) unless actual_value != unexpected_value
  end
end

#test_not_in(name, unexpected_values, message = nil) ⇒ true (protected)

Tests a method does not have a value in the unexpected values.

Parameters:

  • name (Symbol)

    The method name.

  • unexpected_values (#include?)

    The unexpected values.

  • message (String) (defaults to: nil)

    Optional failure message.

Returns:

  • (true)

    The method did not return one of the unexpected values.

Raises:

  • (TestFailed)

    The method did return one of the unexpected values.

Since:

  • 1.1.0



353
354
355
356
357
358
359
360
361
362
# File 'lib/ronin/script/testable.rb', line 353

def test_not_in(name,unexpected_values,message=nil)
  name = name.to_sym

  test do
    actual_value = self.send(name)
    message ||= "#{name} (#{actual_value.inspect}) cannot be one of #{unexpected_values.inspect}"

    flunk(message) unless !unexpected_values.include?(actual_value)
  end
end

#test_set(name, message = nil) ⇒ true (protected)

Tests whether a method returns a non-nil value.

Parameters:

  • name (Symbol)

    The method to call.

  • message (String) (defaults to: nil)

    Optional failure message.

Returns:

  • (true)

    The method returned a non-nil value.

Raises:

Since:

  • 1.1.0



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/ronin/script/testable.rb', line 215

def test_set(name,message=nil)
  name = name.to_sym

  test do
    actual_value = self.send(name)
    message ||= "#{name} is not set"

    blank = if actual_value.respond_to?(:empty?)
              actual_value.empty?
            else
              actual_value.nil?
            end

    flunk(message) if blank
  end
end