tests/test_jsonlint.py
0e178acb
 import unittest
 import glob
 import json
 import sys
 import difflib
9a788944
 import re
0e178acb
 
 
14f972db
 def versioncompare(safe_version, find_version):
     safe_version_tup = [int(x) for x in safe_version.split(".")]
     find_version_tup = [int(x) for x in find_version.split(".")]
     return find_version_tup < safe_version_tup
 
 
0e178acb
 class TestJsonLint(unittest.TestCase):
5c7da81c
 
     @unittest.skipIf(sys.version_info < (3, 6, 0),
                      "json.dumps force-sorts on python 3.5")
0e178acb
     def test_json_lint(self):
         valid = True
         for f in glob.glob("freewvsdb/*.json"):
9a93de7f
             fp = open(f)
0e178acb
             orig = fp.read()
             fp.close()
             tmp = json.loads(orig)
             new = json.dumps(tmp, indent=2)
0aeada3c
             if orig != new:
0e178acb
                 print("json %s not valid" % f)
                 sys.stdout.writelines(difflib.unified_diff(orig, new))
                 valid = False
0aeada3c
         self.assertTrue(valid)
0e178acb
 
9a788944
     def test_json_values(self):
         jconfig = []
         for cfile in glob.glob('freewvsdb/*.json'):
             with open(cfile) as json_file:
                 jconfig += json.load(json_file)
 
9a93de7f
         mkeys = {'name', 'url', 'safe', 'vuln', 'detection'}
9a788944
         for item in jconfig:
 
             # check for all mandatory keys
             self.assertEqual(mkeys.intersection(item.keys()), mkeys,
                              msg="Missing key in %s" % item['name'])
 
             # check we have at least one detection
             self.assertTrue(len(item['detection']) >= 1,
                             msg="No detection in %s" % item['name'])
 
             # vuln needs to be CVE or HTTPS URL
             self.assertTrue(re.match("^CVE-[0-9]*-[0-9]*$", item['vuln'])
                             or item['vuln'].startswith("https://"),
                             msg="%s: Invalid vuln %s" %
                             (item['name'], item['vuln']))
14f972db
 
             # make sure old_safe is properly sorted
             if 'old_safe' in item:
                 old_safe = item['old_safe'].split(',')
                 for i in range(1, len(old_safe)):
                     self.assertTrue(versioncompare(old_safe[i - 1],
                                                    old_safe[i]),
                                     msg="%s: Invalid old_safe ordering %s" %
                                     (item['name'], item['old_safe']))
 
118b2165
             # make sure latest is not outdated
             if 'latest' in item and item['safe'] != "":
                 self.assertTrue(not versioncompare(item['safe'],
                                                    item['latest']),
                                 msg="%s: Safe version %s newer than latest %s"
                                 % (item['name'], item['safe'], item['latest']))
 
14f972db
             # subdir needs to be integer
f53ebb0b
             for det in item['detection']:
14f972db
                 self.assertTrue(isinstance(det['subdir'], int),
f53ebb0b
                                 msg="%s: subdir not int" % item['name'])
9a788944
 
0e178acb
 
 if __name__ == '__main__':
     unittest.main()