/usr/share/pyshared/tftp/test/test_protocol.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 | '''
@author: shylent
'''
from tftp.backend import FilesystemSynchronousBackend, IReader, IWriter
from tftp.bootstrap import RemoteOriginWriteSession, RemoteOriginReadSession
from tftp.datagram import (WRQDatagram, TFTPDatagramFactory, split_opcode,
ERR_ILLEGAL_OP, RRQDatagram, ERR_ACCESS_VIOLATION, ERR_FILE_EXISTS,
ERR_FILE_NOT_FOUND, ERR_NOT_DEFINED)
from tftp.errors import (Unsupported, AccessViolation, FileExists, FileNotFound,
BackendError)
from tftp.netascii import NetasciiReceiverProxy, NetasciiSenderProxy
from tftp.protocol import TFTP
from twisted.internet import reactor
from twisted.internet.address import IPv4Address
from twisted.internet.defer import Deferred, inlineCallbacks
from twisted.internet.protocol import DatagramProtocol
from twisted.internet.task import Clock
from twisted.python import context
from twisted.python.filepath import FilePath
from twisted.test.proto_helpers import StringTransport
from twisted.trial import unittest
import tempfile
class DummyBackend(object):
pass
def BackendFactory(exc_val=None):
if exc_val is not None:
class FailingBackend(object):
def get_reader(self, filename):
raise exc_val
def get_writer(self, filename):
raise exc_val
return FailingBackend()
else:
return DummyBackend()
class FakeTransport(StringTransport):
stopListening = StringTransport.loseConnection
def write(self, bytes, addr=None):
StringTransport.write(self, bytes)
def connect(self, host, port):
self._connectedAddr = (host, port)
class DispatchErrors(unittest.TestCase):
port = 11111
def setUp(self):
self.clock = Clock()
self.transport = FakeTransport(
hostAddress=IPv4Address('UDP', '127.0.0.1', self.port))
def test_malformed_datagram(self):
tftp = TFTP(BackendFactory(), _clock=self.clock)
tftp.datagramReceived('foobar', ('127.0.0.1', 1111))
self.failIf(self.transport.disconnecting)
self.failIf(self.transport.value())
test_malformed_datagram.skip = 'Not done yet'
def test_bad_mode(self):
tftp = TFTP(DummyBackend(), _clock=self.clock)
tftp.transport = self.transport
wrq_datagram = WRQDatagram('foobar', 'badmode', {})
tftp.datagramReceived(wrq_datagram.to_wire(), ('127.0.0.1', 1111))
error_datagram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assertEqual(error_datagram.errorcode, ERR_ILLEGAL_OP)
def test_unsupported(self):
tftp = TFTP(BackendFactory(Unsupported("I don't support you")), _clock=self.clock)
tftp.transport = self.transport
wrq_datagram = WRQDatagram('foobar', 'netascii', {})
tftp.datagramReceived(wrq_datagram.to_wire(), ('127.0.0.1', 1111))
self.clock.advance(1)
error_datagram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assertEqual(error_datagram.errorcode, ERR_ILLEGAL_OP)
self.transport.clear()
rrq_datagram = RRQDatagram('foobar', 'octet', {})
tftp.datagramReceived(rrq_datagram.to_wire(), ('127.0.0.1', 1111))
self.clock.advance(1)
error_datagram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assertEqual(error_datagram.errorcode, ERR_ILLEGAL_OP)
def test_access_violation(self):
tftp = TFTP(BackendFactory(AccessViolation("No!")), _clock=self.clock)
tftp.transport = self.transport
wrq_datagram = WRQDatagram('foobar', 'netascii', {})
tftp.datagramReceived(wrq_datagram.to_wire(), ('127.0.0.1', 1111))
self.clock.advance(1)
error_datagram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assertEqual(error_datagram.errorcode, ERR_ACCESS_VIOLATION)
self.transport.clear()
rrq_datagram = RRQDatagram('foobar', 'octet', {})
tftp.datagramReceived(rrq_datagram.to_wire(), ('127.0.0.1', 1111))
self.clock.advance(1)
error_datagram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assertEqual(error_datagram.errorcode, ERR_ACCESS_VIOLATION)
def test_file_exists(self):
tftp = TFTP(BackendFactory(FileExists("Already have one")), _clock=self.clock)
tftp.transport = self.transport
wrq_datagram = WRQDatagram('foobar', 'netascii', {})
tftp.datagramReceived(wrq_datagram.to_wire(), ('127.0.0.1', 1111))
self.clock.advance(1)
error_datagram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assertEqual(error_datagram.errorcode, ERR_FILE_EXISTS)
def test_file_not_found(self):
tftp = TFTP(BackendFactory(FileNotFound("Not found")), _clock=self.clock)
tftp.transport = self.transport
rrq_datagram = RRQDatagram('foobar', 'netascii', {})
tftp.datagramReceived(rrq_datagram.to_wire(), ('127.0.0.1', 1111))
self.clock.advance(1)
error_datagram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assertEqual(error_datagram.errorcode, ERR_FILE_NOT_FOUND)
def test_generic_backend_error(self):
tftp = TFTP(BackendFactory(BackendError("A backend that couldn't")), _clock=self.clock)
tftp.transport = self.transport
rrq_datagram = RRQDatagram('foobar', 'netascii', {})
tftp.datagramReceived(rrq_datagram.to_wire(), ('127.0.0.1', 1111))
self.clock.advance(1)
error_datagram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assertEqual(error_datagram.errorcode, ERR_NOT_DEFINED)
self.transport.clear()
rrq_datagram = RRQDatagram('foobar', 'octet', {})
tftp.datagramReceived(rrq_datagram.to_wire(), ('127.0.0.1', 1111))
self.clock.advance(1)
error_datagram = TFTPDatagramFactory(*split_opcode(self.transport.value()))
self.assertEqual(error_datagram.errorcode, ERR_NOT_DEFINED)
class DummyClient(DatagramProtocol):
def __init__(self, *args, **kwargs):
self.ready = Deferred()
def startProtocol(self):
self.ready.callback(None)
class TFTPWrapper(TFTP):
def _startSession(self, *args, **kwargs):
d = TFTP._startSession(self, *args, **kwargs)
def save_session(session):
self.session = session
return session
d.addCallback(save_session)
return d
class SuccessfulDispatch(unittest.TestCase):
def setUp(self):
self.tmp_dir_path = tempfile.mkdtemp()
with FilePath(self.tmp_dir_path).child('nonempty').open('w') as fd:
fd.write('Something uninteresting')
self.backend = FilesystemSynchronousBackend(self.tmp_dir_path)
self.tftp = TFTPWrapper(self.backend)
self.client = DummyClient()
reactor.listenUDP(0, self.client)
self.server_port = reactor.listenUDP(1069, self.tftp)
# Ok. I am going to hell for these two tests
def test_WRQ(self):
self.client.transport.write(WRQDatagram('foobar', 'NetASCiI', {}).to_wire(), ('127.0.0.1', 1069))
d = Deferred()
def cb(ign):
self.assertIsInstance(self.tftp.session, RemoteOriginWriteSession)
self.assertIsInstance(self.tftp.session.backend, NetasciiReceiverProxy)
self.tftp.session.cancel()
d.addCallback(cb)
reactor.callLater(0.5, d.callback, None)
return d
def test_RRQ(self):
self.client.transport.write(RRQDatagram('nonempty', 'NetASCiI', {}).to_wire(), ('127.0.0.1', 1069))
d = Deferred()
def cb(ign):
self.assertIsInstance(self.tftp.session, RemoteOriginReadSession)
self.assertIsInstance(self.tftp.session.backend, NetasciiSenderProxy)
self.tftp.session.cancel()
d.addCallback(cb)
reactor.callLater(0.5, d.callback, None)
return d
def tearDown(self):
self.tftp.transport.stopListening()
self.client.transport.stopListening()
class FilesystemAsyncBackend(FilesystemSynchronousBackend):
def __init__(self, base_path, clock):
super(FilesystemAsyncBackend, self).__init__(
base_path, can_read=True, can_write=True)
self.clock = clock
def get_reader(self, file_name):
d_get = super(FilesystemAsyncBackend, self).get_reader(file_name)
d = Deferred()
# d_get has already fired, so don't chain d_get to d until later,
# otherwise d will be fired too early.
self.clock.callLater(0, d_get.chainDeferred, d)
return d
def get_writer(self, file_name):
d_get = super(FilesystemAsyncBackend, self).get_writer(file_name)
d = Deferred()
# d_get has already fired, so don't chain d_get to d until later,
# otherwise d will be fired too early.
self.clock.callLater(0, d_get.chainDeferred, d)
return d
class SuccessfulAsyncDispatch(unittest.TestCase):
def setUp(self):
self.clock = Clock()
self.tmp_dir_path = tempfile.mkdtemp()
with FilePath(self.tmp_dir_path).child('nonempty').open('w') as fd:
fd.write('Something uninteresting')
self.backend = FilesystemAsyncBackend(self.tmp_dir_path, self.clock)
self.tftp = TFTP(self.backend, self.clock)
def test_get_reader_defers(self):
rrq_datagram = RRQDatagram('nonempty', 'NetASCiI', {})
rrq_addr = ('127.0.0.1', 1069)
rrq_mode = "octet"
d = self.tftp._startSession(rrq_datagram, rrq_addr, rrq_mode)
self.assertFalse(d.called)
self.clock.advance(1)
self.assertTrue(d.called)
self.assertTrue(IReader.providedBy(d.result.backend))
def test_get_writer_defers(self):
wrq_datagram = WRQDatagram('foobar', 'NetASCiI', {})
wrq_addr = ('127.0.0.1', 1069)
wrq_mode = "octet"
d = self.tftp._startSession(wrq_datagram, wrq_addr, wrq_mode)
self.assertFalse(d.called)
self.clock.advance(1)
self.assertTrue(d.called)
self.assertTrue(IWriter.providedBy(d.result.backend))
class CapturedContext(Exception):
"""A donkey, to carry the call context back up the stack."""
def __init__(self, args, names):
super(CapturedContext, self).__init__(*args)
self.context = dict((name, context.get(name)) for name in names)
class ContextCapturingBackend(object):
"""A fake `IBackend` that raises `CapturedContext`.
Calling `get_reader` or `get_writer` raises a `CapturedContext` exception,
which captures the values of the call context for the given `names`.
"""
def __init__(self, *names):
self.names = names
def get_reader(self, file_name):
raise CapturedContext(("get_reader", file_name), self.names)
def get_writer(self, file_name):
raise CapturedContext(("get_writer", file_name), self.names)
class HostTransport(object):
"""A fake `ITransport` that only responds to `getHost`."""
def __init__(self, host):
self.host = host
def getHost(self):
return IPv4Address("UDP", *self.host)
class BackendCallingContext(unittest.TestCase):
def setUp(self):
super(BackendCallingContext, self).setUp()
self.backend = ContextCapturingBackend("local", "remote")
self.tftp = TFTP(self.backend)
self.tftp.transport = HostTransport(("12.34.56.78", 1234))
@inlineCallbacks
def test_context_rrq(self):
rrq_datagram = RRQDatagram('nonempty', 'NetASCiI', {})
rrq_addr = ('127.0.0.1', 1069)
error = yield self.assertFailure(
self.tftp._startSession(rrq_datagram, rrq_addr, "octet"),
CapturedContext)
self.assertEqual(("get_reader", rrq_datagram.filename), error.args)
self.assertEqual(
{"local": self.tftp.transport.host, "remote": rrq_addr},
error.context)
@inlineCallbacks
def test_context_wrq(self):
wrq_datagram = WRQDatagram('nonempty', 'NetASCiI', {})
wrq_addr = ('127.0.0.1', 1069)
error = yield self.assertFailure(
self.tftp._startSession(wrq_datagram, wrq_addr, "octet"),
CapturedContext)
self.assertEqual(("get_writer", wrq_datagram.filename), error.args)
self.assertEqual(
{"local": self.tftp.transport.host, "remote": wrq_addr},
error.context)
|