3 # http://www.drdobbs.com/testing/unit-testing-with-python/240165163
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)
18 import fdroidserver.common
19 import fdroidserver.update
20 from fdroidserver.common import FDroidPopen
23 class UpdateTest(unittest.TestCase):
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)
31 p = FDroidPopen(['java', '-cp', os.path.join(os.path.dirname(__file__), 'getsig'),
32 'getsig', os.path.join(os.getcwd(), apkfile)])
34 for line in p.output.splitlines():
35 if line.startswith('Result:'):
36 sig = line[7:].strip()
43 def testGoodGetsig(self):
44 # config needed to use jarsigner and keytool
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!")
58 self.assertEquals(sig.decode('hex'), pysig.decode('hex'),
59 "the length of the two sigs are different!")
60 except TypeError as e:
62 self.assertTrue(False, 'TypeError!')
64 def testBadGetsig(self):
65 # config needed to use jarsigner and keytool
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))
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))
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'])
88 newSuite = unittest.TestSuite()
89 newSuite.addTest(unittest.makeSuite(UpdateTest))