bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
1185.16.127
by Martin Pool
[patch] paramiko sftp tests (robey) |
1 |
# Copyright (C) 2005 Robey Pointer <robey@lag.net>, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
17 |
"""
|
|
18 |
A stub SFTP server for loopback SFTP testing.
|
|
19 |
Adapted from the one in paramiko's unit tests.
|
|
20 |
"""
|
|
21 |
||
22 |
import os |
|
23 |
from paramiko import ServerInterface, SFTPServerInterface, SFTPServer, SFTPAttributes, \ |
|
24 |
SFTPHandle, SFTP_OK, AUTH_SUCCESSFUL, OPEN_SUCCEEDED |
|
|
1185.31.32
by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \ |
25 |
from bzrlib.osutils import pathjoin |
|
1185.58.2
by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work. |
26 |
from bzrlib.trace import mutter |
|
1185.16.127
by Martin Pool
[patch] paramiko sftp tests (robey) |
27 |
|
28 |
||
29 |
class StubServer (ServerInterface): |
|
|
1185.49.10
by John Arbash Meinel
Use a weakref dictionary to enable re-use of a connection (for sftp). |
30 |
def __init__(self, test_case): |
31 |
ServerInterface.__init__(self) |
|
32 |
self._test_case = test_case |
|
33 |
||
|
1185.16.127
by Martin Pool
[patch] paramiko sftp tests (robey) |
34 |
def check_auth_password(self, username, password): |
35 |
# all are allowed
|
|
|
1185.49.10
by John Arbash Meinel
Use a weakref dictionary to enable re-use of a connection (for sftp). |
36 |
self._test_case.log('sftpserver - authorizing: %s' % (username,)) |
|
1185.16.127
by Martin Pool
[patch] paramiko sftp tests (robey) |
37 |
return AUTH_SUCCESSFUL |
38 |
||
39 |
def check_channel_request(self, kind, chanid): |
|
|
1185.49.10
by John Arbash Meinel
Use a weakref dictionary to enable re-use of a connection (for sftp). |
40 |
self._test_case.log('sftpserver - channel request: %s, %s' % (kind, chanid)) |
|
1185.16.127
by Martin Pool
[patch] paramiko sftp tests (robey) |
41 |
return OPEN_SUCCEEDED |
42 |
||
43 |
||
44 |
class StubSFTPHandle (SFTPHandle): |
|
45 |
def stat(self): |
|
46 |
try: |
|
47 |
return SFTPAttributes.from_stat(os.fstat(self.readfile.fileno())) |
|
48 |
except OSError, e: |
|
49 |
return SFTPServer.convert_errno(e.errno) |
|
50 |
||
51 |
def chattr(self, attr): |
|
52 |
# python doesn't have equivalents to fchown or fchmod, so we have to
|
|
53 |
# use the stored filename
|
|
|
1185.58.2
by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work. |
54 |
mutter('Changing permissions on %s to %s', self.filename, attr) |
|
1185.16.127
by Martin Pool
[patch] paramiko sftp tests (robey) |
55 |
try: |
56 |
SFTPServer.set_file_attr(self.filename, attr) |
|
57 |
except OSError, e: |
|
58 |
return SFTPServer.convert_errno(e.errno) |
|
59 |
||
60 |
||
61 |
class StubSFTPServer (SFTPServerInterface): |
|
62 |
def __init__(self, server, root): |
|
63 |
SFTPServerInterface.__init__(self, server) |
|
64 |
self.root = root |
|
65 |
||
66 |
def _realpath(self, path): |
|
67 |
return self.root + self.canonicalize(path) |
|
68 |
||
|
1185.58.2
by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work. |
69 |
def chattr(self, path, attr): |
70 |
try: |
|
71 |
SFTPServer.set_file_attr(path, attr) |
|
72 |
except OSError, e: |
|
73 |
return SFTPServer.convert_errno(e.errno) |
|
74 |
return SFTP_OK |
|
75 |
||
|
1185.16.127
by Martin Pool
[patch] paramiko sftp tests (robey) |
76 |
def list_folder(self, path): |
77 |
path = self._realpath(path) |
|
78 |
try: |
|
79 |
out = [ ] |
|
80 |
flist = os.listdir(path) |
|
81 |
for fname in flist: |
|
|
1185.31.32
by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \ |
82 |
attr = SFTPAttributes.from_stat(os.stat(pathjoin(path, fname))) |
|
1185.16.127
by Martin Pool
[patch] paramiko sftp tests (robey) |
83 |
attr.filename = fname |
84 |
out.append(attr) |
|
85 |
return out |
|
86 |
except OSError, e: |
|
87 |
return SFTPServer.convert_errno(e.errno) |
|
88 |
||
89 |
def stat(self, path): |
|
90 |
path = self._realpath(path) |
|
91 |
try: |
|
92 |
return SFTPAttributes.from_stat(os.stat(path)) |
|
93 |
except OSError, e: |
|
94 |
return SFTPServer.convert_errno(e.errno) |
|
95 |
||
96 |
def lstat(self, path): |
|
97 |
path = self._realpath(path) |
|
98 |
try: |
|
99 |
return SFTPAttributes.from_stat(os.lstat(path)) |
|
100 |
except OSError, e: |
|
101 |
return SFTPServer.convert_errno(e.errno) |
|
102 |
||
103 |
def open(self, path, flags, attr): |
|
104 |
path = self._realpath(path) |
|
105 |
try: |
|
|
1185.31.52
by John Arbash Meinel
O_BINARY doesn't exist outside of win32, don't pass it if it isn't there. |
106 |
if hasattr(os, 'O_BINARY'): |
107 |
flags |= os.O_BINARY |
|
|
1185.58.10
by John Arbash Meinel
[patch] Robey Pointer to fix sftp server using umask for files (failing tests for directories) |
108 |
if (attr is not None) and hasattr(attr, 'st_mode'): |
109 |
fd = os.open(path, flags, attr.st_mode) |
|
110 |
else: |
|
111 |
fd = os.open(path, flags) |
|
|
1185.16.127
by Martin Pool
[patch] paramiko sftp tests (robey) |
112 |
except OSError, e: |
113 |
return SFTPServer.convert_errno(e.errno) |
|
114 |
if (flags & os.O_CREAT) and (attr is not None): |
|
|
1185.58.10
by John Arbash Meinel
[patch] Robey Pointer to fix sftp server using umask for files (failing tests for directories) |
115 |
attr._flags &= ~attr.FLAG_PERMISSIONS |
|
1185.16.127
by Martin Pool
[patch] paramiko sftp tests (robey) |
116 |
SFTPServer.set_file_attr(path, attr) |
117 |
if flags & os.O_WRONLY: |
|
|
1185.31.51
by John Arbash Meinel
Setting binary flags for sftp. |
118 |
fstr = 'wb' |
|
1185.16.127
by Martin Pool
[patch] paramiko sftp tests (robey) |
119 |
elif flags & os.O_RDWR: |
|
1185.31.51
by John Arbash Meinel
Setting binary flags for sftp. |
120 |
fstr = 'rb+' |
|
1185.16.127
by Martin Pool
[patch] paramiko sftp tests (robey) |
121 |
else: |
122 |
# O_RDONLY (== 0)
|
|
|
1185.31.51
by John Arbash Meinel
Setting binary flags for sftp. |
123 |
fstr = 'rb' |
|
1185.16.127
by Martin Pool
[patch] paramiko sftp tests (robey) |
124 |
try: |
125 |
f = os.fdopen(fd, fstr) |
|
126 |
except OSError, e: |
|
127 |
return SFTPServer.convert_errno(e.errno) |
|
128 |
fobj = StubSFTPHandle() |
|
129 |
fobj.filename = path |
|
130 |
fobj.readfile = f |
|
131 |
fobj.writefile = f |
|
132 |
return fobj |
|
133 |
||
134 |
def remove(self, path): |
|
135 |
path = self._realpath(path) |
|
136 |
try: |
|
137 |
os.remove(path) |
|
138 |
except OSError, e: |
|
139 |
return SFTPServer.convert_errno(e.errno) |
|
140 |
return SFTP_OK |
|
141 |
||
142 |
def rename(self, oldpath, newpath): |
|
143 |
oldpath = self._realpath(oldpath) |
|
144 |
newpath = self._realpath(newpath) |
|
145 |
try: |
|
146 |
os.rename(oldpath, newpath) |
|
147 |
except OSError, e: |
|
148 |
return SFTPServer.convert_errno(e.errno) |
|
149 |
return SFTP_OK |
|
150 |
||
151 |
def mkdir(self, path, attr): |
|
152 |
path = self._realpath(path) |
|
153 |
try: |
|
|
1185.58.11
by John Arbash Meinel
Made the StubSFTPServer use umask even for mkdir() |
154 |
if attr is not None and hasattr(attr, 'st_mode'): |
155 |
os.mkdir(path, attr.st_mode) |
|
156 |
else: |
|
157 |
os.mkdir(path) |
|
|
1185.16.127
by Martin Pool
[patch] paramiko sftp tests (robey) |
158 |
if attr is not None: |
|
1185.58.11
by John Arbash Meinel
Made the StubSFTPServer use umask even for mkdir() |
159 |
attr._flags &= ~attr.FLAG_PERMISSIONS |
|
1185.16.127
by Martin Pool
[patch] paramiko sftp tests (robey) |
160 |
SFTPServer.set_file_attr(path, attr) |
161 |
except OSError, e: |
|
162 |
return SFTPServer.convert_errno(e.errno) |
|
163 |
return SFTP_OK |
|
164 |
||
165 |
def rmdir(self, path): |
|
166 |
path = self._realpath(path) |
|
167 |
try: |
|
168 |
os.rmdir(path) |
|
169 |
except OSError, e: |
|
170 |
return SFTPServer.convert_errno(e.errno) |
|
171 |
return SFTP_OK |
|
172 |
||
173 |
# removed: chattr, symlink, readlink
|
|
174 |
# (nothing in bzr's sftp transport uses those)
|