|
1 | 1 | import os |
2 | 2 | import base64 |
3 | 3 | import gettext |
| 4 | +import io |
4 | 5 | import unittest |
5 | 6 | import unittest.mock |
6 | 7 | from functools import partial |
|
39 | 40 | ''' |
40 | 41 |
|
41 | 42 | # .mo file with an invalid magic number |
42 | | -GNU_MO_DATA_BAD_MAGIC_NUMBER = base64.b64encode(b'ABCD') |
| 43 | +GNU_MO_DATA_BAD_MAGIC_NUMBER = base64.b64encode(b'ABCD' + bytes(16)) |
43 | 44 |
|
44 | 45 | # This data contains an invalid major version number (5) |
45 | 46 | # An unexpected major version number should be treated as an error when |
@@ -314,6 +315,39 @@ def test_corrupt_file(self): |
314 | 315 | self.assertEqual(exception.strerror, "File is corrupt") |
315 | 316 | self.assertEqual(exception.filename, MOFILE_CORRUPT) |
316 | 317 |
|
| 318 | + def test_truncated_header(self): |
| 319 | + magic = b'\xde\x12\x04\x95' |
| 320 | + for buf in (b'', magic, magic + bytes(15)): |
| 321 | + with self.subTest(buflen=len(buf)): |
| 322 | + with self.assertRaises(OSError) as cm: |
| 323 | + gettext.GNUTranslations(io.BytesIO(buf)) |
| 324 | + self.assertEqual(cm.exception.errno, 0) |
| 325 | + self.assertEqual(cm.exception.strerror, "File is corrupt") |
| 326 | + |
| 327 | + def test_offsets_out_of_bounds(self): |
| 328 | + buf = bytes([ |
| 329 | + 0xde, 0x12, 0x04, 0x95, # Magic |
| 330 | + 0x00, 0x00, 0x00, 0x00, # Version |
| 331 | + 0x01, 0x00, 0x00, 0x00, # Message count |
| 332 | + 0xe8, 0x03, 0x00, 0x00, # Message offset (past EOF) |
| 333 | + 0xd0, 0x07, 0x00, 0x00, # Translation offset (past EOF) |
| 334 | + ]) |
| 335 | + with self.assertRaises(OSError) as cm: |
| 336 | + gettext.GNUTranslations(io.BytesIO(buf)) |
| 337 | + self.assertEqual(cm.exception.errno, 0) |
| 338 | + self.assertEqual(cm.exception.strerror, "File is corrupt") |
| 339 | + |
| 340 | + def test_empty_catalog(self): |
| 341 | + buf = bytes([ |
| 342 | + 0xde, 0x12, 0x04, 0x95, # Magic |
| 343 | + 0x00, 0x00, 0x00, 0x00, # Version |
| 344 | + 0x00, 0x00, 0x00, 0x00, # Message count |
| 345 | + 0x14, 0x00, 0x00, 0x00, # Message offset |
| 346 | + 0x14, 0x00, 0x00, 0x00, # Translation offset |
| 347 | + ]) |
| 348 | + t = gettext.GNUTranslations(io.BytesIO(buf)) |
| 349 | + self.assertEqual(t.gettext('foo'), 'foo') |
| 350 | + |
317 | 351 | def test_big_endian_file(self): |
318 | 352 | with open(MOFILE_BIG_ENDIAN, 'rb') as fp: |
319 | 353 | t = gettext.GNUTranslations(fp) |
|
0 commit comments