Class: Pedant::CheckPluginTypeNotSpecified

Inherits:
Check
  • Object
show all
Defined in:
lib/pedant/checks/plugin_type_not_specified.rb

Constant Summary collapse

@@valid_types =
['combined', 'local', 'reputation', 'remote', 'settings', 'summary', 'thirdparty']

Instance Attribute Summary

Attributes inherited from Check

#result

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Check

all, depends, #fail, #fatal, friendly_name, inherited, #initialize, initialize!, list, #pass, ready?, #report, run_checks_in_dependency_order, #skip, #warn

Constructor Details

This class inherits a constructor from Pedant::Check

Class Method Details

.providesObject



36
37
38
# File 'lib/pedant/checks/plugin_type_not_specified.rb', line 36

def self.provides
  super + @@valid_types.map {|t| "plugin_type_#{t}".to_sym}
end

.requiresObject



32
33
34
# File 'lib/pedant/checks/plugin_type_not_specified.rb', line 32

def self.requires
  super + [:main, :trees]
end

Instance Method Details

#runObject



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
# File 'lib/pedant/checks/plugin_type_not_specified.rb', line 40

def run
  # This check only applies to plugins.
  return skip unless @kb[:main].extname == '.nasl'

  args = []

  tree = @kb[:trees][@kb[:main]]

  tree.all(:Call).each do |node|
    next unless ((node.name.ident.name == 'script_set_attribute') or (node.name.ident.name == 'xscript_set_attribute'))
    next unless node.name.indexes == []
    next unless node.arg.has_key? 'attribute'

    # Pull out the attribute argument.
    arg = node.arg['attribute']
    next if !arg.is_a? Nasl::String
    next if arg.text != 'plugin_type'

    # Pull out the value argument.
    arg = node.arg['value']
    next if !arg.is_a? Nasl::String

    # Ensure that the plugin type is valid.
    type = arg.text
    unless @@valid_types.include? type
      report(:info, "Plugin is of unknown type #{type}:\n#{arg.context(node)}")
      return fail
    end

    # Turn the plugin type into a symbol on which other checks can depend.
    @kb["plugin_type_#{type}".to_sym] = true

    args << [arg, node]
  end

  case args.length
  when 0
    report(:error, "Plugin does not specify a type.")
    fail
  when 1
    arg = args.first[0]
    call = args.first[1]
    report(:info, "Plugin is of type #{arg.text}:\n#{arg.context(call)}")
    pass
  else
    report(:error, "Plugin specifies multiple types.")
    args.each { |arg, call| report(:error, arg.context(call)) }
    fail
  end
end