def test_fuzz(self): random.seed(42) for i in range(100): # parse random file with errors disabled s = random_ini_file() c = ini.INIConfig(parse_exc=False) c._readfp(StringIO(s)) # check that file is preserved, except for # commenting out erroneous lines l1 = s.split('\n') l2 = str(c).split('\n') self.assertEqual(len(l1), len(l2)) good_lines = [] for i in range(len(l1)): try: self.assertEqual(l1[i], l2[i]) good_lines.append(l1[i]) except AssertionError: self.assertEqual('#' + l1[i], l2[i]) # parse the good subset of the file # using ConfigParser s = '\n'.join(good_lines) cc = compat.RawConfigParser() cc.readfp(StringIO(s)) cc_py = ConfigParser.RawConfigParser() cc_py.readfp(StringIO(s)) # compare the two configparsers self.assertEqualSorted(cc_py.sections(), cc.sections()) self.assertEqualSorted(cc_py.defaults().items(), cc.defaults().items()) for sec in cc_py.sections(): self.assertEqualSorted(cc_py.items(sec), cc.items(sec))
def test_fuzz(self): random.seed(42) try: num_iter = int(os.environ['INIPARSE_FUZZ_ITERATIONS']) except (KeyError, ValueError): num_iter = 100 for fuzz_iter in range(num_iter): try: # parse random file with errors disabled s = random_ini_file() c = ini.INIConfig(parse_exc=False) c._readfp(StringIO(s)) # check that file is preserved, except for # commenting out erroneous lines l1 = s.split('\n') l2 = str(c).split('\n') self.assertEqual(len(l1), len(l2)) good_lines = [] for i in range(len(l1)): try: self.assertEqual(l1[i], l2[i]) good_lines.append(l1[i]) except AssertionError: self.assertEqual('#'+l1[i], l2[i]) # parse the good subset of the file # using ConfigParser s = '\n'.join(good_lines) cc = compat.RawConfigParser() cc.readfp(StringIO(s)) cc_py = configparser.RawConfigParser() cc_py.readfp(StringIO(s)) # compare the two configparsers self.assertEqualConfig(cc_py, cc) # check that tidy does not change semantics tidy(cc) cc_tidy = configparser.RawConfigParser() cc_tidy.readfp(StringIO(str(cc.data))) self.assertEqualConfig(cc_py, cc_tidy) except AssertionError: fname = 'fuzz-test-iter-%d.ini' % fuzz_iter print('Fuzz test failed at iteration', fuzz_iter) print('Writing out failing INI file as', fname) f = open(fname, 'w') f.write(s) f.close() raise
@@ -1,9 +1,10 @@ import re import os import random +import sys import unittest -import ConfigParser -from StringIO import StringIO +from six import StringIO +from six.moves import configparser from iniparse import compat, ini, tidy # TODO: @@ -96,24 +97,25 @@ class test_fuzz(unittest.TestCase): s = '\n'.join(good_lines) cc = compat.RawConfigParser() cc.readfp(StringIO(s)) - cc_py = ConfigParser.RawConfigParser() + cc_py = configparser.RawConfigParser() cc_py.readfp(StringIO(s)) # compare the two configparsers self.assertEqualConfig(cc_py, cc) # check that tidy does not change semantics tidy(cc) - cc_tidy = ConfigParser.RawConfigParser() + cc_tidy = configparser.RawConfigParser() cc_tidy.readfp(StringIO(str(cc.data))) self.assertEqualConfig(cc_py, cc_tidy) except AssertionError: fname = 'fuzz-test-iter-%d.ini' % fuzz_iter - print 'Fuzz test failed at iteration', fuzz_iter