Class: Pedant::CheckScriptSummary

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

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, provides, ready?, #report, #skip, #warn

Constructor Details

This class inherits a constructor from Pedant::Check

Class Method Details

.requiresObject



29
30
31
# File 'lib/pedant/checks/script_summary.rb', line 29

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

Instance Method Details

#runObject



33
34
35
36
37
38
39
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/pedant/checks/script_summary.rb', line 33

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

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

  tree.all(:Call).each do |node|
    next unless node.name.ident.name == 'script_summary'
    next unless node.name.indexes == []
    ss_nodes << node
  end

  if ss_nodes.length == 0
    report(:error, "Plugin does not call script_summary() in the description.")
    return fail
  elsif ss_nodes.length > 1
    report(:error, "Plugin calls script_summary() multiple times:")
    ss_nodes.each { |call| report(:error, call.context()) }
    return fail
  end

  ss_node = ss_nodes.first

  if ss_node.args.empty?
    report(:error, "script_summary() was called with no arguments:\n#{ss_node.context()}")
    return fail
  end

  if ss_node.args.length > 1
    report(:error, "script_summary() was called with too many arguments:\n#{ss_node.context()}")
    return fail
  end
    
  # Pull out argument
  type = ss_node.args.first.type
  param = ss_node.args.first.name
  arg = ss_node.args.first.expr

  if type == :anonymous
    report(
      :error,
      "script_summary() was called using a positional parameter.\n" +
      "It requires using an argument to the 'english' named parameter.\n" +
      arg.context(ss_node)
    )
    return fail
  end

  if param.name != "english"
    report(
      :error,
      "script_summary() was called using an invalid named parameter.\n" +
      "The 'english' named parameter must be used.\n" +
      param.context(ss_node)
    )
    return fail
  end
   
  unless arg.is_a? Nasl::String
    report(
      :error,
      "script_summary() was called with the wrong type of argument. A string is required.\n" +
      arg.context(ss_node)
    )
    return fail
  end

  if arg.text.length == 0
    report(
      :error,
      "script_summary() was called with an empty string.\n" +
      arg.context(ss_node)
    )
    return fail
  end

  if arg.text.slice(0) == " " && arg.text.slice(-1) == " "
    ws_error = "Script summary has leading and trailing whitespace:\n"
  elsif arg.text.slice(0) == " "
    ws_error = "Script summary has leading whitespace:\n"
  elsif arg.text.slice(-1) == " "
    ws_error = "Script summary has trailing whitespace:\n"
  else
    ws_error = nil
  end
 
  unless ws_error.nil?
    report(
      :error,
      ws_error +
      arg.context(ss_node)
    )
    return fail
  end

  if arg.text.include? "\n"
    report(
      :error,
      "Script summary includes newline characters:\n" +
      arg.context(ss_node)
    )
    return fail
  end

  report(:info, "Plugin has a script summary of '#{arg.text}':\n#{arg.context(ss_node)}")
  pass
end