diff --git a/fuzzing/dateparser_fuzzer.py b/fuzzing/dateparser_fuzzer.py index d7c441840..b8ca8bf12 100644 --- a/fuzzing/dateparser_fuzzer.py +++ b/fuzzing/dateparser_fuzzer.py @@ -1,29 +1,63 @@ +import sys from typing import List import atheris -import sys - from fuzz_helpers import EnhancedFuzzedDataProvider with atheris.instrument_imports(): import dateparser -import dateparser.data -import dateparser.parser +import re import pytz -import re + +import dateparser.data +import dateparser.parser language_codes = dateparser.data.languages_info.language_order -directives = ["%a", "%A", "%w", "%d", "%b", "%B", "%m", "%y", "%Y", "%H", "%I", "%p", "%M", - "%S", "%f", "%z", "%Z", "%j", "%U", "%W", "%c", "%x", "%X", "%%", "%G", "%u", - "%V", "%:Z"] +directives = [ + "%a", + "%A", + "%w", + "%d", + "%b", + "%B", + "%m", + "%y", + "%Y", + "%H", + "%I", + "%p", + "%M", + "%S", + "%f", + "%z", + "%Z", + "%j", + "%U", + "%W", + "%c", + "%x", + "%X", + "%%", + "%G", + "%u", + "%V", + "%:Z", +] locale_codes = ["fr-PF", "qu-EC", "af-NA"] date_order = list(dateparser.parser.date_order_chart.keys()) timezone = list(pytz.all_timezones) preferred_date = ["last", "first", "current"] preferred_dates_from = ["past", "future", "current_period"] -parsers = ["timestamp", "negative-timestamp", "relative-time", "custom-formats", "absolute-time", "no-spaces-time"] +parsers = [ + "timestamp", + "negative-timestamp", + "relative-time", + "custom-formats", + "absolute-time", + "no-spaces-time", +] def _get_format_strings(fdp: EnhancedFuzzedDataProvider) -> List[str]: @@ -48,7 +82,9 @@ def TestOneInput(data): "RELATIVE_BASE": fdp.ConsumeDate(), "STRICT_PARSING": fdp.ConsumeBool(), "REQUIRE_PARTS": [], - "SKIP_TOKENS": [fdp.ConsumeRandomString() for _ in range(fdp.ConsumeIntInRange(0, 3))], + "SKIP_TOKENS": [ + fdp.ConsumeRandomString() for _ in range(fdp.ConsumeIntInRange(0, 3)) + ], "NORMALIZE": fdp.ConsumeBool(), "RETURN_TIME_AS_PERIOD": fdp.ConsumeBool(), "PARSERS": fdp.ConsumeSublist(parsers), @@ -63,7 +99,7 @@ def TestOneInput(data): languages=fdp.ConsumeSublist(language_codes), locales=fdp.ConsumeSublist(locale_codes), region=fdp.ConsumeString(2), - settings=settings + settings=settings, ) except re.error: return -1 diff --git a/fuzzing/fuzz_helpers.py b/fuzzing/fuzz_helpers.py index ce55bcbe4..1ed057131 100644 --- a/fuzzing/fuzz_helpers.py +++ b/fuzzing/fuzz_helpers.py @@ -1,12 +1,12 @@ +import contextlib +import datetime import io import tempfile -import datetime +from typing import List, TypeVar import atheris -import contextlib -from typing import TypeVar, List -T = TypeVar('T') +T = TypeVar("T") class EnhancedFuzzedDataProvider(atheris.FuzzedDataProvider): @@ -14,7 +14,9 @@ def ConsumeRandomBytes(self) -> bytes: return self.ConsumeBytes(self.ConsumeIntInRange(0, self.remaining_bytes())) def ConsumeRandomString(self) -> str: - return self.ConsumeUnicodeNoSurrogates(self.ConsumeIntInRange(0, self.remaining_bytes())) + return self.ConsumeUnicodeNoSurrogates( + self.ConsumeIntInRange(0, self.remaining_bytes()) + ) def ConsumeRemainingString(self) -> str: return self.ConsumeUnicodeNoSurrogates(self.remaining_bytes()) @@ -42,24 +44,40 @@ def ConsumeDate(self) -> datetime.datetime: return datetime.datetime(year=1970, month=1, day=1) @contextlib.contextmanager - def ConsumeMemoryFile(self, all_data: bool = False, as_bytes: bool = True) -> io.BytesIO: + def ConsumeMemoryFile( + self, all_data: bool = False, as_bytes: bool = True + ) -> io.BytesIO: if all_data: - file_data = self.ConsumeRemainingBytes() if as_bytes else self.ConsumeRemainingString() + file_data = ( + self.ConsumeRemainingBytes() + if as_bytes + else self.ConsumeRemainingString() + ) else: - file_data = self.ConsumeRandomBytes() if as_bytes else self.ConsumeRandomString() + file_data = ( + self.ConsumeRandomBytes() if as_bytes else self.ConsumeRandomString() + ) file = io.BytesIO(file_data) if as_bytes else io.StringIO(file_data) yield file file.close() @contextlib.contextmanager - def ConsumeTemporaryFile(self, suffix: str, all_data: bool = False, as_bytes: bool = True) -> str: + def ConsumeTemporaryFile( + self, suffix: str, all_data: bool = False, as_bytes: bool = True + ) -> str: if all_data: - file_data = self.ConsumeRemainingBytes() if as_bytes else self.ConsumeRemainingString() + file_data = ( + self.ConsumeRemainingBytes() + if as_bytes + else self.ConsumeRemainingString() + ) else: - file_data = self.ConsumeRandomBytes() if as_bytes else self.ConsumeRandomString() + file_data = ( + self.ConsumeRandomBytes() if as_bytes else self.ConsumeRandomString() + ) - mode = 'w+b' if as_bytes else 'w+' + mode = "w+b" if as_bytes else "w+" tfile = tempfile.NamedTemporaryFile(mode=mode, suffix=suffix) tfile.write(file_data) tfile.seek(0)