/usr/share/pyshared/tftp/test/test_sessions.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 | '''
@author: shylent
'''
from tftp.backend import FilesystemWriter, FilesystemReader, IReader, IWriter
from tftp.datagram import (ACKDatagram, ERRORDatagram,
ERR_NOT_DEFINED, DATADatagram, TFTPDatagramFactory, split_opcode)
from tftp.session import WriteSession, ReadSession
from twisted.internet import reactor
from twisted.internet.defer import Deferred, inlineCallbacks
from twisted.internet.task import Clock
from twisted.python.filepath import FilePath
from twisted.test.proto_helpers import StringTransport
from twisted.trial import unittest
from zope import interface
import shutil
import tempfile
ReadSession.timeout = (2, 2, 2)
WriteSession.timeout = (2, 2, 2)
class DelayedReader(FilesystemReader):
def __init__(self, *args, **kwargs):
self.delay = kwargs.pop('delay')
self._clock = kwargs.pop('_clock', reactor)
FilesystemReader.__init__(self, *args, **kwargs)
def read(self, size):
data = FilesystemReader.read(self, size)
d = Deferred()
def c(ign):
return data
d.addCallback(c)
self._clock.callLater(self.delay, d.callback, None)
return d
class DelayedWriter(FilesystemWriter):
def __init__(self, *args, **kwargs):
self.delay = kwargs.pop('delay')
self._clock = kwargs.pop('_clock', reactor)
FilesystemWriter.__init__(self, *args, **kwargs)
def write(self, data):
d = Deferred()
def c(ign):
return FilesystemWriter.write(self, data)
d.addCallback(c)
self._clock.callLater(self.delay, d.callback, None)
return d
class FailingReader(object):
interface.implements(IReader)
size = None
def read(self, size):
raise IOError('A failure')
def finish(self):
pass
class FailingWriter(object):
interface.implements(IWriter)
def write(self, data):
raise IOError("I fail")
def cancel(self):
pass
def finish(self):
pass
class FakeTransport(StringTransport):
stopListening = StringTransport.loseConnection
def connect(self, host, port):
self._connectedAddr = (host, port)
class WriteSessions(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 = WriteSession(self.writer, _clock=self.clock)
self.ws.timeout = (4, 4, 4)
self.ws.transport = self.transport
self.ws.startProtocol()
def test_ERROR(self):
err_dgram = ERRORDatagram.from_code(ERR_NOT_DEFINED, 'no reason')
self.ws.datagramReceived(err_dgram)
self.clock.advance(0.1)
self.failIf(self.transport.value())
self.failUnless(self.transport.disconnecting)
@inlineCallbacks
def test_DATA_stale_blocknum(self):
self.ws.block_size = 6
self.ws.blocknum = 2
data_datagram = DATADatagram(1, 'foobar')
yield self.ws.datagramReceived(data_datagram)
self.writer.finish()
self.failIf(self.target.open('r').read())
self.failIf(self.transport.disconnecting)
ack_dgram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assertEqual(ack_dgram.blocknum, 1)
self.addCleanup(self.ws.cancel)
@inlineCallbacks
def test_DATA_invalid_blocknum(self):
self.ws.block_size = 6
data_datagram = DATADatagram(3, 'foobar')
yield self.ws.datagramReceived(data_datagram)
self.writer.finish()
self.failIf(self.target.open('r').read())
self.failIf(self.transport.disconnecting)
err_dgram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assert_(isinstance(err_dgram, ERRORDatagram))
self.addCleanup(self.ws.cancel)
def test_DATA(self):
self.ws.block_size = 6
data_datagram = DATADatagram(1, 'foobar')
d = self.ws.datagramReceived(data_datagram)
def cb(ign):
self.clock.advance(0.1)
#self.writer.finish()
#self.assertEqual(self.target.open('r').read(), 'foobar')
self.failIf(self.transport.disconnecting)
ack_dgram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assertEqual(ack_dgram.blocknum, 1)
self.failIf(self.ws.completed,
"Data length is equal to blocksize, no reason to stop")
data_datagram = DATADatagram(2, 'barbaz')
self.transport.clear()
d = self.ws.datagramReceived(data_datagram)
d.addCallback(cb_)
self.clock.advance(3)
return d
def cb_(ign):
self.clock.advance(0.1)
self.failIf(self.transport.disconnecting)
ack_dgram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assertEqual(ack_dgram.blocknum, 2)
self.failIf(self.ws.completed,
"Data length is equal to blocksize, no reason to stop")
d.addCallback(cb)
self.addCleanup(self.ws.cancel)
self.clock.advance(3)
return d
def test_DATA_finished(self):
self.ws.block_size = 6
# Send a terminating datagram
data_datagram = DATADatagram(1, 'foo')
d = self.ws.datagramReceived(data_datagram)
def cb(res):
self.clock.advance(0.1)
self.assertEqual(self.target.open('r').read(), 'foo')
ack_dgram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.failUnless(isinstance(ack_dgram, ACKDatagram))
self.failUnless(self.ws.completed,
"Data length is less, than blocksize, time to stop")
self.transport.clear()
# Send another datagram after the transfer is considered complete
data_datagram = DATADatagram(2, 'foobar')
self.ws.datagramReceived(data_datagram)
self.assertEqual(self.target.open('r').read(), 'foo')
err_dgram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.failUnless(isinstance(err_dgram, ERRORDatagram))
# Check for proper disconnection after grace timeout expires
self.clock.pump((4,)*4)
self.failUnless(self.transport.disconnecting,
"We are done and the grace timeout is over, should disconnect")
d.addCallback(cb)
self.clock.advance(2)
return d
def test_DATA_backoff(self):
self.ws.block_size = 5
data_datagram = DATADatagram(1, 'foobar')
d = self.ws.datagramReceived(data_datagram)
def cb(ign):
self.clock.advance(0.1)
ack_datagram = ACKDatagram(1)
self.clock.pump((1,)*5)
# Sent two times - initial send and a retransmit after first timeout
self.assertEqual(self.transport.value(),
ack_datagram.to_wire()*2)
# Sent three times - initial send and two retransmits
self.clock.pump((1,)*4)
self.assertEqual(self.transport.value(),
ack_datagram.to_wire()*3)
# Sent still three times - initial send, two retransmits and the last wait
self.clock.pump((1,)*4)
self.assertEqual(self.transport.value(),
ack_datagram.to_wire()*3)
self.failUnless(self.transport.disconnecting)
d.addCallback(cb)
self.clock.advance(2.1)
return d
@inlineCallbacks
def test_failed_write(self):
self.writer.cancel()
self.ws.writer = FailingWriter()
data_datagram = DATADatagram(1, 'foobar')
yield self.ws.datagramReceived(data_datagram)
self.flushLoggedErrors()
self.clock.advance(0.1)
err_datagram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.failUnless(isinstance(err_datagram, ERRORDatagram))
self.failUnless(self.transport.disconnecting)
def test_time_out(self):
data_datagram = DATADatagram(1, 'foobar')
d = self.ws.datagramReceived(data_datagram)
def cb(ign):
self.clock.pump((1,)*13)
self.failUnless(self.transport.disconnecting)
d.addCallback(cb)
self.clock.advance(4)
return d
def tearDown(self):
shutil.rmtree(self.tmp_dir_path)
class ReadSessions(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 = ReadSession(self.reader, _clock=self.clock)
self.rs.transport = self.transport
self.rs.startProtocol()
@inlineCallbacks
def test_ERROR(self):
err_dgram = ERRORDatagram.from_code(ERR_NOT_DEFINED, 'no reason')
yield self.rs.datagramReceived(err_dgram)
self.failIf(self.transport.value())
self.failUnless(self.transport.disconnecting)
@inlineCallbacks
def test_ACK_invalid_blocknum(self):
ack_datagram = ACKDatagram(3)
yield self.rs.datagramReceived(ack_datagram)
self.failIf(self.transport.disconnecting)
err_dgram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assert_(isinstance(err_dgram, ERRORDatagram))
self.addCleanup(self.rs.cancel)
@inlineCallbacks
def test_ACK_stale_blocknum(self):
self.rs.blocknum = 2
ack_datagram = ACKDatagram(1)
yield self.rs.datagramReceived(ack_datagram)
self.failIf(self.transport.disconnecting)
self.failIf(self.transport.value(),
"Stale ACK datagram, we should not write anything back")
self.addCleanup(self.rs.cancel)
def test_ACK(self):
self.rs.block_size = 5
self.rs.blocknum = 1
ack_datagram = ACKDatagram(1)
d = self.rs.datagramReceived(ack_datagram)
def cb(ign):
self.clock.advance(0.1)
self.failIf(self.transport.disconnecting)
data_datagram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assertEqual(data_datagram.data, 'line1')
self.failIf(self.rs.completed,
"Got enough bytes from the reader, there is no reason to stop")
d.addCallback(cb)
self.clock.advance(2.5)
self.addCleanup(self.rs.cancel)
return d
def test_ACK_finished(self):
self.rs.block_size = 512
self.rs.blocknum = 1
# Send a terminating datagram
ack_datagram = ACKDatagram(1)
d = self.rs.datagramReceived(ack_datagram)
def cb(ign):
self.clock.advance(0.1)
ack_datagram = ACKDatagram(2)
# This datagram doesn't trigger any sends
self.rs.datagramReceived(ack_datagram)
self.assertEqual(self.transport.value(), DATADatagram(2, self.test_data).to_wire())
self.failUnless(self.rs.completed,
"Data length is less, than blocksize, time to stop")
self.addCleanup(self.rs.cancel)
d.addCallback(cb)
self.clock.advance(3)
return d
def test_ACK_backoff(self):
self.rs.block_size = 5
self.rs.blocknum = 1
ack_datagram = ACKDatagram(1)
d = self.rs.datagramReceived(ack_datagram)
def cb(ign):
self.clock.pump((1,)*4)
# Sent two times - initial send and a retransmit after first timeout
self.assertEqual(self.transport.value(),
DATADatagram(2, self.test_data[:5]).to_wire()*2)
# Sent three times - initial send and two retransmits
self.clock.pump((1,)*5)
self.assertEqual(self.transport.value(),
DATADatagram(2, self.test_data[:5]).to_wire()*3)
# Sent still three times - initial send, two retransmits and the last wait
self.clock.pump((1,)*10)
self.assertEqual(self.transport.value(),
DATADatagram(2, self.test_data[:5]).to_wire()*3)
self.failUnless(self.transport.disconnecting)
d.addCallback(cb)
self.clock.advance(2.5)
return d
@inlineCallbacks
def test_failed_read(self):
self.reader.finish()
self.rs.reader = FailingReader()
self.rs.blocknum = 1
ack_datagram = ACKDatagram(1)
yield self.rs.datagramReceived(ack_datagram)
self.flushLoggedErrors()
self.clock.advance(0.1)
err_datagram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.failUnless(isinstance(err_datagram, ERRORDatagram))
self.failUnless(self.transport.disconnecting)
def tearDown(self):
shutil.rmtree(self.tmp_dir_path)
|