3 # http://www.drdobbs.com/testing/unit-testing-with-python/240165163
11 from binascii import unhexlify
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 # FDroidPopen needs some config to work
34 fdroidserver.common.fill_config_defaults(config)
35 fdroidserver.common.config = config
36 p = FDroidPopen(['java', '-cp', os.path.join(os.path.dirname(__file__), 'getsig'),
37 'getsig', os.path.join(os.getcwd(), apkfile)])
39 for line in p.output.splitlines():
40 if line.startswith('Result:'):
41 sig = line[7:].strip()
48 def testGoodGetsig(self):
49 # config needed to use jarsigner and keytool
51 fdroidserver.common.fill_config_defaults(config)
52 fdroidserver.update.config = config
53 apkfile = os.path.join(os.path.dirname(__file__), 'urzip.apk')
54 sig = self.javagetsig(apkfile)
55 self.assertIsNotNone(sig, "sig is None")
56 pysig = fdroidserver.update.getsig(apkfile)
57 self.assertIsNotNone(pysig, "pysig is None")
58 self.assertEqual(sig, fdroidserver.update.getsig(apkfile),
59 "python sig not equal to java sig!")
60 self.assertEqual(len(sig), len(pysig),
61 "the length of the two sigs are different!")
63 self.assertEqual(unhexlify(sig), unhexlify(pysig),
64 "the length of the two sigs are different!")
65 except TypeError as e:
67 self.assertTrue(False, 'TypeError!')
69 def testBadGetsig(self):
70 # config needed to use jarsigner and keytool
72 fdroidserver.common.fill_config_defaults(config)
73 fdroidserver.update.config = config
74 apkfile = os.path.join(os.path.dirname(__file__), 'urzip-badsig.apk')
75 sig = self.javagetsig(apkfile)
76 self.assertIsNone(sig, "sig should be None: " + str(sig))
77 pysig = fdroidserver.update.getsig(apkfile)
78 self.assertIsNone(pysig, "python sig should be None: " + str(sig))
80 apkfile = os.path.join(os.path.dirname(__file__), 'urzip-badcert.apk')
81 sig = self.javagetsig(apkfile)
82 self.assertIsNone(sig, "sig should be None: " + str(sig))
83 pysig = fdroidserver.update.getsig(apkfile)
84 self.assertIsNone(pysig, "python sig should be None: " + str(sig))
86 def testScanApks(self):
87 os.chdir(os.path.dirname(__file__))
88 if os.path.basename(os.getcwd()) != 'tests':
89 raise Exception('This test must be run in the "tests/" subdir')
92 fdroidserver.common.fill_config_defaults(config)
93 config['ndk_paths'] = dict()
94 config['accepted_formats'] = ['json', 'txt', 'xml', 'yml']
95 fdroidserver.common.config = config
96 fdroidserver.update.config = config
98 fdroidserver.update.options = type('', (), {})()
99 fdroidserver.update.options.clean = True
101 alltestapps = fdroidserver.metadata.read_metadata(xref=True)
103 apps['info.guardianproject.urzip'] = alltestapps['info.guardianproject.urzip']
104 knownapks = fdroidserver.common.KnownApks()
105 apks, cachechanged = fdroidserver.update.scan_apks(apps, {}, 'repo', knownapks, False)
106 self.assertEqual(len(apks), 1)
108 self.assertEqual(apk['minSdkVersion'], '4')
109 self.assertEqual(apk['targetSdkVersion'], '18')
110 self.assertFalse('maxSdkVersion' in apk)
113 if __name__ == "__main__":
114 parser = optparse.OptionParser()
115 parser.add_option("-v", "--verbose", action="store_true", default=False,
116 help="Spew out even more information than normal")
117 (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
119 newSuite = unittest.TestSuite()
120 newSuite.addTest(unittest.makeSuite(UpdateTest))