| 1341 | tar.close() |
| 1342 | |
| 1343 | def test_pax_header_bad_formats(self): |
| 1344 | # The fields from the pax header have priority over the |
| 1345 | # TarInfo. |
| 1346 | pax_header_replacements = ( |
| 1347 | b" foo=bar\n", |
| 1348 | b"0 \n", |
| 1349 | b"1 \n", |
| 1350 | b"2 \n", |
| 1351 | b"3 =\n", |
| 1352 | b"4 =a\n", |
| 1353 | b"1000000 foo=bar\n", |
| 1354 | b"0 foo=bar\n", |
| 1355 | b"-12 foo=bar\n", |
| 1356 | b"000000000000000000000000036 foo=bar\n", |
| 1357 | ) |
| 1358 | pax_headers = {"foo": "bar"} |
| 1359 | |
| 1360 | for replacement in pax_header_replacements: |
| 1361 | with self.subTest(header=replacement): |
| 1362 | tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, |
| 1363 | encoding="iso8859-1") |
| 1364 | try: |
| 1365 | t = tarfile.TarInfo() |
| 1366 | t.name = "pax" # non-ASCII |
| 1367 | t.uid = 1 |
| 1368 | t.pax_headers = pax_headers |
| 1369 | tar.addfile(t) |
| 1370 | finally: |
| 1371 | tar.close() |
| 1372 | |
| 1373 | with open(tmpname, "rb") as f: |
| 1374 | data = f.read() |
| 1375 | self.assertIn(b"11 foo=bar\n", data) |
| 1376 | data = data.replace(b"11 foo=bar\n", replacement) |
| 1377 | |
| 1378 | with open(tmpname, "wb") as f: |
| 1379 | f.truncate() |
| 1380 | f.write(data) |
| 1381 | |
| 1382 | with self.assertRaisesRegex(tarfile.ReadError, r"method tar: ReadError\('invalid header'\)"): |
| 1383 | tarfile.open(tmpname, encoding="iso8859-1") |
| 1384 | |
| 1385 | |
| 1386 | class WriteTestBase(TarTest): |