Ejemplo n.º 1
0
 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()
Ejemplo n.º 2
0
 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
Ejemplo n.º 3
0
class BootstrapRemoteOriginWrite(unittest.TestCase):

    port = 65466

    def setUp(self):
        self.clock = Clock()
        self.temp_dir = FilePath(tempfile.mkdtemp()).asBytesMode()
        self.target = self.temp_dir.child(b'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.assertFalse(self.transport.disconnecting)

        # Normal exchange
        self.transport.clear()
        d = self.ws.datagramReceived(
            DATADatagram(1, b'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(), b'foobar')
            self.assertFalse(self.transport.disconnecting)
            self.addCleanup(self.ws.cancel)

        d.addCallback(cb)
        self.clock.advance(3)
        return d

    def tearDown(self):
        self.temp_dir.remove()
Ejemplo n.º 4
0
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.ws = RemoteOriginWriteSession(('127.0.0.1', 65465), self.writer,
                                           options={'blksize':'9'}, _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()
        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({'blksize':'9'}).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 tearDown(self):
        shutil.rmtree(self.tmp_dir_path)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
 def _startSession(self, datagram, addr, mode):
     # Set up a call context so that we can pass extra arbitrary
     # information to interested backends without adding extra call
     # arguments, or switching to using a request object, for example.
     context = {}
     if self.transport is not None:
         # Add the local and remote addresses to the call context.
         local = self.transport.getHost()
         context["local"] = local.host, local.port
         context["remote"] = addr
     try:
         if datagram.opcode == OP_WRQ:
             fs_interface = yield call(context, self.backend.get_writer,
                                       datagram.filename)
         elif datagram.opcode == OP_RRQ:
             fs_interface = yield call(context, self.backend.get_reader,
                                       datagram.filename)
     except Unsupported as e:
         self.transport.write(
             ERRORDatagram.from_code(
                 ERR_ILLEGAL_OP,
                 u"{}".format(e).encode("ascii", "replace")).to_wire(),
             addr)
     except AccessViolation:
         self.transport.write(
             ERRORDatagram.from_code(ERR_ACCESS_VIOLATION).to_wire(), addr)
     except FileExists:
         self.transport.write(
             ERRORDatagram.from_code(ERR_FILE_EXISTS).to_wire(), addr)
     except FileNotFound:
         self.transport.write(
             ERRORDatagram.from_code(ERR_FILE_NOT_FOUND).to_wire(), addr)
     except BackendError as e:
         self.transport.write(
             ERRORDatagram.from_code(
                 ERR_NOT_DEFINED,
                 u"{}".format(e).encode("ascii", "replace")).to_wire(),
             addr)
     else:
         if datagram.opcode == OP_WRQ:
             if mode == b'netascii':
                 fs_interface = NetasciiReceiverProxy(fs_interface)
             session = RemoteOriginWriteSession(addr,
                                                fs_interface,
                                                datagram.options,
                                                _clock=self._clock)
             reactor.listenUDP(0, session)
             returnValue(session)
         elif datagram.opcode == OP_RRQ:
             if mode == b'netascii':
                 fs_interface = NetasciiSenderProxy(fs_interface)
             session = RemoteOriginReadSession(addr,
                                               fs_interface,
                                               datagram.options,
                                               _clock=self._clock)
             reactor.listenUDP(0, session)
             returnValue(session)
Ejemplo n.º 7
0
 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()
Ejemplo n.º 8
0
 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
Ejemplo n.º 9
0
 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
Ejemplo n.º 10
0
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.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)
Ejemplo n.º 11
0
                ERRORDatagram.from_code(ERR_ACCESS_VIOLATION).to_wire(), addr)
        except FileExists:
            self.transport.write(
                ERRORDatagram.from_code(ERR_FILE_EXISTS).to_wire(), addr)
        except FileNotFound:
            self.transport.write(
                ERRORDatagram.from_code(ERR_FILE_NOT_FOUND).to_wire(), addr)
        except BackendError, e:
            self.transport.write(
                ERRORDatagram.from_code(ERR_NOT_DEFINED, str(e)).to_wire(),
                addr)
        else:
            if datagram.opcode == OP_WRQ:
                if mode == 'netascii':
                    fs_interface = NetasciiReceiverProxy(fs_interface)
                session = RemoteOriginWriteSession(addr,
                                                   fs_interface,
                                                   datagram.options,
                                                   _clock=self._clock)
                reactor.listenUDP(0, session)
                returnValue(session)
            elif datagram.opcode == OP_RRQ:
                if mode == 'netascii':
                    fs_interface = NetasciiSenderProxy(fs_interface)
                session = RemoteOriginReadSession(addr,
                                                  fs_interface,
                                                  datagram.options,
                                                  _clock=self._clock)
                reactor.listenUDP(0, session)
                returnValue(session)
Ejemplo n.º 12
0
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)