8888ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
8989'''
9090
91+ # Corrupt .mo file
92+ # Generated from
93+ #
94+ # msgid "foo"
95+ # msgstr "bar"
96+ #
97+ # with msgfmt --no-hash
98+ #
99+ # The translation offset is changed to 0xFFFFFFFF,
100+ # making it larger than the file size, which should
101+ # raise an error when parsing.
102+ GNU_MO_DATA_CORRUPT = base64 .b64encode (bytes ([
103+ 0xDE , 0x12 , 0x04 , 0x95 , # Magic
104+ 0x00 , 0x00 , 0x00 , 0x00 , # Version
105+ 0x01 , 0x00 , 0x00 , 0x00 , # Message count
106+ 0x1C , 0x00 , 0x00 , 0x00 , # Message offset
107+ 0x24 , 0x00 , 0x00 , 0x00 , # Translation offset
108+ 0x00 , 0x00 , 0x00 , 0x00 , # Hash table size
109+ 0x2C , 0x00 , 0x00 , 0x00 , # Hash table offset
110+ 0x03 , 0x00 , 0x00 , 0x00 , # 1st message length
111+ 0x2C , 0x00 , 0x00 , 0x00 , # 1st message offset
112+ 0x03 , 0x00 , 0x00 , 0x00 , # 1st trans length
113+ 0xFF , 0xFF , 0xFF , 0xFF , # 1st trans offset (Modified to make it invalid)
114+ 0x66 , 0x6F , 0x6F , 0x00 , # Message data
115+ 0x62 , 0x61 , 0x72 , 0x00 , # Message data
116+ ]))
91117
92118UMO_DATA = b'''\
93119 3hIElQAAAAADAAAAHAAAADQAAAAAAAAAAAAAAAAAAABMAAAABAAAAE0AAAAQAAAAUgAAAA8BAABj
115141MOFILE_BAD_MAGIC_NUMBER = os .path .join (LOCALEDIR , 'gettext_bad_magic_number.mo' )
116142MOFILE_BAD_MAJOR_VERSION = os .path .join (LOCALEDIR , 'gettext_bad_major_version.mo' )
117143MOFILE_BAD_MINOR_VERSION = os .path .join (LOCALEDIR , 'gettext_bad_minor_version.mo' )
144+ MOFILE_CORRUPT = os .path .join (LOCALEDIR , 'gettext_corrupt.mo' )
118145UMOFILE = os .path .join (LOCALEDIR , 'ugettext.mo' )
119146MMOFILE = os .path .join (LOCALEDIR , 'metadata.mo' )
120147
@@ -139,6 +166,8 @@ def setUpClass(cls):
139166 fp .write (base64 .decodebytes (GNU_MO_DATA_BAD_MAJOR_VERSION ))
140167 with open (MOFILE_BAD_MINOR_VERSION , 'wb' ) as fp :
141168 fp .write (base64 .decodebytes (GNU_MO_DATA_BAD_MINOR_VERSION ))
169+ with open (MOFILE_CORRUPT , 'wb' ) as fp :
170+ fp .write (base64 .decodebytes (GNU_MO_DATA_CORRUPT ))
142171 with open (UMOFILE , 'wb' ) as fp :
143172 fp .write (base64 .decodebytes (UMO_DATA ))
144173 with open (MMOFILE , 'wb' ) as fp :
@@ -254,6 +283,16 @@ def test_bad_minor_version(self):
254283 # Check that no error is thrown with a bad minor version number
255284 gettext .GNUTranslations (fp )
256285
286+ def test_corrupt_file (self ):
287+ with open (MOFILE_CORRUPT , 'rb' ) as fp :
288+ with self .assertRaises (OSError ) as cm :
289+ gettext .GNUTranslations (fp )
290+
291+ exception = cm .exception
292+ self .assertEqual (exception .errno , 0 )
293+ self .assertEqual (exception .strerror , "File is corrupt" )
294+ self .assertEqual (exception .filename , MOFILE_CORRUPT )
295+
257296 def test_some_translations (self ):
258297 eq = self .assertEqual
259298 # test some translations
0 commit comments