Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita126cef

Browse files
authored
gh-130655: Add a test for corrupt.mo files ingettext (#131911)
1 parent16a6270 commita126cef

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

‎Lib/test/test_gettext.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,32 @@
8888
ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
8989
'''
9090

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+
]))
91117

92118
UMO_DATA=b'''\
93119
3hIElQAAAAADAAAAHAAAADQAAAAAAAAAAAAAAAAAAABMAAAABAAAAE0AAAAQAAAAUgAAAA8BAABj
@@ -115,6 +141,7 @@
115141
MOFILE_BAD_MAGIC_NUMBER=os.path.join(LOCALEDIR,'gettext_bad_magic_number.mo')
116142
MOFILE_BAD_MAJOR_VERSION=os.path.join(LOCALEDIR,'gettext_bad_major_version.mo')
117143
MOFILE_BAD_MINOR_VERSION=os.path.join(LOCALEDIR,'gettext_bad_minor_version.mo')
144+
MOFILE_CORRUPT=os.path.join(LOCALEDIR,'gettext_corrupt.mo')
118145
UMOFILE=os.path.join(LOCALEDIR,'ugettext.mo')
119146
MMOFILE=os.path.join(LOCALEDIR,'metadata.mo')
120147

@@ -139,6 +166,8 @@ def setUpClass(cls):
139166
fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MAJOR_VERSION))
140167
withopen(MOFILE_BAD_MINOR_VERSION,'wb')asfp:
141168
fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MINOR_VERSION))
169+
withopen(MOFILE_CORRUPT,'wb')asfp:
170+
fp.write(base64.decodebytes(GNU_MO_DATA_CORRUPT))
142171
withopen(UMOFILE,'wb')asfp:
143172
fp.write(base64.decodebytes(UMO_DATA))
144173
withopen(MMOFILE,'wb')asfp:
@@ -254,6 +283,16 @@ def test_bad_minor_version(self):
254283
# Check that no error is thrown with a bad minor version number
255284
gettext.GNUTranslations(fp)
256285

286+
deftest_corrupt_file(self):
287+
withopen(MOFILE_CORRUPT,'rb')asfp:
288+
withself.assertRaises(OSError)ascm:
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+
257296
deftest_some_translations(self):
258297
eq=self.assertEqual
259298
# test some translations

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp