Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_mgmt_serialization.py: 56%

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

41 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 

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 sys 

27import json 

28import atheris 

29 

30from azure.mgmt.dynatrace import _serialization 

31from azure.mgmt.dynatrace.models import _models_py3 

32from azure.core.exceptions import ( 

33 DeserializationError, 

34 SerializationError 

35) 

36 

37 

38def TestOneInput(data): 

39 fdp = atheris.FuzzedDataProvider(data) 

40 

41 try: 

42 payload = json.loads(fdp.ConsumeUnicodeNoSurrogates(fdp.ConsumeIntInRange(0, 1024))) 

43 except: 

44 return 

45 model = _models_py3.AccountInfo() 

46 

47 # Overwrite the types, this is easier than creating a new class. 

48 model._attribute_map = payload 

49 

50 try: 

51 serialized_data = model.serialize() 

52 serialized = True 

53 except SerializationError: 

54 serialized = False 

55 

56 deserializer = _serialization.Deserializer() 

57 if serialized: 

58 # Anything serialized should be unserializable 

59 deserializer._deserialize(model, serialized_data) 

60 else: 

61 # Otherwise we deserialize random data and catch exceptions 

62 try: 

63 deserializer._deserialize( 

64 model, 

65 fdp.ConsumeUnicodeNoSurrogates(fdp.ConsumeIntInRange(0, 1024)) 

66 ) 

67 except DeserializationError: 

68 pass 

69 

70 

71def main(): 

72 atheris.instrument_all() 

73 atheris.Setup(sys.argv, TestOneInput) 

74 atheris.Fuzz() 

75 

76 

77if __name__ == "__main__": 

78 main()