1
# Copyright (C) 2009, 2010 Canonical Ltd
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.
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.
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
19
Based on pyftpdlib: http://code.google.com/p/pyftpdlib/
24
from pyftpdlib import ftpserver
34
from bzrlib.tests import test_server
37
# Convert the pyftplib string version into a tuple to avoid traps in string
39
pyftplib_version = tuple(map(int, ftpserver.__ver__.split('.')))
42
class AnonymousWithWriteAccessAuthorizer(ftpserver.DummyAuthorizer):
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.
48
if p not in self.read_perms + self.write_perms:
49
raise ftpserver.AuthorizerError('No such permission "%s"' %p)
52
class BzrConformingFS(ftpserver.AbstractedFS):
54
def chmod(self, path, mode):
55
return os.chmod(path, mode)
57
def listdir(self, path):
58
"""List the content of a directory."""
59
return [osutils.safe_utf8(s) for s in os.listdir(path)]
61
def fs2ftp(self, fspath):
62
p = ftpserver.AbstractedFS.fs2ftp(self, osutils.safe_unicode(fspath))
63
return osutils.safe_utf8(p)
65
def ftp2fs(self, ftppath):
66
p = osutils.safe_unicode(ftppath)
67
return ftpserver.AbstractedFS.ftp2fs(self, p)
69
class BzrConformingFTPHandler(ftpserver.FTPHandler):
71
abstracted_fs = BzrConformingFS
73
def __init__(self, conn, server):
74
ftpserver.FTPHandler.__init__(self, conn, server)
75
self.authorizer = server.authorizer
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)
86
ftpserver.FTPHandler.ftp_SIZE(self, path)
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)
97
ftpserver.FTPHandler.ftp_NLST(self, path)
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
104
if cmd in ("DELE", "RMD", "RNFR", "RNTO", "MKD"):
105
line = '"%s" %s' % (' '.join([cmd, unicode(arg)]).strip(), respcode)
109
# An empty password is valid, hence the arg is neither mandatory nor forbidden
110
ftpserver.proto_cmds['PASS']['arg'] = None
112
class ftp_server(ftpserver.FTPServer):
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()
121
class FTPTestServer(test_server.TestServer):
122
"""Common code for FTP server facilities."""
126
self._ftp_server = None
128
self._async_thread = None
131
self._ftpd_running = False
134
"""Calculate an ftp url to this server."""
135
return 'ftp://anonymous@localhost:%d/' % (self._port)
137
def get_bogus_url(self):
138
"""Return a URL which cannot be connected to."""
139
return 'ftp://127.0.0.1:1/'
141
def log(self, message):
142
"""This is used by ftp_server to log connections, etc."""
143
self.logs.append(message)
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"
151
self._root = os.getcwdu()
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,
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 ?
162
ftpserver.log = self.log
163
ftpserver.logline = self.log
164
ftpserver.logerror = self.log
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()
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,))
190
def _run_server(self):
191
"""Run the server until stop_server is called.
193
Shut it down properly then.
195
self._ftpd_running = True
196
self._ftpd_starting.release()
197
while self._ftpd_running:
199
self._ftp_server.serve_forever(timeout=0.1, count=1)
200
except select.error, e:
201
if e.args[0] != errno.EBADF:
203
self._ftp_server.close_all(ignore_all=True)
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,