| 1358 | tar.close() |
| 1359 | |
| 1360 | def test_pax_header_bad_formats(self): |
| 1361 | # The fields from the pax header have priority over the |
| 1362 | # TarInfo. |
| 1363 | pax_header_replacements = ( |
| 1364 | b" foo=bar\n", |
| 1365 | b"0 \n", |
| 1366 | b"1 \n", |
| 1367 | b"2 \n", |
| 1368 | b"3 =\n", |
| 1369 | b"4 =a\n", |
| 1370 | b"1000000 foo=bar\n", |
| 1371 | b"0 foo=bar\n", |
| 1372 | b"-12 foo=bar\n", |
| 1373 | b"000000000000000000000000036 foo=bar\n", |
| 1374 | ) |
| 1375 | pax_headers = {"foo": "bar"} |
| 1376 | |
| 1377 | for replacement in pax_header_replacements: |
| 1378 | with self.subTest(header=replacement): |
| 1379 | tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, |
| 1380 | encoding="iso8859-1") |
| 1381 | try: |
| 1382 | t = tarfile.TarInfo() |
| 1383 | t.name = "pax" # non-ASCII |
| 1384 | t.uid = 1 |
| 1385 | t.pax_headers = pax_headers |
| 1386 | tar.addfile(t) |
| 1387 | finally: |
| 1388 | tar.close() |
| 1389 | |
| 1390 | with open(tmpname, "rb") as f: |
| 1391 | data = f.read() |
| 1392 | self.assertIn(b"11 foo=bar\n", data) |
| 1393 | data = data.replace(b"11 foo=bar\n", replacement) |
| 1394 | |
| 1395 | with open(tmpname, "wb") as f: |
| 1396 | f.truncate() |
| 1397 | f.write(data) |
| 1398 | |
| 1399 | with self.assertRaisesRegex(tarfile.ReadError, r"method tar: ReadError\('invalid header'\)"): |
| 1400 | tarfile.open(tmpname, encoding="iso8859-1") |
| 1401 | |
| 1402 | |
| 1403 | class WriteTestBase(TarTest): |