Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_connection.py: 67%
64 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:28 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:28 +0000
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.
27import sys
28import atheris
29import threading
30import socket
31import time
33import configparser
34import pymysql
35from pymysql import connections, constants
38fuzzed_input = b""
40# somehow ugly as fuzzing cannot be run in parallel
41def SetFuzzedInput(input_bytes):
42 global fuzzed_input
43 fuzzed_input = input_bytes
45class ServerThread(threading.Thread):
46 def __init__(self):
47 self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
48 self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
49 self.s.bind(("127.0.0.1", 8001))
50 self.s.listen(1)
52 threading.Thread.__init__(self)
54 def run(self):
55 global fuzzed_input
56 conn, addr = self.s.accept()
57 conn.settimeout(0.3)
58 try:
59 conn.recv(1024)
60 except:
61 pass
62 conn.send(fuzzed_input)
63 time.sleep(0.005)
64 conn.close()
65 self.s.shutdown(1)
66 self.s.close()
67 time.sleep(0.01)
70def TestOneInput(data):
71 fdp = atheris.FuzzedDataProvider(data)
72 local_config_file = "/tmp/my.cnf"
73 with open(local_config_file, "w") as cfg:
74 cfg.write(fdp.ConsumeUnicodeNoSurrogates(1024))
76 # Catch config parser errors
77 try:
78 c = connections.Connection(
79 db=None,
80 read_default_file=local_config_file,
81 ssl_disabled=True,
82 defer_connect=True
83 )
84 except configparser.Error:
85 return
86 c.port = 8001
87 c.host = "127.0.0.1"
89 SetFuzzedInput(fdp.ConsumeBytes(512))
90 t1 = ServerThread()
91 t1.start()
93 try:
94 c.connect(sock=None)
95 except pymysql.err.MySQLError:
96 pass
97 t1.join()
100def main():
101 atheris.instrument_all()
102 atheris.Setup(sys.argv, TestOneInput, enable_python_coverage=True)
103 atheris.Fuzz()
106if __name__ == "__main__":
107 main()