Selaa lähdekoodia

Extract and rename error message helper function for reuse

Extracts the `is_expected_error` helper function into a dedicated test
utility file that can be reused by other fuzz harnesses in
`fuzz-targets/`. Also renamed and better documented the function now
that it is shared.

The `test_utils.py` file deliberately does not include "fuzz" in the
file name to help distinguish it from the fuzz test files.
David Lakin 10 kuukautta sitten
vanhempi
commit
46f5f28b6a
2 muutettua tiedostoa jossa 23 lisäystä ja 9 poistoa
  1. 3 9
      fuzzing/fuzz-targets/fuzz_configfile.py
  2. 20 0
      fuzzing/fuzz-targets/test_utils.py

+ 3 - 9
fuzzing/fuzz-targets/fuzz_configfile.py

@@ -1,5 +1,6 @@
 import sys
 from io import BytesIO
+from test_utils import is_expected_exception
 
 import atheris
 
@@ -7,18 +8,11 @@ with atheris.instrument_imports():
     from dulwich.config import ConfigFile
 
 
-def is_expected_error(error_list, error_msg):
-    for error in error_list:
-        if error in error_msg:
-            return True
-    return False
-
-
 def TestOneInput(data):
     try:
         ConfigFile.from_file(BytesIO(data))
     except ValueError as e:
-        expected_errors = [
+        expected_exceptions = [
             "without section",
             "invalid variable name",
             "expected trailing ]",
@@ -27,7 +21,7 @@ def TestOneInput(data):
             "escape character",
             "missing end quote",
         ]
-        if is_expected_error(expected_errors, str(e)):
+        if is_expected_exception(expected_exceptions, e):
             return -1
         else:
             raise e

+ 20 - 0
fuzzing/fuzz-targets/test_utils.py

@@ -0,0 +1,20 @@
+import atheris  # pragma: no cover
+from typing import List  # pragma: no cover
+
+
+def is_expected_exception(
+    error_message_list: List[str], exception: Exception
+):  # pragma: no cover
+    """Checks if the message of a given exception matches any of the expected error messages.
+
+     Args:
+         error_message_list (List[str]): A list of error message substrings to check against the exception's message.
+         exception (Exception): The exception object raised during execution.
+
+    Returns:
+      bool: True if the exception's message contains any of the substrings from the error_message_list, otherwise False.
+    """
+    for error in error_message_list:
+        if error in str(exception):
+            return True
+    return False