test_utils.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import atheris # pragma: no cover
  2. from typing import List # pragma: no cover
  3. def is_expected_exception(
  4. error_message_list: List[str], exception: Exception
  5. ): # pragma: no cover
  6. """Checks if the message of a given exception matches any of the expected error messages.
  7. Args:
  8. error_message_list (List[str]): A list of error message substrings to check against the exception's message.
  9. exception (Exception): The exception object raised during execution.
  10. Returns:
  11. bool: True if the exception's message contains any of the substrings from the error_message_list, otherwise False.
  12. """
  13. for error in error_message_list:
  14. if error in str(exception):
  15. return True
  16. return False
  17. class EnhancedFuzzedDataProvider(atheris.FuzzedDataProvider): # pragma: no cover
  18. """Extends atheris.FuzzedDataProvider to offer additional methods to make fuzz testing slightly more DRY."""
  19. def __init__(self, data):
  20. """Initializes the EnhancedFuzzedDataProvider with fuzzing data from the argument provided to TestOneInput.
  21. Args:
  22. data (bytes): The binary data used for fuzzing.
  23. """
  24. super().__init__(data)
  25. def ConsumeRemainingBytes(self) -> bytes:
  26. """Consume the remaining bytes in the bytes container.
  27. Returns:
  28. bytes: Zero or more bytes.
  29. """
  30. return self.ConsumeBytes(self.remaining_bytes())
  31. def ConsumeRandomBytes(self, max_length=None) -> bytes:
  32. """Consume a random count of bytes from the bytes container.
  33. Args:
  34. max_length (int, optional): The maximum length of the string. Defaults to the number of remaining bytes.
  35. Returns:
  36. bytes: Zero or more bytes.
  37. """
  38. if max_length is None:
  39. max_length = self.remaining_bytes()
  40. else:
  41. max_length = min(max_length, self.remaining_bytes())
  42. return self.ConsumeBytes(self.ConsumeIntInRange(0, max_length))
  43. def ConsumeRandomString(self, max_length=None) -> str:
  44. """Consume bytes to produce a Unicode string.
  45. Args:
  46. max_length (int, optional): The maximum length of the string. Defaults to the number of remaining bytes.
  47. Returns:
  48. str: A Unicode string.
  49. """
  50. if max_length is None:
  51. max_length = self.remaining_bytes()
  52. else:
  53. max_length = min(max_length, self.remaining_bytes())
  54. return self.ConsumeUnicode(self.ConsumeIntInRange(0, max_length))