Class: Reflection

Inherits:
Object
  • Object
show all
Defined in:
lib/Reflection.rb

Direct Known Subclasses

Control

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(execution, number, aggregator) ⇒ Reflection

Create a Reflection.

Parameters:

  • execution (Execution)

    The Execution that created this Reflection.

  • number (Integer)

    Multiple Reflections can be created per Execution.

  • aggregator (Aggregator)

    The aggregated RuleSet for this class/method.



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
# File 'lib/Reflection.rb', line 26

def initialize(execution, number, aggregator)

  @execution = execution
  @unique_id = execution.unique_id + number
  @number = number

  # Dependency.
  @aggregator = aggregator

  # Caller.
  @klass = execution.klass
  @method = execution.method

  # Metadata.
  @inputs = []
  @output = nil

  # Clone the execution's calling object.
  @clone = execution.caller_object.clone
  @clone_id = nil

  # Result.
  @status = :pass
  @time = Time.now.to_i

end

Instance Attribute Details

#cloneObject

Returns the value of attribute clone.



17
18
19
# File 'lib/Reflection.rb', line 17

def clone
  @clone
end

Instance Method Details

#randomize(args) ⇒ Dynamic

Create random values for each argument.

Parameters:

  • args (Dynamic)

    The arguments to create random values for.

Returns:

  • (Dynamic)

    Random arguments.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/Reflection.rb', line 106

def randomize(args)

  random_args = []

  args.each do |arg|
    case arg
    when Integer
      random_args << rand(999)
    else
      random_args << arg
    end
  end

  return random_args

end

#reflect(*args) ⇒ Object

Reflect on a method.

Creates a shadow execution stack.

Parameters:

  • *args (Dynamic)

    The method’s arguments.



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
# File 'lib/Reflection.rb', line 59

def reflect(*args)

  # Get aggregated RuleSets.
  agg_input_rule_sets = @aggregator.get_input_rule_sets(@klass, @method)
  agg_output_rule_set = @aggregator.get_output_rule_set(@klass, @method)

  # Create random arguments.
  new_args = randomize(args)

  # Create metadata for each argument.
  @inputs = MetaBuilder.create_many(new_args)

  # Action method with new arguments.
  begin

    # Validate input with aggregated control RuleSets.
    unless agg_input_rule_sets.nil?
      unless @aggregator.validate_inputs(new_args, agg_input_rule_sets)
        @status = :fail
      end
    end

    # Run reflection.
    output = @clone.send(@method, *new_args)
    @output = MetaBuilder.create(output)

    # Validate output with aggregated control RuleSets.
    unless agg_output_rule_set.nil?
      unless @aggregator.validate_output(output, agg_output_rule_set)
        @status = :fail
      end
    end

  # When fail.
  rescue StandardError => message
    @status = :fail
    @message = message
  end

end

#resultHash

Get the results of the reflection.

Returns:

  • (Hash)

    Reflection metadata.



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
# File 'lib/Reflection.rb', line 128

def result()

  # The ID of the first execution in the ShadowStack.
  base_id = nil
  unless @execution.base == nil
    base_id = @execution.base.unique_id
  end

  # Build reflection.
  reflection = {
    :base_id => base_id,
    :exe_id => @execution.unique_id,
    :ref_id => @unique_id,
    :ref_num => @number,
    :time => @time,
    :class => @klass,
    :method => @method,
    :status => @status,
    :message => @message,
    :inputs => [],
    :output => @output,
  }
  @inputs.each do |meta|
    reflection[:inputs] << meta.result()
  end

  return reflection

end