class Pedant::CheckPluginTypeNotSpecified

Public Class Methods

provides() click to toggle source
Calls superclass method Pedant::Check::provides
# 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
requires() click to toggle source
Calls superclass method Pedant::Check::requires
# File lib/pedant/checks/plugin_type_not_specified.rb, line 32
def self.requires
  super + [:main, :trees]
end

Public Instance Methods

run() click to toggle source
# 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