~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/twisted/conch/test/test_telnet.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- test-case-name: twisted.conch.test.test_telnet -*-
 
2
# Copyright (c) 2001-2009 Twisted Matrix Laboratories.
 
3
# See LICENSE for details.
 
4
 
 
5
"""
 
6
Tests for L{twisted.conch.telnet}.
 
7
"""
 
8
 
 
9
from zope.interface import implements
 
10
 
 
11
from twisted.internet import defer
 
12
 
 
13
from twisted.conch import telnet
 
14
 
 
15
from twisted.trial import unittest
 
16
from twisted.test import proto_helpers
 
17
 
 
18
 
 
19
class TestProtocol:
 
20
    implements(telnet.ITelnetProtocol)
 
21
 
 
22
    localEnableable = ()
 
23
    remoteEnableable = ()
 
24
 
 
25
    def __init__(self):
 
26
        self.bytes = ''
 
27
        self.subcmd = ''
 
28
        self.calls = []
 
29
 
 
30
        self.enabledLocal = []
 
31
        self.enabledRemote = []
 
32
        self.disabledLocal = []
 
33
        self.disabledRemote = []
 
34
 
 
35
    def makeConnection(self, transport):
 
36
        d = transport.negotiationMap = {}
 
37
        d['\x12'] = self.neg_TEST_COMMAND
 
38
 
 
39
        d = transport.commandMap = transport.commandMap.copy()
 
40
        for cmd in ('NOP', 'DM', 'BRK', 'IP', 'AO', 'AYT', 'EC', 'EL', 'GA'):
 
41
            d[getattr(telnet, cmd)] = lambda arg, cmd=cmd: self.calls.append(cmd)
 
42
 
 
43
    def dataReceived(self, bytes):
 
44
        self.bytes += bytes
 
45
 
 
46
    def connectionLost(self, reason):
 
47
        pass
 
48
 
 
49
    def neg_TEST_COMMAND(self, payload):
 
50
        self.subcmd = payload
 
51
 
 
52
    def enableLocal(self, option):
 
53
        if option in self.localEnableable:
 
54
            self.enabledLocal.append(option)
 
55
            return True
 
56
        return False
 
57
 
 
58
    def disableLocal(self, option):
 
59
        self.disabledLocal.append(option)
 
60
 
 
61
    def enableRemote(self, option):
 
62
        if option in self.remoteEnableable:
 
63
            self.enabledRemote.append(option)
 
64
            return True
 
65
        return False
 
66
 
 
67
    def disableRemote(self, option):
 
68
        self.disabledRemote.append(option)
 
69
 
 
70
 
 
71
 
 
72
class TelnetTransportTestCase(unittest.TestCase):
 
73
    """
 
74
    Tests for L{telnet.TelnetTransport}.
 
75
    """
 
76
    def setUp(self):
 
77
        self.p = telnet.TelnetTransport(TestProtocol)
 
78
        self.t = proto_helpers.StringTransport()
 
79
        self.p.makeConnection(self.t)
 
80
 
 
81
    def testRegularBytes(self):
 
82
        # Just send a bunch of bytes.  None of these do anything
 
83
        # with telnet.  They should pass right through to the
 
84
        # application layer.
 
85
        h = self.p.protocol
 
86
 
 
87
        L = ["here are some bytes la la la",
 
88
             "some more arrive here",
 
89
             "lots of bytes to play with",
 
90
             "la la la",
 
91
             "ta de da",
 
92
             "dum"]
 
93
        for b in L:
 
94
            self.p.dataReceived(b)
 
95
 
 
96
        self.assertEquals(h.bytes, ''.join(L))
 
97
 
 
98
    def testNewlineHandling(self):
 
99
        # Send various kinds of newlines and make sure they get translated
 
100
        # into \n.
 
101
        h = self.p.protocol
 
102
 
 
103
        L = ["here is the first line\r\n",
 
104
             "here is the second line\r\0",
 
105
             "here is the third line\r\n",
 
106
             "here is the last line\r\0"]
 
107
 
 
108
        for b in L:
 
109
            self.p.dataReceived(b)
 
110
 
 
111
        self.assertEquals(h.bytes, L[0][:-2] + '\n' +
 
112
                          L[1][:-2] + '\r' +
 
113
                          L[2][:-2] + '\n' +
 
114
                          L[3][:-2] + '\r')
 
115
 
 
116
    def testIACEscape(self):
 
117
        # Send a bunch of bytes and a couple quoted \xFFs.  Unquoted,
 
118
        # \xFF is a telnet command.  Quoted, one of them from each pair
 
119
        # should be passed through to the application layer.
 
120
        h = self.p.protocol
 
121
 
 
122
        L = ["here are some bytes\xff\xff with an embedded IAC",
 
123
             "and here is a test of a border escape\xff",
 
124
             "\xff did you get that IAC?"]
 
125
 
 
126
        for b in L:
 
127
            self.p.dataReceived(b)
 
128
 
 
129
        self.assertEquals(h.bytes, ''.join(L).replace('\xff\xff', '\xff'))
 
130
 
 
131
    def _simpleCommandTest(self, cmdName):
 
132
        # Send a single simple telnet command and make sure
 
133
        # it gets noticed and the appropriate method gets
 
134
        # called.
 
135
        h = self.p.protocol
 
136
 
 
137
        cmd = telnet.IAC + getattr(telnet, cmdName)
 
138
        L = ["Here's some bytes, tra la la",
 
139
             "But ono!" + cmd + " an interrupt"]
 
140
 
 
141
        for b in L:
 
142
            self.p.dataReceived(b)
 
143
 
 
144
        self.assertEquals(h.calls, [cmdName])
 
145
        self.assertEquals(h.bytes, ''.join(L).replace(cmd, ''))
 
146
 
 
147
    def testInterrupt(self):
 
148
        self._simpleCommandTest("IP")
 
149
 
 
150
    def testNoOperation(self):
 
151
        self._simpleCommandTest("NOP")
 
152
 
 
153
    def testDataMark(self):
 
154
        self._simpleCommandTest("DM")
 
155
 
 
156
    def testBreak(self):
 
157
        self._simpleCommandTest("BRK")
 
158
 
 
159
    def testAbortOutput(self):
 
160
        self._simpleCommandTest("AO")
 
161
 
 
162
    def testAreYouThere(self):
 
163
        self._simpleCommandTest("AYT")
 
164
 
 
165
    def testEraseCharacter(self):
 
166
        self._simpleCommandTest("EC")
 
167
 
 
168
    def testEraseLine(self):
 
169
        self._simpleCommandTest("EL")
 
170
 
 
171
    def testGoAhead(self):
 
172
        self._simpleCommandTest("GA")
 
173
 
 
174
    def testSubnegotiation(self):
 
175
        # Send a subnegotiation command and make sure it gets
 
176
        # parsed and that the correct method is called.
 
177
        h = self.p.protocol
 
178
 
 
179
        cmd = telnet.IAC + telnet.SB + '\x12hello world' + telnet.IAC + telnet.SE
 
180
        L = ["These are some bytes but soon" + cmd,
 
181
             "there will be some more"]
 
182
 
 
183
        for b in L:
 
184
            self.p.dataReceived(b)
 
185
 
 
186
        self.assertEquals(h.bytes, ''.join(L).replace(cmd, ''))
 
187
        self.assertEquals(h.subcmd, list("hello world"))
 
188
 
 
189
    def testSubnegotiationWithEmbeddedSE(self):
 
190
        # Send a subnegotiation command with an embedded SE.  Make sure
 
191
        # that SE gets passed to the correct method.
 
192
        h = self.p.protocol
 
193
 
 
194
        cmd = (telnet.IAC + telnet.SB +
 
195
               '\x12' + telnet.SE +
 
196
               telnet.IAC + telnet.SE)
 
197
 
 
198
        L = ["Some bytes are here" + cmd + "and here",
 
199
             "and here"]
 
200
 
 
201
        for b in L:
 
202
            self.p.dataReceived(b)
 
203
 
 
204
        self.assertEquals(h.bytes, ''.join(L).replace(cmd, ''))
 
205
        self.assertEquals(h.subcmd, [telnet.SE])
 
206
 
 
207
    def testBoundarySubnegotiation(self):
 
208
        # Send a subnegotiation command.  Split it at every possible byte boundary
 
209
        # and make sure it always gets parsed and that it is passed to the correct
 
210
        # method.
 
211
        cmd = (telnet.IAC + telnet.SB +
 
212
               '\x12' + telnet.SE + 'hello' +
 
213
               telnet.IAC + telnet.SE)
 
214
 
 
215
        for i in range(len(cmd)):
 
216
            h = self.p.protocol = TestProtocol()
 
217
            h.makeConnection(self.p)
 
218
 
 
219
            a, b = cmd[:i], cmd[i:]
 
220
            L = ["first part" + a,
 
221
                 b + "last part"]
 
222
 
 
223
            for bytes in L:
 
224
                self.p.dataReceived(bytes)
 
225
 
 
226
            self.assertEquals(h.bytes, ''.join(L).replace(cmd, ''))
 
227
            self.assertEquals(h.subcmd, [telnet.SE] + list('hello'))
 
228
 
 
229
    def _enabledHelper(self, o, eL=[], eR=[], dL=[], dR=[]):
 
230
        self.assertEquals(o.enabledLocal, eL)
 
231
        self.assertEquals(o.enabledRemote, eR)
 
232
        self.assertEquals(o.disabledLocal, dL)
 
233
        self.assertEquals(o.disabledRemote, dR)
 
234
 
 
235
    def testRefuseWill(self):
 
236
        # Try to enable an option.  The server should refuse to enable it.
 
237
        cmd = telnet.IAC + telnet.WILL + '\x12'
 
238
 
 
239
        bytes = "surrounding bytes" + cmd + "to spice things up"
 
240
        self.p.dataReceived(bytes)
 
241
 
 
242
        self.assertEquals(self.p.protocol.bytes, bytes.replace(cmd, ''))
 
243
        self.assertEquals(self.t.value(), telnet.IAC + telnet.DONT + '\x12')
 
244
        self._enabledHelper(self.p.protocol)
 
245
 
 
246
    def testRefuseDo(self):
 
247
        # Try to enable an option.  The server should refuse to enable it.
 
248
        cmd = telnet.IAC + telnet.DO + '\x12'
 
249
 
 
250
        bytes = "surrounding bytes" + cmd + "to spice things up"
 
251
        self.p.dataReceived(bytes)
 
252
 
 
253
        self.assertEquals(self.p.protocol.bytes, bytes.replace(cmd, ''))
 
254
        self.assertEquals(self.t.value(), telnet.IAC + telnet.WONT + '\x12')
 
255
        self._enabledHelper(self.p.protocol)
 
256
 
 
257
    def testAcceptDo(self):
 
258
        # Try to enable an option.  The option is in our allowEnable
 
259
        # list, so we will allow it to be enabled.
 
260
        cmd = telnet.IAC + telnet.DO + '\x19'
 
261
        bytes = 'padding' + cmd + 'trailer'
 
262
 
 
263
        h = self.p.protocol
 
264
        h.localEnableable = ('\x19',)
 
265
        self.p.dataReceived(bytes)
 
266
 
 
267
        self.assertEquals(self.t.value(), telnet.IAC + telnet.WILL + '\x19')
 
268
        self._enabledHelper(h, eL=['\x19'])
 
269
 
 
270
    def testAcceptWill(self):
 
271
        # Same as testAcceptDo, but reversed.
 
272
        cmd = telnet.IAC + telnet.WILL + '\x91'
 
273
        bytes = 'header' + cmd + 'padding'
 
274
 
 
275
        h = self.p.protocol
 
276
        h.remoteEnableable = ('\x91',)
 
277
        self.p.dataReceived(bytes)
 
278
 
 
279
        self.assertEquals(self.t.value(), telnet.IAC + telnet.DO + '\x91')
 
280
        self._enabledHelper(h, eR=['\x91'])
 
281
 
 
282
    def testAcceptWont(self):
 
283
        # Try to disable an option.  The server must allow any option to
 
284
        # be disabled at any time.  Make sure it disables it and sends
 
285
        # back an acknowledgement of this.
 
286
        cmd = telnet.IAC + telnet.WONT + '\x29'
 
287
 
 
288
        # Jimmy it - after these two lines, the server will be in a state
 
289
        # such that it believes the option to have been previously enabled
 
290
        # via normal negotiation.
 
291
        s = self.p.getOptionState('\x29')
 
292
        s.him.state = 'yes'
 
293
 
 
294
        bytes = "fiddle dee" + cmd
 
295
        self.p.dataReceived(bytes)
 
296
 
 
297
        self.assertEquals(self.p.protocol.bytes, bytes.replace(cmd, ''))
 
298
        self.assertEquals(self.t.value(), telnet.IAC + telnet.DONT + '\x29')
 
299
        self.assertEquals(s.him.state, 'no')
 
300
        self._enabledHelper(self.p.protocol, dR=['\x29'])
 
301
 
 
302
    def testAcceptDont(self):
 
303
        # Try to disable an option.  The server must allow any option to
 
304
        # be disabled at any time.  Make sure it disables it and sends
 
305
        # back an acknowledgement of this.
 
306
        cmd = telnet.IAC + telnet.DONT + '\x29'
 
307
 
 
308
        # Jimmy it - after these two lines, the server will be in a state
 
309
        # such that it believes the option to have beenp previously enabled
 
310
        # via normal negotiation.
 
311
        s = self.p.getOptionState('\x29')
 
312
        s.us.state = 'yes'
 
313
 
 
314
        bytes = "fiddle dum " + cmd
 
315
        self.p.dataReceived(bytes)
 
316
 
 
317
        self.assertEquals(self.p.protocol.bytes, bytes.replace(cmd, ''))
 
318
        self.assertEquals(self.t.value(), telnet.IAC + telnet.WONT + '\x29')
 
319
        self.assertEquals(s.us.state, 'no')
 
320
        self._enabledHelper(self.p.protocol, dL=['\x29'])
 
321
 
 
322
    def testIgnoreWont(self):
 
323
        # Try to disable an option.  The option is already disabled.  The
 
324
        # server should send nothing in response to this.
 
325
        cmd = telnet.IAC + telnet.WONT + '\x47'
 
326
 
 
327
        bytes = "dum de dum" + cmd + "tra la la"
 
328
        self.p.dataReceived(bytes)
 
329
 
 
330
        self.assertEquals(self.p.protocol.bytes, bytes.replace(cmd, ''))
 
331
        self.assertEquals(self.t.value(), '')
 
332
        self._enabledHelper(self.p.protocol)
 
333
 
 
334
    def testIgnoreDont(self):
 
335
        # Try to disable an option.  The option is already disabled.  The
 
336
        # server should send nothing in response to this.  Doing so could
 
337
        # lead to a negotiation loop.
 
338
        cmd = telnet.IAC + telnet.DONT + '\x47'
 
339
 
 
340
        bytes = "dum de dum" + cmd + "tra la la"
 
341
        self.p.dataReceived(bytes)
 
342
 
 
343
        self.assertEquals(self.p.protocol.bytes, bytes.replace(cmd, ''))
 
344
        self.assertEquals(self.t.value(), '')
 
345
        self._enabledHelper(self.p.protocol)
 
346
 
 
347
    def testIgnoreWill(self):
 
348
        # Try to enable an option.  The option is already enabled.  The
 
349
        # server should send nothing in response to this.  Doing so could
 
350
        # lead to a negotiation loop.
 
351
        cmd = telnet.IAC + telnet.WILL + '\x56'
 
352
 
 
353
        # Jimmy it - after these two lines, the server will be in a state
 
354
        # such that it believes the option to have been previously enabled
 
355
        # via normal negotiation.
 
356
        s = self.p.getOptionState('\x56')
 
357
        s.him.state = 'yes'
 
358
 
 
359
        bytes = "tra la la" + cmd + "dum de dum"
 
360
        self.p.dataReceived(bytes)
 
361
 
 
362
        self.assertEquals(self.p.protocol.bytes, bytes.replace(cmd, ''))
 
363
        self.assertEquals(self.t.value(), '')
 
364
        self._enabledHelper(self.p.protocol)
 
365
 
 
366
    def testIgnoreDo(self):
 
367
        # Try to enable an option.  The option is already enabled.  The
 
368
        # server should send nothing in response to this.  Doing so could
 
369
        # lead to a negotiation loop.
 
370
        cmd = telnet.IAC + telnet.DO + '\x56'
 
371
 
 
372
        # Jimmy it - after these two lines, the server will be in a state
 
373
        # such that it believes the option to have been previously enabled
 
374
        # via normal negotiation.
 
375
        s = self.p.getOptionState('\x56')
 
376
        s.us.state = 'yes'
 
377
 
 
378
        bytes = "tra la la" + cmd + "dum de dum"
 
379
        self.p.dataReceived(bytes)
 
380
 
 
381
        self.assertEquals(self.p.protocol.bytes, bytes.replace(cmd, ''))
 
382
        self.assertEquals(self.t.value(), '')
 
383
        self._enabledHelper(self.p.protocol)
 
384
 
 
385
    def testAcceptedEnableRequest(self):
 
386
        # Try to enable an option through the user-level API.  This
 
387
        # returns a Deferred that fires when negotiation about the option
 
388
        # finishes.  Make sure it fires, make sure state gets updated
 
389
        # properly, make sure the result indicates the option was enabled.
 
390
        d = self.p.do('\x42')
 
391
 
 
392
        h = self.p.protocol
 
393
        h.remoteEnableable = ('\x42',)
 
394
 
 
395
        self.assertEquals(self.t.value(), telnet.IAC + telnet.DO + '\x42')
 
396
 
 
397
        self.p.dataReceived(telnet.IAC + telnet.WILL + '\x42')
 
398
 
 
399
        d.addCallback(self.assertEquals, True)
 
400
        d.addCallback(lambda _:  self._enabledHelper(h, eR=['\x42']))
 
401
        return d
 
402
 
 
403
    def testRefusedEnableRequest(self):
 
404
        # Try to enable an option through the user-level API.  This
 
405
        # returns a Deferred that fires when negotiation about the option
 
406
        # finishes.  Make sure it fires, make sure state gets updated
 
407
        # properly, make sure the result indicates the option was enabled.
 
408
        d = self.p.do('\x42')
 
409
 
 
410
        self.assertEquals(self.t.value(), telnet.IAC + telnet.DO + '\x42')
 
411
 
 
412
        self.p.dataReceived(telnet.IAC + telnet.WONT + '\x42')
 
413
 
 
414
        d = self.assertFailure(d, telnet.OptionRefused)
 
415
        d.addCallback(lambda _: self._enabledHelper(self.p.protocol))
 
416
        return d
 
417
 
 
418
    def testAcceptedDisableRequest(self):
 
419
        # Try to disable an option through the user-level API.  This
 
420
        # returns a Deferred that fires when negotiation about the option
 
421
        # finishes.  Make sure it fires, make sure state gets updated
 
422
        # properly, make sure the result indicates the option was enabled.
 
423
        s = self.p.getOptionState('\x42')
 
424
        s.him.state = 'yes'
 
425
 
 
426
        d = self.p.dont('\x42')
 
427
 
 
428
        self.assertEquals(self.t.value(), telnet.IAC + telnet.DONT + '\x42')
 
429
 
 
430
        self.p.dataReceived(telnet.IAC + telnet.WONT + '\x42')
 
431
 
 
432
        d.addCallback(self.assertEquals, True)
 
433
        d.addCallback(lambda _: self._enabledHelper(self.p.protocol,
 
434
                                                    dR=['\x42']))
 
435
        return d
 
436
 
 
437
    def testNegotiationBlocksFurtherNegotiation(self):
 
438
        # Try to disable an option, then immediately try to enable it, then
 
439
        # immediately try to disable it.  Ensure that the 2nd and 3rd calls
 
440
        # fail quickly with the right exception.
 
441
        s = self.p.getOptionState('\x24')
 
442
        s.him.state = 'yes'
 
443
        d2 = self.p.dont('\x24') # fires after the first line of _final
 
444
 
 
445
        def _do(x):
 
446
            d = self.p.do('\x24')
 
447
            return self.assertFailure(d, telnet.AlreadyNegotiating)
 
448
 
 
449
        def _dont(x):
 
450
            d = self.p.dont('\x24')
 
451
            return self.assertFailure(d, telnet.AlreadyNegotiating)
 
452
 
 
453
        def _final(x):
 
454
            self.p.dataReceived(telnet.IAC + telnet.WONT + '\x24')
 
455
            # an assertion that only passes if d2 has fired
 
456
            self._enabledHelper(self.p.protocol, dR=['\x24'])
 
457
            # Make sure we allow this
 
458
            self.p.protocol.remoteEnableable = ('\x24',)
 
459
            d = self.p.do('\x24')
 
460
            self.p.dataReceived(telnet.IAC + telnet.WILL + '\x24')
 
461
            d.addCallback(self.assertEquals, True)
 
462
            d.addCallback(lambda _: self._enabledHelper(self.p.protocol,
 
463
                                                        eR=['\x24'],
 
464
                                                        dR=['\x24']))
 
465
            return d
 
466
 
 
467
        d = _do(None)
 
468
        d.addCallback(_dont)
 
469
        d.addCallback(_final)
 
470
        return d
 
471
 
 
472
    def testSuperfluousDisableRequestRaises(self):
 
473
        # Try to disable a disabled option.  Make sure it fails properly.
 
474
        d = self.p.dont('\xab')
 
475
        return self.assertFailure(d, telnet.AlreadyDisabled)
 
476
 
 
477
    def testSuperfluousEnableRequestRaises(self):
 
478
        # Try to disable a disabled option.  Make sure it fails properly.
 
479
        s = self.p.getOptionState('\xab')
 
480
        s.him.state = 'yes'
 
481
        d = self.p.do('\xab')
 
482
        return self.assertFailure(d, telnet.AlreadyEnabled)
 
483
 
 
484
    def testLostConnectionFailsDeferreds(self):
 
485
        d1 = self.p.do('\x12')
 
486
        d2 = self.p.do('\x23')
 
487
        d3 = self.p.do('\x34')
 
488
 
 
489
        class TestException(Exception):
 
490
            pass
 
491
 
 
492
        self.p.connectionLost(TestException("Total failure!"))
 
493
 
 
494
        d1 = self.assertFailure(d1, TestException)
 
495
        d2 = self.assertFailure(d2, TestException)
 
496
        d3 = self.assertFailure(d3, TestException)
 
497
        return defer.gatherResults([d1, d2, d3])
 
498
 
 
499
 
 
500
class TestTelnet(telnet.Telnet):
 
501
    """
 
502
    A trivial extension of the telnet protocol class useful to unit tests.
 
503
    """
 
504
    def __init__(self):
 
505
        telnet.Telnet.__init__(self)
 
506
        self.events = []
 
507
 
 
508
 
 
509
    def applicationDataReceived(self, bytes):
 
510
        """
 
511
        Record the given data in C{self.events}.
 
512
        """
 
513
        self.events.append(('bytes', bytes))
 
514
 
 
515
 
 
516
    def unhandledCommand(self, command, bytes):
 
517
        """
 
518
        Record the given command in C{self.events}.
 
519
        """
 
520
        self.events.append(('command', command, bytes))
 
521
 
 
522
 
 
523
    def unhandledSubnegotiation(self, command, bytes):
 
524
        """
 
525
        Record the given subnegotiation command in C{self.events}.
 
526
        """
 
527
        self.events.append(('negotiate', command, bytes))
 
528
 
 
529
 
 
530
 
 
531
class TelnetTests(unittest.TestCase):
 
532
    """
 
533
    Tests for L{telnet.Telnet}.
 
534
 
 
535
    L{telnet.Telnet} implements the TELNET protocol (RFC 854), including option
 
536
    and suboption negotiation, and option state tracking.
 
537
    """
 
538
    def setUp(self):
 
539
        """
 
540
        Create an unconnected L{telnet.Telnet} to be used by tests.
 
541
        """
 
542
        self.protocol = TestTelnet()
 
543
 
 
544
 
 
545
    def test_enableLocal(self):
 
546
        """
 
547
        L{telnet.Telnet.enableLocal} should reject all options, since
 
548
        L{telnet.Telnet} does not know how to implement any options.
 
549
        """
 
550
        self.assertFalse(self.protocol.enableLocal('\0'))
 
551
 
 
552
 
 
553
    def test_enableRemote(self):
 
554
        """
 
555
        L{telnet.Telnet.enableRemote} should reject all options, since
 
556
        L{telnet.Telnet} does not know how to implement any options.
 
557
        """
 
558
        self.assertFalse(self.protocol.enableRemote('\0'))
 
559
 
 
560
 
 
561
    def test_disableLocal(self):
 
562
        """
 
563
        It is an error for L{telnet.Telnet.disableLocal} to be called, since
 
564
        L{telnet.Telnet.enableLocal} will never allow any options to be enabled
 
565
        locally.  If a subclass overrides enableLocal, it must also override
 
566
        disableLocal.
 
567
        """
 
568
        self.assertRaises(NotImplementedError, self.protocol.disableLocal, '\0')
 
569
 
 
570
 
 
571
    def test_disableRemote(self):
 
572
        """
 
573
        It is an error for L{telnet.Telnet.disableRemote} to be called, since
 
574
        L{telnet.Telnet.enableRemote} will never allow any options to be
 
575
        enabled remotely.  If a subclass overrides enableRemote, it must also
 
576
        override disableRemote.
 
577
        """
 
578
        self.assertRaises(NotImplementedError, self.protocol.disableRemote, '\0')
 
579
 
 
580
 
 
581
    def test_requestNegotiation(self):
 
582
        """
 
583
        L{telnet.Telnet.requestNegotiation} formats the feature byte and the
 
584
        payload bytes into the subnegotiation format and sends them.
 
585
 
 
586
        See RFC 855.
 
587
        """
 
588
        transport = proto_helpers.StringTransport()
 
589
        self.protocol.makeConnection(transport)
 
590
        self.protocol.requestNegotiation('\x01', '\x02\x03')
 
591
        self.assertEqual(
 
592
            transport.value(),
 
593
            # IAC SB feature bytes IAC SE
 
594
            '\xff\xfa\x01\x02\x03\xff\xf0')
 
595
 
 
596
 
 
597
    def test_requestNegotiationEscapesIAC(self):
 
598
        """
 
599
        If the payload for a subnegotiation includes I{IAC}, it is escaped by
 
600
        L{telnet.Telnet.requestNegotiation} with another I{IAC}.
 
601
 
 
602
        See RFC 855.
 
603
        """
 
604
        transport = proto_helpers.StringTransport()
 
605
        self.protocol.makeConnection(transport)
 
606
        self.protocol.requestNegotiation('\x01', '\xff')
 
607
        self.assertEqual(
 
608
            transport.value(),
 
609
            '\xff\xfa\x01\xff\xff\xff\xf0')
 
610
 
 
611
 
 
612
    def _deliver(self, bytes, *expected):
 
613
        """
 
614
        Pass the given bytes to the protocol's C{dataReceived} method and
 
615
        assert that the given events occur.
 
616
        """
 
617
        received = self.protocol.events = []
 
618
        self.protocol.dataReceived(bytes)
 
619
        self.assertEqual(received, list(expected))
 
620
 
 
621
 
 
622
    def test_oneApplicationDataByte(self):
 
623
        """
 
624
        One application-data byte in the default state gets delivered right
 
625
        away.
 
626
        """
 
627
        self._deliver('a', ('bytes', 'a'))
 
628
 
 
629
 
 
630
    def test_twoApplicationDataBytes(self):
 
631
        """
 
632
        Two application-data bytes in the default state get delivered
 
633
        together.
 
634
        """
 
635
        self._deliver('bc', ('bytes', 'bc'))
 
636
 
 
637
 
 
638
    def test_threeApplicationDataBytes(self):
 
639
        """
 
640
        Three application-data bytes followed by a control byte get
 
641
        delivered, but the control byte doesn't.
 
642
        """
 
643
        self._deliver('def' + telnet.IAC, ('bytes', 'def'))
 
644
 
 
645
 
 
646
    def test_escapedControl(self):
 
647
        """
 
648
        IAC in the escaped state gets delivered and so does another
 
649
        application-data byte following it.
 
650
        """
 
651
        self._deliver(telnet.IAC)
 
652
        self._deliver(telnet.IAC + 'g', ('bytes', telnet.IAC + 'g'))
 
653
 
 
654
 
 
655
    def test_carriageReturn(self):
 
656
        """
 
657
        A carriage return only puts the protocol into the newline state.  A
 
658
        linefeed in the newline state causes just the newline to be
 
659
        delivered.  A nul in the newline state causes a carriage return to
 
660
        be delivered.  An IAC in the newline state causes a carriage return
 
661
        to be delivered and puts the protocol into the escaped state. 
 
662
        Anything else causes a carriage return and that thing to be
 
663
        delivered.
 
664
        """
 
665
        self._deliver('\r')
 
666
        self._deliver('\n', ('bytes', '\n'))
 
667
        self._deliver('\r\n', ('bytes', '\n'))
 
668
 
 
669
        self._deliver('\r')
 
670
        self._deliver('\0', ('bytes', '\r'))
 
671
        self._deliver('\r\0', ('bytes', '\r'))
 
672
 
 
673
        self._deliver('\r')
 
674
        self._deliver('a', ('bytes', '\ra'))
 
675
        self._deliver('\ra', ('bytes', '\ra'))
 
676
 
 
677
        self._deliver('\r')
 
678
        self._deliver(
 
679
            telnet.IAC + telnet.IAC + 'x', ('bytes', '\r' + telnet.IAC + 'x'))
 
680
 
 
681
 
 
682
    def test_applicationDataBeforeSimpleCommand(self):
 
683
        """
 
684
        Application bytes received before a command are delivered before the
 
685
        command is processed.
 
686
        """
 
687
        self._deliver(
 
688
            'x' + telnet.IAC + telnet.NOP,
 
689
            ('bytes', 'x'), ('command', telnet.NOP, None))
 
690
 
 
691
 
 
692
    def test_applicationDataBeforeCommand(self):
 
693
        """
 
694
        Application bytes received before a WILL/WONT/DO/DONT are delivered
 
695
        before the command is processed.
 
696
        """
 
697
        self.protocol.commandMap = {}
 
698
        self._deliver(
 
699
            'y' + telnet.IAC + telnet.WILL + '\x00',
 
700
            ('bytes', 'y'), ('command', telnet.WILL, '\x00'))
 
701
 
 
702
 
 
703
    def test_applicationDataBeforeSubnegotiation(self):
 
704
        """
 
705
        Application bytes received before a subnegotiation command are
 
706
        delivered before the negotiation is processed.
 
707
        """
 
708
        self._deliver(
 
709
            'z' + telnet.IAC + telnet.SB + 'Qx' + telnet.IAC + telnet.SE,
 
710
            ('bytes', 'z'), ('negotiate', 'Q', ['x']))