Coverage for /pythoncovmergedfiles/medio/medio/src/jsonpickle/fuzzing/fuzz-targets/fuzz_unpickle.py: 24%

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

34 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 

12import os 

13import sys 

14from json import JSONDecodeError 

15 

16import atheris 

17from utils import is_expected_error 

18 

19with atheris.instrument_imports(): 

20 import jsonpickle 

21 

22# Keys are filenames and values are lists of tuples. 

23# Each tuple contains a substring of an expected exception message and a line number 

24# (or -1 to match any line number). 

25expected_errors = { 

26 "jsonpickle/unpickler.py": [ 

27 ("object has no attribute 'split'", 283), 

28 ("not enough values to unpack", 283), 

29 ("object has no attribute 'split'", 197), 

30 ("object has no attribute 'split'", 307), 

31 ("object has no attribute 'encode'", 404), 

32 ("object has no attribute 'encode'", 407), 

33 ("object has no attribute 'encode'", 431), 

34 ("object has no attribute 'encode'", 434), 

35 ("object is not iterable", 448), 

36 ("list indices must be integers or slices", 539), 

37 ("object is not iterable", 448), 

38 ("object is not iterable", 475), 

39 ("object is not iterable", 481), 

40 ("object is not iterable", 508), 

41 ("object is not iterable", 841), 

42 ("object is not iterable", 808), 

43 ("object is not iterable", 859), 

44 ("object is not iterable", 881), 

45 ("object is not iterable", 892), 

46 ("unhashable type", 407), 

47 ("unhashable type", 841), 

48 ("object argument after * must be an iterable", 493), 

49 ("object argument after * must be an iterable", 520), 

50 ("list index out of range", 160), 

51 ("'utf-8' codec can't encode character", 407), 

52 ("too many values to unpack", 283), 

53 ("too many values to unpack", 511), 

54 ("object is not callable", 493), 

55 ("object is not callable", 520), 

56 ], 

57 "base64.py": [ 

58 ("bad base85 character at", -1), 

59 ("base85 overflow in hunk", -1), 

60 ("Invalid base64-encoded string", -1), 

61 ("Incorrect padding", -1), 

62 ], 

63} 

64 

65debug_mode = os.environ.get("JSONPICKLE_DEBUG_OSSFUZZ", "") == "1" 

66 

67 

68def TestOneInput(data): 

69 fdp = atheris.FuzzedDataProvider(data) 

70 fuzz_string = fdp.ConsumeUnicodeNoSurrogates( 

71 fdp.ConsumeIntInRange(0, fdp.remaining_bytes()) 

72 ) 

73 

74 try: 

75 jsonpickle.unpickler.decode(fuzz_string) 

76 except (RecursionError, JSONDecodeError): 

77 return -1 

78 except Exception as e: 

79 if is_expected_error(e, expected_errors): 

80 return 0 

81 raise 

82 

83 

84def main(): 

85 atheris.Setup(sys.argv, TestOneInput) 

86 atheris.Fuzz() 

87 

88 

89if __name__ == "__main__": 

90 main()