Module: T::Props::GeneratedCodeValidation

Extended by:
Private::Parse
Defined in:
lib/types/props/generated_code_validation.rb

Overview

Helper to validate generated code, to mitigate security concerns around ‘class_eval`. Not called by default; the expectation is this will be used in a test iterating over all T::Props::Serializable subclasses.

We validate the exact expected structure of the generated methods as far as we can, and then where cloning produces an arbitrarily nested structure, we just validate a lack of side effects.

Defined Under Namespace

Classes: ValidationError

Class Method Summary collapse

Methods included from Private::Parse

parse, s

Class Method Details

.validate_deserialize(source) ⇒ Object



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
# File 'lib/types/props/generated_code_validation.rb', line 17

def self.validate_deserialize(source)
  parsed = parse(source)

  # def %<name>(hash)
  #   ...
  # end
  assert_equal(:def, parsed.type)
  name, args, body = parsed.children
  assert_equal(:__t_props_generated_deserialize, name)
  assert_equal(s(:args, s(:arg, :hash)), args)

  assert_equal(:begin, body.type)
  init, *prop_clauses, ret = body.children

  # found = %<prop_count>
  # ...
  # found
  assert_equal(:lvasgn, init.type)
  init_name, init_val = init.children
  assert_equal(:found, init_name)
  assert_equal(:int, init_val.type)
  assert_equal(s(:lvar, :found), ret)

  prop_clauses.each_with_index do |clause, i|
    if i.even?
      validate_deserialize_hash_read(clause)
    else
      validate_deserialize_ivar_set(clause)
    end
  end
end

.validate_serialize(source) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/types/props/generated_code_validation.rb', line 49

def self.validate_serialize(source)
  parsed = parse(source)

  # def %<name>(strict)
  # ...
  # end
  assert_equal(:def, parsed.type)
  name, args, body = parsed.children
  assert_equal(:__t_props_generated_serialize, name)
  assert_equal(s(:args, s(:arg, :strict)), args)

  assert_equal(:begin, body.type)
  init, *prop_clauses, ret = body.children

  # h = {}
  # ...
  # h
  assert_equal(s(:lvasgn, :h, s(:hash)), init)
  assert_equal(s(:lvar, :h), ret)

  prop_clauses.each do |clause|
    validate_serialize_clause(clause)
  end
end