Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1 | import socket |
Charles-François Natali | 6459025 | 2013-10-21 14:02:12 +0200 | [diff] [blame] | 2 | import selectors |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3 | import telnetlib |
| 4 | import time |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 5 | import contextlib |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6 | |
| 7 | from unittest import TestCase |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 8 | from test import support |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 9 | threading = support.import_module('threading') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 10 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 11 | HOST = support.HOST |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 12 | |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 13 | def server(evt, serv): |
Charles-François Natali | 6e20460 | 2014-07-23 19:28:13 +0100 | [diff] [blame] | 14 | serv.listen() |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 15 | evt.set() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 16 | try: |
| 17 | conn, addr = serv.accept() |
Jesus Cea | bc91b46 | 2011-11-08 16:24:43 +0100 | [diff] [blame] | 18 | conn.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 19 | except socket.timeout: |
| 20 | pass |
| 21 | finally: |
| 22 | serv.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 23 | |
| 24 | class GeneralTests(TestCase): |
| 25 | |
| 26 | def setUp(self): |
| 27 | self.evt = threading.Event() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 28 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Jesus Cea | bc91b46 | 2011-11-08 16:24:43 +0100 | [diff] [blame] | 29 | self.sock.settimeout(60) # Safety net. Look issue 11812 |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 30 | self.port = support.bind_port(self.sock) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 31 | self.thread = threading.Thread(target=server, args=(self.evt,self.sock)) |
Jesus Cea | bc91b46 | 2011-11-08 16:24:43 +0100 | [diff] [blame] | 32 | self.thread.setDaemon(True) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 33 | self.thread.start() |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 34 | self.evt.wait() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 35 | |
| 36 | def tearDown(self): |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 37 | self.thread.join() |
Brett Cannon | 40ccea7 | 2011-08-04 22:37:55 -0700 | [diff] [blame] | 38 | del self.thread # Clear out any dangling Thread objects. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 39 | |
| 40 | def testBasic(self): |
| 41 | # connects |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 42 | telnet = telnetlib.Telnet(HOST, self.port) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 43 | telnet.sock.close() |
| 44 | |
| 45 | def testTimeoutDefault(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 46 | self.assertTrue(socket.getdefaulttimeout() is None) |
| 47 | socket.setdefaulttimeout(30) |
| 48 | try: |
Jesus Cea | bc91b46 | 2011-11-08 16:24:43 +0100 | [diff] [blame] | 49 | telnet = telnetlib.Telnet(HOST, self.port) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 50 | finally: |
| 51 | socket.setdefaulttimeout(None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 52 | self.assertEqual(telnet.sock.gettimeout(), 30) |
| 53 | telnet.sock.close() |
| 54 | |
| 55 | def testTimeoutNone(self): |
| 56 | # None, having other default |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 57 | self.assertTrue(socket.getdefaulttimeout() is None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 58 | socket.setdefaulttimeout(30) |
| 59 | try: |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 60 | telnet = telnetlib.Telnet(HOST, self.port, timeout=None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 61 | finally: |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 62 | socket.setdefaulttimeout(None) |
| 63 | self.assertTrue(telnet.sock.gettimeout() is None) |
| 64 | telnet.sock.close() |
| 65 | |
| 66 | def testTimeoutValue(self): |
Jesus Cea | bc91b46 | 2011-11-08 16:24:43 +0100 | [diff] [blame] | 67 | telnet = telnetlib.Telnet(HOST, self.port, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 68 | self.assertEqual(telnet.sock.gettimeout(), 30) |
| 69 | telnet.sock.close() |
| 70 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 71 | def testTimeoutOpen(self): |
| 72 | telnet = telnetlib.Telnet() |
Jesus Cea | bc91b46 | 2011-11-08 16:24:43 +0100 | [diff] [blame] | 73 | telnet.open(HOST, self.port, timeout=30) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 74 | self.assertEqual(telnet.sock.gettimeout(), 30) |
| 75 | telnet.sock.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 76 | |
Ezio Melotti | 4f1353a | 2013-08-25 23:56:43 +0300 | [diff] [blame] | 77 | def testGetters(self): |
| 78 | # Test telnet getter methods |
| 79 | telnet = telnetlib.Telnet(HOST, self.port, timeout=30) |
| 80 | t_sock = telnet.sock |
| 81 | self.assertEqual(telnet.get_socket(), t_sock) |
| 82 | self.assertEqual(telnet.fileno(), t_sock.fileno()) |
| 83 | telnet.sock.close() |
| 84 | |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 85 | class SocketStub(object): |
| 86 | ''' a socket proxy that re-defines sendall() ''' |
Gregory P. Smith | dad5711 | 2012-07-15 23:42:26 -0700 | [diff] [blame] | 87 | def __init__(self, reads=()): |
| 88 | self.reads = list(reads) # Intentionally make a copy. |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 89 | self.writes = [] |
| 90 | self.block = False |
| 91 | def sendall(self, data): |
| 92 | self.writes.append(data) |
| 93 | def recv(self, size): |
| 94 | out = b'' |
| 95 | while self.reads and len(out) < size: |
| 96 | out += self.reads.pop(0) |
| 97 | if len(out) > size: |
| 98 | self.reads.insert(0, out[size:]) |
| 99 | out = out[:size] |
| 100 | return out |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 101 | |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 102 | class TelnetAlike(telnetlib.Telnet): |
| 103 | def fileno(self): |
| 104 | raise NotImplementedError() |
| 105 | def close(self): pass |
| 106 | def sock_avail(self): |
| 107 | return (not self.sock.block) |
| 108 | def msg(self, msg, *args): |
| 109 | with support.captured_stdout() as out: |
| 110 | telnetlib.Telnet.msg(self, msg, *args) |
| 111 | self._messages += out.getvalue() |
| 112 | return |
| 113 | |
Charles-François Natali | 6459025 | 2013-10-21 14:02:12 +0200 | [diff] [blame] | 114 | class MockSelector(selectors.BaseSelector): |
Gregory P. Smith | dad5711 | 2012-07-15 23:42:26 -0700 | [diff] [blame] | 115 | |
| 116 | def __init__(self): |
Charles-François Natali | 6459025 | 2013-10-21 14:02:12 +0200 | [diff] [blame] | 117 | self.keys = {} |
Gregory P. Smith | dad5711 | 2012-07-15 23:42:26 -0700 | [diff] [blame] | 118 | |
Victor Stinner | 3c2f175 | 2014-01-25 23:53:28 +0100 | [diff] [blame] | 119 | @property |
| 120 | def resolution(self): |
| 121 | return 1e-3 |
| 122 | |
Charles-François Natali | 6459025 | 2013-10-21 14:02:12 +0200 | [diff] [blame] | 123 | def register(self, fileobj, events, data=None): |
| 124 | key = selectors.SelectorKey(fileobj, 0, events, data) |
| 125 | self.keys[fileobj] = key |
| 126 | return key |
Gregory P. Smith | dad5711 | 2012-07-15 23:42:26 -0700 | [diff] [blame] | 127 | |
Charles-François Natali | 6459025 | 2013-10-21 14:02:12 +0200 | [diff] [blame] | 128 | def unregister(self, fileobj): |
Charles-François Natali | b3330a0a | 2013-12-01 11:04:17 +0100 | [diff] [blame] | 129 | return self.keys.pop(fileobj) |
Charles-François Natali | 6459025 | 2013-10-21 14:02:12 +0200 | [diff] [blame] | 130 | |
| 131 | def select(self, timeout=None): |
Gregory P. Smith | dad5711 | 2012-07-15 23:42:26 -0700 | [diff] [blame] | 132 | block = False |
Charles-François Natali | 6459025 | 2013-10-21 14:02:12 +0200 | [diff] [blame] | 133 | for fileobj in self.keys: |
| 134 | if isinstance(fileobj, TelnetAlike): |
| 135 | block = fileobj.sock.block |
| 136 | break |
Gregory P. Smith | dad5711 | 2012-07-15 23:42:26 -0700 | [diff] [blame] | 137 | if block: |
| 138 | return [] |
| 139 | else: |
Charles-François Natali | 6459025 | 2013-10-21 14:02:12 +0200 | [diff] [blame] | 140 | return [(key, key.events) for key in self.keys.values()] |
Gregory P. Smith | dad5711 | 2012-07-15 23:42:26 -0700 | [diff] [blame] | 141 | |
Charles-François Natali | b3330a0a | 2013-12-01 11:04:17 +0100 | [diff] [blame] | 142 | def get_map(self): |
| 143 | return self.keys |
| 144 | |
Gregory P. Smith | dad5711 | 2012-07-15 23:42:26 -0700 | [diff] [blame] | 145 | |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 146 | @contextlib.contextmanager |
| 147 | def test_socket(reads): |
| 148 | def new_conn(*ignored): |
| 149 | return SocketStub(reads) |
| 150 | try: |
| 151 | old_conn = socket.create_connection |
| 152 | socket.create_connection = new_conn |
| 153 | yield None |
| 154 | finally: |
| 155 | socket.create_connection = old_conn |
| 156 | return |
| 157 | |
Charles-François Natali | 6459025 | 2013-10-21 14:02:12 +0200 | [diff] [blame] | 158 | def test_telnet(reads=(), cls=TelnetAlike): |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 159 | ''' return a telnetlib.Telnet object that uses a SocketStub with |
| 160 | reads queued up to be read ''' |
| 161 | for x in reads: |
| 162 | assert type(x) is bytes, x |
| 163 | with test_socket(reads): |
| 164 | telnet = cls('dummy', 0) |
| 165 | telnet._messages = '' # debuglevel output |
| 166 | return telnet |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 167 | |
Gregory P. Smith | dad5711 | 2012-07-15 23:42:26 -0700 | [diff] [blame] | 168 | class ExpectAndReadTestCase(TestCase): |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 169 | def setUp(self): |
Charles-François Natali | 6459025 | 2013-10-21 14:02:12 +0200 | [diff] [blame] | 170 | self.old_selector = telnetlib._TelnetSelector |
| 171 | telnetlib._TelnetSelector = MockSelector |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 172 | def tearDown(self): |
Charles-François Natali | 6459025 | 2013-10-21 14:02:12 +0200 | [diff] [blame] | 173 | telnetlib._TelnetSelector = self.old_selector |
Gregory P. Smith | dad5711 | 2012-07-15 23:42:26 -0700 | [diff] [blame] | 174 | |
| 175 | class ReadTests(ExpectAndReadTestCase): |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 176 | def test_read_until(self): |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 177 | """ |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 178 | read_until(expected, timeout=None) |
| 179 | test the blocking version of read_util |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 180 | """ |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 181 | want = [b'xxxmatchyyy'] |
| 182 | telnet = test_telnet(want) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 183 | data = telnet.read_until(b'match') |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 184 | self.assertEqual(data, b'xxxmatch', msg=(telnet.cookedq, telnet.rawq, telnet.sock.reads)) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 185 | |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 186 | reads = [b'x' * 50, b'match', b'y' * 50] |
| 187 | expect = b''.join(reads[:-1]) |
| 188 | telnet = test_telnet(reads) |
| 189 | data = telnet.read_until(b'match') |
| 190 | self.assertEqual(data, expect) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 191 | |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 192 | |
| 193 | def test_read_all(self): |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 194 | """ |
| 195 | read_all() |
| 196 | Read all data until EOF; may block. |
| 197 | """ |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 198 | reads = [b'x' * 500, b'y' * 500, b'z' * 500] |
| 199 | expect = b''.join(reads) |
| 200 | telnet = test_telnet(reads) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 201 | data = telnet.read_all() |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 202 | self.assertEqual(data, expect) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 203 | return |
| 204 | |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 205 | def test_read_some(self): |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 206 | """ |
| 207 | read_some() |
| 208 | Read at least one byte or EOF; may block. |
| 209 | """ |
| 210 | # test 'at least one byte' |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 211 | telnet = test_telnet([b'x' * 500]) |
| 212 | data = telnet.read_some() |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 213 | self.assertTrue(len(data) >= 1) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 214 | # test EOF |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 215 | telnet = test_telnet() |
| 216 | data = telnet.read_some() |
| 217 | self.assertEqual(b'', data) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 218 | |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 219 | def _read_eager(self, func_name): |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 220 | """ |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 221 | read_*_eager() |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 222 | Read all data available already queued or on the socket, |
| 223 | without blocking. |
| 224 | """ |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 225 | want = b'x' * 100 |
| 226 | telnet = test_telnet([want]) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 227 | func = getattr(telnet, func_name) |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 228 | telnet.sock.block = True |
| 229 | self.assertEqual(b'', func()) |
| 230 | telnet.sock.block = False |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 231 | data = b'' |
| 232 | while True: |
| 233 | try: |
| 234 | data += func() |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 235 | except EOFError: |
| 236 | break |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 237 | self.assertEqual(data, want) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 238 | |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 239 | def test_read_eager(self): |
| 240 | # read_eager and read_very_eager make the same gaurantees |
| 241 | # (they behave differently but we only test the gaurantees) |
| 242 | self._read_eager('read_eager') |
| 243 | self._read_eager('read_very_eager') |
| 244 | # NB -- we need to test the IAC block which is mentioned in the |
| 245 | # docstring but not in the module docs |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 246 | |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 247 | def read_very_lazy(self): |
| 248 | want = b'x' * 100 |
| 249 | telnet = test_telnet([want]) |
| 250 | self.assertEqual(b'', telnet.read_very_lazy()) |
| 251 | while telnet.sock.reads: |
| 252 | telnet.fill_rawq() |
| 253 | data = telnet.read_very_lazy() |
| 254 | self.assertEqual(want, data) |
| 255 | self.assertRaises(EOFError, telnet.read_very_lazy) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 256 | |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 257 | def test_read_lazy(self): |
| 258 | want = b'x' * 100 |
| 259 | telnet = test_telnet([want]) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 260 | self.assertEqual(b'', telnet.read_lazy()) |
| 261 | data = b'' |
| 262 | while True: |
| 263 | try: |
| 264 | read_data = telnet.read_lazy() |
| 265 | data += read_data |
| 266 | if not read_data: |
| 267 | telnet.fill_rawq() |
| 268 | except EOFError: |
| 269 | break |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 270 | self.assertTrue(want.startswith(data)) |
| 271 | self.assertEqual(data, want) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 272 | |
| 273 | class nego_collector(object): |
| 274 | def __init__(self, sb_getter=None): |
| 275 | self.seen = b'' |
| 276 | self.sb_getter = sb_getter |
| 277 | self.sb_seen = b'' |
| 278 | |
| 279 | def do_nego(self, sock, cmd, opt): |
| 280 | self.seen += cmd + opt |
| 281 | if cmd == tl.SE and self.sb_getter: |
| 282 | sb_data = self.sb_getter() |
| 283 | self.sb_seen += sb_data |
| 284 | |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 285 | tl = telnetlib |
Jack Diederich | f31f7bb | 2009-09-03 20:37:58 +0000 | [diff] [blame] | 286 | |
| 287 | class WriteTests(TestCase): |
| 288 | '''The only thing that write does is replace each tl.IAC for |
| 289 | tl.IAC+tl.IAC''' |
Jack Diederich | f31f7bb | 2009-09-03 20:37:58 +0000 | [diff] [blame] | 290 | |
| 291 | def test_write(self): |
Jack Diederich | f31f7bb | 2009-09-03 20:37:58 +0000 | [diff] [blame] | 292 | data_sample = [b'data sample without IAC', |
| 293 | b'data sample with' + tl.IAC + b' one IAC', |
| 294 | b'a few' + tl.IAC + tl.IAC + b' iacs' + tl.IAC, |
| 295 | tl.IAC, |
| 296 | b''] |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 297 | for data in data_sample: |
| 298 | telnet = test_telnet() |
| 299 | telnet.write(data) |
| 300 | written = b''.join(telnet.sock.writes) |
| 301 | self.assertEqual(data.replace(tl.IAC,tl.IAC+tl.IAC), written) |
Jack Diederich | 36596a3 | 2009-07-26 22:23:04 +0000 | [diff] [blame] | 302 | |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 303 | class OptionTests(TestCase): |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 304 | # RFC 854 commands |
| 305 | cmds = [tl.AO, tl.AYT, tl.BRK, tl.EC, tl.EL, tl.GA, tl.IP, tl.NOP] |
| 306 | |
| 307 | def _test_command(self, data): |
| 308 | """ helper for testing IAC + cmd """ |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 309 | telnet = test_telnet(data) |
| 310 | data_len = len(b''.join(data)) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 311 | nego = nego_collector() |
| 312 | telnet.set_option_negotiation_callback(nego.do_nego) |
| 313 | txt = telnet.read_all() |
| 314 | cmd = nego.seen |
| 315 | self.assertTrue(len(cmd) > 0) # we expect at least one command |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 316 | self.assertIn(cmd[:1], self.cmds) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 317 | self.assertEqual(cmd[1:2], tl.NOOPT) |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 318 | self.assertEqual(data_len, len(txt + cmd)) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 319 | nego.sb_getter = None # break the nego => telnet cycle |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 320 | |
| 321 | def test_IAC_commands(self): |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 322 | for cmd in self.cmds: |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 323 | self._test_command([tl.IAC, cmd]) |
| 324 | self._test_command([b'x' * 100, tl.IAC, cmd, b'y'*100]) |
| 325 | self._test_command([b'x' * 10, tl.IAC, cmd, b'y'*10]) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 326 | # all at once |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 327 | self._test_command([tl.IAC + cmd for (cmd) in self.cmds]) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 328 | |
| 329 | def test_SB_commands(self): |
| 330 | # RFC 855, subnegotiations portion |
| 331 | send = [tl.IAC + tl.SB + tl.IAC + tl.SE, |
| 332 | tl.IAC + tl.SB + tl.IAC + tl.IAC + tl.IAC + tl.SE, |
| 333 | tl.IAC + tl.SB + tl.IAC + tl.IAC + b'aa' + tl.IAC + tl.SE, |
| 334 | tl.IAC + tl.SB + b'bb' + tl.IAC + tl.IAC + tl.IAC + tl.SE, |
| 335 | tl.IAC + tl.SB + b'cc' + tl.IAC + tl.IAC + b'dd' + tl.IAC + tl.SE, |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 336 | ] |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 337 | telnet = test_telnet(send) |
Jack Diederich | 1c8f38c | 2009-04-10 05:33:26 +0000 | [diff] [blame] | 338 | nego = nego_collector(telnet.read_sb_data) |
| 339 | telnet.set_option_negotiation_callback(nego.do_nego) |
| 340 | txt = telnet.read_all() |
| 341 | self.assertEqual(txt, b'') |
| 342 | want_sb_data = tl.IAC + tl.IAC + b'aabb' + tl.IAC + b'cc' + tl.IAC + b'dd' |
| 343 | self.assertEqual(nego.sb_seen, want_sb_data) |
| 344 | self.assertEqual(b'', telnet.read_sb_data()) |
| 345 | nego.sb_getter = None # break the nego => telnet cycle |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 346 | |
Jack Diederich | f31f7bb | 2009-09-03 20:37:58 +0000 | [diff] [blame] | 347 | def test_debuglevel_reads(self): |
Jack Diederich | 36596a3 | 2009-07-26 22:23:04 +0000 | [diff] [blame] | 348 | # test all the various places that self.msg(...) is called |
| 349 | given_a_expect_b = [ |
| 350 | # Telnet.fill_rawq |
| 351 | (b'a', ": recv b''\n"), |
| 352 | # Telnet.process_rawq |
| 353 | (tl.IAC + bytes([88]), ": IAC 88 not recognized\n"), |
| 354 | (tl.IAC + tl.DO + bytes([1]), ": IAC DO 1\n"), |
| 355 | (tl.IAC + tl.DONT + bytes([1]), ": IAC DONT 1\n"), |
| 356 | (tl.IAC + tl.WILL + bytes([1]), ": IAC WILL 1\n"), |
| 357 | (tl.IAC + tl.WONT + bytes([1]), ": IAC WONT 1\n"), |
Jack Diederich | 36596a3 | 2009-07-26 22:23:04 +0000 | [diff] [blame] | 358 | ] |
| 359 | for a, b in given_a_expect_b: |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 360 | telnet = test_telnet([a]) |
| 361 | telnet.set_debuglevel(1) |
| 362 | txt = telnet.read_all() |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 363 | self.assertIn(b, telnet._messages) |
Jack Diederich | 36596a3 | 2009-07-26 22:23:04 +0000 | [diff] [blame] | 364 | return |
| 365 | |
Jack Diederich | f31f7bb | 2009-09-03 20:37:58 +0000 | [diff] [blame] | 366 | def test_debuglevel_write(self): |
Jack Diederich | 1766b9d | 2009-11-06 17:20:42 +0000 | [diff] [blame] | 367 | telnet = test_telnet() |
Jack Diederich | f31f7bb | 2009-09-03 20:37:58 +0000 | [diff] [blame] | 368 | telnet.set_debuglevel(1) |
Jack Diederich | f31f7bb | 2009-09-03 20:37:58 +0000 | [diff] [blame] | 369 | telnet.write(b'xxx') |
| 370 | expected = "send b'xxx'\n" |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 371 | self.assertIn(expected, telnet._messages) |
Jack Diederich | f31f7bb | 2009-09-03 20:37:58 +0000 | [diff] [blame] | 372 | |
R. David Murray | 32ef70c | 2010-12-14 14:16:20 +0000 | [diff] [blame] | 373 | def test_debug_accepts_str_port(self): |
| 374 | # Issue 10695 |
| 375 | with test_socket([]): |
| 376 | telnet = TelnetAlike('dummy', '0') |
| 377 | telnet._messages = '' |
| 378 | telnet.set_debuglevel(1) |
| 379 | telnet.msg('test') |
| 380 | self.assertRegex(telnet._messages, r'0.*test') |
| 381 | |
| 382 | |
Gregory P. Smith | dad5711 | 2012-07-15 23:42:26 -0700 | [diff] [blame] | 383 | class ExpectTests(ExpectAndReadTestCase): |
| 384 | def test_expect(self): |
| 385 | """ |
| 386 | expect(expected, [timeout]) |
| 387 | Read until the expected string has been seen, or a timeout is |
| 388 | hit (default is no timeout); may block. |
| 389 | """ |
| 390 | want = [b'x' * 10, b'match', b'y' * 10] |
| 391 | telnet = test_telnet(want) |
| 392 | (_,_,data) = telnet.expect([b'match']) |
| 393 | self.assertEqual(data, b''.join(want[:-1])) |
| 394 | |
Gregory P. Smith | dad5711 | 2012-07-15 23:42:26 -0700 | [diff] [blame] | 395 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 396 | if __name__ == '__main__': |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame^] | 397 | unittest.main() |