9090ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
9191'''
9292
93+ # Corrupt .mo file
94+ # Generated from
95+ #
96+ # msgid "foo"
97+ # msgstr "bar"
98+ #
99+ # with msgfmt --no-hash
100+ #
101+ # The translation offset is changed to 0xFFFFFFFF,
102+ # making it larger than the file size, which should
103+ # raise an error when parsing.
104+ GNU_MO_DATA_CORRUPT = base64 .b64encode (bytes ([
105+ 0xDE , 0x12 , 0x04 , 0x95 , # Magic
106+ 0x00 , 0x00 , 0x00 , 0x00 , # Version
107+ 0x01 , 0x00 , 0x00 , 0x00 , # Message count
108+ 0x1C , 0x00 , 0x00 , 0x00 , # Message offset
109+ 0x24 , 0x00 , 0x00 , 0x00 , # Translation offset
110+ 0x00 , 0x00 , 0x00 , 0x00 , # Hash table size
111+ 0x2C , 0x00 , 0x00 , 0x00 , # Hash table offset
112+ 0x03 , 0x00 , 0x00 , 0x00 , # 1st message length
113+ 0x2C , 0x00 , 0x00 , 0x00 , # 1st message offset
114+ 0x03 , 0x00 , 0x00 , 0x00 , # 1st trans length
115+ 0xFF , 0xFF , 0xFF , 0xFF , # 1st trans offset (Modified to make it invalid)
116+ 0x66 , 0x6F , 0x6F , 0x00 , # Message data
117+ 0x62 , 0x61 , 0x72 , 0x00 , # Message data
118+ ]))
93119
94120UMO_DATA = b'''\
95121 3hIElQAAAAADAAAAHAAAADQAAAAAAAAAAAAAAAAAAABMAAAABAAAAE0AAAAQAAAAUgAAAA8BAABj
117143MOFILE_BAD_MAGIC_NUMBER = os .path .join (LOCALEDIR , 'gettext_bad_magic_number.mo' )
118144MOFILE_BAD_MAJOR_VERSION = os .path .join (LOCALEDIR , 'gettext_bad_major_version.mo' )
119145MOFILE_BAD_MINOR_VERSION = os .path .join (LOCALEDIR , 'gettext_bad_minor_version.mo' )
146+ MOFILE_CORRUPT = os .path .join (LOCALEDIR , 'gettext_corrupt.mo' )
120147UMOFILE = os .path .join (LOCALEDIR , 'ugettext.mo' )
121148MMOFILE = os .path .join (LOCALEDIR , 'metadata.mo' )
122149
@@ -141,6 +168,8 @@ def setUpClass(cls):
141168 fp .write (base64 .decodebytes (GNU_MO_DATA_BAD_MAJOR_VERSION ))
142169 with open (MOFILE_BAD_MINOR_VERSION , 'wb' ) as fp :
143170 fp .write (base64 .decodebytes (GNU_MO_DATA_BAD_MINOR_VERSION ))
171+ with open (MOFILE_CORRUPT , 'wb' ) as fp :
172+ fp .write (base64 .decodebytes (GNU_MO_DATA_CORRUPT ))
144173 with open (UMOFILE , 'wb' ) as fp :
145174 fp .write (base64 .decodebytes (UMO_DATA ))
146175 with open (MMOFILE , 'wb' ) as fp :
@@ -280,6 +309,16 @@ def test_bad_minor_version(self):
280309 # Check that no error is thrown with a bad minor version number
281310 gettext .GNUTranslations (fp )
282311
312+ def test_corrupt_file (self ):
313+ with open (MOFILE_CORRUPT , 'rb' ) as fp :
314+ with self .assertRaises (OSError ) as cm :
315+ gettext .GNUTranslations (fp )
316+
317+ exception = cm .exception
318+ self .assertEqual (exception .errno , 0 )
319+ self .assertEqual (exception .strerror , "File is corrupt" )
320+ self .assertEqual (exception .filename , MOFILE_CORRUPT )
321+
283322 def test_some_translations (self ):
284323 eq = self .assertEqual
285324 # test some translations
0 commit comments