Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(87697)

Side by Side Diff: Lib/test/test_telnetlib.py

Issue 16510: Using appropriate checks in tests
Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_sys.py ('k') | Lib/test/test_tempfile.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import socket 1 import socket
2 import selectors 2 import selectors
3 import telnetlib 3 import telnetlib
4 import time 4 import time
5 import contextlib 5 import contextlib
6 6
7 from unittest import TestCase 7 from unittest import TestCase
8 from test import support 8 from test import support
9 threading = support.import_module('threading') 9 threading = support.import_module('threading')
10 10
(...skipping 25 matching lines...) Expand all
36 def tearDown(self): 36 def tearDown(self):
37 self.thread.join() 37 self.thread.join()
38 del self.thread # Clear out any dangling Thread objects. 38 del self.thread # Clear out any dangling Thread objects.
39 39
40 def testBasic(self): 40 def testBasic(self):
41 # connects 41 # connects
42 telnet = telnetlib.Telnet(HOST, self.port) 42 telnet = telnetlib.Telnet(HOST, self.port)
43 telnet.sock.close() 43 telnet.sock.close()
44 44
45 def testTimeoutDefault(self): 45 def testTimeoutDefault(self):
46 self.assertTrue(socket.getdefaulttimeout() is None) 46 self.assertIsNone(socket.getdefaulttimeout())
47 socket.setdefaulttimeout(30) 47 socket.setdefaulttimeout(30)
48 try: 48 try:
49 telnet = telnetlib.Telnet(HOST, self.port) 49 telnet = telnetlib.Telnet(HOST, self.port)
50 finally: 50 finally:
51 socket.setdefaulttimeout(None) 51 socket.setdefaulttimeout(None)
52 self.assertEqual(telnet.sock.gettimeout(), 30) 52 self.assertEqual(telnet.sock.gettimeout(), 30)
53 telnet.sock.close() 53 telnet.sock.close()
54 54
55 def testTimeoutNone(self): 55 def testTimeoutNone(self):
56 # None, having other default 56 # None, having other default
57 self.assertTrue(socket.getdefaulttimeout() is None) 57 self.assertIsNone(socket.getdefaulttimeout())
58 socket.setdefaulttimeout(30) 58 socket.setdefaulttimeout(30)
59 try: 59 try:
60 telnet = telnetlib.Telnet(HOST, self.port, timeout=None) 60 telnet = telnetlib.Telnet(HOST, self.port, timeout=None)
61 finally: 61 finally:
62 socket.setdefaulttimeout(None) 62 socket.setdefaulttimeout(None)
63 self.assertTrue(telnet.sock.gettimeout() is None) 63 self.assertIsNone(telnet.sock.gettimeout())
64 telnet.sock.close() 64 telnet.sock.close()
65 65
66 def testTimeoutValue(self): 66 def testTimeoutValue(self):
67 telnet = telnetlib.Telnet(HOST, self.port, timeout=30) 67 telnet = telnetlib.Telnet(HOST, self.port, timeout=30)
68 self.assertEqual(telnet.sock.gettimeout(), 30) 68 self.assertEqual(telnet.sock.gettimeout(), 30)
69 telnet.sock.close() 69 telnet.sock.close()
70 70
71 def testTimeoutOpen(self): 71 def testTimeoutOpen(self):
72 telnet = telnetlib.Telnet() 72 telnet = telnetlib.Telnet()
73 telnet.open(HOST, self.port, timeout=30) 73 telnet.open(HOST, self.port, timeout=30)
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 return 203 return
204 204
205 def test_read_some(self): 205 def test_read_some(self):
206 """ 206 """
207 read_some() 207 read_some()
208 Read at least one byte or EOF; may block. 208 Read at least one byte or EOF; may block.
209 """ 209 """
210 # test 'at least one byte' 210 # test 'at least one byte'
211 telnet = test_telnet([b'x' * 500]) 211 telnet = test_telnet([b'x' * 500])
212 data = telnet.read_some() 212 data = telnet.read_some()
213 self.assertTrue(len(data) >= 1) 213 self.assertGreaterEqual(len(data), 1)
214 # test EOF 214 # test EOF
215 telnet = test_telnet() 215 telnet = test_telnet()
216 data = telnet.read_some() 216 data = telnet.read_some()
217 self.assertEqual(b'', data) 217 self.assertEqual(b'', data)
218 218
219 def _read_eager(self, func_name): 219 def _read_eager(self, func_name):
220 """ 220 """
221 read_*_eager() 221 read_*_eager()
222 Read all data available already queued or on the socket, 222 Read all data available already queued or on the socket,
223 without blocking. 223 without blocking.
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 cmds = [tl.AO, tl.AYT, tl.BRK, tl.EC, tl.EL, tl.GA, tl.IP, tl.NOP] 305 cmds = [tl.AO, tl.AYT, tl.BRK, tl.EC, tl.EL, tl.GA, tl.IP, tl.NOP]
306 306
307 def _test_command(self, data): 307 def _test_command(self, data):
308 """ helper for testing IAC + cmd """ 308 """ helper for testing IAC + cmd """
309 telnet = test_telnet(data) 309 telnet = test_telnet(data)
310 data_len = len(b''.join(data)) 310 data_len = len(b''.join(data))
311 nego = nego_collector() 311 nego = nego_collector()
312 telnet.set_option_negotiation_callback(nego.do_nego) 312 telnet.set_option_negotiation_callback(nego.do_nego)
313 txt = telnet.read_all() 313 txt = telnet.read_all()
314 cmd = nego.seen 314 cmd = nego.seen
315 self.assertTrue(len(cmd) > 0) # we expect at least one command 315 self.assertGreater(len(cmd), 0) # we expect at least one command
316 self.assertIn(cmd[:1], self.cmds) 316 self.assertIn(cmd[:1], self.cmds)
317 self.assertEqual(cmd[1:2], tl.NOOPT) 317 self.assertEqual(cmd[1:2], tl.NOOPT)
318 self.assertEqual(data_len, len(txt + cmd)) 318 self.assertEqual(data_len, len(txt + cmd))
319 nego.sb_getter = None # break the nego => telnet cycle 319 nego.sb_getter = None # break the nego => telnet cycle
320 320
321 def test_IAC_commands(self): 321 def test_IAC_commands(self):
322 for cmd in self.cmds: 322 for cmd in self.cmds:
323 self._test_command([tl.IAC, cmd]) 323 self._test_command([tl.IAC, cmd])
324 self._test_command([b'x' * 100, tl.IAC, cmd, b'y'*100]) 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]) 325 self._test_command([b'x' * 10, tl.IAC, cmd, b'y'*10])
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 (_,_,data) = telnet.expect([b'match']) 392 (_,_,data) = telnet.expect([b'match'])
393 self.assertEqual(data, b''.join(want[:-1])) 393 self.assertEqual(data, b''.join(want[:-1]))
394 394
395 395
396 def test_main(verbose=None): 396 def test_main(verbose=None):
397 support.run_unittest(GeneralTests, ReadTests, WriteTests, OptionTests, 397 support.run_unittest(GeneralTests, ReadTests, WriteTests, OptionTests,
398 ExpectTests) 398 ExpectTests)
399 399
400 if __name__ == '__main__': 400 if __name__ == '__main__':
401 test_main() 401 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_sys.py ('k') | Lib/test/test_tempfile.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+