chiark / gitweb /
lint: ban all dangerous HTML tags
[fdroidserver.git] / tests / lint.TestCase
1 #!/usr/bin/env python3
2
3 # http://www.drdobbs.com/testing/unit-testing-with-python/240165163
4
5 import inspect
6 import logging
7 import optparse
8 import os
9 import shutil
10 import sys
11 import tempfile
12 import unittest
13
14 localmodule = os.path.realpath(
15     os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..'))
16 print('localmodule: ' + localmodule)
17 if localmodule not in sys.path:
18     sys.path.insert(0, localmodule)
19
20 import fdroidserver.common
21 import fdroidserver.lint
22
23
24 class LintTest(unittest.TestCase):
25     '''fdroidserver/lint.py'''
26
27     def setUp(self):
28         logging.basicConfig(level=logging.INFO)
29         self.basedir = os.path.join(localmodule, 'tests')
30         self.tmpdir = os.path.abspath(os.path.join(self.basedir, '..', '.testfiles'))
31         if not os.path.exists(self.tmpdir):
32             os.makedirs(self.tmpdir)
33         os.chdir(self.basedir)
34
35     def test_check_for_unsupported_metadata_files(self):
36         config = dict()
37         fdroidserver.common.fill_config_defaults(config)
38         config['accepted_formats'] = ('txt', 'yml')
39         fdroidserver.common.config = config
40         fdroidserver.lint.config = config
41         self.assertTrue(fdroidserver.lint.check_for_unsupported_metadata_files())
42
43         tmptestsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name,
44                                        dir=self.tmpdir)
45         self.assertFalse(fdroidserver.lint.check_for_unsupported_metadata_files(tmptestsdir + '/'))
46         shutil.copytree(os.path.join(localmodule, 'tests', 'metadata'),
47                         os.path.join(tmptestsdir, 'metadata'),
48                         ignore=shutil.ignore_patterns('apk', 'dump', '*.json'))
49         self.assertFalse(fdroidserver.lint.check_for_unsupported_metadata_files(tmptestsdir + '/'))
50         shutil.copy(os.path.join(localmodule, 'tests', 'metadata', 'org.adaway.json'),
51                     os.path.join(tmptestsdir, 'metadata'))
52         self.assertTrue(fdroidserver.lint.check_for_unsupported_metadata_files(tmptestsdir + '/'))
53
54     def test_forbidden_html_tags(self):
55         config = dict()
56         fdroidserver.common.fill_config_defaults(config)
57         fdroidserver.common.config = config
58         fdroidserver.lint.config = config
59
60         app = {
61             'Name': 'Bad App',
62             'Summary': 'We pwn you',
63             'Description': 'This way: <style><img src="</style><img src=x onerror=alert(1)//">',
64         }
65
66         anywarns = False
67         for warn in fdroidserver.lint.check_regexes(app):
68             anywarns = True
69             logging.debug(warn)
70         self.assertTrue(anywarns)
71
72
73 if __name__ == "__main__":
74     parser = optparse.OptionParser()
75     parser.add_option("-v", "--verbose", action="store_true", default=False,
76                       help="Spew out even more information than normal")
77     (fdroidserver.lint.options, args) = parser.parse_args(['--verbose'])
78     fdroidserver.common.options = fdroidserver.lint.options
79
80     newSuite = unittest.TestSuite()
81     newSuite.addTest(unittest.makeSuite(LintTest))
82     unittest.main(failfast=False)