| 1 |
# Copyright 2017 David Malcolm <[email protected]> |
| 2 |
# Copyright 2017 Red Hat, Inc. |
| 3 |
# |
| 4 |
# This library is free software; you can redistribute it and/or |
| 5 |
# modify it under the terms of the GNU Lesser General Public |
| 6 |
# License as published by the Free Software Foundation; either |
| 7 |
# version 2.1 of the License, or (at your option) any later version. |
| 8 |
# |
| 9 |
# This library is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
# Lesser General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU Lesser General Public |
| 15 |
# License along with this library; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 17 |
# USA |
| 18 |
|
| 19 |
import os |
| 20 |
import unittest |
| 21 |
|
| 22 |
from firehose.model import Issue |
| 23 |
|
| 24 |
from firehose.parsers.splint import parse_splint_csv, parse_splint_stderr |
| 25 |
|
| 26 |
class TestParser(unittest.TestCase): |
| 27 |
def locate_filename(self, filename): |
| 28 |
return os.path.join(os.path.dirname(__file__), |
| 29 |
'example-output', |
| 30 |
'splint', |
| 31 |
filename) |
| 32 |
|
| 33 |
def parse_example_csv(self, filename): |
| 34 |
return parse_splint_csv(self.locate_filename(filename)) |
| 35 |
|
| 36 |
def test_unconditional_file_leak(self): |
| 37 |
a = self.parse_example_csv('unconditional-file-leak.csv') |
| 38 |
self.assertEqual(a.metadata.generator.name, 'splint') |
| 39 |
self.assertEqual(a.metadata.generator.version, None) # FIXME |
| 40 |
self.assertEqual(a.metadata.sut, None) |
| 41 |
self.assertEqual(a.metadata.file_, None) |
| 42 |
self.assertEqual(a.metadata.stats, None) |
| 43 |
self.assertEqual(len(a.results), 8) |
| 44 |
r0 = a.results[0] |
| 45 |
self.assertIsInstance(r0, Issue) |
| 46 |
self.assertEqual(r0.cwe, None) |
| 47 |
self.assertEqual(r0.testid, 'internalglobs') |
| 48 |
self.assertEqual(r0.message.text, |
| 49 |
'Called procedure fopen may access file system' |
| 50 |
' state, but globals list does not include globals' |
| 51 |
' fileSystem') |
| 52 |
self.assertEqual(r0.notes.text, |
| 53 |
'A called function uses internal state, but the' |
| 54 |
' globals list for the function being checked does' |
| 55 |
' not include internalState') |
| 56 |
self.assertEqual(r0.location.file.givenpath, |
| 57 |
'examples/unconditional-file-leak.c') |
| 58 |
self.assertEqual(r0.location.file.abspath, None) |
| 59 |
self.assertEqual(r0.location.function, None) |
| 60 |
self.assertEqual(r0.trace, None) |
| 61 |
self.assertEqual(r0.severity, '1') |
| 62 |
|
| 63 |
# Verify that rows with unescaped quotes are correctly worked around |
| 64 |
# In this example, this affects warnings #4 and #6 (these are 1-based, |
| 65 |
# so index 3 and 5 in the list). |
| 66 |
r3 = a.results[3] |
| 67 |
self.assertEqual(r3.location.line, 9) |
| 68 |
self.assertEqual(r3.location.column, 5) |
| 69 |
# Quote-handling is not quite perfect, we've lost the open quote of |
| 70 |
# the format string, and there an erroneous trailing quote in this |
| 71 |
# message. |
| 72 |
self.assertEqual(r3.message.text, |
| 73 |
'Undocumented modification of file system state' |
| 74 |
' possible from call to fprintf:' |
| 75 |
' fprintf(f, %i: %i", i, i * i)"') |
| 76 |
|
| 77 |
r5 = a.results[5] |
| 78 |
# Similar issues here: |
| 79 |
self.assertEqual(r5.message.text, |
| 80 |
'Body of for statement is not a block:' |
| 81 |
' fprintf(f, %i: %i", i, i * i);"') |
| 82 |
|
| 83 |
def parse_example_stderr(self, filename): |
| 84 |
with open(self.locate_filename(filename)) as f: |
| 85 |
stderr = f.read() |
| 86 |
return parse_splint_stderr(stderr) |
| 87 |
|
| 88 |
def test_parse_stderr(self): |
| 89 |
v = self.parse_example_stderr('unconditional-file-leak.stderr') |
| 90 |
self.assertEqual(v, '3.1.2') |