90
90
ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
91
91
'''
92
92
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
+ ]))
93
119
94
120
UMO_DATA = b'''\
95
121
3hIElQAAAAADAAAAHAAAADQAAAAAAAAAAAAAAAAAAABMAAAABAAAAE0AAAAQAAAAUgAAAA8BAABj
117
143
MOFILE_BAD_MAGIC_NUMBER = os .path .join (LOCALEDIR , 'gettext_bad_magic_number.mo' )
118
144
MOFILE_BAD_MAJOR_VERSION = os .path .join (LOCALEDIR , 'gettext_bad_major_version.mo' )
119
145
MOFILE_BAD_MINOR_VERSION = os .path .join (LOCALEDIR , 'gettext_bad_minor_version.mo' )
146
+ MOFILE_CORRUPT = os .path .join (LOCALEDIR , 'gettext_corrupt.mo' )
120
147
UMOFILE = os .path .join (LOCALEDIR , 'ugettext.mo' )
121
148
MMOFILE = os .path .join (LOCALEDIR , 'metadata.mo' )
122
149
@@ -141,6 +168,8 @@ def setUpClass(cls):
141
168
fp .write (base64 .decodebytes (GNU_MO_DATA_BAD_MAJOR_VERSION ))
142
169
with open (MOFILE_BAD_MINOR_VERSION , 'wb' ) as fp :
143
170
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 ))
144
173
with open (UMOFILE , 'wb' ) as fp :
145
174
fp .write (base64 .decodebytes (UMO_DATA ))
146
175
with open (MMOFILE , 'wb' ) as fp :
@@ -280,6 +309,16 @@ def test_bad_minor_version(self):
280
309
# Check that no error is thrown with a bad minor version number
281
310
gettext .GNUTranslations (fp )
282
311
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
+
283
322
def test_some_translations (self ):
284
323
eq = self .assertEqual
285
324
# test some translations
0 commit comments