Skip to content

Commit 001fe0f

Browse files
tomasr8miss-islington
authored andcommitted
pythongh-130655: Add a test for bad magic numbers in .mo files parsed by gettext (pythonGH-131909)
(cherry picked from commit 16a6270) Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
1 parent 701ac74 commit 001fe0f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Lib/test/test_gettext.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
bmsgd2luayAoaW4gIm15IG90aGVyIGNvbnRleHQiKQB3aW5rIHdpbmsA
4040
'''
4141

42+
# .mo file with an invalid magic number
43+
GNU_MO_DATA_BAD_MAGIC_NUMBER = base64.b64encode(b'ABCD')
44+
4245
# This data contains an invalid major version number (5)
4346
# An unexpected major version number should be treated as an error when
4447
# parsing a .mo file
@@ -111,6 +114,7 @@
111114

112115
LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
113116
MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
117+
MOFILE_BAD_MAGIC_NUMBER = os.path.join(LOCALEDIR, 'gettext_bad_magic_number.mo')
114118
MOFILE_BAD_MAJOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_major_version.mo')
115119
MOFILE_BAD_MINOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_minor_version.mo')
116120
UMOFILE = os.path.join(LOCALEDIR, 'ugettext.mo')
@@ -131,6 +135,8 @@ def setUpClass(cls):
131135
os.makedirs(LOCALEDIR)
132136
with open(MOFILE, 'wb') as fp:
133137
fp.write(base64.decodebytes(GNU_MO_DATA))
138+
with open(MOFILE_BAD_MAGIC_NUMBER, 'wb') as fp:
139+
fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MAGIC_NUMBER))
134140
with open(MOFILE_BAD_MAJOR_VERSION, 'wb') as fp:
135141
fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MAJOR_VERSION))
136142
with open(MOFILE_BAD_MINOR_VERSION, 'wb') as fp:
@@ -249,6 +255,16 @@ def test_bindtextdomain(self):
249255
def test_textdomain(self):
250256
self.assertEqual(gettext.textdomain(), 'gettext')
251257

258+
def test_bad_magic_number(self):
259+
with open(MOFILE_BAD_MAGIC_NUMBER, 'rb') as fp:
260+
with self.assertRaises(OSError) as cm:
261+
gettext.GNUTranslations(fp)
262+
263+
exception = cm.exception
264+
self.assertEqual(exception.errno, 0)
265+
self.assertEqual(exception.strerror, "Bad magic number")
266+
self.assertEqual(exception.filename, MOFILE_BAD_MAGIC_NUMBER)
267+
252268
def test_bad_major_version(self):
253269
with open(MOFILE_BAD_MAJOR_VERSION, 'rb') as fp:
254270
with self.assertRaises(OSError) as cm:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy