Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_loader.py: 44%

39 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-09 06:12 +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. 

26 

27import atheris 

28import sys 

29 

30with atheris.instrument_imports(): 

31 from io import StringIO 

32 import numpy as np 

33 

34def get_fuzz_types(): 

35 # Define the rows 

36 dtype = np.dtype( 

37 [('f0', np.uint16), ('f1', np.float64), ('f2', 'S7'), ('f3', np.int8)] 

38 ) 

39 

40 # An expected match 

41 expected = np.array( 

42 [ 

43 (1, 2.4, "a", -34), 

44 (2, 3.1, "b", 29), 

45 (3, 9.9, "g", 120), 

46 ], 

47 dtype=dtype 

48 ) 

49 return dtype, expected 

50 

51def TestOneInput(fuzz_data): 

52 dtype, expected = get_fuzz_types() 

53 fdp = atheris.FuzzedDataProvider(fuzz_data) 

54 new_data = StringIO(fdp.ConsumeString(sys.maxsize)) 

55 try: 

56 np.loadtxt(new_data, dtype=dtype, delimiter=";", skiprows=True) 

57 # Catch all of the exceptions that are caught in  

58 # https://github.com/numpy/numpy/blob/main/numpy/lib/tests/test_loadtxt.py 

59 except StopIteration: 

60 return 

61 except ValueError: 

62 return 

63 except IndexError: 

64 return 

65 except TypeError: 

66 return 

67 except RuntimeError: 

68 return 

69 

70def main(): 

71 atheris.instrument_all() 

72 atheris.Setup(sys.argv, TestOneInput, enable_python_coverage=True) 

73 atheris.Fuzz() 

74 

75if __name__ == "__main__": 

76 main()