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

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

28import sys 

29import time 

30import socket 

31import threading 

32import cx_Oracle 

33 

34fuzzed_input = b"" 

35 

36 

37def SetFuzzedInput(input_bytes): 

38 global fuzzed_input 

39 fuzzed_input = input_bytes 

40 

41 

42class ServerThread(threading.Thread): 

43 def __init__(self, port): 

44 self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

45 self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 

46 self.s.bind(("127.0.0.1", port)) 

47 self.s.settimeout(0.4) 

48 self.s.listen(1) 

49 

50 threading.Thread.__init__(self) 

51 

52 def run(self): 

53 global fuzzed_input 

54 try: 

55 conn, addr = self.s.accept() 

56 except socket.timeout: 

57 return 

58 conn.recv(1024) 

59 conn.send(fuzzed_input) 

60 time.sleep(0.005) 

61 conn.close() 

62 self.s.shutdown(1) 

63 self.s.close() 

64 time.sleep(0.01) 

65 

66 

67@atheris.instrument_func 

68def TestInput(data): 

69 port = None 

70 for i in range(8009, 8100): 

71 try: 

72 t1 = ServerThread(i) 

73 except OSError: 

74 # If the address is already in use by another 

75 continue 

76 port = i 

77 break 

78 

79 if port == None: 

80 return 

81 

82 t1.start() 

83 

84 fdp = atheris.FuzzedDataProvider(data) 

85 args = dict() 

86 args['user'] = fdp.ConsumeUnicodeNoSurrogates(64) 

87 args['password'] = fdp.ConsumeUnicodeNoSurrogates(64) 

88 args['dsn'] = f"127.0.0.1:{port}" 

89 if fdp.ConsumeBool(): 

90 args['newpassword'] = fdp.ConsumeUnicodeNoSurrogates(64) 

91 if fdp.ConsumeBool(): 

92 args['nencoding'] = fdp.ConsumeUnicodeNoSurrogates(64) 

93 

94 SetFuzzedInput(fdp.ConsumeBytes(400)) 

95 try: 

96 connection = cx_Oracle.connect(**args) 

97 except cx_Oracle.DatabaseError: 

98 connection = None 

99 pass 

100 

101 if connection is None: 

102 t1.join() 

103 return 

104 

105 cursor = connection.cursor() 

106 cursor.execute(fdp.ConsumeUnicodeNoSurrogates(1024)) 

107 t1.join() 

108 

109 

110def main(): 

111 atheris.Setup(sys.argv, TestInput, enable_python_coverage=True) 

112 atheris.instrument_all() 

113 atheris.Fuzz() 

114 

115 

116if __name__ == "__main__": 

117 main()