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