Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1 | import sys |
| 2 | import os |
| 3 | import marshal |
| 4 | import imp |
| 5 | import struct |
| 6 | import time |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame^] | 7 | import unittest |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 8 | |
| 9 | import zlib # implied prerequisite |
| 10 | from zipfile import ZipFile, ZipInfo, ZIP_STORED, ZIP_DEFLATED |
| 11 | from test import test_support |
| 12 | from test.test_importhooks import ImportHooksBaseTestCase, test_src, test_co |
| 13 | |
| 14 | import zipimport |
| 15 | |
| 16 | |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame^] | 17 | # so we only run testAFakeZlib once if this test is run repeatedly |
| 18 | # which happens when we look for ref leaks |
| 19 | test_imported = False |
| 20 | |
| 21 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 22 | def make_pyc(co, mtime): |
| 23 | data = marshal.dumps(co) |
Jack Jansen | 472e7db | 2003-01-08 16:37:03 +0000 | [diff] [blame] | 24 | if type(mtime) is type(0.0): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 25 | # Mac mtimes need a bit of special casing |
| 26 | if mtime < 0x7fffffff: |
| 27 | mtime = int(mtime) |
| 28 | else: |
| 29 | mtime = int(-0x100000000L + long(mtime)) |
Jack Jansen | 472e7db | 2003-01-08 16:37:03 +0000 | [diff] [blame] | 30 | pyc = imp.get_magic() + struct.pack("<i", int(mtime)) + data |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 31 | return pyc |
| 32 | |
| 33 | NOW = time.time() |
| 34 | test_pyc = make_pyc(test_co, NOW) |
| 35 | |
| 36 | |
| 37 | if __debug__: |
| 38 | pyc_ext = ".pyc" |
| 39 | else: |
| 40 | pyc_ext = ".pyo" |
| 41 | |
| 42 | |
| 43 | TESTMOD = "ziptestmodule" |
| 44 | TESTPACK = "ziptestpackage" |
Just van Rossum | d35c6db | 2003-01-02 12:55:48 +0000 | [diff] [blame] | 45 | TESTPACK2 = "ziptestpackage2" |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 46 | TEMP_ZIP = os.path.abspath("junk95142" + os.extsep + "zip") |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 47 | |
| 48 | class UncompressedZipImportTestCase(ImportHooksBaseTestCase): |
| 49 | |
| 50 | compression = ZIP_STORED |
| 51 | |
| 52 | def setUp(self): |
| 53 | # We're reusing the zip archive path, so we must clear the |
| 54 | # cached directory info. |
| 55 | zipimport._zip_directory_cache.clear() |
| 56 | ImportHooksBaseTestCase.setUp(self) |
| 57 | |
Thomas Heller | 354e3d9 | 2003-07-22 18:10:15 +0000 | [diff] [blame] | 58 | def doTest(self, expected_ext, files, *modules, **kw): |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 59 | z = ZipFile(TEMP_ZIP, "w") |
| 60 | try: |
| 61 | for name, (mtime, data) in files.items(): |
| 62 | zinfo = ZipInfo(name, time.localtime(mtime)) |
| 63 | zinfo.compress_type = self.compression |
| 64 | z.writestr(zinfo, data) |
| 65 | z.close() |
Thomas Heller | 354e3d9 | 2003-07-22 18:10:15 +0000 | [diff] [blame] | 66 | |
| 67 | stuff = kw.get("stuff", None) |
| 68 | if stuff is not None: |
| 69 | # Prepend 'stuff' to the start of the zipfile |
| 70 | f = open(TEMP_ZIP, "rb") |
| 71 | data = f.read() |
| 72 | f.close() |
| 73 | |
| 74 | f = open(TEMP_ZIP, "wb") |
| 75 | f.write(stuff) |
| 76 | f.write(data) |
| 77 | f.close() |
| 78 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 79 | sys.path.insert(0, TEMP_ZIP) |
| 80 | |
| 81 | mod = __import__(".".join(modules), globals(), locals(), |
| 82 | ["__dummy__"]) |
Just van Rossum | 9a3129c | 2003-01-03 11:18:56 +0000 | [diff] [blame] | 83 | if expected_ext: |
| 84 | file = mod.get_file() |
| 85 | self.assertEquals(file, os.path.join(TEMP_ZIP, |
Just van Rossum | 6706c4d | 2003-01-09 22:27:10 +0000 | [diff] [blame] | 86 | *modules) + expected_ext) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 87 | finally: |
| 88 | z.close() |
| 89 | os.remove(TEMP_ZIP) |
| 90 | |
| 91 | def testAFakeZlib(self): |
| 92 | # |
| 93 | # This could cause a stack overflow before: importing zlib.py |
| 94 | # from a compressed archive would cause zlib to be imported |
| 95 | # which would find zlib.py in the archive, which would... etc. |
| 96 | # |
| 97 | # This test *must* be executed first: it must be the first one |
| 98 | # to trigger zipimport to import zlib (zipimport caches the |
| 99 | # zlib.decompress function object, after which the problem being |
| 100 | # tested here wouldn't be a problem anymore... |
| 101 | # (Hence the 'A' in the test method name: to make it the first |
| 102 | # item in a list sorted by name, like unittest.makeSuite() does.) |
| 103 | # |
Just van Rossum | 5949854 | 2003-11-18 23:00:55 +0000 | [diff] [blame] | 104 | # This test fails on platforms on which the zlib module is |
| 105 | # statically linked, but the problem it tests for can't |
| 106 | # occur in that case (builtin modules are always found first), |
| 107 | # so we'll simply skip it then. Bug #765456. |
| 108 | # |
| 109 | if "zlib" in sys.builtin_module_names: |
| 110 | return |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 111 | if "zlib" in sys.modules: |
| 112 | del sys.modules["zlib"] |
| 113 | files = {"zlib.py": (NOW, test_src)} |
| 114 | try: |
| 115 | self.doTest(".py", files, "zlib") |
| 116 | except ImportError: |
| 117 | if self.compression != ZIP_DEFLATED: |
| 118 | self.fail("expected test to not raise ImportError") |
| 119 | else: |
| 120 | if self.compression != ZIP_STORED: |
| 121 | self.fail("expected test to raise ImportError") |
| 122 | |
| 123 | def testPy(self): |
| 124 | files = {TESTMOD + ".py": (NOW, test_src)} |
| 125 | self.doTest(".py", files, TESTMOD) |
| 126 | |
| 127 | def testPyc(self): |
| 128 | files = {TESTMOD + pyc_ext: (NOW, test_pyc)} |
| 129 | self.doTest(pyc_ext, files, TESTMOD) |
| 130 | |
| 131 | def testBoth(self): |
| 132 | files = {TESTMOD + ".py": (NOW, test_src), |
| 133 | TESTMOD + pyc_ext: (NOW, test_pyc)} |
| 134 | self.doTest(pyc_ext, files, TESTMOD) |
| 135 | |
Just van Rossum | 9a3129c | 2003-01-03 11:18:56 +0000 | [diff] [blame] | 136 | def testEmptyPy(self): |
| 137 | files = {TESTMOD + ".py": (NOW, "")} |
| 138 | self.doTest(None, files, TESTMOD) |
| 139 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 140 | def testBadMagic(self): |
| 141 | # make pyc magic word invalid, forcing loading from .py |
| 142 | m0 = ord(test_pyc[0]) |
| 143 | m0 ^= 0x04 # flip an arbitrary bit |
| 144 | badmagic_pyc = chr(m0) + test_pyc[1:] |
| 145 | files = {TESTMOD + ".py": (NOW, test_src), |
| 146 | TESTMOD + pyc_ext: (NOW, badmagic_pyc)} |
| 147 | self.doTest(".py", files, TESTMOD) |
| 148 | |
| 149 | def testBadMagic2(self): |
| 150 | # make pyc magic word invalid, causing an ImportError |
| 151 | m0 = ord(test_pyc[0]) |
| 152 | m0 ^= 0x04 # flip an arbitrary bit |
| 153 | badmagic_pyc = chr(m0) + test_pyc[1:] |
| 154 | files = {TESTMOD + pyc_ext: (NOW, badmagic_pyc)} |
| 155 | try: |
| 156 | self.doTest(".py", files, TESTMOD) |
| 157 | except ImportError: |
| 158 | pass |
| 159 | else: |
| 160 | self.fail("expected ImportError; import from bad pyc") |
| 161 | |
| 162 | def testBadMTime(self): |
| 163 | t3 = ord(test_pyc[7]) |
| 164 | t3 ^= 0x02 # flip the second bit -- not the first as that one |
| 165 | # isn't stored in the .py's mtime in the zip archive. |
| 166 | badtime_pyc = test_pyc[:7] + chr(t3) + test_pyc[8:] |
| 167 | files = {TESTMOD + ".py": (NOW, test_src), |
| 168 | TESTMOD + pyc_ext: (NOW, badtime_pyc)} |
| 169 | self.doTest(".py", files, TESTMOD) |
| 170 | |
| 171 | def testPackage(self): |
| 172 | packdir = TESTPACK + os.sep |
| 173 | files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc), |
| 174 | packdir + TESTMOD + pyc_ext: (NOW, test_pyc)} |
| 175 | self.doTest(pyc_ext, files, TESTPACK, TESTMOD) |
| 176 | |
| 177 | def testDeepPackage(self): |
| 178 | packdir = TESTPACK + os.sep |
Just van Rossum | d35c6db | 2003-01-02 12:55:48 +0000 | [diff] [blame] | 179 | packdir2 = packdir + TESTPACK2 + os.sep |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 180 | files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc), |
| 181 | packdir2 + "__init__" + pyc_ext: (NOW, test_pyc), |
| 182 | packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)} |
Just van Rossum | d35c6db | 2003-01-02 12:55:48 +0000 | [diff] [blame] | 183 | self.doTest(pyc_ext, files, TESTPACK, TESTPACK2, TESTMOD) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 184 | |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame^] | 185 | def testZipImporterMethods(self): |
| 186 | packdir = TESTPACK + os.sep |
| 187 | packdir2 = packdir + TESTPACK2 + os.sep |
| 188 | files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc), |
| 189 | packdir2 + "__init__" + pyc_ext: (NOW, test_pyc), |
| 190 | packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)} |
| 191 | |
| 192 | z = ZipFile(TEMP_ZIP, "w") |
| 193 | try: |
| 194 | for name, (mtime, data) in files.items(): |
| 195 | zinfo = ZipInfo(name, time.localtime(mtime)) |
| 196 | zinfo.compress_type = self.compression |
| 197 | z.writestr(zinfo, data) |
| 198 | z.close() |
| 199 | |
| 200 | zi = zipimport.zipimporter(TEMP_ZIP) |
| 201 | self.assertEquals(zi.is_package(TESTPACK), True) |
| 202 | zi.load_module(TESTPACK) |
| 203 | |
| 204 | self.assertEquals(zi.is_package(packdir + '__init__'), False) |
| 205 | self.assertEquals(zi.is_package(packdir + TESTPACK2), True) |
| 206 | self.assertEquals(zi.is_package(packdir2 + TESTMOD), False) |
| 207 | |
| 208 | mod_name = packdir2 + TESTMOD |
| 209 | mod = __import__(mod_name.replace('/', '.')) |
| 210 | self.assertEquals(zi.get_source(TESTPACK), None) |
| 211 | self.assertEquals(zi.get_source(mod_name), None) |
| 212 | finally: |
| 213 | z.close() |
| 214 | os.remove(TEMP_ZIP) |
| 215 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 216 | def testGetData(self): |
| 217 | z = ZipFile(TEMP_ZIP, "w") |
| 218 | z.compression = self.compression |
| 219 | try: |
| 220 | name = "testdata.dat" |
| 221 | data = "".join([chr(x) for x in range(256)]) * 500 |
| 222 | z.writestr(name, data) |
| 223 | z.close() |
| 224 | zi = zipimport.zipimporter(TEMP_ZIP) |
| 225 | self.assertEquals(data, zi.get_data(name)) |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame^] | 226 | self.assert_('zipimporter object' in repr(zi)) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 227 | finally: |
| 228 | z.close() |
| 229 | os.remove(TEMP_ZIP) |
| 230 | |
| 231 | def testImporterAttr(self): |
| 232 | src = """if 1: # indent hack |
| 233 | def get_file(): |
| 234 | return __file__ |
Just van Rossum | d35c6db | 2003-01-02 12:55:48 +0000 | [diff] [blame] | 235 | if __loader__.get_data("some.data") != "some data": |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 236 | raise AssertionError, "bad data"\n""" |
| 237 | pyc = make_pyc(compile(src, "<???>", "exec"), NOW) |
| 238 | files = {TESTMOD + pyc_ext: (NOW, pyc), |
| 239 | "some.data": (NOW, "some data")} |
| 240 | self.doTest(pyc_ext, files, TESTMOD) |
| 241 | |
Thomas Heller | 354e3d9 | 2003-07-22 18:10:15 +0000 | [diff] [blame] | 242 | def testImport_WithStuff(self): |
| 243 | # try importing from a zipfile which contains additional |
| 244 | # stuff at the beginning of the file |
| 245 | files = {TESTMOD + ".py": (NOW, test_src)} |
| 246 | self.doTest(".py", files, TESTMOD, |
| 247 | stuff="Some Stuff"*31) |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 248 | |
| 249 | class CompressedZipImportTestCase(UncompressedZipImportTestCase): |
| 250 | compression = ZIP_DEFLATED |
| 251 | |
| 252 | |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame^] | 253 | class BadFileZipImportTestCase(unittest.TestCase): |
| 254 | def assertZipFailure(self, filename): |
| 255 | self.assertRaises(zipimport.ZipImportError, |
| 256 | zipimport.zipimporter, filename) |
| 257 | |
| 258 | def testNoFile(self): |
| 259 | self.assertZipFailure('AdfjdkFJKDFJjdklfjs') |
| 260 | |
| 261 | def testEmptyFilename(self): |
| 262 | self.assertZipFailure('') |
| 263 | |
| 264 | def testBadArgs(self): |
| 265 | self.assertRaises(TypeError, zipimport.zipimporter, None) |
| 266 | self.assertRaises(TypeError, zipimport.zipimporter, TESTMOD, kwd=None) |
| 267 | |
| 268 | def testFilenameTooLong(self): |
| 269 | self.assertZipFailure('A' * 33000) |
| 270 | |
| 271 | def testEmptyFile(self): |
| 272 | test_support.unlink(TESTMOD) |
| 273 | open(TESTMOD, 'w+').close() |
| 274 | self.assertZipFailure(TESTMOD) |
| 275 | |
| 276 | def testFileUnreadable(self): |
| 277 | test_support.unlink(TESTMOD) |
| 278 | fd = os.open(TESTMOD, os.O_CREAT, 000) |
| 279 | os.close(fd) |
| 280 | self.assertZipFailure(TESTMOD) |
| 281 | |
| 282 | def testNotZipFile(self): |
| 283 | test_support.unlink(TESTMOD) |
| 284 | fp = open(TESTMOD, 'w+') |
| 285 | fp.write('a' * 22) |
| 286 | fp.close() |
| 287 | self.assertZipFailure(TESTMOD) |
| 288 | |
| 289 | def testBogusZipFile(self): |
| 290 | test_support.unlink(TESTMOD) |
| 291 | fp = open(TESTMOD, 'w+') |
| 292 | fp.write(struct.pack('=I', 0x06054B50)) |
| 293 | fp.write('a' * 18) |
| 294 | fp.close() |
| 295 | z = zipimport.zipimporter(TESTMOD) |
| 296 | |
| 297 | try: |
| 298 | self.assertRaises(TypeError, z.find_module, None) |
| 299 | self.assertRaises(TypeError, z.load_module, None) |
| 300 | self.assertRaises(TypeError, z.is_package, None) |
| 301 | self.assertRaises(TypeError, z.get_code, None) |
| 302 | self.assertRaises(TypeError, z.get_data, None) |
| 303 | self.assertRaises(TypeError, z.get_source, None) |
| 304 | |
| 305 | error = zipimport.ZipImportError |
| 306 | self.assertEqual(z.find_module('abc'), None) |
| 307 | |
| 308 | self.assertRaises(error, z.load_module, 'abc') |
| 309 | self.assertRaises(error, z.get_code, 'abc') |
| 310 | self.assertRaises(IOError, z.get_data, 'abc') |
| 311 | self.assertRaises(error, z.get_source, 'abc') |
| 312 | self.assertRaises(error, z.is_package, 'abc') |
| 313 | finally: |
| 314 | zipimport._zip_directory_cache.clear() |
| 315 | |
| 316 | |
| 317 | def cleanup(): |
| 318 | # this is necessary if test is run repeated (like when finding leaks) |
| 319 | global test_imported |
| 320 | if test_imported: |
| 321 | zipimport._zip_directory_cache.clear() |
| 322 | if hasattr(UncompressedZipImportTestCase, 'testAFakeZlib'): |
| 323 | delattr(UncompressedZipImportTestCase, 'testAFakeZlib') |
| 324 | if hasattr(CompressedZipImportTestCase, 'testAFakeZlib'): |
| 325 | delattr(CompressedZipImportTestCase, 'testAFakeZlib') |
| 326 | test_imported = True |
| 327 | |
Neal Norwitz | 5c1ba53 | 2003-02-17 18:05:20 +0000 | [diff] [blame] | 328 | def test_main(): |
Neal Norwitz | b155b62 | 2006-01-23 07:52:13 +0000 | [diff] [blame^] | 329 | cleanup() |
| 330 | try: |
| 331 | test_support.run_unittest( |
| 332 | UncompressedZipImportTestCase, |
| 333 | CompressedZipImportTestCase, |
| 334 | BadFileZipImportTestCase, |
| 335 | ) |
| 336 | finally: |
| 337 | test_support.unlink(TESTMOD) |
Neal Norwitz | 5c1ba53 | 2003-02-17 18:05:20 +0000 | [diff] [blame] | 338 | |
| 339 | if __name__ == "__main__": |
| 340 | test_main() |