Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_dump.py: 2%
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# Copyright 2023 Google LLC
13#
14# Licensed under the Apache License, Version 2.0 (the "License");
15# you may not use this file except in compliance with the License.
16# You may obtain a copy of the License at
17#
18# http://www.apache.org/licenses/LICENSE-2.0
19#
20# Unless required by applicable law or agreed to in writing, software
21# distributed under the License is distributed on an "AS IS" BASIS,
22# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23# See the License for the specific language governing permissions and
24# limitations under the License.
25#
26################################################################################
28import io
29import sys
30import atheris
32# Libraries used as part of the test
33import dictgen
34from numpy import array, float64, int32
35import pathlib
36from random import randint
38import toml
39from toml import ordered as toml_ordered
41def random_path(**kwargs):
42 return pathlib.Path(f"/{dictgen.random_string()}")
44def random_inlinetabledict(**kwargs):
45 class TestDict(dict, toml.decoder.InlineTableDict):
46 pass
48 val_generators = kwargs["val_generators"]
49 key_generators = kwargs["key_generators"]
50 nested_generators = kwargs["nested_generators"]
51 max_height = kwargs["max_height"]
52 max_depth = kwargs["max_depth"]
54 all_generators = val_generators + nested_generators
56 target = TestDict()
57 for i in range(randint(0, max_height)):
58 # If we are at a top level depth don't allow any nested generators
59 if max_depth > 2:
60 target[key_generators[randint(0, len(key_generators) - 1)](**kwargs)] = all_generators[randint(0, len(all_generators) - 1)](**kwargs)
61 else:
62 target[key_generators[randint(0, len(key_generators) - 1)](**kwargs)] = val_generators[randint(0, len(val_generators) - 1)](**kwargs)
63 return target
65def random_numpy(**kwargs):
66 df = {
67 dictgen.random_string() : array([1, .3], dtype=float64),
68 dictgen.random_string(): array([1, 3], dtype=int32)
69 }
70 return df
72def TestOneInput(data):
73 fdp = atheris.FuzzedDataProvider(data)
75 # Pick from a random set of encoders
76 ENCODERS = [
77 toml.TomlEncoder(preserve=fdp.ConsumeBool()), # Optional formatting argument
78 toml.TomlPreserveInlineDictEncoder(),
79 toml.TomlArraySeparatorEncoder(),
80 toml.TomlPreserveCommentEncoder(),
81 toml.TomlPathlibEncoder(),
82 toml_ordered.TomlOrderedEncoder(),
83 toml.TomlNumpyEncoder(),
84 None,
85 ]
87 # Generate a random dictionary object
88 fuzz_dict = dictgen.generate(
89 key_generators=(
90 dictgen.random_string,
91 ),
92 val_generators=(
93 dictgen.random_int,
94 dictgen.random_float,
95 dictgen.random_string,
96 dictgen.random_datetime,
97 random_path,
98 random_numpy
99 ),
100 nested_generators=(
101 dictgen.random_dict,
102 dictgen.random_array,
103 random_inlinetabledict
104 ),
105 rand_seed=fdp.ConsumeInt(32)
106 )
108 try:
109 with io.StringIO("") as outfile:
110 result = toml.encoder.dump(fuzz_dict, outfile, fdp.PickValueInList(ENCODERS))
111 except TypeError:
112 pass
114def main():
115 atheris.instrument_all()
116 atheris.Setup(sys.argv, TestOneInput)
117 atheris.Fuzz()
120if __name__ == "__main__":
121 main()