Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_tabulate.py: 69%

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

65 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 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 

27 

28import sys 

29import json 

30import atheris 

31import tabulate 

32 

33 

34def ConsumeListofLists(fdp): 

35 list = [] 

36 num_lists = fdp.ConsumeIntInRange(1, 100) 

37 for _ in range(num_lists): 

38 list.append(fdp.ConsumeFloatListInRange(num_lists, 1, 1000)) 

39 return list 

40 

41 

42def ConsumeDictionary(fdp): 

43 dictionary = {} 

44 dict_size = fdp.ConsumeIntInRange(1, 100) 

45 for _ in range(dict_size): 

46 dictionary[fdp.ConsumeUnicodeNoSurrogates( 

47 fdp.ConsumeIntInRange(0, 100))] = fdp.ConsumeUnicodeNoSurrogates(fdp.ConsumeIntInRange(0, 100)) 

48 return dictionary 

49 

50 

51def ConsumeNestedDictionary(fdp): 

52 dictionary = {} 

53 dict_size = fdp.ConsumeIntInRange(1, 100) 

54 for _ in range(dict_size): 

55 t_or_f = fdp.ConsumeBool() 

56 if t_or_f is True: 

57 dictionary[fdp.ConsumeUnicodeNoSurrogates( 

58 fdp.ConsumeIntInRange(0, 100))] = ConsumeDictionary(fdp) 

59 else: 

60 dictionary[fdp.ConsumeUnicodeNoSurrogates( 

61 fdp.ConsumeIntInRange(0, 100))] = fdp.ConsumeUnicodeNoSurrogates( 

62 fdp.ConsumeIntInRange(0, 100)) 

63 return dictionary 

64 

65 

66def ConsumeDictionaryWithList(fdp): 

67 dictionary = {} 

68 dict_size = fdp.ConsumeIntInRange(1, 100) 

69 for _ in range(dict_size): 

70 dictionary[fdp.ConsumeUnicodeNoSurrogates( 

71 fdp.ConsumeIntInRange(1, 100))] = fdp.ConsumeIntListInRange( 

72 fdp.ConsumeIntInRange(1, 100), 1, 100) 

73 return dictionary 

74 

75 

76def TestOneInput(data): 

77 fdp = atheris.FuzzedDataProvider(data) 

78 table_format = fdp.PickValueInList(tabulate.tabulate_formats) 

79 col_align_num = fdp.PickValueInList( 

80 ["right", "center", "left", "decimal", None]) 

81 col_align_str = fdp.PickValueInList( 

82 ["right", "center", "left", None]) 

83 

84 # Create random dictionary 

85 try: 

86 fuzzed_dict = json.loads( 

87 fdp.ConsumeUnicodeNoSurrogates(fdp.ConsumeIntInRange(0, 100))) 

88 except: 

89 return 

90 if type(fuzzed_dict) is not dict: 

91 return 

92 

93 # Test tabulate with various inputs 

94 t1 = tabulate.tabulate( 

95 fuzzed_dict, 

96 tablefmt=table_format 

97 ) 

98 t2 = tabulate.tabulate( 

99 ConsumeDictionary(fdp), 

100 tablefmt=table_format, 

101 headers="keys" 

102 ) 

103 t3 = tabulate.tabulate( 

104 ConsumeListofLists(fdp), 

105 tablefmt=table_format, 

106 headers="firstrow" 

107 ) 

108 t4 = tabulate.tabulate( 

109 ConsumeNestedDictionary(fdp), 

110 tablefmt=table_format 

111 ) 

112 t5 = tabulate.tabulate( 

113 ConsumeDictionaryWithList(fdp), 

114 tablefmt=table_format 

115 ) 

116 almost_all_args = tabulate.tabulate( 

117 ConsumeListofLists(fdp), 

118 tablefmt=table_format, 

119 headers="firstrow", 

120 showindex="always", 

121 numalign=col_align_num, 

122 stralign=col_align_str, 

123 floatfmt=".4f" 

124 ) 

125 

126 # make and implement custom table formator 

127 custom_separator = tabulate.simple_separated_format( 

128 fdp.ConsumeUnicodeNoSurrogates(fdp.ConsumeIntInRange(1, 100))) 

129 tabulate.tabulate(ConsumeDictionary(fdp), tablefmt=custom_separator) 

130 

131 

132def main(): 

133 atheris.instrument_all() 

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

135 atheris.Fuzz() 

136 

137 

138if __name__ == "__main__": 

139 main()