8585ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
8686'''
8787
88+ # Corrupt .mo file
89+ # Generated from
90+ #
91+ # msgid "foo"
92+ # msgstr "bar"
93+ #
94+ # with msgfmt --no-hash
95+ #
96+ # The translation offset is changed to 0xFFFFFFFF,
97+ # making it larger than the file size, which should
98+ # raise an error when parsing.
99+ GNU_MO_DATA_CORRUPT = base64 .b64encode (bytes ([
100+ 0xDE , 0x12 , 0x04 , 0x95 , # Magic
101+ 0x00 , 0x00 , 0x00 , 0x00 , # Version
102+ 0x01 , 0x00 , 0x00 , 0x00 , # Message count
103+ 0x1C , 0x00 , 0x00 , 0x00 , # Message offset
104+ 0x24 , 0x00 , 0x00 , 0x00 , # Translation offset
105+ 0x00 , 0x00 , 0x00 , 0x00 , # Hash table size
106+ 0x2C , 0x00 , 0x00 , 0x00 , # Hash table offset
107+ 0x03 , 0x00 , 0x00 , 0x00 , # 1st message length
108+ 0x2C , 0x00 , 0x00 , 0x00 , # 1st message offset
109+ 0x03 , 0x00 , 0x00 , 0x00 , # 1st trans length
110+ 0xFF , 0xFF , 0xFF , 0xFF , # 1st trans offset (Modified to make it invalid)
111+ 0x66 , 0x6F , 0x6F , 0x00 , # Message data
112+ 0x62 , 0x61 , 0x72 , 0x00 , # Message data
113+ ]))
88114
89115UMO_DATA = b'''\
90116 3hIElQAAAAADAAAAHAAAADQAAAAAAAAAAAAAAAAAAABMAAAABAAAAE0AAAAQAAAAUgAAAA8BAABj
111137MOFILE = os .path .join (LOCALEDIR , 'gettext.mo' )
112138MOFILE_BAD_MAJOR_VERSION = os .path .join (LOCALEDIR , 'gettext_bad_major_version.mo' )
113139MOFILE_BAD_MINOR_VERSION = os .path .join (LOCALEDIR , 'gettext_bad_minor_version.mo' )
140+ MOFILE_CORRUPT = os .path .join (LOCALEDIR , 'gettext_corrupt.mo' )
114141UMOFILE = os .path .join (LOCALEDIR , 'ugettext.mo' )
115142MMOFILE = os .path .join (LOCALEDIR , 'metadata.mo' )
116143
@@ -133,6 +160,8 @@ def setUpClass(cls):
133160 fp .write (base64 .decodebytes (GNU_MO_DATA_BAD_MAJOR_VERSION ))
134161 with open (MOFILE_BAD_MINOR_VERSION , 'wb' ) as fp :
135162 fp .write (base64 .decodebytes (GNU_MO_DATA_BAD_MINOR_VERSION ))
163+ with open (MOFILE_CORRUPT , 'wb' ) as fp :
164+ fp .write (base64 .decodebytes (GNU_MO_DATA_CORRUPT ))
136165 with open (UMOFILE , 'wb' ) as fp :
137166 fp .write (base64 .decodebytes (UMO_DATA ))
138167 with open (MMOFILE , 'wb' ) as fp :
@@ -238,6 +267,16 @@ def test_bad_minor_version(self):
238267 # Check that no error is thrown with a bad minor version number
239268 gettext .GNUTranslations (fp )
240269
270+ def test_corrupt_file (self ):
271+ with open (MOFILE_CORRUPT , 'rb' ) as fp :
272+ with self .assertRaises (OSError ) as cm :
273+ gettext .GNUTranslations (fp )
274+
275+ exception = cm .exception
276+ self .assertEqual (exception .errno , 0 )
277+ self .assertEqual (exception .strerror , "File is corrupt" )
278+ self .assertEqual (exception .filename , MOFILE_CORRUPT )
279+
241280 def test_some_translations (self ):
242281 eq = self .assertEqual
243282 # test some translations
0 commit comments