Debian Codegraph @

tests/parsers/test_clanganalyzer_parser.py

raw
1 # Copyright 2013, 2017 Red Hat, Inc.
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
16 # USA
17
18 import os
19 import unittest
20
21 from firehose.parsers.clanganalyzer import parse_plist
22 from firehose.model import Analysis, Issue, Sut, Trace
23
24 class TestParsePlist(unittest.TestCase):
25 def parse_example(self, filename):
26 a = parse_plist(os.path.join(os.path.dirname(__file__),
27 'example-output/clanganalyzer',
28 filename),
29 file_=None,
30 stats=None)
31 return a
32
33 def test_example_001(self):
34 a = self.parse_example('report-001.plist')
35 self.assertEqual(a.metadata.generator.name, 'clang-analyzer')
36 self.assertEqual(a.metadata.generator.version, None)
37
38 self.assertEqual(len(a.results), 2)
39
40 w0 = a.results[0]
41 self.assertEqual(w0.cwe, None)
42 self.assertEqual(w0.testid, 'Dead assignment')
43 self.assertEqual(w0.message.text,
44 "Value stored to 'ret' is never read")
45 self.assertEqual(w0.notes, None)
46 self.assertEqual(w0.location.file.givenpath,
47 'python-ethtool/ethtool.c')
48 self.assertEqual(w0.location.file.abspath, None)
49 self.assertEqual(w0.location.function, None)
50 self.assertEqual(w0.location.line, 130)
51 self.assertEqual(w0.location.column, 2)
52 self.assertNotEqual(w0.trace, None)
53 self.assertEqual(len(w0.trace.states), 1)
54 s0 = w0.trace.states[0]
55 self.assertEqual(s0.location.file.givenpath,
56 'python-ethtool/ethtool.c')
57 self.assertEqual(s0.location.file.abspath, None)
58 self.assertEqual(s0.location.function.name, '')
59 self.assertEqual(s0.location.line, 130)
60 self.assertEqual(s0.location.column, 2)
61 self.assertEqual(s0.notes.text,
62 "Value stored to 'ret' is never read")
63
64 def test_example_002(self):
65 a = self.parse_example('report-002.plist')
66 self.assertEqual(a.metadata.generator.name, 'clang-analyzer')
67 self.assertEqual(a.metadata.generator.version, None)
68
69 self.assertEqual(len(a.results), 4)
70
71 w0 = a.results[0]
72 self.assertEqual(w0.testid, 'Dead assignment')
73 self.assertEqual(w0.message.text,
74 "Value stored to 'error' is never read")
75 self.assertEqual(w0.location.file.givenpath, 'search.c')
76 self.assertEqual(w0.location.line, 454)
77 self.assertEqual(w0.location.column, 3)
78
79 w1 = a.results[1]
80 self.assertEqual(w1.testid, 'Dead increment')
81 self.assertEqual(w1.message.text,
82 "Value stored to 'pol_opt' is never read")
83 self.assertEqual(w1.location.file.givenpath, 'search.c')
84 self.assertEqual(w1.location.line, 824)
85 self.assertEqual(w1.location.column, 2)
86
87 w2 = a.results[2]
88 self.assertEqual(w2.testid, 'Dereference of null pointer')
89 self.assertEqual(w2.message.text,
90 "Access to field 'ob_refcnt' results in a dereference of a null pointer (loaded from variable 'dict')")
91 self.assertEqual(w2.location.file.givenpath, 'search.c')
92 self.assertEqual(w2.location.line, 215)
93 self.assertEqual(w2.location.column, 2)
94 trace2 = w2.trace
95 self.assertIsInstance(trace2, Trace)
96
97 self.assertEqual(len(trace2.states), 13)
98
99 # s0 and s1 come from the first control edge in the input file:
100 # 'path': [{'edges': [{'end': [{'col': 9,
101 # 'file': 0,
102 # 'line': 161},
103 # {'col': 9,
104 # 'file': 0,
105 # 'line': 161}],
106 # 'start': [{'col': 2,
107 # 'file': 0,
108 # 'line': 161},
109 # {'col': 2,
110 # 'file': 0,
111 # 'line': 161}]}],
112 # 'kind': 'control'},
113
114 s0 = trace2.states[0]
115 self.assertEqual(s0.location.point.line, 161)
116 self.assertEqual(s0.location.point.column, 2)
117 self.assertEqual(s0.location.range_, None)
118
119
120 s1 = trace2.states[1]
121 self.assertEqual(s1.location.point.line, 161)
122 self.assertEqual(s1.location.point.column, 9)
123 self.assertEqual(s1.location.range_, None)
124
125 # s2 comes from the endpoint of the second control edge in the
126 # input file (the startpoint == s1).
127 # {'edges': [{'end': [{'col': 18,
128 # 'file': 0,
129 # 'line': 165},
130 # {'col': 21,
131 # 'file': 0,
132 # 'line': 165}],
133 # 'start': [{'col': 9,
134 # 'file': 0,
135 # 'line': 161},
136 # {'col': 9,
137 # 'file': 0,
138 # 'line': 161}]}],
139 # 'kind': 'control'},
140 #
141 # It is a range rather than a point:
142 s2 = trace2.states[2]
143 self.assertEqual(s2.location.point, None)
144 self.assertEqual(s2.location.range_.start.line, 165)
145 self.assertEqual(s2.location.range_.start.column, 18)
146 self.assertEqual(s2.location.range_.end.line, 165)
147 self.assertEqual(s2.location.range_.end.column, 21)
148
149 # s3 comes from the next entry in the input file, which is the first
150 # "event" in the trace:
151 # {'extended_message': "Variable 'dict' initialized to a null pointer value",
152 # 'kind': 'event',
153 # 'location': {'col': 18,
154 # 'file': 0,
155 # 'line': 165},
156 # 'message': "Variable 'dict' initialized to a null pointer value",
157 # 'ranges': [[{'col': 18,
158 # 'file': 0,
159 # 'line': 165},
160 # {'col': 21,
161 # 'file': 0,
162 # 'line': 165}]]},
163 s3 = trace2.states[3]
164 # The importer uses the 'location' point for the event, and hence
165 # is treated as a different location. However, as an event, it is
166 # always given its own state in the imported data:
167 self.assertEqual(s3.location.point.line, 165)
168 self.assertEqual(s3.location.point.column, 18)
169 self.assertEqual(s3.notes.text,
170 "Variable 'dict' initialized to a null pointer value")
171 self.assertEqual(s3.location.range_, None)
172
173 # The next entry in the input file is another kind == 'control':
174 # {'edges': [{'end': [{'col': 2,
175 # 'file': 0,
176 # 'line': 171},
177 # {'col': 2,
178 # 'file': 0,
179 # 'line': 171}],
180 # 'start': [{'col': 18,
181 # 'file': 0,
182 # 'line': 165},
183 # {'col': 21,
184 # 'file': 0,
185 # 'line': 165}]}],
186 # 'kind': 'control'},
187 # The "start" is range-based, whereas the previous event was
188 # handled as a point, so the importer will treat it as two
189 # different locations (s3 and s4):
190 s4 = trace2.states[4]
191 self.assertEqual(s4.location.point, None)
192 self.assertEqual(s4.location.range_.start.line, 165)
193 self.assertEqual(s4.location.range_.start.column, 18)
194 self.assertEqual(s4.location.range_.end.line, 165)
195 self.assertEqual(s4.location.range_.end.column, 21)
196
197 s5 = trace2.states[5]
198 self.assertEqual(s5.location.point.line, 171)
199 self.assertEqual(s5.location.point.column, 2)
200 self.assertEqual(s5.location.range_, None)
201
202 # etc
203
204 def test_example_003(self):
205 a = self.parse_example('report-003.plist')
206 self.assertEqual(a.metadata.generator.name, 'clang-analyzer')
207
208 # This example has a version
209 self.assertEqual(a.metadata.generator.version,
210 'clang version 3.4.2 (tags/RELEASE_34/dot2-final)')
211
212 self.assertEqual(len(a.results), 1)
213
214 w0 = a.results[0]
215 self.assertEqual(w0.testid, 'Garbage return value')
216 self.assertEqual(w0.message.text,
217 "Undefined or garbage value returned to caller")
218 self.assertEqual(w0.location.file.givenpath,
219 '../../src/test-sources/out-of-bounds.c')
220 self.assertEqual(w0.location.line, 5)
221 self.assertEqual(w0.location.column, 3)
222
223 # Verify that we capture various clang-specific per-issue metadata
224 self.assertEqual(w0.customfields['category'], 'Logic error')
225 self.assertEqual(w0.customfields['issue_context'], 'out_of_bounds')
226 self.assertEqual(w0.customfields['issue_context_kind'], 'function')
227
228 if __name__ == '__main__':
229 unittest.main()