chiark / gitweb /
tests: switch to python3
[fdroidserver.git] / tests / update.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 sys
10 import unittest
11
12 localmodule = os.path.realpath(
13     os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..'))
14 print('localmodule: ' + localmodule)
15 if localmodule not in sys.path:
16     sys.path.insert(0, localmodule)
17
18 import fdroidserver.common
19 import fdroidserver.update
20 from fdroidserver.common import FDroidPopen
21
22
23 class UpdateTest(unittest.TestCase):
24     '''fdroid update'''
25
26     def javagetsig(self, apkfile):
27         getsig_dir = os.path.join(os.path.dirname(__file__), 'getsig')
28         if not os.path.exists(getsig_dir + "/getsig.class"):
29             logging.critical("getsig.class not found. To fix: cd '%s' && ./make.sh" % getsig_dir)
30             sys.exit(1)
31         p = FDroidPopen(['java', '-cp', os.path.join(os.path.dirname(__file__), 'getsig'),
32                          'getsig', os.path.join(os.getcwd(), apkfile)])
33         sig = None
34         for line in p.output.splitlines():
35             if line.startswith('Result:'):
36                 sig = line[7:].strip()
37                 break
38         if p.returncode == 0:
39             return sig
40         else:
41             return None
42
43     def testGoodGetsig(self):
44         # config needed to use jarsigner and keytool
45         config = dict()
46         fdroidserver.common.fill_config_defaults(config)
47         fdroidserver.update.config = config
48         apkfile = os.path.join(os.path.dirname(__file__), 'urzip.apk')
49         sig = self.javagetsig(apkfile)
50         self.assertIsNotNone(sig, "sig is None")
51         pysig = fdroidserver.update.getsig(apkfile)
52         self.assertIsNotNone(pysig, "pysig is None")
53         self.assertEquals(sig, fdroidserver.update.getsig(apkfile),
54                           "python sig not equal to java sig!")
55         self.assertEquals(len(sig), len(pysig),
56                           "the length of the two sigs are different!")
57         try:
58             self.assertEquals(sig.decode('hex'), pysig.decode('hex'),
59                               "the length of the two sigs are different!")
60         except TypeError as e:
61             print(e)
62             self.assertTrue(False, 'TypeError!')
63
64     def testBadGetsig(self):
65         # config needed to use jarsigner and keytool
66         config = dict()
67         fdroidserver.common.fill_config_defaults(config)
68         fdroidserver.update.config = config
69         apkfile = os.path.join(os.path.dirname(__file__), 'urzip-badsig.apk')
70         sig = self.javagetsig(apkfile)
71         self.assertIsNone(sig, "sig should be None: " + str(sig))
72         pysig = fdroidserver.update.getsig(apkfile)
73         self.assertIsNone(pysig, "python sig should be None: " + str(sig))
74
75         apkfile = os.path.join(os.path.dirname(__file__), 'urzip-badcert.apk')
76         sig = self.javagetsig(apkfile)
77         self.assertIsNone(sig, "sig should be None: " + str(sig))
78         pysig = fdroidserver.update.getsig(apkfile)
79         self.assertIsNone(pysig, "python sig should be None: " + str(sig))
80
81
82 if __name__ == "__main__":
83     parser = optparse.OptionParser()
84     parser.add_option("-v", "--verbose", action="store_true", default=False,
85                       help="Spew out even more information than normal")
86     (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
87
88     newSuite = unittest.TestSuite()
89     newSuite.addTest(unittest.makeSuite(UpdateTest))
90     unittest.main()