Skip to content
Browse files

Enable tsize option for a write request.

  • Loading branch information...
1 parent 89db593 commit c7b5a4100502ba2af457c60e27a0e460aefd28c0 @allenap committed Jul 2, 2012
Showing with 28 additions and 4 deletions.
  1. +4 −0 tftp/session.py
  2. +24 −4 tftp/test/test_bootstrap.py
View
4 tftp/session.py
@@ -34,6 +34,7 @@ class WriteSession(DatagramProtocol):
block_size = 512
timeout = (1, 3, 7)
+ tsize = None
def __init__(self, writer, _clock=None):
self.writer = writer
@@ -124,6 +125,9 @@ def blockWriteSuccess(self, ign, datagram):
if len(datagram.data) < self.block_size:
self.completed = True
self.writer.finish()
+ # TODO: If self.tsize is not None, compare it with the actual
+ # count of bytes written. Log if there's a mismatch. Should it
+ # also emit an error datagram?
def blockWriteFailure(self, failure):
"""Write failed"""
View
28 tftp/test/test_bootstrap.py
@@ -343,14 +343,18 @@ def setUp(self):
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,
- options={'blksize':'9'}, _clock=self.clock)
+ 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({'blksize':'9'}).to_wire()
+ 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)
@@ -373,7 +377,7 @@ def test_option_normal(self):
def test_option_timeout(self):
self.ws.startProtocol()
self.clock.advance(0.1)
- oack_datagram = OACKDatagram({'blksize':'9'}).to_wire()
+ oack_datagram = OACKDatagram(self.options).to_wire()
self.assertEqual(self.transport.value(), oack_datagram)
self.failIf(self.transport.disconnecting)
@@ -389,6 +393,22 @@ def test_option_timeout(self):
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.assertIsNone(self.ws.session.tsize)
+ 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)

0 comments on commit c7b5a41

Please sign in to comment.
Something went wrong with that request. Please try again.