118b216551dbef54067fd8a6e0a88925b97fe88d
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) 
Hanno Böck check old_safe for consiste...

Hanno Böck authored 4 years ago

9) def versioncompare(safe_version, find_version):
10)     safe_version_tup = [int(x) for x in safe_version.split(".")]
11)     find_version_tup = [int(x) for x in find_version.split(".")]
12)     return find_version_tup < safe_version_tup
13) 
14) 
Hanno Böck add codingstyle and json li...

Hanno Böck authored 4 years ago

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

Hanno Böck authored 4 years ago

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

Hanno Böck authored 4 years ago

19)     def test_json_lint(self):
20)         valid = True
21)         for f in glob.glob("freewvsdb/*.json"):
22)             fp = open(f, "r")
23)             orig = fp.read()
24)             fp.close()
25)             tmp = json.loads(orig)
26)             new = json.dumps(tmp, indent=2)
Hanno Böck avoid some pylint warnings

Hanno Böck authored 4 years ago

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

Hanno Böck authored 4 years ago

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

Hanno Böck authored 4 years ago

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

Hanno Böck authored 4 years ago

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

Hanno Böck authored 4 years ago

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

Hanno Böck authored 4 years ago

55) 
56)             # make sure old_safe is properly sorted
57)             if 'old_safe' in item:
58)                 old_safe = item['old_safe'].split(',')
59)                 for i in range(1, len(old_safe)):
60)                     self.assertTrue(versioncompare(old_safe[i - 1],
61)                                                    old_safe[i]),
62)                                     msg="%s: Invalid old_safe ordering %s" %
63)                                     (item['name'], item['old_safe']))
64) 
Hanno Böck test sanity of safe vs late...

Hanno Böck authored 4 years ago

65)             # make sure latest is not outdated
66)             if 'latest' in item and item['safe'] != "":
67)                 self.assertTrue(not versioncompare(item['safe'],
68)                                                    item['latest']),
69)                                 msg="%s: Safe version %s newer than latest %s"
70)                                 % (item['name'], item['safe'], item['latest']))
71) 
Hanno Böck check old_safe for consiste...

Hanno Böck authored 4 years ago

72)             # subdir needs to be integer
Hanno Böck check subdir is int

Hanno Böck authored 4 years ago

73)             for det in item['detection']:
Hanno Böck check old_safe for consiste...

Hanno Böck authored 4 years ago

74)                 self.assertTrue(isinstance(det['subdir'], int),
Hanno Böck check subdir is int

Hanno Böck authored 4 years ago

75)                                 msg="%s: subdir not int" % item['name'])
Hanno Böck add sanity checks for json...

Hanno Böck authored 4 years ago

76)