Coverage for /pythoncovmergedfiles/medio/medio/src/dulwich/fuzzing/fuzz-targets/test_utils.py: 100%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from typing import List # pragma: no cover
3import atheris # pragma: no cover
6@atheris.instrument_func
7def is_expected_exception(
8 error_message_list: List[str], exception: Exception
9): # pragma: no cover
10 """Checks if the message of a given exception matches any of the expected error messages.
12 Args:
13 error_message_list (List[str]): A list of error message substrings to check against the exception's message.
14 exception (Exception): The exception object raised during execution.
16 Returns:
17 bool: True if the exception's message contains any of the substrings from the error_message_list, otherwise False.
18 """
19 for error in error_message_list:
20 if error in str(exception):
21 return True
22 return False
25class EnhancedFuzzedDataProvider(atheris.FuzzedDataProvider): # pragma: no cover
26 """Extends atheris.FuzzedDataProvider to offer additional methods to make fuzz testing slightly more DRY."""
28 def __init__(self, data):
29 """Initializes the EnhancedFuzzedDataProvider with fuzzing data from the argument provided to TestOneInput.
31 Args:
32 data (bytes): The binary data used for fuzzing.
33 """
34 super().__init__(data)
36 def ConsumeRemainingBytes(self) -> bytes:
37 """Consume the remaining bytes in the bytes container.
39 Returns:
40 bytes: Zero or more bytes.
41 """
42 return self.ConsumeBytes(self.remaining_bytes())
44 def ConsumeRandomBytes(self, max_length=None) -> bytes:
45 """Consume a random count of bytes from the bytes container.
47 Args:
48 max_length (int, optional): The maximum length of the string. Defaults to the number of remaining bytes.
50 Returns:
51 bytes: Zero or more bytes.
52 """
53 if max_length is None:
54 max_length = self.remaining_bytes()
55 else:
56 max_length = min(max_length, self.remaining_bytes())
58 return self.ConsumeBytes(self.ConsumeIntInRange(0, max_length))
60 def ConsumeRandomString(self, max_length=None, without_surrogates=False) -> str:
61 """Consume bytes to produce a Unicode string.
63 Args:
64 max_length (int, optional): The maximum length of the string. Defaults to the number of remaining bytes.
65 without_surrogates (bool, optional): If True, never generate surrogate pair characters. Defaults to False.
67 Returns:
68 str: A Unicode string.
69 """
70 if max_length is None:
71 max_length = self.remaining_bytes()
72 else:
73 max_length = min(max_length, self.remaining_bytes())
75 count = self.ConsumeIntInRange(0, max_length)
77 if without_surrogates:
78 return self.ConsumeUnicodeNoSurrogates(count)
79 else:
80 return self.ConsumeUnicode(count)
82 def ConsumeRandomInt(self, minimum=0, maximum=1234567890) -> int:
83 """Consume bytes to produce an integer.
85 Args:
86 minimum (int, optional): The minimum value of the integer. Defaults to 0.
87 maximum (int, optional): The maximum value of the integer. Defaults to 1234567890.
89 Returns:
90 int: An integer.
91 """
92 return self.ConsumeIntInRange(minimum, maximum)