Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_http.py: 41%

59 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:56 +0000

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 

13# Copyright 2023 Google LLC 

14# 

15# Licensed under the Apache License, Version 2.0 (the "License"); 

16# you may not use this file except in compliance with the License. 

17# You may obtain a copy of the License at 

18# 

19# http://www.apache.org/licenses/LICENSE-2.0 

20# 

21# Unless required by applicable law or agreed to in writing, software 

22# distributed under the License is distributed on an "AS IS" BASIS, 

23# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

24# See the License for the specific language governing permissions and 

25# limitations under the License. 

26import io 

27import sys 

28import atheris 

29import pathlib 

30 

31from fsspec.implementations import http 

32import fsspec 

33from fsspec.exceptions import FSTimeoutError 

34 

35# Import aiohttp and requests for pyinstaller 

36from requests import * 

37from aiohttp import * 

38from aiohttp.client_exceptions import ClientError 

39 

40 

41@atheris.instrument_func 

42def TestOneInput(data): 

43 fdp = atheris.FuzzedDataProvider(data) 

44 

45 path = fdp.ConsumeUnicodeNoSurrogates(124) 

46 

47 # Ensure it's a file vs directory 

48 path = path + "the_file.txt" 

49 h = fsspec.filesystem("http", use_listings_cache=True) 

50 tmp_path = pathlib.Path(path) 

51 try: 

52 tmp_path.write_bytes(data) 

53 except: 

54 # Don't care about errors in pathlib 

55 try: 

56 tmp_path.unlink() 

57 except: 

58 pass 

59 return 

60 

61 try: 

62 h.put_file(tmp_path, path, method="put", timeout=0.5) 

63 except FSTimeoutError: 

64 try: 

65 tmp_path.unlink() 

66 except: 

67 pass 

68 return 

69 except ( 

70 ClientError, 

71 TypeError, 

72 AssertionError 

73 ) as e: 

74 # Abandon if aiohttp threw an error. 

75 try: 

76 tmp_path.unlink() 

77 except: 

78 pass 

79 return 

80 

81 # Reading the file should be possible now. 

82 with h.open(path) as http_f: 

83 http_f.read() 

84 

85 try: 

86 tmp_path.unlink() 

87 except: 

88 pass 

89 

90 

91def main(): 

92 atheris.instrument_all() 

93 atheris.Setup(sys.argv, TestOneInput) 

94 atheris.Fuzz() 

95 

96 

97if __name__ == "__main__": 

98 main()