| 1 |
#!/usr/bin/env python |
| 2 |
# |
| 3 |
# Copyright 2017 David Carlos <[email protected]> |
| 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.parsers.flawfinder import parse_file |
| 23 |
from firehose.model import Analysis, Issue, Sut, Trace |
| 24 |
|
| 25 |
class TestParseXml(unittest.TestCase): |
| 26 |
def parse_example(self, filename): |
| 27 |
try: |
| 28 |
path = os.path.join(os.path.dirname(__file__), |
| 29 |
'example-output', |
| 30 |
'flawfinder', |
| 31 |
filename) |
| 32 |
with open(path) as infile: |
| 33 |
return parse_file(infile) |
| 34 |
except IOError: |
| 35 |
print("Example input not found") |
| 36 |
|
| 37 |
def test_flawfinder_report(self): |
| 38 |
a = self.parse_example('flawfinder-report-1') |
| 39 |
self.assertEqual(a.metadata.generator.name, 'flawfinder') |
| 40 |
self.assertEqual(a.metadata.generator.version, '1.31') |
| 41 |
self.assertEqual(a.metadata.sut, None) |
| 42 |
self.assertEqual(a.metadata.file_, None) |
| 43 |
self.assertEqual(a.metadata.stats, None) |
| 44 |
self.assertEqual(a.metadata.stats, None) |
| 45 |
|
| 46 |
self.assertEqual(len(a.results), 1804) |
| 47 |
|
| 48 |
w0 = a.results[0] |
| 49 |
self.assertEqual(w0.cwe, 78) |
| 50 |
expected_message = 'This causes a new program to execute ' \ |
| 51 |
'and is difficult to use safely (CWE-78). ' \ |
| 52 |
'try using a library call that implements ' \ |
| 53 |
'the same functionality if available.' |
| 54 |
self.assertEqual(w0.message.text, expected_message) |
| 55 |
self.assertEqual(w0.testid, 'shell') |
| 56 |
self.assertEqual(w0.location.file.givenpath , "./docs/examples/asiohiper.cpp") |
| 57 |
self.assertEqual(w0.location.point.line, 78) |
| 58 |
self.assertEqual(w0.location.point.column, 0) |
| 59 |
|
| 60 |
w3 = a.results[4] |
| 61 |
self.assertEqual(w3.location.file.givenpath , "./docs/examples/cookie_interface.c") |
| 62 |
self.assertEqual(w3.testid, 'format') |
| 63 |
some_w = a.results[1801] |
| 64 |
self.assertEqual(some_w.cwe, 126) |
| 65 |
self.assertEqual(some_w.testid, 'buffer') |
| 66 |
other_w = a.results[1802] |
| 67 |
self.assertEqual(other_w.cwe, None) |
| 68 |
self.assertEqual(other_w.location.file.givenpath, |
| 69 |
"./tests/unit/unit1604.c") |
| 70 |
self.assertEqual(other_w.location.point.line, 49) |
| 71 |
self.assertEqual(other_w.location.point.column, 0) |