chiark / gitweb /
Merge branch 'changelog' of https://gitlab.com/krt/fdroidserver
[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 optparse
8 import os
9 import sys
10 import unittest
11
12 localmodule = os.path.realpath(os.path.join(
13         os.path.dirname(inspect.getfile(inspect.currentframe())),
14         '..'))
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 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         apkfile = os.path.join(os.path.dirname(__file__), 'urzip.apk')
45         sig = self.javagetsig(apkfile)
46         self.assertIsNotNone(sig, "sig is None")
47         pysig = fdroidserver.update.getsig(apkfile)
48         self.assertIsNotNone(pysig, "pysig is None")        
49         self.assertEquals(sig, fdroidserver.update.getsig(apkfile),
50                           "python sig not equal to java sig!")
51         self.assertEquals(len(sig), len(pysig),
52                           "the length of the two sigs are different!")
53         try:
54             self.assertEquals(sig.decode('hex'), pysig.decode('hex'),
55                               "the length of the two sigs are different!")
56         except TypeError as e:
57             print e
58             self.assertTrue(False, 'TypeError!')
59
60     def testBadGetsig(self):
61         apkfile = os.path.join(os.path.dirname(__file__), 'urzip-badsig.apk')
62         sig = self.javagetsig(apkfile)
63         self.assertIsNone(sig, "sig should be None: " + str(sig))
64         pysig = fdroidserver.update.getsig(apkfile)
65         self.assertIsNone(pysig, "python sig should be None: " + str(sig))
66
67         apkfile = os.path.join(os.path.dirname(__file__), 'urzip-badcert.apk')
68         sig = self.javagetsig(apkfile)
69         self.assertIsNone(sig, "sig should be None: " + str(sig))
70         pysig = fdroidserver.update.getsig(apkfile)
71         self.assertIsNone(pysig, "python sig should be None: " + str(sig))
72
73
74 if __name__ == "__main__":
75     parser = optparse.OptionParser()
76     parser.add_option("-v", "--verbose", action="store_true", default=False,
77                       help="Spew out even more information than normal")
78     (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
79
80     newSuite = unittest.TestSuite()
81     newSuite.addTest(unittest.makeSuite(UpdateTest))
82     unittest.main()