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)) error_datagram = TFTPDatagramFactory(*split_opcode(self.transport.value())) self.assertEqual(error_datagram.errorcode, ERR_FILE_EXISTS)
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_bad_mode(self): tftp = TFTP(DummyBackend(), _clock=self.clock) tftp.transport = self.transport wrq_datagram = WRQDatagram(b'foobar', b'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_file_exists(self): tftp = TFTP(BackendFactory(FileExists("Already have one")), _clock=self.clock) tftp.transport = self.transport wrq_datagram = WRQDatagram(b'foobar', b'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_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)) 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)) error_datagram = TFTPDatagramFactory(*split_opcode(self.transport.value())) self.assertEqual(error_datagram.errorcode, ERR_ACCESS_VIOLATION)
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)) 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)) 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(b'foobar', b'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(b'foobar', b'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_unsupported(self): tftp = TFTP(BackendFactory(Unsupported("I don't support you")), _clock=self.clock) tftp.transport = self.transport wrq_datagram = WRQDatagram(b'foobar', b'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(b'foobar', b'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_get_writer_defers(self): wrq_datagram = WRQDatagram(b'foobar', b'NetASCiI', {}) wrq_addr = ('127.0.0.1', 1069) wrq_mode = b"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))
def test_context_wrq(self): wrq_datagram = WRQDatagram(b'nonempty', b'NetASCiI', {}) wrq_addr = ('127.0.0.1', 1069) error = yield self.assertFailure( self.tftp._startSession(wrq_datagram, wrq_addr, b"octet"), CapturedContext) self.assertEqual(("get_writer", wrq_datagram.filename), error.args) self.assertEqual( { "local": self.tftp.transport.host, "remote": wrq_addr }, error.context)
def test_WRQ(self): self.client.transport.write( WRQDatagram(b'foobar', b'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