mirror of
https://github.com/python/cpython.git
synced 2025-09-25 09:50:37 +00:00
gh-130655: Add a test for bad magic numbers in .mo
files parsed by gettext
(#131909)
This commit is contained in:
parent
891c61c1fa
commit
16a6270aa6
1 changed files with 16 additions and 0 deletions
|
@ -37,6 +37,9 @@ cyBmaWNoZXJvcyAoY29udGV4dCkAYmFjb24Ad2luayB3aW5rIChpbiAibXkgY29udGV4dCIpAHdp
|
||||||
bmsgd2luayAoaW4gIm15IG90aGVyIGNvbnRleHQiKQB3aW5rIHdpbmsA
|
bmsgd2luayAoaW4gIm15IG90aGVyIGNvbnRleHQiKQB3aW5rIHdpbmsA
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
# .mo file with an invalid magic number
|
||||||
|
GNU_MO_DATA_BAD_MAGIC_NUMBER = base64.b64encode(b'ABCD')
|
||||||
|
|
||||||
# This data contains an invalid major version number (5)
|
# This data contains an invalid major version number (5)
|
||||||
# An unexpected major version number should be treated as an error when
|
# An unexpected major version number should be treated as an error when
|
||||||
# parsing a .mo file
|
# parsing a .mo file
|
||||||
|
@ -109,6 +112,7 @@ bGUKR2VuZXJhdGVkLUJ5OiBweWdldHRleHQucHkgMS4zCgA=
|
||||||
|
|
||||||
LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
|
LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
|
||||||
MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
|
MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
|
||||||
|
MOFILE_BAD_MAGIC_NUMBER = os.path.join(LOCALEDIR, 'gettext_bad_magic_number.mo')
|
||||||
MOFILE_BAD_MAJOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_major_version.mo')
|
MOFILE_BAD_MAJOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_major_version.mo')
|
||||||
MOFILE_BAD_MINOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_minor_version.mo')
|
MOFILE_BAD_MINOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_minor_version.mo')
|
||||||
UMOFILE = os.path.join(LOCALEDIR, 'ugettext.mo')
|
UMOFILE = os.path.join(LOCALEDIR, 'ugettext.mo')
|
||||||
|
@ -129,6 +133,8 @@ class GettextBaseTest(unittest.TestCase):
|
||||||
os.makedirs(LOCALEDIR)
|
os.makedirs(LOCALEDIR)
|
||||||
with open(MOFILE, 'wb') as fp:
|
with open(MOFILE, 'wb') as fp:
|
||||||
fp.write(base64.decodebytes(GNU_MO_DATA))
|
fp.write(base64.decodebytes(GNU_MO_DATA))
|
||||||
|
with open(MOFILE_BAD_MAGIC_NUMBER, 'wb') as fp:
|
||||||
|
fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MAGIC_NUMBER))
|
||||||
with open(MOFILE_BAD_MAJOR_VERSION, 'wb') as fp:
|
with open(MOFILE_BAD_MAJOR_VERSION, 'wb') as fp:
|
||||||
fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MAJOR_VERSION))
|
fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MAJOR_VERSION))
|
||||||
with open(MOFILE_BAD_MINOR_VERSION, 'wb') as fp:
|
with open(MOFILE_BAD_MINOR_VERSION, 'wb') as fp:
|
||||||
|
@ -223,6 +229,16 @@ class GettextTestCase2(GettextBaseTest):
|
||||||
def test_textdomain(self):
|
def test_textdomain(self):
|
||||||
self.assertEqual(gettext.textdomain(), 'gettext')
|
self.assertEqual(gettext.textdomain(), 'gettext')
|
||||||
|
|
||||||
|
def test_bad_magic_number(self):
|
||||||
|
with open(MOFILE_BAD_MAGIC_NUMBER, 'rb') as fp:
|
||||||
|
with self.assertRaises(OSError) as cm:
|
||||||
|
gettext.GNUTranslations(fp)
|
||||||
|
|
||||||
|
exception = cm.exception
|
||||||
|
self.assertEqual(exception.errno, 0)
|
||||||
|
self.assertEqual(exception.strerror, "Bad magic number")
|
||||||
|
self.assertEqual(exception.filename, MOFILE_BAD_MAGIC_NUMBER)
|
||||||
|
|
||||||
def test_bad_major_version(self):
|
def test_bad_major_version(self):
|
||||||
with open(MOFILE_BAD_MAJOR_VERSION, 'rb') as fp:
|
with open(MOFILE_BAD_MAJOR_VERSION, 'rb') as fp:
|
||||||
with self.assertRaises(OSError) as cm:
|
with self.assertRaises(OSError) as cm:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue