| 471 | self.assertRaises(tarfile.ReadError, tarfile.open, tmpname) |
| 472 | |
| 473 | def test_ignore_zeros(self): |
| 474 | # Test TarFile's ignore_zeros option. |
| 475 | # generate 512 pseudorandom bytes |
| 476 | data = Random(0).randbytes(512) |
| 477 | for char in (b'\0', b'a'): |
| 478 | # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a') |
| 479 | # are ignored correctly. |
| 480 | with self.open(tmpname, "w") as fobj: |
| 481 | fobj.write(char * 1024) |
| 482 | tarinfo = tarfile.TarInfo("foo") |
| 483 | tarinfo.size = len(data) |
| 484 | fobj.write(tarinfo.tobuf()) |
| 485 | fobj.write(data) |
| 486 | |
| 487 | tar = tarfile.open(tmpname, mode="r", ignore_zeros=True) |
| 488 | try: |
| 489 | self.assertListEqual(tar.getnames(), ["foo"], |
| 490 | "ignore_zeros=True should have skipped the %r-blocks" % |
| 491 | char) |
| 492 | finally: |
| 493 | tar.close() |
| 494 | |
| 495 | def test_premature_end_of_archive(self): |
| 496 | for size in (512, 600, 1024, 1200): |