d378a65b5e90b6448eb775d3256b643a55c83389
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"):
Hanno Böck fix new pylint warnings

Hanno Böck authored 2 years ago

22)             fp = open(f, encoding="ascii")
Hanno Böck add codingstyle and json li...

Hanno Böck authored 4 years ago

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 fix f-strings in tests

Hanno Böck authored 2 years ago

28)                 print(f"json {f} not valid")
Hanno Böck add codingstyle and json li...

Hanno Böck authored 4 years ago

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'):
Hanno Böck fix new pylint warnings

Hanno Böck authored 2 years ago

36)             with open(cfile, encoding="ascii") as json_file:
Hanno Böck add sanity checks for json...

Hanno Böck authored 4 years ago

37)                 jconfig += json.load(json_file)
38) 
Hanno Böck use curly set syntax and re...

Hanno Böck authored 3 years ago

39)         mkeys = {'name', 'url', 'safe', 'vuln', 'detection'}
Hanno Böck add sanity checks for json...

Hanno Böck authored 4 years ago

40)         for item in jconfig:
41) 
42)             # check for all mandatory keys
43)             self.assertEqual(mkeys.intersection(item.keys()), mkeys,
Hanno Böck fix f-strings in tests

Hanno Böck authored 2 years ago

44)                              msg=f"Missing key in {item['name']}")
Hanno Böck add sanity checks for json...

Hanno Böck authored 4 years ago

45) 
46)             # check we have at least one detection
47)             self.assertTrue(len(item['detection']) >= 1,
Hanno Böck fix f-strings in tests

Hanno Böck authored 2 years ago

48)                             msg=f"No detection in {item['name']}")
Hanno Böck add sanity checks for json...

Hanno Böck authored 4 years ago

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://"),
Hanno Böck fix f-strings in tests

Hanno Böck authored 2 years ago

53)                             msg=f"{item['name']}: Invalid vuln {item['vuln']}")
Hanno Böck check old_safe for consiste...

Hanno Böck authored 4 years ago

54) 
55)             # make sure old_safe is properly sorted
56)             if 'old_safe' in item:
57)                 old_safe = item['old_safe'].split(',')
58)                 for i in range(1, len(old_safe)):
59)                     self.assertTrue(versioncompare(old_safe[i - 1],
60)                                                    old_safe[i]),
Hanno Böck fix f-strings in tests

Hanno Böck authored 2 years ago

61)                                     msg=f"{item['name']}: Invalid old_safe"
62)                                         " ordering {item['old_safe']}")
Hanno Böck check old_safe for consiste...

Hanno Böck authored 4 years ago

63) 
Hanno Böck test sanity of safe vs late...

Hanno Böck authored 4 years ago

64)             # make sure latest is not outdated
65)             if 'latest' in item and item['safe'] != "":
66)                 self.assertTrue(not versioncompare(item['safe'],
67)                                                    item['latest']),
Hanno Böck fix f-strings in tests

Hanno Böck authored 2 years ago

68)                                 msg=f"{item['name']}: Safe version "
69)                                     "{item['safe']} newer than latest"
70)                                     " {item['latest']}")
Hanno Böck test sanity of safe vs late...

Hanno Böck authored 4 years ago

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 fix f-strings in tests

Hanno Böck authored 2 years ago

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

Hanno Böck authored 4 years ago

76)