Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_rfc3986.py: 71%
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
13# Copyright 2023 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.
26import sys
27import atheris
29import rfc3986
32def fuzz_parseresult(data):
33 fdp = atheris.FuzzedDataProvider(data)
34 try:
35 rfc3986.parseresult.ParseResult.from_string(
36 fdp.ConsumeUnicodeNoSurrogates(256))
37 except (rfc3986.exceptions.RFC3986Exception):
38 pass
39 try:
40 rfc3986.parseresult.ParseResultBytes.from_string(
41 fdp.ConsumeUnicodeNoSurrogates(256))
42 except (rfc3986.exceptions.RFC3986Exception):
43 pass
46def fuzz_normalizers(data):
47 fdp = atheris.FuzzedDataProvider(data)
48 rfc3986.normalizers.normalize_host(fdp.ConsumeUnicodeNoSurrogates(256))
49 rfc3986.normalizers.normalize_percent_characters(
50 fdp.ConsumeUnicodeNoSurrogates(256))
51 rfc3986.normalizers.normalize_scheme(fdp.ConsumeUnicodeNoSurrogates(256))
54def fuzz_uri(data):
55 fdp = atheris.FuzzedDataProvider(data)
56 uri = rfc3986.uri.URIReference.from_string(
57 fdp.ConsumeUnicodeNoSurrogates(256))
58 uri.is_valid()
59 uri.is_absolute()
62def fuzz_iri(data):
63 fdp = atheris.FuzzedDataProvider(data)
64 iri_ref = rfc3986.IRIReference.from_string(
65 fdp.ConsumeUnicodeNoSurrogates(256))
68def fuzz_api(data):
69 fdp = atheris.FuzzedDataProvider(data)
70 rfc3986.api.uri_reference(fdp.ConsumeUnicodeNoSurrogates(256))
71 rfc3986.api.iri_reference(fdp.ConsumeUnicodeNoSurrogates(256))
72 rfc3986.api.is_valid_uri(fdp.ConsumeUnicodeNoSurrogates(256))
73 rfc3986.api.normalize_uri(fdp.ConsumeUnicodeNoSurrogates(256))
74 rfc3986.api.urlparse(fdp.ConsumeUnicodeNoSurrogates(256))
77def fuzz_validators(data):
78 fdp = atheris.FuzzedDataProvider(data)
79 uri = rfc3986.uri.URIReference.from_string(
80 fdp.ConsumeUnicodeNoSurrogates(256))
81 if uri.is_valid():
82 validator = rfc3986.validators.Validator().forbid_use_of_password()
83 try:
84 validator.validate(uri)
85 except rfc3986.eceptions.RFC3986Exception:
86 pass
88 validator2 = rfc3986.validators.Validator()
89 try:
90 validator2.validate(uri)
91 except rfc3986.eceptions.RFC3986Exception:
92 pass
94 validator3 = rfc3986.validators.Validator()
95 validator3.allow_schemes(fdp.ConsumeUnicodeNoSurrogates(24))
96 validator3.allow_hosts(fdp.ConsumeUnicodeNoSurrogates(24))
97 validator3.allow_ports(str(fdp.ConsumeIntInRange(1, 65000)))
98 try:
99 validator3.require_presence_of(fdp.ConsumeUnicodeNoSurrogates(8))
100 except ValueError:
101 pass
102 try:
103 validator3.check_validity_of(fdp.ConsumeUnicodeNoSurrogates(24))
104 except ValueError:
105 pass
106 try:
107 validator3.validate(uri)
108 except rfc3986.exceptions.RFC3986Exception:
109 pass
112def TestOneInput(data):
113 fuzz_parseresult(data)
114 fuzz_normalizers(data)
115 fuzz_uri(data)
116 fuzz_iri(data)
117 fuzz_api(data)
118 fuzz_validators(data)
121def main():
122 atheris.instrument_all()
123 atheris.Setup(sys.argv, TestOneInput)
124 atheris.Fuzz()
127if __name__ == "__main__":
128 main()