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