Permalink
Browse files

Support the tsize option for read requests.

  • Loading branch information...
1 parent f9b3b81 commit a189926592eeab50f00537a757d08182ed5c803d @allenap committed Jul 3, 2012
Showing with 35 additions and 3 deletions.
  1. +16 −0 tftp/bootstrap.py
  2. +19 −3 tftp/test/test_bootstrap.py
View
@@ -350,6 +350,22 @@ def __init__(self, remote, reader, options=None, _clock=None):
TFTPBootstrap.__init__(self, remote, reader, options, _clock)
self.session = ReadSession(reader, self._clock)
+ def option_tsize(self, val):
+ """Process tsize option.
+
+ If tsize is zero, get the size of the file to be read so that it can
+ be returned in the OACK datagram.
+
+ @see: L{TFTPBootstrap.option_tsize}
+
+ """
+ val = TFTPBootstrap.option_tsize(self, val)
+ if val == str(0):
+ val = self.session.reader.size
+ if val is not None:
+ val = str(val)
+ return val
+
def startProtocol(self):
"""Start sending an OACK datagram if we were initialized with options
or start the L{ReadSession} immediately.
@@ -571,14 +571,17 @@ def setUp(self):
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={'blksize':'9'}, _clock=self.clock)
+ 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({'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)
@@ -593,7 +596,7 @@ def test_option_normal(self):
def test_option_timeout(self):
self.rs.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)
@@ -609,5 +612,18 @@ 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 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)

0 comments on commit a189926

Please sign in to comment.