Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_solver.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

50 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 kiwisolver import ( 

32 Solver, 

33 UnsatisfiableConstraint, 

34 Variable, 

35) 

36 

37 

38def TestOneInput(data): 

39 fdp = atheris.FuzzedDataProvider(data) 

40 

41 s = Solver() 

42 

43 # Create an arbitrary set of variables 

44 variables = [] 

45 num_of_variables = fdp.ConsumeIntInRange(2, 10) 

46 for v in range(num_of_variables): 

47 variables.append(Variable(fdp.ConsumeUnicodeNoSurrogates(20))) 

48 s.addEditVariable(variables[-1], "weak") 

49 

50 # Apply a random set of constraints on these variables 

51 num_of_ops = fdp.ConsumeIntInRange(2, 10) 

52 for i in range(num_of_ops): 

53 op = fdp.ConsumeIntInRange(1,3) 

54 var = variables[fdp.ConsumeIntInRange(0,len(variables)-1)] 

55 c = 1 * var 

56 for i2 in range(fdp.ConsumeIntInRange(2, 5)): 

57 if op == 1: 

58 c = c + fdp.ConsumeIntInRange(1, 100) 

59 elif op == 2: 

60 c = c - fdp.ConsumeIntInRange(1, 100) 

61 elif op == 3: 

62 c = c * fdp.ConsumeIntInRange(1, 100) 

63 

64 # Add constraint 

65 constr = fdp.ConsumeIntInRange(0,2) 

66 if constr == 0: 

67 c = c >= 0 

68 elif constr == 1: 

69 c = c <= 0 

70 else: 

71 c = c == 0 

72 

73 try: 

74 s.addConstraint(c) 

75 except UnsatisfiableConstraint: 

76 return 

77 

78 # Set a condition/update variables. 

79 s.updateVariables() 

80 

81def main(): 

82 atheris.instrument_all() 

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

84 atheris.Fuzz() 

85 

86 

87if __name__ == "__main__": 

88 main()