Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_parse.py: 68%

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

56 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 

27import os 

28import sys 

29import atheris 

30 

31from pyparsing import ( 

32 Literal, 

33 Word, 

34 ZeroOrMore, 

35 Group, 

36 Dict, 

37 Suppress, 

38 ParseException, 

39) 

40 

41def dict_parse_generator(fdp): 

42 """Generate random parser""" 

43 curr = Literal("f") 

44 special_chars="[]{}/|\<>:;-=" 

45 op_count = fdp.ConsumeIntInRange(2, 15) 

46 for i in range(op_count): 

47 operation = fdp.ConsumeIntInRange(0,4) 

48 if operation == 0: 

49 l1 = Literal(fdp.PickValueInList(list(special_chars))) 

50 l2 = Literal(fdp.PickValueInList(list(special_chars))) 

51 word = Word(fdp.ConsumeUnicodeNoSurrogates(10)) 

52 curr = Group(Dict(ZeroOrMore(word)) + curr) 

53 elif operation == 1: 

54 word1 = Word(fdp.ConsumeUnicodeNoSurrogates(10)) 

55 word2 = Word(fdp.ConsumeUnicodeNoSurrogates(10)) 

56 curr = Group(Dict(OneOrMore(word1, word2)) + curr) 

57 elif operation == 2: 

58 curr = curr + Word(fdp.ConsumeUnicodeNoSurrogates(10)) 

59 elif operation == 3: 

60 curr = curr + Suppress(fdp.ConsumeUnicodeNoSurrogates(2)) 

61 else: 

62 word1 = Word(fdp.ConsumeUnicodeNoSurrogates(10)) 

63 word2 = Word(fdp.ConsumeUnicodeNoSurrogates(10)) 

64 curr = Group(Dict(OneOrMore(word1, word2)) + curr) 

65 return Dict(curr) 

66 

67 

68def TestOneInput(data): 

69 fdp = atheris.FuzzedDataProvider(data) 

70 try: 

71 bnf = dict_parse_generator(fdp) 

72 except: 

73 return 

74 

75 try: 

76 tokens = bnf.parseString(fdp.ConsumeUnicodeNoSurrogates(1024)) 

77 except ParseException: 

78 pass 

79 except TypeError as e: 

80 # Catch the TypeError exception from here: 

81 # https://github.com/pyparsing/pyparsing/blob/d93930308f7fe79f2290812074f62a472c83db59/pyparsing/core.py#L5674 

82 if "could not extract dict values from parsed results" in str(e): 

83 pass 

84 else: 

85 raise e 

86 

87 

88def main(): 

89 atheris.instrument_all() 

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

91 atheris.Fuzz() 

92 

93 

94if __name__ == "__main__": 

95 main()