def testDefaultParseValueFuzz(self, value):
try:
result = parser.DefaultParseValue(value)
except TypeError:
# It's OK to get a TypeError if the string has the null character.
if u'\x00' in value:
return
raise
except MemoryError:
if len(value) > 100:
# This is not what we're testing.
return
raise
try:
uvalue = unicode(value)
uresult = unicode(result)
except UnicodeDecodeError:
# This is not what we're testing.
return
# Check that the parsed value doesn't differ too much from the input.
distance = Levenshtein.distance(uresult, uvalue)
max_distance = (
2 + # Quotes or parenthesis can be implicit.
sum(c.isspace() for c in value) +
value.count('"') + value.count("'") +
3 * (value.count(',') + 1) + # 'a,' can expand to "'a', "
3 * (value.count(':')) + # 'a:' can expand to "'a': "
2 * value.count('\\'))
if '#' in value:
max_distance += len(value) - value.index('#')
if not isinstance(result, six.string_types):
max_distance += value.count('0') # Leading 0s are stripped.
# Note: We don't check distance for dicts since item order can be changed.
if '{' not in value:
self.assertLessEqual(distance, max_distance,
(distance, max_distance, uvalue, uresult))
评论列表
文章目录