87
87
ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
88
88
'''
89
89
90
+ # Corrupt .mo file
91
+ # Generated from
92
+ #
93
+ # msgid "foo"
94
+ # msgstr "bar"
95
+ #
96
+ # with msgfmt --no-hash
97
+ #
98
+ # The translation offset is changed to 0xFFFFFFFF,
99
+ # making it larger than the file size, which should
100
+ # raise an error when parsing.
101
+ GNU_MO_DATA_CORRUPT = base64 .b64encode (bytes ([
102
+ 0xDE , 0x12 , 0x04 , 0x95 , # Magic
103
+ 0x00 , 0x00 , 0x00 , 0x00 , # Version
104
+ 0x01 , 0x00 , 0x00 , 0x00 , # Message count
105
+ 0x1C , 0x00 , 0x00 , 0x00 , # Message offset
106
+ 0x24 , 0x00 , 0x00 , 0x00 , # Translation offset
107
+ 0x00 , 0x00 , 0x00 , 0x00 , # Hash table size
108
+ 0x2C , 0x00 , 0x00 , 0x00 , # Hash table offset
109
+ 0x03 , 0x00 , 0x00 , 0x00 , # 1st message length
110
+ 0x2C , 0x00 , 0x00 , 0x00 , # 1st message offset
111
+ 0x03 , 0x00 , 0x00 , 0x00 , # 1st trans length
112
+ 0xFF , 0xFF , 0xFF , 0xFF , # 1st trans offset (Modified to make it invalid)
113
+ 0x66 , 0x6F , 0x6F , 0x00 , # Message data
114
+ 0x62 , 0x61 , 0x72 , 0x00 , # Message data
115
+ ]))
90
116
91
117
UMO_DATA = b'''\
92
118
3hIElQAAAAADAAAAHAAAADQAAAAAAAAAAAAAAAAAAABMAAAABAAAAE0AAAAQAAAAUgAAAA8BAABj
113
139
MOFILE = os .path .join (LOCALEDIR , 'gettext.mo' )
114
140
MOFILE_BAD_MAJOR_VERSION = os .path .join (LOCALEDIR , 'gettext_bad_major_version.mo' )
115
141
MOFILE_BAD_MINOR_VERSION = os .path .join (LOCALEDIR , 'gettext_bad_minor_version.mo' )
142
+ MOFILE_CORRUPT = os .path .join (LOCALEDIR , 'gettext_corrupt.mo' )
116
143
UMOFILE = os .path .join (LOCALEDIR , 'ugettext.mo' )
117
144
MMOFILE = os .path .join (LOCALEDIR , 'metadata.mo' )
118
145
@@ -135,6 +162,8 @@ def setUpClass(cls):
135
162
fp .write (base64 .decodebytes (GNU_MO_DATA_BAD_MAJOR_VERSION ))
136
163
with open (MOFILE_BAD_MINOR_VERSION , 'wb' ) as fp :
137
164
fp .write (base64 .decodebytes (GNU_MO_DATA_BAD_MINOR_VERSION ))
165
+ with open (MOFILE_CORRUPT , 'wb' ) as fp :
166
+ fp .write (base64 .decodebytes (GNU_MO_DATA_CORRUPT ))
138
167
with open (UMOFILE , 'wb' ) as fp :
139
168
fp .write (base64 .decodebytes (UMO_DATA ))
140
169
with open (MMOFILE , 'wb' ) as fp :
@@ -264,6 +293,16 @@ def test_bad_minor_version(self):
264
293
# Check that no error is thrown with a bad minor version number
265
294
gettext .GNUTranslations (fp )
266
295
296
+ def test_corrupt_file (self ):
297
+ with open (MOFILE_CORRUPT , 'rb' ) as fp :
298
+ with self .assertRaises (OSError ) as cm :
299
+ gettext .GNUTranslations (fp )
300
+
301
+ exception = cm .exception
302
+ self .assertEqual (exception .errno , 0 )
303
+ self .assertEqual (exception .strerror , "File is corrupt" )
304
+ self .assertEqual (exception .filename , MOFILE_CORRUPT )
305
+
267
306
def test_some_translations (self ):
268
307
eq = self .assertEqual
269
308
# test some translations
0 commit comments