3939bmsgd2luayAoaW4gIm15IG90aGVyIGNvbnRleHQiKQB3aW5rIHdpbmsA
4040'''
4141
42+ # .mo file with an invalid magic number
43+ GNU_MO_DATA_BAD_MAGIC_NUMBER = base64 .b64encode (b'ABCD' )
44+
4245# This data contains an invalid major version number (5)
4346# An unexpected major version number should be treated as an error when
4447# parsing a .mo file
111114
112115LOCALEDIR = os .path .join ('xx' , 'LC_MESSAGES' )
113116MOFILE = os .path .join (LOCALEDIR , 'gettext.mo' )
117+ MOFILE_BAD_MAGIC_NUMBER = os .path .join (LOCALEDIR , 'gettext_bad_magic_number.mo' )
114118MOFILE_BAD_MAJOR_VERSION = os .path .join (LOCALEDIR , 'gettext_bad_major_version.mo' )
115119MOFILE_BAD_MINOR_VERSION = os .path .join (LOCALEDIR , 'gettext_bad_minor_version.mo' )
116120UMOFILE = os .path .join (LOCALEDIR , 'ugettext.mo' )
@@ -131,6 +135,8 @@ def setUpClass(cls):
131135 os .makedirs (LOCALEDIR )
132136 with open (MOFILE , 'wb' ) as fp :
133137 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 ))
134140 with open (MOFILE_BAD_MAJOR_VERSION , 'wb' ) as fp :
135141 fp .write (base64 .decodebytes (GNU_MO_DATA_BAD_MAJOR_VERSION ))
136142 with open (MOFILE_BAD_MINOR_VERSION , 'wb' ) as fp :
@@ -249,6 +255,16 @@ def test_bindtextdomain(self):
249255 def test_textdomain (self ):
250256 self .assertEqual (gettext .textdomain (), 'gettext' )
251257
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+
252268 def test_bad_major_version (self ):
253269 with open (MOFILE_BAD_MAJOR_VERSION , 'rb' ) as fp :
254270 with self .assertRaises (OSError ) as cm :
0 commit comments