Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_md.py: 80%

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

114 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 atheris 

28 

29import json 

30import pickle 

31import multidict 

32 

33 

34def run_operation(md, fdp, operation): 

35 if operation == 1: 

36 try: 

37 md.popall(fdp.ConsumeUnicodeNoSurrogates(24)) 

38 except KeyError: 

39 pass 

40 elif operation == 2: 

41 try: 

42 md.popitem() 

43 except KeyError: 

44 pass 

45 elif operation == 3: 

46 try: 

47 md.copy() 

48 except KeyError: 

49 pass 

50 elif operation == 4: 

51 try: 

52 md.__delitem__(fdp.ConsumeUnicodeNoSurrogates(24)) 

53 except KeyError: 

54 pass 

55 elif operation == 5: 

56 md[fdp.ConsumeUnicodeNoSurrogates(24)] = fdp.ConsumeUnicodeNoSurrogates( 

57 24) 

58 MP = multidict._multidict_py.MultiDictProxy(md) 

59 MP.copy() 

60 elif operation == 6: 

61 try: 

62 copymd = md.copy() 

63 is_eq = copymd == md 

64 copymd['a'] = 2 

65 is_eq = copymd == md 

66 except KeyError: 

67 pass 

68 elif operation == 7: 

69 try: 

70 md.popone(fdp.ConsumeUnicodeNoSurrogates(24)) 

71 except KeyError: 

72 pass 

73 elif operation == 8: 

74 try: 

75 copymd = md.copy() 

76 copymd['b'] = 'c' 

77 md.update(copymd) 

78 except KeyError: 

79 pass 

80 elif operation == 9: 

81 c2 = multidict._multidict_py.CIMultiDict(md.copy()) 

82 elif operation == 10: 

83 try: 

84 md.getall(fdp.ConsumeUnicodeNoSurrogates(24)) 

85 except KeyError: 

86 pass 

87 elif operation == 11: 

88 try: 

89 md.getone(fdp.ConsumeUnicodeNoSurrogates(24)) 

90 except KeyError: 

91 pass 

92 elif operation == 12: 

93 for elem in md: 

94 pass 

95 elif operation == 13: 

96 len(md) 

97 elif operation == 14: 

98 value_view = md.values() 

99 elif operation == 15: 

100 representation = str(md) 

101 

102def TestOneInput(data): 

103 fdp = atheris.FuzzedDataProvider(data) 

104 try: 

105 random_dict = json.loads(fdp.ConsumeUnicodeNoSurrogates(fdp.ConsumeIntInRange(0, 1024))) 

106 except: 

107 return 

108 if not isinstance(random_dict, dict): 

109 return 

110 

111 md = multidict._multidict_py.MultiDict(random_dict) 

112 r = pickle.loads(pickle.dumps(md)) 

113 

114 md = multidict._multidict_py.MultiDict(random_dict) 

115 # Run one of all operations 

116 run_operation(md, fdp, 1) 

117 run_operation(md, fdp, 2) 

118 run_operation(md, fdp, 3) 

119 run_operation(md, fdp, 4) 

120 run_operation(md, fdp, 5) 

121 run_operation(md, fdp, 6) 

122 run_operation(md, fdp, 7) 

123 run_operation(md, fdp, 8) 

124 run_operation(md, fdp, 9) 

125 run_operation(md, fdp, 10) 

126 run_operation(md, fdp, 11) 

127 run_operation(md, fdp, 12) 

128 run_operation(md, fdp, 13) 

129 run_operation(md, fdp, 14) 

130 run_operation(md, fdp, 15) 

131 

132 # Run a random sequence of operations 

133 for idx in range(30): 

134 operation = fdp.ConsumeIntInRange(1, 15) 

135 run_operation(md, fdp, operation) 

136 

137 

138def main(): 

139 atheris.instrument_all() 

140 atheris.Setup(sys.argv, TestOneInput) 

141 atheris.Fuzz() 

142 

143 

144if __name__ == "__main__": 

145 main()