PageRenderTime 3390ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/test/test_telnetlib.py

https://bitbucket.org/mirror/python-trunk/
Python | 370 lines | 328 code | 17 blank | 25 comment | 10 complexity | 1d63907e78a1f9c2d0a5c899fcd4cf3a MD5 | raw file
Possible License(s): BSD-3-Clause, 0BSD
  1. import socket
  2. import threading
  3. import telnetlib
  4. import time
  5. import Queue
  6. from unittest import TestCase
  7. from test import test_support
  8. HOST = test_support.HOST
  9. EOF_sigil = object()
  10. def server(evt, serv, dataq=None):
  11. """ Open a tcp server in three steps
  12. 1) set evt to true to let the parent know we are ready
  13. 2) [optional] if is not False, write the list of data from dataq.get()
  14. to the socket.
  15. 3) set evt to true to let the parent know we're done
  16. """
  17. serv.listen(5)
  18. evt.set()
  19. try:
  20. conn, addr = serv.accept()
  21. if dataq:
  22. data = ''
  23. new_data = dataq.get(True, 0.5)
  24. dataq.task_done()
  25. for item in new_data:
  26. if item == EOF_sigil:
  27. break
  28. if type(item) in [int, float]:
  29. time.sleep(item)
  30. else:
  31. data += item
  32. written = conn.send(data)
  33. data = data[written:]
  34. except socket.timeout:
  35. pass
  36. finally:
  37. serv.close()
  38. evt.set()
  39. class GeneralTests(TestCase):
  40. def setUp(self):
  41. self.evt = threading.Event()
  42. self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  43. self.sock.settimeout(3)
  44. self.port = test_support.bind_port(self.sock)
  45. self.thread = threading.Thread(target=server, args=(self.evt,self.sock))
  46. self.thread.start()
  47. self.evt.wait()
  48. self.evt.clear()
  49. time.sleep(.1)
  50. def tearDown(self):
  51. self.evt.wait()
  52. self.thread.join()
  53. def testBasic(self):
  54. # connects
  55. telnet = telnetlib.Telnet(HOST, self.port)
  56. telnet.sock.close()
  57. def testTimeoutDefault(self):
  58. self.assertTrue(socket.getdefaulttimeout() is None)
  59. socket.setdefaulttimeout(30)
  60. try:
  61. telnet = telnetlib.Telnet("localhost", self.port)
  62. finally:
  63. socket.setdefaulttimeout(None)
  64. self.assertEqual(telnet.sock.gettimeout(), 30)
  65. telnet.sock.close()
  66. def testTimeoutNone(self):
  67. # None, having other default
  68. self.assertTrue(socket.getdefaulttimeout() is None)
  69. socket.setdefaulttimeout(30)
  70. try:
  71. telnet = telnetlib.Telnet(HOST, self.port, timeout=None)
  72. finally:
  73. socket.setdefaulttimeout(None)
  74. self.assertTrue(telnet.sock.gettimeout() is None)
  75. telnet.sock.close()
  76. def testTimeoutValue(self):
  77. telnet = telnetlib.Telnet("localhost", self.port, timeout=30)
  78. self.assertEqual(telnet.sock.gettimeout(), 30)
  79. telnet.sock.close()
  80. def testTimeoutOpen(self):
  81. telnet = telnetlib.Telnet()
  82. telnet.open("localhost", self.port, timeout=30)
  83. self.assertEqual(telnet.sock.gettimeout(), 30)
  84. telnet.sock.close()
  85. def _read_setUp(self):
  86. self.evt = threading.Event()
  87. self.dataq = Queue.Queue()
  88. self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  89. self.sock.settimeout(3)
  90. self.port = test_support.bind_port(self.sock)
  91. self.thread = threading.Thread(target=server, args=(self.evt,self.sock, self.dataq))
  92. self.thread.start()
  93. self.evt.wait()
  94. self.evt.clear()
  95. time.sleep(.1)
  96. def _read_tearDown(self):
  97. self.evt.wait()
  98. self.thread.join()
  99. class ReadTests(TestCase):
  100. setUp = _read_setUp
  101. tearDown = _read_tearDown
  102. # use a similar approach to testing timeouts as test_timeout.py
  103. # these will never pass 100% but make the fuzz big enough that it is rare
  104. block_long = 0.6
  105. block_short = 0.3
  106. def test_read_until_A(self):
  107. """
  108. read_until(expected, [timeout])
  109. Read until the expected string has been seen, or a timeout is
  110. hit (default is no timeout); may block.
  111. """
  112. want = ['x' * 10, 'match', 'y' * 10, EOF_sigil]
  113. self.dataq.put(want)
  114. telnet = telnetlib.Telnet(HOST, self.port)
  115. self.dataq.join()
  116. data = telnet.read_until('match')
  117. self.assertEqual(data, ''.join(want[:-2]))
  118. def test_read_until_B(self):
  119. # test the timeout - it does NOT raise socket.timeout
  120. want = ['hello', self.block_long, 'not seen', EOF_sigil]
  121. self.dataq.put(want)
  122. telnet = telnetlib.Telnet(HOST, self.port)
  123. self.dataq.join()
  124. data = telnet.read_until('not seen', self.block_short)
  125. self.assertEqual(data, want[0])
  126. self.assertEqual(telnet.read_all(), 'not seen')
  127. def test_read_all_A(self):
  128. """
  129. read_all()
  130. Read all data until EOF; may block.
  131. """
  132. want = ['x' * 500, 'y' * 500, 'z' * 500, EOF_sigil]
  133. self.dataq.put(want)
  134. telnet = telnetlib.Telnet(HOST, self.port)
  135. self.dataq.join()
  136. data = telnet.read_all()
  137. self.assertEqual(data, ''.join(want[:-1]))
  138. return
  139. def _test_blocking(self, func):
  140. self.dataq.put([self.block_long, EOF_sigil])
  141. self.dataq.join()
  142. start = time.time()
  143. data = func()
  144. self.assertTrue(self.block_short <= time.time() - start)
  145. def test_read_all_B(self):
  146. self._test_blocking(telnetlib.Telnet(HOST, self.port).read_all)
  147. def test_read_all_C(self):
  148. self.dataq.put([EOF_sigil])
  149. telnet = telnetlib.Telnet(HOST, self.port)
  150. self.dataq.join()
  151. telnet.read_all()
  152. telnet.read_all() # shouldn't raise
  153. def test_read_some_A(self):
  154. """
  155. read_some()
  156. Read at least one byte or EOF; may block.
  157. """
  158. # test 'at least one byte'
  159. want = ['x' * 500, EOF_sigil]
  160. self.dataq.put(want)
  161. telnet = telnetlib.Telnet(HOST, self.port)
  162. self.dataq.join()
  163. data = telnet.read_all()
  164. self.assertTrue(len(data) >= 1)
  165. def test_read_some_B(self):
  166. # test EOF
  167. self.dataq.put([EOF_sigil])
  168. telnet = telnetlib.Telnet(HOST, self.port)
  169. self.dataq.join()
  170. self.assertEqual('', telnet.read_some())
  171. def test_read_some_C(self):
  172. self._test_blocking(telnetlib.Telnet(HOST, self.port).read_some)
  173. def _test_read_any_eager_A(self, func_name):
  174. """
  175. read_very_eager()
  176. Read all data available already queued or on the socket,
  177. without blocking.
  178. """
  179. want = [self.block_long, 'x' * 100, 'y' * 100, EOF_sigil]
  180. expects = want[1] + want[2]
  181. self.dataq.put(want)
  182. telnet = telnetlib.Telnet(HOST, self.port)
  183. self.dataq.join()
  184. func = getattr(telnet, func_name)
  185. data = ''
  186. while True:
  187. try:
  188. data += func()
  189. self.assertTrue(expects.startswith(data))
  190. except EOFError:
  191. break
  192. self.assertEqual(expects, data)
  193. def _test_read_any_eager_B(self, func_name):
  194. # test EOF
  195. self.dataq.put([EOF_sigil])
  196. telnet = telnetlib.Telnet(HOST, self.port)
  197. self.dataq.join()
  198. time.sleep(self.block_short)
  199. func = getattr(telnet, func_name)
  200. self.assertRaises(EOFError, func)
  201. # read_eager and read_very_eager make the same gaurantees
  202. # (they behave differently but we only test the gaurantees)
  203. def test_read_very_eager_A(self):
  204. self._test_read_any_eager_A('read_very_eager')
  205. def test_read_very_eager_B(self):
  206. self._test_read_any_eager_B('read_very_eager')
  207. def test_read_eager_A(self):
  208. self._test_read_any_eager_A('read_eager')
  209. def test_read_eager_B(self):
  210. self._test_read_any_eager_B('read_eager')
  211. # NB -- we need to test the IAC block which is mentioned in the docstring
  212. # but not in the module docs
  213. def _test_read_any_lazy_B(self, func_name):
  214. self.dataq.put([EOF_sigil])
  215. telnet = telnetlib.Telnet(HOST, self.port)
  216. self.dataq.join()
  217. func = getattr(telnet, func_name)
  218. telnet.fill_rawq()
  219. self.assertRaises(EOFError, func)
  220. def test_read_lazy_A(self):
  221. want = ['x' * 100, EOF_sigil]
  222. self.dataq.put(want)
  223. telnet = telnetlib.Telnet(HOST, self.port)
  224. self.dataq.join()
  225. time.sleep(self.block_short)
  226. self.assertEqual('', telnet.read_lazy())
  227. data = ''
  228. while True:
  229. try:
  230. read_data = telnet.read_lazy()
  231. data += read_data
  232. if not read_data:
  233. telnet.fill_rawq()
  234. except EOFError:
  235. break
  236. self.assertTrue(want[0].startswith(data))
  237. self.assertEqual(data, want[0])
  238. def test_read_lazy_B(self):
  239. self._test_read_any_lazy_B('read_lazy')
  240. def test_read_very_lazy_A(self):
  241. want = ['x' * 100, EOF_sigil]
  242. self.dataq.put(want)
  243. telnet = telnetlib.Telnet(HOST, self.port)
  244. self.dataq.join()
  245. time.sleep(self.block_short)
  246. self.assertEqual('', telnet.read_very_lazy())
  247. data = ''
  248. while True:
  249. try:
  250. read_data = telnet.read_very_lazy()
  251. except EOFError:
  252. break
  253. data += read_data
  254. if not read_data:
  255. telnet.fill_rawq()
  256. self.assertEqual('', telnet.cookedq)
  257. telnet.process_rawq()
  258. self.assertTrue(want[0].startswith(data))
  259. self.assertEqual(data, want[0])
  260. def test_read_very_lazy_B(self):
  261. self._test_read_any_lazy_B('read_very_lazy')
  262. class nego_collector(object):
  263. def __init__(self, sb_getter=None):
  264. self.seen = ''
  265. self.sb_getter = sb_getter
  266. self.sb_seen = ''
  267. def do_nego(self, sock, cmd, opt):
  268. self.seen += cmd + opt
  269. if cmd == tl.SE and self.sb_getter:
  270. sb_data = self.sb_getter()
  271. self.sb_seen += sb_data
  272. tl = telnetlib
  273. class OptionTests(TestCase):
  274. setUp = _read_setUp
  275. tearDown = _read_tearDown
  276. # RFC 854 commands
  277. cmds = [tl.AO, tl.AYT, tl.BRK, tl.EC, tl.EL, tl.GA, tl.IP, tl.NOP]
  278. def _test_command(self, data):
  279. """ helper for testing IAC + cmd """
  280. self.setUp()
  281. self.dataq.put(data)
  282. telnet = telnetlib.Telnet(HOST, self.port)
  283. self.dataq.join()
  284. nego = nego_collector()
  285. telnet.set_option_negotiation_callback(nego.do_nego)
  286. txt = telnet.read_all()
  287. cmd = nego.seen
  288. self.assertTrue(len(cmd) > 0) # we expect at least one command
  289. self.assertTrue(cmd[0] in self.cmds)
  290. self.assertEqual(cmd[1], tl.NOOPT)
  291. self.assertEqual(len(''.join(data[:-1])), len(txt + cmd))
  292. nego.sb_getter = None # break the nego => telnet cycle
  293. self.tearDown()
  294. def test_IAC_commands(self):
  295. # reset our setup
  296. self.dataq.put([EOF_sigil])
  297. telnet = telnetlib.Telnet(HOST, self.port)
  298. self.dataq.join()
  299. self.tearDown()
  300. for cmd in self.cmds:
  301. self._test_command(['x' * 100, tl.IAC + cmd, 'y'*100, EOF_sigil])
  302. self._test_command(['x' * 10, tl.IAC + cmd, 'y'*10, EOF_sigil])
  303. self._test_command([tl.IAC + cmd, EOF_sigil])
  304. # all at once
  305. self._test_command([tl.IAC + cmd for (cmd) in self.cmds] + [EOF_sigil])
  306. self.assertEqual('', telnet.read_sb_data())
  307. def test_SB_commands(self):
  308. # RFC 855, subnegotiations portion
  309. send = [tl.IAC + tl.SB + tl.IAC + tl.SE,
  310. tl.IAC + tl.SB + tl.IAC + tl.IAC + tl.IAC + tl.SE,
  311. tl.IAC + tl.SB + tl.IAC + tl.IAC + 'aa' + tl.IAC + tl.SE,
  312. tl.IAC + tl.SB + 'bb' + tl.IAC + tl.IAC + tl.IAC + tl.SE,
  313. tl.IAC + tl.SB + 'cc' + tl.IAC + tl.IAC + 'dd' + tl.IAC + tl.SE,
  314. EOF_sigil,
  315. ]
  316. self.dataq.put(send)
  317. telnet = telnetlib.Telnet(HOST, self.port)
  318. self.dataq.join()
  319. nego = nego_collector(telnet.read_sb_data)
  320. telnet.set_option_negotiation_callback(nego.do_nego)
  321. txt = telnet.read_all()
  322. self.assertEqual(txt, '')
  323. want_sb_data = tl.IAC + tl.IAC + 'aabb' + tl.IAC + 'cc' + tl.IAC + 'dd'
  324. self.assertEqual(nego.sb_seen, want_sb_data)
  325. self.assertEqual('', telnet.read_sb_data())
  326. nego.sb_getter = None # break the nego => telnet cycle
  327. def test_main(verbose=None):
  328. test_support.run_unittest(GeneralTests, ReadTests, OptionTests)
  329. if __name__ == '__main__':
  330. test_main()