9a78894428fabc39fbcd18a52f40890439f9d34a
Hanno Böck add codingstyle and json li...

Hanno Böck authored 4 years ago

1) import unittest
2) import glob
3) import json
4) import sys
5) import difflib
Hanno Böck add sanity checks for json...

Hanno Böck authored 4 years ago

6) import re
Hanno Böck add codingstyle and json li...

Hanno Böck authored 4 years ago

7) 
8) 
9) class TestJsonLint(unittest.TestCase):
Hanno Böck disable jsonlint test on py...

Hanno Böck authored 4 years ago

10) 
11)     @unittest.skipIf(sys.version_info < (3, 6, 0),
12)                      "json.dumps force-sorts on python 3.5")
Hanno Böck add codingstyle and json li...

Hanno Böck authored 4 years ago

13)     def test_json_lint(self):
14)         valid = True
15)         for f in glob.glob("freewvsdb/*.json"):
16)             fp = open(f, "r")
17)             orig = fp.read()
18)             fp.close()
19)             tmp = json.loads(orig)
20)             new = json.dumps(tmp, indent=2)
Hanno Böck avoid some pylint warnings

Hanno Böck authored 4 years ago

21)             if orig != new:
Hanno Böck add codingstyle and json li...

Hanno Böck authored 4 years ago

22)                 print("json %s not valid" % f)
23)                 sys.stdout.writelines(difflib.unified_diff(orig, new))
24)                 valid = False
Hanno Böck avoid some pylint warnings

Hanno Böck authored 4 years ago

25)         self.assertTrue(valid)
Hanno Böck add codingstyle and json li...

Hanno Böck authored 4 years ago

26) 
Hanno Böck add sanity checks for json...

Hanno Böck authored 4 years ago

27)     def test_json_values(self):
28)         jconfig = []
29)         for cfile in glob.glob('freewvsdb/*.json'):
30)             with open(cfile) as json_file:
31)                 jconfig += json.load(json_file)
32) 
33)         mkeys = set(['name', 'url', 'safe', 'vuln', 'detection'])
34)         for item in jconfig:
35) 
36)             # check for all mandatory keys
37)             self.assertEqual(mkeys.intersection(item.keys()), mkeys,
38)                              msg="Missing key in %s" % item['name'])
39) 
40)             # check we have at least one detection
41)             self.assertTrue(len(item['detection']) >= 1,
42)                             msg="No detection in %s" % item['name'])
43) 
44)             # vuln needs to be CVE or HTTPS URL
45)             self.assertTrue(re.match("^CVE-[0-9]*-[0-9]*$", item['vuln'])
46)                             or item['vuln'].startswith("https://"),
47)                             msg="%s: Invalid vuln %s" %
48)                             (item['name'], item['vuln']))
49)