2 # -*- coding: utf-8 -*-
4 # http://www.drdobbs.com/testing/unit-testing-with-python/240165163
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)
19 import fdroidserver.common
20 import fdroidserver.update
21 from fdroidserver.common import FDroidPopen
24 class UpdateTest(unittest.TestCase):
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)
32 p = FDroidPopen(['java', '-cp', os.path.join(os.path.dirname(__file__), 'getsig'),
33 'getsig', os.path.join(os.getcwd(), apkfile)])
35 for line in p.output.splitlines():
36 if line.startswith('Result:'):
37 sig = line[7:].strip()
44 def testGoodGetsig(self):
45 apkfile = os.path.join(os.path.dirname(__file__), 'urzip.apk')
46 sig = self.javagetsig(apkfile)
47 self.assertIsNotNone(sig, "sig is None")
48 pysig = fdroidserver.update.getsig(apkfile)
49 self.assertIsNotNone(pysig, "pysig is None")
50 self.assertEquals(sig, fdroidserver.update.getsig(apkfile),
51 "python sig not equal to java sig!")
52 self.assertEquals(len(sig), len(pysig),
53 "the length of the two sigs are different!")
55 self.assertEquals(sig.decode('hex'), pysig.decode('hex'),
56 "the length of the two sigs are different!")
57 except TypeError as e:
59 self.assertTrue(False, 'TypeError!')
61 def testBadGetsig(self):
62 apkfile = os.path.join(os.path.dirname(__file__), 'urzip-badsig.apk')
63 sig = self.javagetsig(apkfile)
64 self.assertIsNone(sig, "sig should be None: " + str(sig))
65 pysig = fdroidserver.update.getsig(apkfile)
66 self.assertIsNone(pysig, "python sig should be None: " + str(sig))
68 apkfile = os.path.join(os.path.dirname(__file__), 'urzip-badcert.apk')
69 sig = self.javagetsig(apkfile)
70 self.assertIsNone(sig, "sig should be None: " + str(sig))
71 pysig = fdroidserver.update.getsig(apkfile)
72 self.assertIsNone(pysig, "python sig should be None: " + str(sig))
75 if __name__ == "__main__":
76 parser = optparse.OptionParser()
77 parser.add_option("-v", "--verbose", action="store_true", default=False,
78 help="Spew out even more information than normal")
79 (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
81 newSuite = unittest.TestSuite()
82 newSuite.addTest(unittest.makeSuite(UpdateTest))