Coverage for /pythoncovmergedfiles/medio/medio/src/XlsxWriter/dev/fuzzing/fuzz_helpers.py: 43%
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
1import io
2import tempfile
3import atheris
4import contextlib
5from typing import List, Set, Dict, Tuple, Any
8class EnhancedFuzzedDataProvider(atheris.FuzzedDataProvider):
9 def ConsumeRandomBytes(self) -> bytes:
10 return self.ConsumeBytes(self.ConsumeIntInRange(0, self.remaining_bytes()))
12 def ConsumeRandomString(self) -> str:
13 return self.ConsumeUnicodeNoSurrogates(self.ConsumeIntInRange(0, self.remaining_bytes()))
15 def ConsumeRemainingString(self) -> str:
16 return self.ConsumeUnicodeNoSurrogates(self.remaining_bytes())
18 def ConsumeRemainingBytes(self) -> bytes:
19 return self.ConsumeBytes(self.remaining_bytes())
21 @contextlib.contextmanager
22 def ConsumeMemoryFile(self, all_data: bool = False, as_bytes: bool = True) -> io.BytesIO:
23 if all_data:
24 file_data = self.ConsumeRemainingBytes() if as_bytes else self.ConsumeRemainingString()
25 else:
26 file_data = self.ConsumeRandomBytes() if as_bytes else self.ConsumeRandomString()
28 file = io.BytesIO(file_data) if as_bytes else io.StringIO(file_data)
29 yield file
30 file.close()
32 @contextlib.contextmanager
33 def ConsumeTemporaryFile(self, suffix: str, all_data: bool = False, as_bytes: bool = True) -> str:
34 if all_data:
35 file_data = self.ConsumeRemainingBytes() if as_bytes else self.ConsumeRemainingString()
36 else:
37 file_data = self.ConsumeRandomBytes() if as_bytes else self.ConsumeRandomString()
39 mode = 'w+b' if as_bytes else 'w+'
40 tfile = tempfile.NamedTemporaryFile(mode=mode, suffix=suffix)
41 tfile.write(file_data)
42 tfile.seek(0)
43 tfile.flush()
44 yield tfile.name
45 tfile.close()