Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_requests.py: 62%
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
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
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
14# Copyright 2023 Google LLC
15#
16# Licensed under the Apache License, Version 2.0 (the "License");
17# you may not use this file except in compliance with the License.
18# You may obtain a copy of the License at
19#
20# http://www.apache.org/licenses/LICENSE-2.0
21#
22# Unless required by applicable law or agreed to in writing, software
23# distributed under the License is distributed on an "AS IS" BASIS,
24# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25# See the License for the specific language governing permissions and
26# limitations under the License.
28import atheris
29import socket
30import sys
31import threading
32import time
34import urllib3
36# Setup http mocking
37GLOBAL_RESPONSE_BODY = b""
38GLOBAL_RESPONSE_CODE = 0
39GLOBAL_CONTENT_TYPE = b""
42class ServerThread(threading.Thread):
43 def __init__(self) -> None:
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", 8001))
47 self.s.listen(1)
48 super().__init__()
50 def run(self) -> None:
51 global GLOBAL_RESPONSE_CODE, GLOBAL_CONTENT_TYPE, GLOBAL_RESPONSE_BODY
52 conn, addr = self.s.accept()
53 conn.recv(1024)
54 conn.send(
55 b"HTTP/1.1 %d FOO\r\nContent-Type: %b\r\n\r\n%b"
56 % (GLOBAL_RESPONSE_CODE, GLOBAL_CONTENT_TYPE, GLOBAL_RESPONSE_BODY)
57 )
58 time.sleep(0.005)
59 conn.close()
60 self.s.shutdown(1)
61 self.s.close()
62 time.sleep(0.01)
65REQUEST_METHODS = ["POST", "GET", "HEAD", "PUT"]
67def TestOneInput(data):
68 fdp = atheris.FuzzedDataProvider(data)
70 global GLOBAL_RESPONSE_BODY, GLOBAL_RESPONSE_CODE, GLOBAL_CONTENT_TYPE
71 GLOBAL_RESPONSE_BODY = fdp.ConsumeBytes(sys.maxsize)
72 GLOBAL_RESPONSE_CODE = fdp.ConsumeIntInRange(200, 599)
73 GLOBAL_CONTENT_TYPE = fdp.ConsumeBytes(sys.maxsize)
75 requestType = fdp.PickValueInList(REQUEST_METHODS)
77 # Optionally provide request headers
78 requestHeaders = urllib3._collections.HTTPHeaderDict({})
79 for i in range(0, fdp.ConsumeIntInRange(0, 10)):
80 requestHeaders.add(
81 fdp.ConsumeString(sys.maxsize), fdp.ConsumeString(sys.maxsize)
82 )
83 requestHeaders = None if fdp.ConsumeBool() else requestHeaders
85 # Optionally generate form data
86 formData = {}
87 for i in range(0, fdp.ConsumeIntInRange(0, 100)):
88 formData[fdp.ConsumeString(sys.maxsize)] = fdp.ConsumeString(sys.maxsize)
89 formData = None if fdp.ConsumeBool() else formData
91 timeout = urllib3.util.Timeout(connect=0.1, read=0.1)
92 urllib_pool = urllib3.poolmanager.PoolManager(timeout=timeout)
94 t1 = ServerThread()
95 t1.start()
97 response = urllib_pool.request(
98 requestType,
99 "http://localhost:8001/",
100 headers=requestHeaders,
101 fields=formData
102 )
104 response.status
105 response.data
106 response.headers
108 t1.join()
111def main():
112 atheris.instrument_all()
113 atheris.Setup(sys.argv, TestOneInput)
114 atheris.Fuzz()
116if __name__ == "__main__":
117 main()