a3e985fc81e20c7b7c001a379384a2336ee246d3
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 use black codingstyle

Hanno Böck authored 1 year ago

16)     @unittest.skipIf(
17)         sys.version_info < (3, 6, 0), "json.dumps force-sorts on python 3.5"
18)     )
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)
Hanno Böck add newline at end of json...

Hanno Böck authored 1 year ago

26)             new = json.dumps(tmp, indent=2) + "\n"
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 = []
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

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 black codingstyle

Hanno Böck authored 1 year 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
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

43)             self.assertEqual(
44)                 mkeys.intersection(item.keys()),
45)                 mkeys,
46)                 msg=f"Missing key in {item['name']}",
47)             )
Hanno Böck add sanity checks for json...

Hanno Böck authored 4 years ago

48) 
49)             # check we have at least one detection
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

50)             self.assertTrue(
51)                 len(item["detection"]) >= 1, msg=f"No detection in {item['name']}"
52)             )
Hanno Böck add sanity checks for json...

Hanno Böck authored 4 years ago

53) 
54)             # vuln needs to be CVE or HTTPS URL
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

55)             self.assertTrue(
56)                 re.match("^CVE-[0-9]*-[0-9]*$", item["vuln"])
57)                 or item["vuln"].startswith("https://"),
58)                 msg=f"{item['name']}: Invalid vuln {item['vuln']}",
59)             )
Hanno Böck check old_safe for consiste...

Hanno Böck authored 4 years ago

60) 
61)             # make sure old_safe is properly sorted
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

62)             if "old_safe" in item:
63)                 old_safe = item["old_safe"].split(",")
Hanno Böck check old_safe for consiste...

Hanno Böck authored 4 years ago

64)                 for i in range(1, len(old_safe)):
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

65)                     self.assertTrue(
66)                         versioncompare(old_safe[i - 1], old_safe[i]),
67)                         msg=f"{item['name']}: Invalid old_safe"
68)                         " ordering {item['old_safe']}",
69)                     )
Hanno Böck check old_safe for consiste...

Hanno Böck authored 4 years ago

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

Hanno Böck authored 4 years ago

71)             # make sure latest is not outdated
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

72)             if "latest" in item and item["safe"] != "":
73)                 self.assertTrue(
74)                     not versioncompare(item["safe"], item["latest"]),
75)                     msg=f"{item['name']}: Safe version "
76)                     "{item['safe']} newer than latest"
77)                     " {item['latest']}",
78)                 )
Hanno Böck test sanity of safe vs late...

Hanno Böck authored 4 years ago

79) 
Hanno Böck check old_safe for consiste...

Hanno Böck authored 4 years ago

80)             # subdir needs to be integer
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

81)             for det in item["detection"]:
82)                 self.assertTrue(
83)                     isinstance(det["subdir"], int),
84)                     msg=f"{item['name']}: subdir not int",
85)                 )
Hanno Böck add sanity checks for json...

Hanno Böck authored 4 years ago

86) 
Hanno Böck add codingstyle and json li...

Hanno Böck authored 4 years ago

87) 
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

88) if __name__ == "__main__":