/usr/share/pyshared/tftp/test/test_bootstrap.py is in python-txtftp 0.1~bzr38-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 | '''
@author: shylent
'''
from tftp.bootstrap import (LocalOriginWriteSession, LocalOriginReadSession,
RemoteOriginReadSession, RemoteOriginWriteSession, TFTPBootstrap)
from tftp.datagram import (ACKDatagram, TFTPDatagramFactory, split_opcode,
ERR_TID_UNKNOWN, DATADatagram, OACKDatagram)
from tftp.test.test_sessions import DelayedWriter, FakeTransport, DelayedReader
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks
from twisted.internet.task import Clock
from twisted.python.filepath import FilePath
from twisted.python.util import OrderedDict
from twisted.trial import unittest
import shutil
import tempfile
from tftp.session import MAX_BLOCK_SIZE, WriteSession, ReadSession
ReadSession.timeout = (2, 2, 2)
WriteSession.timeout = (2, 2, 2)
RemoteOriginReadSession.timeout = (2, 2, 2)
RemoteOriginWriteSession.timeout = (2, 2, 2)
class MockHandshakeWatchdog(object):
def __init__(self, when, f, args=None, kwargs=None, _clock=None):
self._clock = _clock
self.when = when
self.f = f
self.args = args or []
self.kwargs = kwargs or {}
if _clock is None:
self._clock = reactor
else:
self._clock = _clock
def start(self):
self.wd = self._clock.callLater(self.when, self.f, *self.args, **self.kwargs)
def cancel(self):
if self.wd.active():
self.wd.cancel()
def active(self):
return self.wd.active()
class MockSession(object):
block_size = 512
timeout = (1, 3, 5)
tsize = None
# Testing implementation here, but if I don't, I'll have a TON of duplicate code
class TestOptionProcessing(unittest.TestCase):
def setUp(self):
self.proto = TFTPBootstrap(('127.0.0.1', 1111), None)
def test_empty_options(self):
self.s = MockSession()
opts = self.proto.processOptions(OrderedDict())
self.proto.applyOptions(self.s, opts)
self.assertEqual(self.s.block_size, 512)
self.assertEqual(self.s.timeout, (1, 3, 5))
def test_blksize(self):
self.s = MockSession()
opts = self.proto.processOptions(OrderedDict({'blksize':'8'}))
self.proto.applyOptions(self.s, opts)
self.assertEqual(self.s.block_size, 8)
self.assertEqual(opts, OrderedDict({'blksize':'8'}))
self.s = MockSession()
opts = self.proto.processOptions(OrderedDict({'blksize':'foo'}))
self.proto.applyOptions(self.s, opts)
self.assertEqual(self.s.block_size, 512)
self.assertEqual(opts, OrderedDict())
self.s = MockSession()
opts = self.proto.processOptions(OrderedDict({'blksize':'65464'}))
self.proto.applyOptions(self.s, opts)
self.assertEqual(self.s.block_size, MAX_BLOCK_SIZE)
self.assertEqual(opts, OrderedDict({'blksize':str(MAX_BLOCK_SIZE)}))
self.s = MockSession()
opts = self.proto.processOptions(OrderedDict({'blksize':'65465'}))
self.proto.applyOptions(self.s, opts)
self.assertEqual(self.s.block_size, 512)
self.assertEqual(opts, OrderedDict())
self.s = MockSession()
opts = self.proto.processOptions(OrderedDict({'blksize':'7'}))
self.proto.applyOptions(self.s, opts)
self.assertEqual(self.s.block_size, 512)
self.assertEqual(opts, OrderedDict())
def test_timeout(self):
self.s = MockSession()
opts = self.proto.processOptions(OrderedDict({'timeout':'1'}))
self.proto.applyOptions(self.s, opts)
self.assertEqual(self.s.timeout, (1, 1, 1))
self.assertEqual(opts, OrderedDict({'timeout':'1'}))
self.s = MockSession()
opts = self.proto.processOptions(OrderedDict({'timeout':'foo'}))
self.proto.applyOptions(self.s, opts)
self.assertEqual(self.s.timeout, (1, 3, 5))
self.assertEqual(opts, OrderedDict())
self.s = MockSession()
opts = self.proto.processOptions(OrderedDict({'timeout':'0'}))
self.proto.applyOptions(self.s, opts)
self.assertEqual(self.s.timeout, (1, 3, 5))
self.assertEqual(opts, OrderedDict())
self.s = MockSession()
opts = self.proto.processOptions(OrderedDict({'timeout':'255'}))
self.proto.applyOptions(self.s, opts)
self.assertEqual(self.s.timeout, (255, 255, 255))
self.assertEqual(opts, OrderedDict({'timeout':'255'}))
self.s = MockSession()
opts = self.proto.processOptions(OrderedDict({'timeout':'256'}))
self.proto.applyOptions(self.s, opts)
self.assertEqual(self.s.timeout, (1, 3, 5))
self.assertEqual(opts, OrderedDict())
def test_tsize(self):
self.s = MockSession()
opts = self.proto.processOptions(OrderedDict({'tsize':'1'}))
self.proto.applyOptions(self.s, opts)
self.assertEqual(self.s.tsize, 1)
self.assertEqual(opts, OrderedDict({'tsize':'1'}))
def test_tsize_ignored_when_not_a_number(self):
self.s = MockSession()
opts = self.proto.processOptions(OrderedDict({'tsize':'foo'}))
self.proto.applyOptions(self.s, opts)
self.assertTrue(self.s.tsize is None)
self.assertEqual(opts, OrderedDict({}))
def test_tsize_ignored_when_less_than_zero(self):
self.s = MockSession()
opts = self.proto.processOptions(OrderedDict({'tsize':'-1'}))
self.proto.applyOptions(self.s, opts)
self.assertTrue(self.s.tsize is None)
self.assertEqual(opts, OrderedDict({}))
def test_multiple_options(self):
got_options = OrderedDict()
got_options['timeout'] = '123'
got_options['blksize'] = '1024'
self.s = MockSession()
opts = self.proto.processOptions(got_options)
self.proto.applyOptions(self.s, opts)
self.assertEqual(self.s.timeout, (123, 123, 123))
self.assertEqual(self.s.block_size, 1024)
self.assertEqual(opts.items(), got_options.items())
got_options = OrderedDict()
got_options['blksize'] = '1024'
got_options['timeout'] = '123'
self.s = MockSession()
opts = self.proto.processOptions(got_options)
self.proto.applyOptions(self.s, opts)
self.assertEqual(self.s.timeout, (123, 123, 123))
self.assertEqual(self.s.block_size, 1024)
self.assertEqual(opts.items(), got_options.items())
got_options = OrderedDict()
got_options['blksize'] = '1024'
got_options['foobar'] = 'barbaz'
got_options['timeout'] = '123'
self.s = MockSession()
opts = self.proto.processOptions(got_options)
self.proto.applyOptions(self.s, opts)
self.assertEqual(self.s.timeout, (123, 123, 123))
self.assertEqual(self.s.block_size, 1024)
actual_options = OrderedDict()
actual_options['blksize'] = '1024'
actual_options['timeout'] = '123'
self.assertEqual(opts.items(), actual_options.items())
class BootstrapLocalOriginWrite(unittest.TestCase):
port = 65466
def setUp(self):
self.clock = Clock()
self.tmp_dir_path = tempfile.mkdtemp()
self.target = FilePath(self.tmp_dir_path).child('foo')
self.writer = DelayedWriter(self.target, _clock=self.clock, delay=2)
self.transport = FakeTransport(hostAddress=('127.0.0.1', self.port))
self.ws = LocalOriginWriteSession(('127.0.0.1', 65465), self.writer, _clock=self.clock)
self.wd = MockHandshakeWatchdog(4, self.ws.timedOut, _clock=self.clock)
self.ws.timeout_watchdog = self.wd
self.ws.transport = self.transport
def test_invalid_tid(self):
self.ws.startProtocol()
bad_tid_dgram = ACKDatagram(123)
self.ws.datagramReceived(bad_tid_dgram.to_wire(), ('127.0.0.1', 1111))
err_dgram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assertEqual(err_dgram.errorcode, ERR_TID_UNKNOWN)
self.addCleanup(self.ws.cancel)
#test_invalid_tid.skip = 'Will go to another test case'
def test_local_origin_write_session_handshake_timeout(self):
self.ws.startProtocol()
self.clock.advance(5)
self.failIf(self.transport.value())
self.failUnless(self.transport.disconnecting)
def test_local_origin_write_session_handshake_success(self):
self.ws.session.block_size = 6
self.ws.startProtocol()
self.clock.advance(1)
data_datagram = DATADatagram(1, 'foobar')
self.ws.datagramReceived(data_datagram.to_wire(), ('127.0.0.1', 65465))
self.clock.pump((1,)*3)
self.assertEqual(self.transport.value(), ACKDatagram(1).to_wire())
self.failIf(self.transport.disconnecting)
self.failIf(self.wd.active())
self.addCleanup(self.ws.cancel)
def tearDown(self):
shutil.rmtree(self.tmp_dir_path)
class LocalOriginWriteOptionNegotiation(unittest.TestCase):
port = 65466
def setUp(self):
self.clock = Clock()
self.tmp_dir_path = tempfile.mkdtemp()
self.target = FilePath(self.tmp_dir_path).child('foo')
self.writer = DelayedWriter(self.target, _clock=self.clock, delay=2)
self.transport = FakeTransport(hostAddress=('127.0.0.1', self.port))
self.ws = LocalOriginWriteSession(('127.0.0.1', 65465), self.writer,
options={'blksize':'123'}, _clock=self.clock)
self.wd = MockHandshakeWatchdog(4, self.ws.timedOut, _clock=self.clock)
self.ws.timeout_watchdog = self.wd
self.ws.transport = self.transport
def test_option_normal(self):
self.ws.startProtocol()
self.ws.datagramReceived(OACKDatagram({'blksize':'12'}).to_wire(), ('127.0.0.1', 65465))
self.clock.advance(0.1)
self.assertEqual(self.ws.session.block_size, WriteSession.block_size)
self.assertEqual(self.transport.value(), ACKDatagram(0).to_wire())
self.transport.clear()
self.ws.datagramReceived(OACKDatagram({'blksize':'9'}).to_wire(), ('127.0.0.1', 65465))
self.clock.advance(0.1)
self.assertEqual(self.ws.session.block_size, WriteSession.block_size)
self.assertEqual(self.transport.value(), ACKDatagram(0).to_wire())
self.transport.clear()
self.ws.datagramReceived(DATADatagram(1, 'foobarbaz').to_wire(), ('127.0.0.1', 65465))
self.clock.advance(3)
self.failUnless(self.ws.session.started)
self.clock.advance(0.1)
self.assertEqual(self.ws.session.block_size, 9)
self.assertEqual(self.transport.value(), ACKDatagram(1).to_wire())
self.transport.clear()
self.ws.datagramReceived(DATADatagram(2, 'asdfghjkl').to_wire(), ('127.0.0.1', 65465))
self.clock.advance(3)
self.assertEqual(self.transport.value(), ACKDatagram(2).to_wire())
self.writer.finish()
self.assertEqual(self.writer.file_path.open('r').read(), 'foobarbazasdfghjkl')
self.transport.clear()
self.ws.datagramReceived(OACKDatagram({'blksize':'12'}).to_wire(), ('127.0.0.1', 65465))
self.clock.advance(0.1)
self.assertEqual(self.ws.session.block_size, 9)
self.assertEqual(self.transport.value(), ACKDatagram(0).to_wire())
def test_option_timeout(self):
self.ws.startProtocol()
self.clock.advance(5)
self.failUnless(self.transport.disconnecting)
def tearDown(self):
shutil.rmtree(self.tmp_dir_path)
class BootstrapRemoteOriginWrite(unittest.TestCase):
port = 65466
def setUp(self):
self.clock = Clock()
self.tmp_dir_path = tempfile.mkdtemp()
self.target = FilePath(self.tmp_dir_path).child('foo')
self.writer = DelayedWriter(self.target, _clock=self.clock, delay=2)
self.transport = FakeTransport(hostAddress=('127.0.0.1', self.port))
self.ws = RemoteOriginWriteSession(('127.0.0.1', 65465), self.writer, _clock=self.clock)
self.ws.transport = self.transport
self.ws.startProtocol()
@inlineCallbacks
def test_invalid_tid(self):
bad_tid_dgram = ACKDatagram(123)
yield self.ws.datagramReceived(bad_tid_dgram.to_wire(), ('127.0.0.1', 1111))
err_dgram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assertEqual(err_dgram.errorcode, ERR_TID_UNKNOWN)
self.addCleanup(self.ws.cancel)
def test_remote_origin_write_bootstrap(self):
# Initial ACK
ack_datagram_0 = ACKDatagram(0)
self.clock.advance(0.1)
self.assertEqual(self.transport.value(), ack_datagram_0.to_wire())
self.failIf(self.transport.disconnecting)
# Normal exchange
self.transport.clear()
d = self.ws.datagramReceived(DATADatagram(1, 'foobar').to_wire(), ('127.0.0.1', 65465))
def cb(res):
self.clock.advance(0.1)
ack_datagram_1 = ACKDatagram(1)
self.assertEqual(self.transport.value(), ack_datagram_1.to_wire())
self.assertEqual(self.target.open('r').read(), 'foobar')
self.failIf(self.transport.disconnecting)
self.addCleanup(self.ws.cancel)
d.addCallback(cb)
self.clock.advance(3)
return d
def tearDown(self):
shutil.rmtree(self.tmp_dir_path)
class RemoteOriginWriteOptionNegotiation(unittest.TestCase):
port = 65466
def setUp(self):
self.clock = Clock()
self.tmp_dir_path = tempfile.mkdtemp()
self.target = FilePath(self.tmp_dir_path).child('foo')
self.writer = DelayedWriter(self.target, _clock=self.clock, delay=2)
self.transport = FakeTransport(hostAddress=('127.0.0.1', self.port))
self.options = OrderedDict()
self.options['blksize'] = '9'
self.options['tsize'] = '45'
self.ws = RemoteOriginWriteSession(
('127.0.0.1', 65465), self.writer, options=self.options,
_clock=self.clock)
self.ws.transport = self.transport
def test_option_normal(self):
self.ws.startProtocol()
self.clock.advance(0.1)
oack_datagram = OACKDatagram(self.options).to_wire()
self.assertEqual(self.transport.value(), oack_datagram)
self.clock.advance(3)
self.assertEqual(self.transport.value(), oack_datagram * 2)
self.transport.clear()
self.ws.datagramReceived(DATADatagram(1, 'foobarbaz').to_wire(), ('127.0.0.1', 65465))
self.clock.pump((1,)*3)
self.assertEqual(self.transport.value(), ACKDatagram(1).to_wire())
self.assertEqual(self.ws.session.block_size, 9)
self.transport.clear()
self.ws.datagramReceived(DATADatagram(2, 'smthng').to_wire(), ('127.0.0.1', 65465))
self.clock.pump((1,)*3)
self.assertEqual(self.transport.value(), ACKDatagram(2).to_wire())
self.clock.pump((1,)*10)
self.writer.finish()
self.assertEqual(self.writer.file_path.open('r').read(), 'foobarbazsmthng')
self.failUnless(self.transport.disconnecting)
def test_option_timeout(self):
self.ws.startProtocol()
self.clock.advance(0.1)
oack_datagram = OACKDatagram(self.options).to_wire()
self.assertEqual(self.transport.value(), oack_datagram)
self.failIf(self.transport.disconnecting)
self.clock.advance(3)
self.assertEqual(self.transport.value(), oack_datagram * 2)
self.failIf(self.transport.disconnecting)
self.clock.advance(2)
self.assertEqual(self.transport.value(), oack_datagram * 3)
self.failIf(self.transport.disconnecting)
self.clock.advance(2)
self.assertEqual(self.transport.value(), oack_datagram * 3)
self.failUnless(self.transport.disconnecting)
def test_option_tsize(self):
# A tsize option sent as part of a write session is recorded.
self.ws.startProtocol()
self.clock.advance(0.1)
oack_datagram = OACKDatagram(self.options).to_wire()
self.assertEqual(self.transport.value(), oack_datagram)
self.failIf(self.transport.disconnecting)
self.assertIsInstance(self.ws.session, WriteSession)
# Options are not applied to the WriteSession until the first DATA
# datagram is received,
self.assertTrue(self.ws.session.tsize is None)
self.ws.datagramReceived(
DATADatagram(1, 'foobarbaz').to_wire(), ('127.0.0.1', 65465))
# The tsize option has been applied to the WriteSession.
self.assertEqual(45, self.ws.session.tsize)
def tearDown(self):
shutil.rmtree(self.tmp_dir_path)
class BootstrapLocalOriginRead(unittest.TestCase):
test_data = """line1
line2
anotherline"""
port = 65466
def setUp(self):
self.clock = Clock()
self.tmp_dir_path = tempfile.mkdtemp()
self.target = FilePath(self.tmp_dir_path).child('foo')
with self.target.open('wb') as temp_fd:
temp_fd.write(self.test_data)
self.reader = DelayedReader(self.target, _clock=self.clock, delay=2)
self.transport = FakeTransport(hostAddress=('127.0.0.1', self.port))
self.rs = LocalOriginReadSession(('127.0.0.1', 65465), self.reader, _clock=self.clock)
self.wd = MockHandshakeWatchdog(4, self.rs.timedOut, _clock=self.clock)
self.rs.timeout_watchdog = self.wd
self.rs.transport = self.transport
self.rs.startProtocol()
def test_invalid_tid(self):
data_datagram = DATADatagram(1, 'foobar')
self.rs.datagramReceived(data_datagram, ('127.0.0.1', 11111))
self.clock.advance(0.1)
err_dgram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assertEqual(err_dgram.errorcode, ERR_TID_UNKNOWN)
self.addCleanup(self.rs.cancel)
def test_local_origin_read_session_handshake_timeout(self):
self.clock.advance(5)
self.failIf(self.transport.value())
self.failUnless(self.transport.disconnecting)
def test_local_origin_read_session_handshake_success(self):
self.clock.advance(1)
ack_datagram = ACKDatagram(0)
self.rs.datagramReceived(ack_datagram.to_wire(), ('127.0.0.1', 65465))
self.clock.advance(2)
self.failUnless(self.transport.value())
self.failIf(self.transport.disconnecting)
self.failIf(self.wd.active())
self.addCleanup(self.rs.cancel)
def tearDown(self):
shutil.rmtree(self.tmp_dir_path)
class LocalOriginReadOptionNegotiation(unittest.TestCase):
test_data = """line1
line2
anotherline"""
port = 65466
def setUp(self):
self.clock = Clock()
self.tmp_dir_path = tempfile.mkdtemp()
self.target = FilePath(self.tmp_dir_path).child('foo')
with self.target.open('wb') as temp_fd:
temp_fd.write(self.test_data)
self.reader = DelayedReader(self.target, _clock=self.clock, delay=2)
self.transport = FakeTransport(hostAddress=('127.0.0.1', self.port))
self.rs = LocalOriginReadSession(('127.0.0.1', 65465), self.reader, _clock=self.clock)
self.wd = MockHandshakeWatchdog(4, self.rs.timedOut, _clock=self.clock)
self.rs.timeout_watchdog = self.wd
self.rs.transport = self.transport
def test_option_normal(self):
self.rs.startProtocol()
self.rs.datagramReceived(OACKDatagram({'blksize':'9'}).to_wire(), ('127.0.0.1', 65465))
self.clock.advance(0.1)
self.assertEqual(self.rs.session.block_size, 9)
self.clock.pump((1,)*3)
self.assertEqual(self.transport.value(), DATADatagram(1, self.test_data[:9]).to_wire())
self.rs.datagramReceived(OACKDatagram({'blksize':'12'}).to_wire(), ('127.0.0.1', 65465))
self.clock.advance(0.1)
self.assertEqual(self.rs.session.block_size, 9)
self.transport.clear()
self.rs.datagramReceived(ACKDatagram(1).to_wire(), ('127.0.0.1', 65465))
self.clock.pump((1,)*3)
self.assertEqual(self.transport.value(), DATADatagram(2, self.test_data[9:18]).to_wire())
self.addCleanup(self.rs.cancel)
def test_local_origin_read_option_timeout(self):
self.rs.startProtocol()
self.clock.advance(5)
self.failUnless(self.transport.disconnecting)
def tearDown(self):
shutil.rmtree(self.tmp_dir_path)
class BootstrapRemoteOriginRead(unittest.TestCase):
test_data = """line1
line2
anotherline"""
port = 65466
def setUp(self):
self.clock = Clock()
self.tmp_dir_path = tempfile.mkdtemp()
self.target = FilePath(self.tmp_dir_path).child('foo')
with self.target.open('wb') as temp_fd:
temp_fd.write(self.test_data)
self.reader = DelayedReader(self.target, _clock=self.clock, delay=2)
self.transport = FakeTransport(hostAddress=('127.0.0.1', self.port))
self.rs = RemoteOriginReadSession(('127.0.0.1', 65465), self.reader, _clock=self.clock)
self.rs.transport = self.transport
@inlineCallbacks
def test_invalid_tid(self):
self.rs.startProtocol()
data_datagram = DATADatagram(1, 'foobar')
yield self.rs.datagramReceived(data_datagram, ('127.0.0.1', 11111))
err_dgram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assertEqual(err_dgram.errorcode, ERR_TID_UNKNOWN)
self.addCleanup(self.rs.cancel)
def test_remote_origin_read_bootstrap(self):
# First datagram
self.rs.session.block_size = 5
self.rs.startProtocol()
self.clock.pump((1,)*3)
data_datagram_1 = DATADatagram(1, self.test_data[:5])
self.assertEqual(self.transport.value(), data_datagram_1.to_wire())
self.failIf(self.transport.disconnecting)
# Normal exchange continues
self.transport.clear()
self.rs.datagramReceived(ACKDatagram(1).to_wire(), ('127.0.0.1', 65465))
self.clock.pump((1,)*3)
data_datagram_2 = DATADatagram(2, self.test_data[5:10])
self.assertEqual(self.transport.value(), data_datagram_2.to_wire())
self.failIf(self.transport.disconnecting)
self.addCleanup(self.rs.cancel)
def tearDown(self):
shutil.rmtree(self.tmp_dir_path)
class RemoteOriginReadOptionNegotiation(unittest.TestCase):
test_data = """line1
line2
anotherline"""
port = 65466
def setUp(self):
self.clock = Clock()
self.tmp_dir_path = tempfile.mkdtemp()
self.target = FilePath(self.tmp_dir_path).child('foo')
with self.target.open('wb') as temp_fd:
temp_fd.write(self.test_data)
self.reader = DelayedReader(self.target, _clock=self.clock, delay=2)
self.transport = FakeTransport(hostAddress=('127.0.0.1', self.port))
self.options = OrderedDict()
self.options['blksize'] = '9'
self.options['tsize'] = '34'
self.rs = RemoteOriginReadSession(('127.0.0.1', 65465), self.reader,
options=self.options, _clock=self.clock)
self.rs.transport = self.transport
def test_option_normal(self):
self.rs.startProtocol()
self.clock.advance(0.1)
oack_datagram = OACKDatagram(self.options).to_wire()
self.assertEqual(self.transport.value(), oack_datagram)
self.clock.advance(3)
self.assertEqual(self.transport.value(), oack_datagram * 2)
self.transport.clear()
self.rs.datagramReceived(ACKDatagram(0).to_wire(), ('127.0.0.1', 65465))
self.clock.pump((1,)*3)
self.assertEqual(self.transport.value(), DATADatagram(1, self.test_data[:9]).to_wire())
self.addCleanup(self.rs.cancel)
def test_option_timeout(self):
self.rs.startProtocol()
self.clock.advance(0.1)
oack_datagram = OACKDatagram(self.options).to_wire()
self.assertEqual(self.transport.value(), oack_datagram)
self.failIf(self.transport.disconnecting)
self.clock.advance(3)
self.assertEqual(self.transport.value(), oack_datagram * 2)
self.failIf(self.transport.disconnecting)
self.clock.advance(2)
self.assertEqual(self.transport.value(), oack_datagram * 3)
self.failIf(self.transport.disconnecting)
self.clock.advance(2)
self.assertEqual(self.transport.value(), oack_datagram * 3)
self.failUnless(self.transport.disconnecting)
def test_option_tsize(self):
# A tsize option of 0 sent as part of a read session prompts a tsize
# response with the actual size of the file.
self.options['tsize'] = '0'
self.rs.startProtocol()
self.clock.advance(0.1)
self.transport.clear()
self.clock.advance(3)
# The response contains the size of the test data.
self.options['tsize'] = str(len(self.test_data))
oack_datagram = OACKDatagram(self.options).to_wire()
self.assertEqual(self.transport.value(), oack_datagram)
def tearDown(self):
shutil.rmtree(self.tmp_dir_path)
|