1
# Copyright (C) 2005 Robey Pointer <robey@lag.net>, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
A stub SFTP server for loopback SFTP testing.
19
Adapted from the one in paramiko's unit tests.
23
from paramiko import ServerInterface, SFTPServerInterface, SFTPServer, SFTPAttributes, \
24
SFTPHandle, SFTP_OK, AUTH_SUCCESSFUL, OPEN_SUCCEEDED
27
class StubServer (ServerInterface):
28
def check_auth_password(self, username, password):
30
return AUTH_SUCCESSFUL
32
def check_channel_request(self, kind, chanid):
36
class StubSFTPHandle (SFTPHandle):
39
return SFTPAttributes.from_stat(os.fstat(self.readfile.fileno()))
41
return SFTPServer.convert_errno(e.errno)
43
def chattr(self, attr):
44
# python doesn't have equivalents to fchown or fchmod, so we have to
45
# use the stored filename
47
SFTPServer.set_file_attr(self.filename, attr)
49
return SFTPServer.convert_errno(e.errno)
52
class StubSFTPServer (SFTPServerInterface):
53
def __init__(self, server, root):
54
SFTPServerInterface.__init__(self, server)
57
def _realpath(self, path):
58
return self.root + self.canonicalize(path)
60
def list_folder(self, path):
61
path = self._realpath(path)
64
flist = os.listdir(path)
66
attr = SFTPAttributes.from_stat(os.stat(os.path.join(path, fname)))
71
return SFTPServer.convert_errno(e.errno)
74
path = self._realpath(path)
76
return SFTPAttributes.from_stat(os.stat(path))
78
return SFTPServer.convert_errno(e.errno)
80
def lstat(self, path):
81
path = self._realpath(path)
83
return SFTPAttributes.from_stat(os.lstat(path))
85
return SFTPServer.convert_errno(e.errno)
87
def open(self, path, flags, attr):
88
path = self._realpath(path)
90
fd = os.open(path, flags)
92
return SFTPServer.convert_errno(e.errno)
93
if (flags & os.O_CREAT) and (attr is not None):
94
SFTPServer.set_file_attr(path, attr)
95
if flags & os.O_WRONLY:
97
elif flags & os.O_RDWR:
103
f = os.fdopen(fd, fstr)
105
return SFTPServer.convert_errno(e.errno)
106
fobj = StubSFTPHandle()
112
def remove(self, path):
113
path = self._realpath(path)
117
return SFTPServer.convert_errno(e.errno)
120
def rename(self, oldpath, newpath):
121
oldpath = self._realpath(oldpath)
122
newpath = self._realpath(newpath)
124
os.rename(oldpath, newpath)
126
return SFTPServer.convert_errno(e.errno)
129
def mkdir(self, path, attr):
130
path = self._realpath(path)
134
SFTPServer.set_file_attr(path, attr)
136
return SFTPServer.convert_errno(e.errno)
139
def rmdir(self, path):
140
path = self._realpath(path)
144
return SFTPServer.convert_errno(e.errno)
147
# removed: chattr, symlink, readlink
148
# (nothing in bzr's sftp transport uses those)