|
5 | 5 | import pathlib |
6 | 6 | import posixpath |
7 | 7 | import struct |
| 8 | +import subprocess |
| 9 | +import sys |
8 | 10 | import time |
9 | 11 | import unittest |
10 | 12 | import zipfile |
@@ -2470,6 +2472,44 @@ def build_alpharep_fixture(): |
2470 | 2472 | return zf |
2471 | 2473 |
|
2472 | 2474 |
|
| 2475 | +class TestExecutablePrependedZip(unittest.TestCase): |
| 2476 | + """Test our ability to open zip files with an executable prepended.""" |
| 2477 | + |
| 2478 | + def setUp(self): |
| 2479 | + self.exe_zip = findfile('exe_with_zip', subdir='ziptestdata') |
| 2480 | + self.exe_zip64 = findfile('exe_with_z64', subdir='ziptestdata') |
| 2481 | + |
| 2482 | + def _test_zip_works(self, name): |
| 2483 | + # bpo-28494 sanity check: ensure is_zipfile works on these. |
| 2484 | + self.assertTrue(zipfile.is_zipfile(name), |
| 2485 | + f'is_zipfile failed on {name}') |
| 2486 | + # Ensure we can operate on these via ZipFile. |
| 2487 | + with zipfile.ZipFile(name) as zipfp: |
| 2488 | + for n in zipfp.namelist(): |
| 2489 | + data = zipfp.read(n) |
| 2490 | + self.assertIn(b'FAVORITE_NUMBER', data) |
| 2491 | + |
| 2492 | + def test_read_zip_with_exe_prepended(self): |
| 2493 | + self._test_zip_works(self.exe_zip) |
| 2494 | + |
| 2495 | + def test_read_zip64_with_exe_prepended(self): |
| 2496 | + self._test_zip_works(self.exe_zip64) |
| 2497 | + |
| 2498 | + @unittest.skipUnless(sys.executable, 'sys.executable required.') |
| 2499 | + @unittest.skipUnless(os.access('/bin/bash', os.X_OK), |
| 2500 | + 'Test relies on #!/bin/bash working.') |
| 2501 | + def test_execute_zip2(self): |
| 2502 | + output = subprocess.check_output([self.exe_zip, sys.executable]) |
| 2503 | + self.assertIn(b'number in executable: 5', output) |
| 2504 | + |
| 2505 | + @unittest.skipUnless(sys.executable, 'sys.executable required.') |
| 2506 | + @unittest.skipUnless(os.access('/bin/bash', os.X_OK), |
| 2507 | + 'Test relies on #!/bin/bash working.') |
| 2508 | + def test_execute_zip64(self): |
| 2509 | + output = subprocess.check_output([self.exe_zip64, sys.executable]) |
| 2510 | + self.assertIn(b'number in executable: 5', output) |
| 2511 | + |
| 2512 | + |
2473 | 2513 | class TestPath(unittest.TestCase): |
2474 | 2514 | def setUp(self): |
2475 | 2515 | self.fixtures = contextlib.ExitStack() |
|
0 commit comments