~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/ftp_server/pyftpdlib_based.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
"""
17
 
FTP test server.
18
 
 
19
 
Based on pyftpdlib: http://code.google.com/p/pyftpdlib/
20
 
"""
21
 
 
22
 
import errno
23
 
import os
24
 
from pyftpdlib import ftpserver
25
 
import select
26
 
import threading
27
 
 
28
 
 
29
 
from bzrlib import (
30
 
    osutils,
31
 
    tests,
32
 
    trace,
33
 
    )
34
 
from bzrlib.tests import test_server
35
 
 
36
 
 
37
 
# Convert the pyftplib string version into a tuple to avoid traps in string
38
 
# comparison.
39
 
pyftplib_version = tuple(map(int, ftpserver.__ver__.split('.')))
40
 
 
41
 
 
42
 
class AnonymousWithWriteAccessAuthorizer(ftpserver.DummyAuthorizer):
43
 
 
44
 
    def _check_permissions(self, username, perm):
45
 
        # Like base implementation but don't warn about write permissions
46
 
        # assigned to anonymous, since that's exactly our purpose.
47
 
        for p in perm:
48
 
            if p not in self.read_perms + self.write_perms:
49
 
                raise ftpserver.AuthorizerError('No such permission "%s"' %p)
50
 
 
51
 
 
52
 
class BzrConformingFS(ftpserver.AbstractedFS):
53
 
 
54
 
    def chmod(self, path, mode):
55
 
        return os.chmod(path, mode)
56
 
 
57
 
    def listdir(self, path):
58
 
        """List the content of a directory."""
59
 
        return [osutils.safe_utf8(s) for s in os.listdir(path)]
60
 
 
61
 
    def fs2ftp(self, fspath):
62
 
        p = ftpserver.AbstractedFS.fs2ftp(self, osutils.safe_unicode(fspath))
63
 
        return osutils.safe_utf8(p)
64
 
 
65
 
    def ftp2fs(self, ftppath):
66
 
        p = osutils.safe_unicode(ftppath)
67
 
        return ftpserver.AbstractedFS.ftp2fs(self, p)
68
 
 
69
 
class BzrConformingFTPHandler(ftpserver.FTPHandler):
70
 
 
71
 
    abstracted_fs = BzrConformingFS
72
 
 
73
 
    def __init__(self, conn, server):
74
 
        ftpserver.FTPHandler.__init__(self, conn, server)
75
 
        self.authorizer = server.authorizer
76
 
 
77
 
    def ftp_SIZE(self, path):
78
 
        # bzr is overly picky here, but we want to make the test suite pass
79
 
        # first. This may need to be revisited -- vila 20090226
80
 
        line = self.fs.fs2ftp(path)
81
 
        if self.fs.isdir(self.fs.realpath(path)):
82
 
            why = "%s is a directory" % line
83
 
            self.log('FAIL SIZE "%s". %s.' % (line, why))
84
 
            self.respond("550 %s."  %why)
85
 
        else:
86
 
            ftpserver.FTPHandler.ftp_SIZE(self, path)
87
 
 
88
 
    def ftp_NLST(self, path):
89
 
        # bzr is overly picky here, but we want to make the test suite pass
90
 
        # first. This may need to be revisited -- vila 20090226
91
 
        line = self.fs.fs2ftp(path)
92
 
        if self.fs.isfile(self.fs.realpath(path)):
93
 
            why = "Not a directory: %s" % line
94
 
            self.log('FAIL NLST "%s". %s.' % (line, why))
95
 
            self.respond("550 %s."  %why)
96
 
        else:
97
 
            ftpserver.FTPHandler.ftp_NLST(self, path)
98
 
 
99
 
    def log_cmd(self, cmd, arg, respcode, respstr):
100
 
        # base class version choke on unicode, the alternative is to just
101
 
        # provide an empty implementation and relies on the client to do
102
 
        # the logging for debugging purposes. Not worth the trouble so far
103
 
        # -- vila 20110607
104
 
        if cmd in ("DELE", "RMD", "RNFR", "RNTO", "MKD"):
105
 
            line = '"%s" %s' % (' '.join([cmd, unicode(arg)]).strip(), respcode)
106
 
            self.log(line)
107
 
 
108
 
 
109
 
# An empty password is valid, hence the arg is neither mandatory nor forbidden
110
 
ftpserver.proto_cmds['PASS']['arg'] = None
111
 
 
112
 
class ftp_server(ftpserver.FTPServer):
113
 
 
114
 
    def __init__(self, address, handler, authorizer):
115
 
        ftpserver.FTPServer.__init__(self, address, handler)
116
 
        self.authorizer = authorizer
117
 
        # Worth backporting upstream ?
118
 
        self.addr = self.socket.getsockname()
119
 
 
120
 
 
121
 
class FTPTestServer(test_server.TestServer):
122
 
    """Common code for FTP server facilities."""
123
 
 
124
 
    def __init__(self):
125
 
        self._root = None
126
 
        self._ftp_server = None
127
 
        self._port = None
128
 
        self._async_thread = None
129
 
        # ftp server logs
130
 
        self.logs = []
131
 
        self._ftpd_running = False
132
 
 
133
 
    def get_url(self):
134
 
        """Calculate an ftp url to this server."""
135
 
        return 'ftp://anonymous@localhost:%d/' % (self._port)
136
 
 
137
 
    def get_bogus_url(self):
138
 
        """Return a URL which cannot be connected to."""
139
 
        return 'ftp://127.0.0.1:1/'
140
 
 
141
 
    def log(self, message):
142
 
        """This is used by ftp_server to log connections, etc."""
143
 
        self.logs.append(message)
144
 
 
145
 
    def start_server(self, vfs_server=None):
146
 
        if not (vfs_server is None or isinstance(vfs_server,
147
 
                                                 test_server.LocalURLServer)):
148
 
            raise AssertionError(
149
 
                "FTPServer currently assumes local transport, got %s"
150
 
                % vfs_server)
151
 
        self._root = os.getcwdu()
152
 
 
153
 
        address = ('localhost', 0) # bind to a random port
154
 
        authorizer = AnonymousWithWriteAccessAuthorizer()
155
 
        authorizer.add_anonymous(self._root, perm='elradfmwM')
156
 
        self._ftp_server = ftp_server(address, BzrConformingFTPHandler,
157
 
                                      authorizer)
158
 
        # This is hacky as hell, will not work if we need two servers working
159
 
        # at the same time, but that's the best we can do so far...
160
 
        # FIXME: At least log and logline could be overriden in the handler ?
161
 
        # -- vila 20090227
162
 
        ftpserver.log = self.log
163
 
        ftpserver.logline = self.log
164
 
        ftpserver.logerror = self.log
165
 
 
166
 
        self._port = self._ftp_server.socket.getsockname()[1]
167
 
        self._ftpd_starting = threading.Lock()
168
 
        self._ftpd_starting.acquire() # So it can be released by the server
169
 
        self._ftpd_thread = threading.Thread(target=self._run_server,)
170
 
        self._ftpd_thread.start()
171
 
        if 'threads' in tests.selftest_debug_flags:
172
 
            sys.stderr.write('Thread started: %s\n'
173
 
                             % (self._ftpd_thread.ident,))
174
 
        # Wait for the server thread to start (i.e release the lock)
175
 
        self._ftpd_starting.acquire()
176
 
        self._ftpd_starting.release()
177
 
 
178
 
    def stop_server(self):
179
 
        """See bzrlib.transport.Server.stop_server."""
180
 
        # Tell the server to stop, but also close the server socket for tests
181
 
        # that start the server but never initiate a connection. Closing the
182
 
        # socket should be done first though, to avoid further connections.
183
 
        self._ftp_server.close()
184
 
        self._ftpd_running = False
185
 
        self._ftpd_thread.join()
186
 
        if 'threads' in tests.selftest_debug_flags:
187
 
            sys.stderr.write('Thread  joined: %s\n'
188
 
                             % (self._ftpd_thread.ident,))
189
 
 
190
 
    def _run_server(self):
191
 
        """Run the server until stop_server is called.
192
 
 
193
 
        Shut it down properly then.
194
 
        """
195
 
        self._ftpd_running = True
196
 
        self._ftpd_starting.release()
197
 
        while self._ftpd_running:
198
 
            try:
199
 
                self._ftp_server.serve_forever(timeout=0.1, count=1)
200
 
            except select.error, e:
201
 
                if e.args[0] != errno.EBADF:
202
 
                    raise
203
 
        self._ftp_server.close_all(ignore_all=True)
204
 
 
205
 
    def add_user(self, user, password):
206
 
        """Add a user with write access."""
207
 
        self._ftp_server.authorizer.add_user(user, password, self._root,
208
 
                                             perm='elradfmwM')