39
39
bmsgd2luayAoaW4gIm15IG90aGVyIGNvbnRleHQiKQB3aW5rIHdpbmsA
40
40
'''
41
41
42
+ # .mo file with an invalid magic number
43
+ GNU_MO_DATA_BAD_MAGIC_NUMBER = base64 .b64encode (b'ABCD' )
44
+
42
45
# This data contains an invalid major version number (5)
43
46
# An unexpected major version number should be treated as an error when
44
47
# parsing a .mo file
111
114
112
115
LOCALEDIR = os .path .join ('xx' , 'LC_MESSAGES' )
113
116
MOFILE = os .path .join (LOCALEDIR , 'gettext.mo' )
117
+ MOFILE_BAD_MAGIC_NUMBER = os .path .join (LOCALEDIR , 'gettext_bad_magic_number.mo' )
114
118
MOFILE_BAD_MAJOR_VERSION = os .path .join (LOCALEDIR , 'gettext_bad_major_version.mo' )
115
119
MOFILE_BAD_MINOR_VERSION = os .path .join (LOCALEDIR , 'gettext_bad_minor_version.mo' )
116
120
UMOFILE = os .path .join (LOCALEDIR , 'ugettext.mo' )
@@ -131,6 +135,8 @@ def setUpClass(cls):
131
135
os .makedirs (LOCALEDIR )
132
136
with open (MOFILE , 'wb' ) as fp :
133
137
fp .write (base64 .decodebytes (GNU_MO_DATA ))
138
+ with open (MOFILE_BAD_MAGIC_NUMBER , 'wb' ) as fp :
139
+ fp .write (base64 .decodebytes (GNU_MO_DATA_BAD_MAGIC_NUMBER ))
134
140
with open (MOFILE_BAD_MAJOR_VERSION , 'wb' ) as fp :
135
141
fp .write (base64 .decodebytes (GNU_MO_DATA_BAD_MAJOR_VERSION ))
136
142
with open (MOFILE_BAD_MINOR_VERSION , 'wb' ) as fp :
@@ -249,6 +255,16 @@ def test_bindtextdomain(self):
249
255
def test_textdomain (self ):
250
256
self .assertEqual (gettext .textdomain (), 'gettext' )
251
257
258
+ def test_bad_magic_number (self ):
259
+ with open (MOFILE_BAD_MAGIC_NUMBER , 'rb' ) as fp :
260
+ with self .assertRaises (OSError ) as cm :
261
+ gettext .GNUTranslations (fp )
262
+
263
+ exception = cm .exception
264
+ self .assertEqual (exception .errno , 0 )
265
+ self .assertEqual (exception .strerror , "Bad magic number" )
266
+ self .assertEqual (exception .filename , MOFILE_BAD_MAGIC_NUMBER )
267
+
252
268
def test_bad_major_version (self ):
253
269
with open (MOFILE_BAD_MAJOR_VERSION , 'rb' ) as fp :
254
270
with self .assertRaises (OSError ) as cm :
0 commit comments