blob: eb7cbf6527b3a031eff79fef36b716a2ea8752ec [file] [log] [blame]
Just van Rossum52e14d62002-12-30 22:08:05 +00001import sys
2import os
3import marshal
4import imp
5import struct
6import time
Neal Norwitzb155b622006-01-23 07:52:13 +00007import unittest
Just van Rossum52e14d62002-12-30 22:08:05 +00008
9import zlib # implied prerequisite
10from zipfile import ZipFile, ZipInfo, ZIP_STORED, ZIP_DEFLATED
11from test import test_support
12from test.test_importhooks import ImportHooksBaseTestCase, test_src, test_co
13
14import zipimport
15
16
Neal Norwitzb155b622006-01-23 07:52:13 +000017# so we only run testAFakeZlib once if this test is run repeatedly
18# which happens when we look for ref leaks
19test_imported = False
20
21
Just van Rossum52e14d62002-12-30 22:08:05 +000022def make_pyc(co, mtime):
23 data = marshal.dumps(co)
Jack Jansen472e7db2003-01-08 16:37:03 +000024 if type(mtime) is type(0.0):
Tim Petersf2715e02003-02-19 02:35:07 +000025 # 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 Jansen472e7db2003-01-08 16:37:03 +000030 pyc = imp.get_magic() + struct.pack("<i", int(mtime)) + data
Just van Rossum52e14d62002-12-30 22:08:05 +000031 return pyc
32
Tim Peters68f2d002006-01-23 22:19:24 +000033def module_path_to_dotted_name(path):
34 return path.replace(os.sep, '.')
35
Just van Rossum52e14d62002-12-30 22:08:05 +000036NOW = time.time()
37test_pyc = make_pyc(test_co, NOW)
38
39
40if __debug__:
41 pyc_ext = ".pyc"
42else:
43 pyc_ext = ".pyo"
44
45
46TESTMOD = "ziptestmodule"
47TESTPACK = "ziptestpackage"
Just van Rossumd35c6db2003-01-02 12:55:48 +000048TESTPACK2 = "ziptestpackage2"
Martin v. Löwisa94568a2003-05-10 07:36:56 +000049TEMP_ZIP = os.path.abspath("junk95142" + os.extsep + "zip")
Just van Rossum52e14d62002-12-30 22:08:05 +000050
51class 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 Heller354e3d92003-07-22 18:10:15 +000061 def doTest(self, expected_ext, files, *modules, **kw):
Just van Rossum52e14d62002-12-30 22:08:05 +000062 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 Heller354e3d92003-07-22 18:10:15 +000069
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 Rossum52e14d62002-12-30 22:08:05 +000082 sys.path.insert(0, TEMP_ZIP)
83
84 mod = __import__(".".join(modules), globals(), locals(),
85 ["__dummy__"])
Just van Rossum9a3129c2003-01-03 11:18:56 +000086 if expected_ext:
87 file = mod.get_file()
88 self.assertEquals(file, os.path.join(TEMP_ZIP,
Just van Rossum6706c4d2003-01-09 22:27:10 +000089 *modules) + expected_ext)
Just van Rossum52e14d62002-12-30 22:08:05 +000090 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 Rossum59498542003-11-18 23:00:55 +0000107 # 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 Rossum52e14d62002-12-30 22:08:05 +0000114 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 Rossum9a3129c2003-01-03 11:18:56 +0000139 def testEmptyPy(self):
140 files = {TESTMOD + ".py": (NOW, "")}
141 self.doTest(None, files, TESTMOD)
142
Just van Rossum52e14d62002-12-30 22:08:05 +0000143 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 Rossumd35c6db2003-01-02 12:55:48 +0000182 packdir2 = packdir + TESTPACK2 + os.sep
Just van Rossum52e14d62002-12-30 22:08:05 +0000183 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 Rossumd35c6db2003-01-02 12:55:48 +0000186 self.doTest(pyc_ext, files, TESTPACK, TESTPACK2, TESTMOD)
Just van Rossum52e14d62002-12-30 22:08:05 +0000187
Neal Norwitzb155b622006-01-23 07:52:13 +0000188 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 Petersbc29c1a2006-01-23 21:28:42 +0000206
Neal Norwitzb155b622006-01-23 07:52:13 +0000207 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 Peters68f2d002006-01-23 22:19:24 +0000212 mod = __import__(module_path_to_dotted_name(mod_name))
Neal Norwitzb155b622006-01-23 07:52:13 +0000213 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 Rossum52e14d62002-12-30 22:08:05 +0000219 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 Norwitzb155b622006-01-23 07:52:13 +0000229 self.assert_('zipimporter object' in repr(zi))
Just van Rossum52e14d62002-12-30 22:08:05 +0000230 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 Rossumd35c6db2003-01-02 12:55:48 +0000238 if __loader__.get_data("some.data") != "some data":
Just van Rossum52e14d62002-12-30 22:08:05 +0000239 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 Heller354e3d92003-07-22 18:10:15 +0000245 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 Rossum52e14d62002-12-30 22:08:05 +0000251
252class CompressedZipImportTestCase(UncompressedZipImportTestCase):
253 compression = ZIP_DEFLATED
254
255
Neal Norwitzb155b622006-01-23 07:52:13 +0000256class 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 Peters68f2d002006-01-23 22:19:24 +0000282 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 Norwitzb155b622006-01-23 07:52:13 +0000290
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 Norwitzdbc95f42006-01-23 08:48:03 +0000298 # XXX: disabled until this works on Big-endian machines
299 def _testBogusZipFile(self):
Neal Norwitzb155b622006-01-23 07:52:13 +0000300 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
327def 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 Norwitz5c1ba532003-02-17 18:05:20 +0000338def test_main():
Neal Norwitzb155b622006-01-23 07:52:13 +0000339 cleanup()
340 try:
341 test_support.run_unittest(
342 UncompressedZipImportTestCase,
343 CompressedZipImportTestCase,
344 BadFileZipImportTestCase,
345 )
346 finally:
347 test_support.unlink(TESTMOD)
Neal Norwitz5c1ba532003-02-17 18:05:20 +0000348
349if __name__ == "__main__":
350 test_main()