Coverage for /pythoncovmergedfiles/medio/medio/src/paramiko/tests/fuzz_packetizer.py: 53%

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

59 statements  

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"""Fuzzer for Packetizer""" 

27 

28import os 

29import sys 

30import atheris 

31 

32from hashlib import sha1 

33 

34from cryptography.hazmat.backends import default_backend 

35from cryptography.hazmat.primitives.ciphers import algorithms, Cipher, modes 

36 

37from paramiko import Message, Packetizer 

38from paramiko.common import byte_chr, zero_byte 

39 

40# Extract path of fuzzer so we can include loop.py 

41if getattr(sys, 'frozen', False): 

42 app_path = os.path.dirname(sys.executable) 

43elif __file__: 

44 app_path = os.path.dirname(__file__) 

45else: 

46 raise Exception("Could not extract path needed to import loop.py") 

47sys.path.append(app_path) 

48from _loop import LoopSocket 

49 

50def TestOneInput(data): 

51 fdp = atheris.FuzzedDataProvider(data) 

52 

53 rsock = LoopSocket() 

54 wsock = LoopSocket() 

55 rsock.link(wsock) 

56 p = Packetizer(wsock) 

57 encryptor = Cipher( 

58 algorithms.AES(zero_byte * 16), 

59 modes.CBC(byte_chr(fdp.ConsumeIntInRange(0, 255)) * 16), 

60 backend=default_backend(), 

61 ).encryptor() 

62 p.set_outbound_cipher( 

63 encryptor, 

64 16, 

65 sha1, 

66 12, 

67 byte_chr(fdp.ConsumeIntInRange(0, 255)) * 20 

68 ) 

69 

70 m = Message() 

71 # Messages need to be at least 16 bytes long, so we'll include 

72 # at least 16 items. 

73 for i in range(fdp.ConsumeIntInRange(16, 32)): 

74 op = fdp.ConsumeIntInRange(0,5) 

75 if op == 0: 

76 m.add(fdp.ConsumeUnicodeNoSurrogates(20)) 

77 elif op == 1: 

78 m.add(fdp.ConsumeIntInRange(0, 4294967295)) 

79 elif op == 2: 

80 m.add(fdp.ConsumeBool()) 

81 elif op == 3: 

82 l1 = list() 

83 for i in range(1, 10): 

84 l1.append(fdp.ConsumeUnicodeNoSurrogates(20)) 

85 m.add(l1) 

86 elif op == 4: 

87 m.add_bytes(fdp.ConsumeBytes(20)) 

88 elif op == 5: 

89 m.add_byte(byte_chr(fdp.ConsumeIntInRange(0,255))) 

90 p.send_message(m) 

91 rsock.recv(sys.maxsize) 

92 

93 

94def main(): 

95 atheris.instrument_all() 

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

97 atheris.Fuzz() 

98 

99 

100if __name__ == "__main__": 

101 main()