88
88
ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
89
89
'''
90
90
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
+ ]))
91
117
92
118
UMO_DATA = b'''\
93
119
3hIElQAAAAADAAAAHAAAADQAAAAAAAAAAAAAAAAAAABMAAAABAAAAE0AAAAQAAAAUgAAAA8BAABj
115
141
MOFILE_BAD_MAGIC_NUMBER = os .path .join (LOCALEDIR , 'gettext_bad_magic_number.mo' )
116
142
MOFILE_BAD_MAJOR_VERSION = os .path .join (LOCALEDIR , 'gettext_bad_major_version.mo' )
117
143
MOFILE_BAD_MINOR_VERSION = os .path .join (LOCALEDIR , 'gettext_bad_minor_version.mo' )
144
+ MOFILE_CORRUPT = os .path .join (LOCALEDIR , 'gettext_corrupt.mo' )
118
145
UMOFILE = os .path .join (LOCALEDIR , 'ugettext.mo' )
119
146
MMOFILE = os .path .join (LOCALEDIR , 'metadata.mo' )
120
147
@@ -139,6 +166,8 @@ def setUpClass(cls):
139
166
fp .write (base64 .decodebytes (GNU_MO_DATA_BAD_MAJOR_VERSION ))
140
167
with open (MOFILE_BAD_MINOR_VERSION , 'wb' ) as fp :
141
168
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 ))
142
171
with open (UMOFILE , 'wb' ) as fp :
143
172
fp .write (base64 .decodebytes (UMO_DATA ))
144
173
with open (MMOFILE , 'wb' ) as fp :
@@ -254,6 +283,16 @@ def test_bad_minor_version(self):
254
283
# Check that no error is thrown with a bad minor version number
255
284
gettext .GNUTranslations (fp )
256
285
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
+
257
296
def test_some_translations (self ):
258
297
eq = self .assertEqual
259
298
# test some translations
0 commit comments