'Test the NLST command in non-passive mode.
L{ftp.FTPClient.nlst} should return a Deferred which fires with a
list of filenames when the list command has completed.'

def test_NLST(self):

    self.client.passive = False
    def generatePort(portCmd):
        portCmd.text = ('PORT    %s' % (ftp.encodeHostPort('127.0.0.1', 9876),))
        portCmd.protocol.makeConnection(proto_helpers.StringTransport())
        self.client.lineReceived('150    File    status    okay;    about    to    open    data    connection.')
        portCmd.protocol.dataReceived('foo\r\n')
        portCmd.protocol.dataReceived('bar\r\n')
        portCmd.protocol.dataReceived('baz\r\n')
        portCmd.protocol.connectionLost(failure.Failure(error.ConnectionDone('')))
    def cbList(res, proto):
        fls = proto.buffer.decode(self.client._encoding).splitlines()
        expected = ['foo', 'bar', 'baz']
        expected.sort()
        fls.sort()
        self.assertEqual(fls, expected)
    self.client.generatePortCommand = generatePort
    self._testLogin()
    lstproto = _BufferingProtocol()
    d = self.client.nlst('foo/bar', lstproto).addCallback(cbList, lstproto)
    self.assertEqual(self.transport.value(), ('PORT    %s\r\n' % (ftp.encodeHostPort('127.0.0.1', 9876),)).encode(self.client._encoding))
    self.transport.clear()
    self.client.lineReceived('200    PORT    OK')
    self.assertEqual(self.transport.value(), 'NLST    foo/bar\r\n')
    self.client.lineReceived('226    Transfer    Complete.')
    return d
