Coverage for /pythoncovmergedfiles/medio/medio/src/fuzzing/projects/pycups/fuzzer/fuzz_buffer_handling.py: 36%

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

42 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 

12#!/usr/bin/python3 

13import sys 

14import atheris 

15import cups 

16 

17def TestOneInput(data: bytes): 

18 fdp = atheris.FuzzedDataProvider(data) 

19 

20 buf_len = fdp.ConsumeIntInRange(0, 1024) 

21 buffer_obj = fdp.ConsumeBytes(buf_len) 

22 

23 MAX_LEN = 8192 

24 if fdp.ConsumeBool(): 

25 length = fdp.ConsumeIntInRange(0, len(buffer_obj)) if buffer_obj else 0 

26 else: 

27 length = fdp.ConsumeIntInRange(len(buffer_obj), MAX_LEN) 

28 

29 try: 

30 conn = cups.Connection() 

31 choice = fdp.ConsumeIntInRange(0, 3) 

32 

33 if choice == 0: 

34 # normal buffer+length 

35 conn.writeRequestData(buffer_obj, length) 

36 elif choice == 1: 

37 #empty buffer with non-zero length 

38 conn.writeRequestData(b"", length) 

39 elif choice == 2: 

40 # null/none input case 

41 try: 

42 conn.writeRequestData(None, length) 

43 except Exception: 

44 pass 

45 else: 

46 #negative length edge case 

47 negative_len = -fdp.ConsumeIntInRange(1, 100) 

48 conn.writeRequestData(buffer_obj, negative_len) 

49 

50 except Exception: 

51 pass 

52 

53def main(): 

54 atheris.instrument_all() 

55 atheris.Setup(sys.argv, TestOneInput, enable_python_coverage=True) 

56 atheris.Fuzz() 

57 

58if __name__ == "__main__": 

59 main()