Debian Codegraph @

tests/test_packetizer.py

raw
1 # Copyright (C) 2003-2009 Robey Pointer <[email protected]>
2 #
3 # This file is part of paramiko.
4 #
5 # Paramiko is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation; either version 2.1 of the License, or (at your option)
8 # any later version.
9 #
10 # Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with Paramiko; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 """
20 Some unit tests for the ssh2 protocol in Transport.
21 """
22
23 import sys
24 import unittest
25 from hashlib import sha1
26
27 from cryptography.hazmat.backends import default_backend
28 from cryptography.hazmat.primitives.ciphers import algorithms, Cipher, modes
29
30 from paramiko import Message, Packetizer, util
31 from paramiko.common import byte_chr, zero_byte
32
33 from ._loop import LoopSocket
34
35
36 x55 = byte_chr(0x55)
37 x1f = byte_chr(0x1F)
38
39
40 class PacketizerTest(unittest.TestCase):
41 def test_write(self):
42 rsock = LoopSocket()
43 wsock = LoopSocket()
44 rsock.link(wsock)
45 p = Packetizer(wsock)
46 p.set_log(util.get_logger("paramiko.transport"))
47 p.set_hexdump(True)
48 encryptor = Cipher(
49 algorithms.AES(zero_byte * 16),
50 modes.CBC(x55 * 16),
51 backend=default_backend(),
52 ).encryptor()
53 p.set_outbound_cipher(encryptor, 16, sha1, 12, x1f * 20)
54
55 # message has to be at least 16 bytes long, so we'll have at least one
56 # block of data encrypted that contains zero random padding bytes
57 m = Message()
58 m.add_byte(byte_chr(100))
59 m.add_int(100)
60 m.add_int(1)
61 m.add_int(900)
62 p.send_message(m)
63 data = rsock.recv(100)
64 # 32 + 12 bytes of MAC = 44
65 self.assertEqual(44, len(data))
66 self.assertEqual(
67 b"\x43\x91\x97\xbd\x5b\x50\xac\x25\x87\xc2\xc4\x6b\xc7\xe9\x38\xc0", # noqa
68 data[:16],
69 )
70
71 def test_read(self):
72 rsock = LoopSocket()
73 wsock = LoopSocket()
74 rsock.link(wsock)
75 p = Packetizer(rsock)
76 p.set_log(util.get_logger("paramiko.transport"))
77 p.set_hexdump(True)
78 decryptor = Cipher(
79 algorithms.AES(zero_byte * 16),
80 modes.CBC(x55 * 16),
81 backend=default_backend(),
82 ).decryptor()
83 p.set_inbound_cipher(decryptor, 16, sha1, 12, x1f * 20)
84 wsock.send(
85 b"\x43\x91\x97\xbd\x5b\x50\xac\x25\x87\xc2\xc4\x6b\xc7\xe9\x38\xc0\x90\xd2\x16\x56\x0d\x71\x73\x61\x38\x7c\x4c\x3d\xfb\x97\x7d\xe2\x6e\x03\xb1\xa0\xc2\x1c\xd6\x41\x41\x4c\xb4\x59" # noqa
86 )
87 cmd, m = p.read_message()
88 self.assertEqual(100, cmd)
89 self.assertEqual(100, m.get_int())
90 self.assertEqual(1, m.get_int())
91 self.assertEqual(900, m.get_int())
92
93 def test_closed(self):
94 if sys.platform.startswith("win"): # no SIGALRM on windows
95 return
96 rsock = LoopSocket()
97 wsock = LoopSocket()
98 rsock.link(wsock)
99 p = Packetizer(wsock)
100 p.set_log(util.get_logger("paramiko.transport"))
101 p.set_hexdump(True)
102 encryptor = Cipher(
103 algorithms.AES(zero_byte * 16),
104 modes.CBC(x55 * 16),
105 backend=default_backend(),
106 ).encryptor()
107 p.set_outbound_cipher(encryptor, 16, sha1, 12, x1f * 20)
108
109 # message has to be at least 16 bytes long, so we'll have at least one
110 # block of data encrypted that contains zero random padding bytes
111 m = Message()
112 m.add_byte(byte_chr(100))
113 m.add_int(100)
114 m.add_int(1)
115 m.add_int(900)
116 wsock.send = lambda x: 0
117 from functools import wraps
118 import errno
119 import os
120 import signal
121
122 class TimeoutError(Exception):
123 def __init__(self, error_message):
124 if hasattr(errno, "ETIME"):
125 self.message = os.sterror(errno.ETIME)
126 else:
127 self.messaage = error_message
128
129 def timeout(seconds=1, error_message="Timer expired"):
130 def decorator(func):
131 def _handle_timeout(signum, frame):
132 raise TimeoutError(error_message)
133
134 def wrapper(*args, **kwargs):
135 signal.signal(signal.SIGALRM, _handle_timeout)
136 signal.alarm(seconds)
137 try:
138 result = func(*args, **kwargs)
139 finally:
140 signal.alarm(0)
141 return result
142
143 return wraps(func)(wrapper)
144
145 return decorator
146
147 send = timeout()(p.send_message)
148 self.assertRaises(EOFError, send, m)