Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_json.py: 59%

41 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-09 06:08 +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 2022 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. 

26 

27import atheris 

28import sys 

29 

30with atheris.instrument_imports(): 

31 import flask 

32 from flask import Flask as _Flask 

33 from werkzeug.http import parse_set_header 

34 

35 

36class FuzzFlask(_Flask): 

37 testing = True 

38 secret_key = "fuzz test key" 

39 

40def TestOneInput(data): 

41 fdp = atheris.FuzzedDataProvider(data) 

42 app = FuzzFlask("flask_test", root_path=os.path.dirname(__file__)) 

43 app.config["DEBUG"] = True 

44 app.config["TRAP_BAD_REQUEST_ERRORS"] = False 

45 

46 @app.route("/json", methods=["POST"]) 

47 def post_json(): 

48 flask.request.get_json() 

49 return None 

50 

51 parse_set_header(fdp.ConsumeUnicode(fdp.ConsumeIntInRange(0, 512))) 

52 

53 client = app.test_client() 

54 

55 try: 

56 app.add_url_rule( 

57 fdp.ConsumeUnicode(fdp.ConsumeIntInRange(0, 512)), 

58 endpoint = "randomendpoint" 

59 ) 

60 except ValueError: 

61 None 

62 

63 try: 

64 client.post( 

65 "/json", 

66 data=fdp.ConsumeUnicode(fdp.ConsumeIntInRange(0, 512)), 

67 content_type="application/json" 

68 ) 

69 except (TypeError, UnicodeEncodeError): 

70 None 

71 

72 

73def main(): 

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

75 atheris.Fuzz() 

76 

77if __name__ == "__main__": 

78 main()