Coverage for /pythoncovmergedfiles/medio/medio/src/XlsxWriter/dev/fuzzing/xlsx_fuzzer.py: 50%

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

50 statements  

1###### Coverage stub 

2import atexit 

3import coverage 

4cov = coverage.coverage(data_file='.coverage', cover_pylib=True) 

5cov.start() 

6# Register an exist handler that will print coverage 

7def exit_handler(): 

8 cov.stop() 

9 cov.save() 

10atexit.register(exit_handler) 

11####### End of coverage stub 

12from enum import Enum 

13 

14import atheris 

15import sys 

16from io import BytesIO 

17 

18from fuzz_helpers import EnhancedFuzzedDataProvider 

19import struct 

20 

21with atheris.instrument_imports(include=['xlsxwriter']): 

22 import xlsxwriter 

23 import xlsxwriter.worksheet 

24 from xlsxwriter.exceptions import XlsxWriterException 

25 

26 

27class FuncChoice(Enum): 

28 WRITE_STRING = 0 

29 WRITE_NUMBER = 1 

30 WRITE_FORMULA = 2 

31 

32 

33choices = [FuncChoice.WRITE_STRING, FuncChoice.WRITE_NUMBER, FuncChoice.WRITE_FORMULA] 

34 

35 

36def TestOneInput(data): 

37 fdp = EnhancedFuzzedDataProvider(data) 

38 

39 try: 

40 out = BytesIO() 

41 with xlsxwriter.Workbook(out) as wb: 

42 ws = wb.add_worksheet() 

43 

44 data = fdp.ConsumeRandomString() 

45 func_choice = fdp.PickValueInList(choices) 

46 

47 for row in range(fdp.ConsumeIntInRange(0, 10)): 

48 for col in range(fdp.ConsumeIntInRange(0, 10)): 

49 if func_choice is FuncChoice.WRITE_STRING: 

50 ws.write_string(row, col, data) 

51 elif func_choice is FuncChoice.WRITE_NUMBER: 

52 ws.write_number(row, col, data) 

53 else: 

54 ws.write_formula(row, col, data) 

55 except (XlsxWriterException, struct.error): 

56 return -1 

57 except TypeError as e: 

58 if 'must be real number' in str(e): 

59 return -1 

60 raise e 

61 

62 

63def main(): 

64 atheris.Setup(sys.argv, TestOneInput) 

65 atheris.Fuzz() 

66 

67 

68if __name__ == "__main__": 

69 main()