FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-130655: Add a test for bad magic numbers when parsing gettext MO files by tomasr8 · Pull Request #131909 · python/cpython · GitHub

Repository navigation

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
16 changes: 16 additions & 0 deletions Lib/test/test_gettext.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
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)
# An unexpected major version number should be treated as an error when
# parsing a .mo file
Expand Down Expand Up @@ -109,6 +112,7 @@

LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
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_MINOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_minor_version.mo')
UMOFILE = os.path.join(LOCALEDIR, 'ugettext.mo')
Expand All @@ -129,6 +133,8 @@ def setUpClass(cls):
os.makedirs(LOCALEDIR)
with open(MOFILE, 'wb') as fp:
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:
fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MAJOR_VERSION))
with open(MOFILE_BAD_MINOR_VERSION, 'wb') as fp:
Expand Down Expand Up @@ -223,6 +229,16 @@ def test_bindtextdomain(self):
def test_textdomain(self):
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):
with open(MOFILE_BAD_MAJOR_VERSION, 'rb') as fp:
with self.assertRaises(OSError) as cm:
Expand Down

Back | FazBrowse Home | New Git URL