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