Browse code

add sanity checks for json data

Hanno Böck authored on16/12/2019 20:45:03
Showing1 changed files
... ...
@@ -3,6 +3,7 @@ import glob
3 3
 import json
4 4
 import sys
5 5
 import difflib
6
+import re
6 7
 
7 8
 
8 9
 class TestJsonLint(unittest.TestCase):
... ...
@@ -23,6 +24,29 @@ class TestJsonLint(unittest.TestCase):
23 24
                 valid = False
24 25
         self.assertTrue(valid)
25 26
 
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
+
26 50
 
27 51
 if __name__ == '__main__':
28 52
     unittest.main()