Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_cached.py: 49%
41 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 07:03 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 07:03 +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 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.
27import os
28import sys
29import atheris
30import cachetools
31import random
32import datetime
33from threading import Lock
35def get_ttu(_key, value, now):
36 # assume value.ttl contains the item's time-to-live in hours
37 return datetime.datetime.now() + random.random() * datetime.timedelta(days=200000)
39def TestOneInput(data):
40 fdp = atheris.FuzzedDataProvider(data)
42 cache_size = fdp.ConsumeIntInRange(1,32)
43 cache_ttl = fdp.ConsumeProbability()
44 cache_lock = None
45 if fdp.ConsumeBool():
46 cache_lock = Lock()
48 # Random caching types
49 CACHE_TYPES = [
50 {},
51 cachetools.FIFOCache(maxsize=cache_size),
52 cachetools.LFUCache(maxsize=cache_size),
53 cachetools.LRUCache(maxsize=cache_size),
54 cachetools.MRUCache(maxsize=cache_size),
55 cachetools.RRCache(maxsize=cache_size, choice=random.choice),
56 cachetools.TTLCache(maxsize=cache_size, ttl=cache_ttl),
57 cachetools.TLRUCache(maxsize=cache_size, ttu=get_ttu, timer=datetime.datetime.now)
58 ]
60 # Generate a random cached function
61 @cachetools.cached(cache=fdp.PickValueInList(CACHE_TYPES), lock=cache_lock, info=fdp.ConsumeBool())
62 def fib(n):
63 return n if n < 2 else fib(n - 1) + fib(n - 2)
65 for i in range(20):
66 fib(fdp.ConsumeIntInRange(1, 20))
67 # Try and get coverage of different properties
68 try:
69 fib.maxsize()
70 fib.currsize()
71 except AttributeError:
72 pass
74def main():
75 atheris.Setup(sys.argv, TestOneInput)
76 atheris.Fuzz()
78if __name__ == "__main__":
79 atheris.instrument_all()
80 main()