1 | """ |
---|
2 | Ported to Python 3. |
---|
3 | """ |
---|
4 | |
---|
5 | import re, struct, traceback, time, calendar |
---|
6 | from stat import S_IFREG, S_IFDIR |
---|
7 | |
---|
8 | from twisted.trial import unittest |
---|
9 | from twisted.internet import defer, reactor |
---|
10 | from twisted.python.failure import Failure |
---|
11 | from twisted.internet.error import ProcessDone, ProcessTerminated |
---|
12 | from allmydata.util import deferredutil |
---|
13 | |
---|
14 | try: |
---|
15 | from twisted.conch import interfaces as conch_interfaces |
---|
16 | from twisted.conch.ssh import filetransfer as sftp |
---|
17 | from allmydata.frontends import sftpd |
---|
18 | except ImportError as e: |
---|
19 | conch_interfaces = sftp = sftpd = None # type: ignore |
---|
20 | conch_unavailable_reason = e |
---|
21 | else: |
---|
22 | conch_unavailable_reason = None # type: ignore |
---|
23 | |
---|
24 | from allmydata.interfaces import IDirectoryNode, ExistingChildError, NoSuchChildError |
---|
25 | from allmydata.mutable.common import NotWriteableError |
---|
26 | |
---|
27 | from allmydata.util.consumer import download_to_data |
---|
28 | from allmydata.immutable import upload |
---|
29 | from allmydata.mutable import publish |
---|
30 | from allmydata.test.no_network import GridTestMixin |
---|
31 | from allmydata.test.common import ShouldFailMixin |
---|
32 | from allmydata.test.common_util import ReallyEqualMixin |
---|
33 | |
---|
34 | class Handler(GridTestMixin, ShouldFailMixin, ReallyEqualMixin, unittest.TestCase): |
---|
35 | """This is a no-network unit test of the SFTPUserHandler and the abstractions it uses.""" |
---|
36 | |
---|
37 | if conch_unavailable_reason: |
---|
38 | skip = "SFTP support requires Twisted Conch which is not available: {}".format( |
---|
39 | conch_unavailable_reason, |
---|
40 | ) |
---|
41 | |
---|
42 | def shouldFailWithSFTPError(self, expected_code, which, callable, *args, **kwargs): |
---|
43 | assert isinstance(expected_code, int), repr(expected_code) |
---|
44 | assert isinstance(which, str), repr(which) |
---|
45 | s = traceback.format_stack() |
---|
46 | d = defer.maybeDeferred(callable, *args, **kwargs) |
---|
47 | def _done(res): |
---|
48 | if isinstance(res, Failure): |
---|
49 | res.trap(sftp.SFTPError) |
---|
50 | self.failUnlessReallyEqual(res.value.code, expected_code, |
---|
51 | "%s was supposed to raise SFTPError(%r), not SFTPError(%r): %s" % |
---|
52 | (which, expected_code, res.value.code, res)) |
---|
53 | else: |
---|
54 | print('@' + '@'.join(s)) |
---|
55 | self.fail("%s was supposed to raise SFTPError(%r), not get %r" % |
---|
56 | (which, expected_code, res)) |
---|
57 | d.addBoth(_done) |
---|
58 | return d |
---|
59 | |
---|
60 | def _set_up(self, basedir, num_clients=1, num_servers=10): |
---|
61 | self.basedir = "sftp/" + basedir |
---|
62 | self.set_up_grid(num_clients=num_clients, num_servers=num_servers, |
---|
63 | oneshare=True) |
---|
64 | |
---|
65 | self.client = self.g.clients[0] |
---|
66 | self.username = "alice" |
---|
67 | |
---|
68 | d = self.client.create_dirnode() |
---|
69 | def _created_root(node): |
---|
70 | self.root = node |
---|
71 | self.root_uri = node.get_uri() |
---|
72 | sftpd._reload() |
---|
73 | self.handler = sftpd.SFTPUserHandler(self.client, self.root, self.username) |
---|
74 | d.addCallback(_created_root) |
---|
75 | return d |
---|
76 | |
---|
77 | def _set_up_tree(self): |
---|
78 | u = publish.MutableData(b"mutable file contents") |
---|
79 | d = self.client.create_mutable_file(u) |
---|
80 | d.addCallback(lambda node: self.root.set_node(u"mutable", node)) |
---|
81 | def _created_mutable(n): |
---|
82 | self.mutable = n |
---|
83 | self.mutable_uri = n.get_uri() |
---|
84 | d.addCallback(_created_mutable) |
---|
85 | |
---|
86 | d.addCallback(lambda ign: |
---|
87 | self.root._create_and_validate_node(None, self.mutable.get_readonly_uri(), name=u"readonly")) |
---|
88 | d.addCallback(lambda node: self.root.set_node(u"readonly", node)) |
---|
89 | def _created_readonly(n): |
---|
90 | self.readonly = n |
---|
91 | self.readonly_uri = n.get_uri() |
---|
92 | d.addCallback(_created_readonly) |
---|
93 | |
---|
94 | gross = upload.Data(b"0123456789" * 101, None) |
---|
95 | d.addCallback(lambda ign: self.root.add_file(u"gro\u00DF", gross)) |
---|
96 | def _created_gross(n): |
---|
97 | self.gross = n |
---|
98 | self.gross_uri = n.get_uri() |
---|
99 | d.addCallback(_created_gross) |
---|
100 | |
---|
101 | small = upload.Data(b"0123456789", None) |
---|
102 | d.addCallback(lambda ign: self.root.add_file(u"small", small)) |
---|
103 | def _created_small(n): |
---|
104 | self.small = n |
---|
105 | self.small_uri = n.get_uri() |
---|
106 | d.addCallback(_created_small) |
---|
107 | |
---|
108 | small2 = upload.Data(b"Small enough for a LIT too", None) |
---|
109 | d.addCallback(lambda ign: self.root.add_file(u"small2", small2)) |
---|
110 | def _created_small2(n): |
---|
111 | self.small2 = n |
---|
112 | self.small2_uri = n.get_uri() |
---|
113 | d.addCallback(_created_small2) |
---|
114 | |
---|
115 | empty_litdir_uri = b"URI:DIR2-LIT:" |
---|
116 | |
---|
117 | # contains one child which is itself also LIT: |
---|
118 | tiny_litdir_uri = b"URI:DIR2-LIT:gqytunj2onug64tufqzdcosvkjetutcjkq5gw4tvm5vwszdgnz5hgyzufqydulbshj5x2lbm" |
---|
119 | |
---|
120 | unknown_uri = b"x-tahoe-crazy://I_am_from_the_future." |
---|
121 | |
---|
122 | d.addCallback(lambda ign: self.root._create_and_validate_node(None, empty_litdir_uri, name=u"empty_lit_dir")) |
---|
123 | def _created_empty_lit_dir(n): |
---|
124 | self.empty_lit_dir = n |
---|
125 | self.empty_lit_dir_uri = n.get_uri() |
---|
126 | self.root.set_node(u"empty_lit_dir", n) |
---|
127 | d.addCallback(_created_empty_lit_dir) |
---|
128 | |
---|
129 | d.addCallback(lambda ign: self.root._create_and_validate_node(None, tiny_litdir_uri, name=u"tiny_lit_dir")) |
---|
130 | def _created_tiny_lit_dir(n): |
---|
131 | self.tiny_lit_dir = n |
---|
132 | self.tiny_lit_dir_uri = n.get_uri() |
---|
133 | self.root.set_node(u"tiny_lit_dir", n) |
---|
134 | d.addCallback(_created_tiny_lit_dir) |
---|
135 | |
---|
136 | d.addCallback(lambda ign: self.root._create_and_validate_node(None, unknown_uri, name=u"unknown")) |
---|
137 | def _created_unknown(n): |
---|
138 | self.unknown = n |
---|
139 | self.unknown_uri = n.get_uri() |
---|
140 | self.root.set_node(u"unknown", n) |
---|
141 | d.addCallback(_created_unknown) |
---|
142 | |
---|
143 | fall_of_the_Berlin_wall = calendar.timegm(time.strptime("1989-11-09 20:00:00 UTC", "%Y-%m-%d %H:%M:%S %Z")) |
---|
144 | md = {'mtime': fall_of_the_Berlin_wall, 'tahoe': {'linkmotime': fall_of_the_Berlin_wall}} |
---|
145 | d.addCallback(lambda ign: self.root.set_node(u"loop", self.root, metadata=md)) |
---|
146 | return d |
---|
147 | |
---|
148 | def test_basic(self): |
---|
149 | d = self._set_up("basic") |
---|
150 | def _check(ign): |
---|
151 | # Test operations that have no side-effects, and don't need the tree. |
---|
152 | |
---|
153 | version = self.handler.gotVersion(3, {}) |
---|
154 | self.failUnless(isinstance(version, dict)) |
---|
155 | |
---|
156 | self.failUnlessReallyEqual(self.handler._path_from_string(b""), []) |
---|
157 | self.failUnlessReallyEqual(self.handler._path_from_string(b"/"), []) |
---|
158 | self.failUnlessReallyEqual(self.handler._path_from_string(b"."), []) |
---|
159 | self.failUnlessReallyEqual(self.handler._path_from_string(b"//"), []) |
---|
160 | self.failUnlessReallyEqual(self.handler._path_from_string(b"/."), []) |
---|
161 | self.failUnlessReallyEqual(self.handler._path_from_string(b"/./"), []) |
---|
162 | self.failUnlessReallyEqual(self.handler._path_from_string(b"foo"), [u"foo"]) |
---|
163 | self.failUnlessReallyEqual(self.handler._path_from_string(b"/foo"), [u"foo"]) |
---|
164 | self.failUnlessReallyEqual(self.handler._path_from_string(b"foo/"), [u"foo"]) |
---|
165 | self.failUnlessReallyEqual(self.handler._path_from_string(b"/foo/"), [u"foo"]) |
---|
166 | self.failUnlessReallyEqual(self.handler._path_from_string(b"foo/bar"), [u"foo", u"bar"]) |
---|
167 | self.failUnlessReallyEqual(self.handler._path_from_string(b"/foo/bar"), [u"foo", u"bar"]) |
---|
168 | self.failUnlessReallyEqual(self.handler._path_from_string(b"foo/bar//"), [u"foo", u"bar"]) |
---|
169 | self.failUnlessReallyEqual(self.handler._path_from_string(b"/foo/bar//"), [u"foo", u"bar"]) |
---|
170 | self.failUnlessReallyEqual(self.handler._path_from_string(b"foo/./bar"), [u"foo", u"bar"]) |
---|
171 | self.failUnlessReallyEqual(self.handler._path_from_string(b"./foo/./bar"), [u"foo", u"bar"]) |
---|
172 | self.failUnlessReallyEqual(self.handler._path_from_string(b"foo/../bar"), [u"bar"]) |
---|
173 | self.failUnlessReallyEqual(self.handler._path_from_string(b"/foo/../bar"), [u"bar"]) |
---|
174 | self.failUnlessReallyEqual(self.handler._path_from_string(b"../bar"), [u"bar"]) |
---|
175 | self.failUnlessReallyEqual(self.handler._path_from_string(b"/../bar"), [u"bar"]) |
---|
176 | |
---|
177 | self.failUnlessReallyEqual(self.handler.realPath(b""), b"/") |
---|
178 | self.failUnlessReallyEqual(self.handler.realPath(b"/"), b"/") |
---|
179 | self.failUnlessReallyEqual(self.handler.realPath(b"."), b"/") |
---|
180 | self.failUnlessReallyEqual(self.handler.realPath(b"//"), b"/") |
---|
181 | self.failUnlessReallyEqual(self.handler.realPath(b"/."), b"/") |
---|
182 | self.failUnlessReallyEqual(self.handler.realPath(b"/./"), b"/") |
---|
183 | self.failUnlessReallyEqual(self.handler.realPath(b"foo"), b"/foo") |
---|
184 | self.failUnlessReallyEqual(self.handler.realPath(b"/foo"), b"/foo") |
---|
185 | self.failUnlessReallyEqual(self.handler.realPath(b"foo/"), b"/foo") |
---|
186 | self.failUnlessReallyEqual(self.handler.realPath(b"/foo/"), b"/foo") |
---|
187 | self.failUnlessReallyEqual(self.handler.realPath(b"foo/bar"), b"/foo/bar") |
---|
188 | self.failUnlessReallyEqual(self.handler.realPath(b"/foo/bar"), b"/foo/bar") |
---|
189 | self.failUnlessReallyEqual(self.handler.realPath(b"foo/bar//"), b"/foo/bar") |
---|
190 | self.failUnlessReallyEqual(self.handler.realPath(b"/foo/bar//"), b"/foo/bar") |
---|
191 | self.failUnlessReallyEqual(self.handler.realPath(b"foo/./bar"), b"/foo/bar") |
---|
192 | self.failUnlessReallyEqual(self.handler.realPath(b"./foo/./bar"), b"/foo/bar") |
---|
193 | self.failUnlessReallyEqual(self.handler.realPath(b"foo/../bar"), b"/bar") |
---|
194 | self.failUnlessReallyEqual(self.handler.realPath(b"/foo/../bar"), b"/bar") |
---|
195 | self.failUnlessReallyEqual(self.handler.realPath(b"../bar"), b"/bar") |
---|
196 | self.failUnlessReallyEqual(self.handler.realPath(b"/../bar"), b"/bar") |
---|
197 | d.addCallback(_check) |
---|
198 | |
---|
199 | d.addCallback(lambda ign: |
---|
200 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "_path_from_string invalid UTF-8", |
---|
201 | self.handler._path_from_string, b"\xFF")) |
---|
202 | d.addCallback(lambda ign: |
---|
203 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "realPath invalid UTF-8", |
---|
204 | self.handler.realPath, b"\xFF")) |
---|
205 | |
---|
206 | return d |
---|
207 | |
---|
208 | def test_convert_error(self): |
---|
209 | self.failUnlessReallyEqual(sftpd._convert_error(None, "request"), None) |
---|
210 | |
---|
211 | d = defer.succeed(None) |
---|
212 | d.addCallback(lambda ign: |
---|
213 | self.shouldFailWithSFTPError(sftp.FX_FAILURE, "_convert_error SFTPError", |
---|
214 | sftpd._convert_error, Failure(sftp.SFTPError(sftp.FX_FAILURE, "foo")), "request")) |
---|
215 | d.addCallback(lambda ign: |
---|
216 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "_convert_error NoSuchChildError", |
---|
217 | sftpd._convert_error, Failure(NoSuchChildError("foo")), "request")) |
---|
218 | d.addCallback(lambda ign: |
---|
219 | self.shouldFailWithSFTPError(sftp.FX_FAILURE, "_convert_error ExistingChildError", |
---|
220 | sftpd._convert_error, Failure(ExistingChildError("foo")), "request")) |
---|
221 | d.addCallback(lambda ign: |
---|
222 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "_convert_error NotWriteableError", |
---|
223 | sftpd._convert_error, Failure(NotWriteableError("foo")), "request")) |
---|
224 | d.addCallback(lambda ign: |
---|
225 | self.shouldFailWithSFTPError(sftp.FX_OP_UNSUPPORTED, "_convert_error NotImplementedError", |
---|
226 | sftpd._convert_error, Failure(NotImplementedError("foo")), "request")) |
---|
227 | d.addCallback(lambda ign: |
---|
228 | self.shouldFailWithSFTPError(sftp.FX_EOF, "_convert_error EOFError", |
---|
229 | sftpd._convert_error, Failure(EOFError("foo")), "request")) |
---|
230 | d.addCallback(lambda ign: |
---|
231 | self.shouldFailWithSFTPError(sftp.FX_EOF, "_convert_error defer.FirstError", |
---|
232 | sftpd._convert_error, Failure(defer.FirstError( |
---|
233 | Failure(sftp.SFTPError(sftp.FX_EOF, "foo")), 0)), "request")) |
---|
234 | d.addCallback(lambda ign: |
---|
235 | self.shouldFailWithSFTPError(sftp.FX_FAILURE, "_convert_error AssertionError", |
---|
236 | sftpd._convert_error, Failure(AssertionError("foo")), "request")) |
---|
237 | |
---|
238 | return d |
---|
239 | |
---|
240 | def test_not_implemented(self): |
---|
241 | d = self._set_up("not_implemented") |
---|
242 | |
---|
243 | d.addCallback(lambda ign: |
---|
244 | self.shouldFailWithSFTPError(sftp.FX_OP_UNSUPPORTED, "readLink link", |
---|
245 | self.handler.readLink, b"link")) |
---|
246 | d.addCallback(lambda ign: |
---|
247 | self.shouldFailWithSFTPError(sftp.FX_OP_UNSUPPORTED, "makeLink link file", |
---|
248 | self.handler.makeLink, b"link", b"file")) |
---|
249 | |
---|
250 | return d |
---|
251 | |
---|
252 | def _compareDirLists(self, actual, expected): |
---|
253 | actual_list = sorted(actual) |
---|
254 | expected_list = sorted(expected) |
---|
255 | self.failUnlessReallyEqual(len(actual_list), len(expected_list), |
---|
256 | "%r is wrong length, expecting %r" % (actual_list, expected_list)) |
---|
257 | for (a, b) in zip(actual_list, expected_list): |
---|
258 | (name, text, attrs) = a |
---|
259 | (expected_name, expected_text_re, expected_attrs) = b |
---|
260 | self.failUnlessReallyEqual(name, expected_name) |
---|
261 | self.failUnless(re.match(expected_text_re, text), |
---|
262 | "%r does not match %r in\n%r" % (text, expected_text_re, actual_list)) |
---|
263 | self._compareAttributes(attrs, expected_attrs) |
---|
264 | |
---|
265 | def _compareAttributes(self, attrs, expected_attrs): |
---|
266 | # It is ok for there to be extra actual attributes. |
---|
267 | # TODO: check times |
---|
268 | for e in expected_attrs: |
---|
269 | self.failUnless(e in attrs, "%r is not in\n%r" % (e, attrs)) |
---|
270 | self.failUnlessReallyEqual(attrs[e], expected_attrs[e], |
---|
271 | "%r:%r is not %r in\n%r" % (e, attrs[e], expected_attrs[e], attrs)) |
---|
272 | |
---|
273 | def test_openDirectory_and_attrs(self): |
---|
274 | d = self._set_up("openDirectory_and_attrs") |
---|
275 | d.addCallback(lambda ign: self._set_up_tree()) |
---|
276 | |
---|
277 | d.addCallback(lambda ign: |
---|
278 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openDirectory small", |
---|
279 | self.handler.openDirectory, b"small")) |
---|
280 | d.addCallback(lambda ign: |
---|
281 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openDirectory unknown", |
---|
282 | self.handler.openDirectory, b"unknown")) |
---|
283 | d.addCallback(lambda ign: |
---|
284 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "openDirectory nodir", |
---|
285 | self.handler.openDirectory, b"nodir")) |
---|
286 | d.addCallback(lambda ign: |
---|
287 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "openDirectory nodir/nodir", |
---|
288 | self.handler.openDirectory, b"nodir/nodir")) |
---|
289 | |
---|
290 | gross = u"gro\u00DF".encode("utf-8") |
---|
291 | expected_root = [ |
---|
292 | (b'empty_lit_dir', br'dr-xr-xr-x .* 0 .* empty_lit_dir$', {'permissions': S_IFDIR | 0o555}), |
---|
293 | (gross, br'-rw-rw-rw- .* 1010 .* '+gross+b'$', {'permissions': S_IFREG | 0o666, 'size': 1010}), |
---|
294 | # The fall of the Berlin wall may have been on 9th or 10th November 1989 depending on the gateway's timezone. |
---|
295 | #('loop', r'drwxrwxrwx .* 0 Nov (09|10) 1989 loop$', {'permissions': S_IFDIR | 0777}), |
---|
296 | (b'loop', br'drwxrwxrwx .* 0 .* loop$', {'permissions': S_IFDIR | 0o777}), |
---|
297 | (b'mutable', br'-rw-rw-rw- .* 0 .* mutable$', {'permissions': S_IFREG | 0o666}), |
---|
298 | (b'readonly', br'-r--r--r-- .* 0 .* readonly$', {'permissions': S_IFREG | 0o444}), |
---|
299 | (b'small', br'-rw-rw-rw- .* 10 .* small$', {'permissions': S_IFREG | 0o666, 'size': 10}), |
---|
300 | (b'small2', br'-rw-rw-rw- .* 26 .* small2$', {'permissions': S_IFREG | 0o666, 'size': 26}), |
---|
301 | (b'tiny_lit_dir', br'dr-xr-xr-x .* 0 .* tiny_lit_dir$', {'permissions': S_IFDIR | 0o555}), |
---|
302 | (b'unknown', br'\?--------- .* 0 .* unknown$', {'permissions': 0}), |
---|
303 | ] |
---|
304 | |
---|
305 | d.addCallback(lambda ign: self.handler.openDirectory(b"")) |
---|
306 | d.addCallback(lambda res: self._compareDirLists(res, expected_root)) |
---|
307 | |
---|
308 | d.addCallback(lambda ign: self.handler.openDirectory(b"loop")) |
---|
309 | d.addCallback(lambda res: self._compareDirLists(res, expected_root)) |
---|
310 | |
---|
311 | d.addCallback(lambda ign: self.handler.openDirectory(b"loop/loop")) |
---|
312 | d.addCallback(lambda res: self._compareDirLists(res, expected_root)) |
---|
313 | |
---|
314 | d.addCallback(lambda ign: self.handler.openDirectory(b"empty_lit_dir")) |
---|
315 | d.addCallback(lambda res: self._compareDirLists(res, [])) |
---|
316 | |
---|
317 | # The UTC epoch may either be in Jan 1 1970 or Dec 31 1969 depending on the gateway's timezone. |
---|
318 | expected_tiny_lit = [ |
---|
319 | (b'short', br'-r--r--r-- .* 8 (Jan 01 1970|Dec 31 1969) short$', {'permissions': S_IFREG | 0o444, 'size': 8}), |
---|
320 | ] |
---|
321 | |
---|
322 | d.addCallback(lambda ign: self.handler.openDirectory(b"tiny_lit_dir")) |
---|
323 | d.addCallback(lambda res: self._compareDirLists(res, expected_tiny_lit)) |
---|
324 | |
---|
325 | d.addCallback(lambda ign: self.handler.getAttrs(b"small", True)) |
---|
326 | d.addCallback(lambda attrs: self._compareAttributes(attrs, {'permissions': S_IFREG | 0o666, 'size': 10})) |
---|
327 | |
---|
328 | d.addCallback(lambda ign: self.handler.setAttrs(b"small", {})) |
---|
329 | d.addCallback(lambda res: self.failUnlessReallyEqual(res, None)) |
---|
330 | |
---|
331 | d.addCallback(lambda ign: self.handler.getAttrs(b"small", True)) |
---|
332 | d.addCallback(lambda attrs: self._compareAttributes(attrs, {'permissions': S_IFREG | 0o666, 'size': 10})) |
---|
333 | |
---|
334 | d.addCallback(lambda ign: |
---|
335 | self.shouldFailWithSFTPError(sftp.FX_OP_UNSUPPORTED, "setAttrs size", |
---|
336 | self.handler.setAttrs, b"small", {'size': 0})) |
---|
337 | |
---|
338 | d.addCallback(lambda ign: self.failUnlessEqual(sftpd.all_heisenfiles, {})) |
---|
339 | d.addCallback(lambda ign: self.failUnlessEqual(self.handler._heisenfiles, {})) |
---|
340 | return d |
---|
341 | |
---|
342 | def test_openFile_read(self): |
---|
343 | d = self._set_up("openFile_read") |
---|
344 | d.addCallback(lambda ign: self._set_up_tree()) |
---|
345 | |
---|
346 | d.addCallback(lambda ign: |
---|
347 | self.shouldFailWithSFTPError(sftp.FX_BAD_MESSAGE, "openFile small 0 bad", |
---|
348 | self.handler.openFile, b"small", 0, {})) |
---|
349 | |
---|
350 | # attempting to open a non-existent file should fail |
---|
351 | d.addCallback(lambda ign: |
---|
352 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "openFile nofile READ nosuch", |
---|
353 | self.handler.openFile, b"nofile", sftp.FXF_READ, {})) |
---|
354 | d.addCallback(lambda ign: |
---|
355 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "openFile nodir/file READ nosuch", |
---|
356 | self.handler.openFile, b"nodir/file", sftp.FXF_READ, {})) |
---|
357 | |
---|
358 | d.addCallback(lambda ign: |
---|
359 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile unknown READ denied", |
---|
360 | self.handler.openFile, b"unknown", sftp.FXF_READ, {})) |
---|
361 | d.addCallback(lambda ign: |
---|
362 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile unknown/file READ denied", |
---|
363 | self.handler.openFile, b"unknown/file", sftp.FXF_READ, {})) |
---|
364 | d.addCallback(lambda ign: |
---|
365 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile tiny_lit_dir READ denied", |
---|
366 | self.handler.openFile, b"tiny_lit_dir", sftp.FXF_READ, {})) |
---|
367 | d.addCallback(lambda ign: |
---|
368 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile unknown uri READ denied", |
---|
369 | self.handler.openFile, b"uri/"+self.unknown_uri, sftp.FXF_READ, {})) |
---|
370 | d.addCallback(lambda ign: |
---|
371 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile tiny_lit_dir uri READ denied", |
---|
372 | self.handler.openFile, b"uri/"+self.tiny_lit_dir_uri, sftp.FXF_READ, {})) |
---|
373 | # FIXME: should be FX_NO_SUCH_FILE? |
---|
374 | d.addCallback(lambda ign: |
---|
375 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile noexist uri READ denied", |
---|
376 | self.handler.openFile, b"uri/URI:noexist", sftp.FXF_READ, {})) |
---|
377 | d.addCallback(lambda ign: |
---|
378 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "openFile invalid UTF-8 uri READ denied", |
---|
379 | self.handler.openFile, b"uri/URI:\xFF", sftp.FXF_READ, {})) |
---|
380 | |
---|
381 | # reading an existing file should succeed |
---|
382 | d.addCallback(lambda ign: self.handler.openFile(b"small", sftp.FXF_READ, {})) |
---|
383 | def _read_small(rf): |
---|
384 | d2 = rf.readChunk(0, 10) |
---|
385 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"0123456789")) |
---|
386 | |
---|
387 | d2.addCallback(lambda ign: rf.readChunk(2, 6)) |
---|
388 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"234567")) |
---|
389 | |
---|
390 | d2.addCallback(lambda ign: rf.readChunk(1, 0)) |
---|
391 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"")) |
---|
392 | |
---|
393 | d2.addCallback(lambda ign: rf.readChunk(8, 4)) # read that starts before EOF is OK |
---|
394 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"89")) |
---|
395 | |
---|
396 | d2.addCallback(lambda ign: |
---|
397 | self.shouldFailWithSFTPError(sftp.FX_EOF, "readChunk starting at EOF (0-byte)", |
---|
398 | rf.readChunk, 10, 0)) |
---|
399 | d2.addCallback(lambda ign: |
---|
400 | self.shouldFailWithSFTPError(sftp.FX_EOF, "readChunk starting at EOF", |
---|
401 | rf.readChunk, 10, 1)) |
---|
402 | d2.addCallback(lambda ign: |
---|
403 | self.shouldFailWithSFTPError(sftp.FX_EOF, "readChunk starting after EOF", |
---|
404 | rf.readChunk, 11, 1)) |
---|
405 | |
---|
406 | d2.addCallback(lambda ign: rf.getAttrs()) |
---|
407 | d2.addCallback(lambda attrs: self._compareAttributes(attrs, {'permissions': S_IFREG | 0o666, 'size': 10})) |
---|
408 | |
---|
409 | d2.addCallback(lambda ign: self.handler.getAttrs(b"small", followLinks=0)) |
---|
410 | d2.addCallback(lambda attrs: self._compareAttributes(attrs, {'permissions': S_IFREG | 0o666, 'size': 10})) |
---|
411 | |
---|
412 | d2.addCallback(lambda ign: |
---|
413 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "writeChunk on read-only handle denied", |
---|
414 | rf.writeChunk, 0, b"a")) |
---|
415 | d2.addCallback(lambda ign: |
---|
416 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "setAttrs on read-only handle denied", |
---|
417 | rf.setAttrs, {})) |
---|
418 | |
---|
419 | d2.addCallback(lambda ign: rf.close()) |
---|
420 | |
---|
421 | d2.addCallback(lambda ign: |
---|
422 | self.shouldFailWithSFTPError(sftp.FX_BAD_MESSAGE, "readChunk on closed file bad", |
---|
423 | rf.readChunk, 0, 1)) |
---|
424 | d2.addCallback(lambda ign: |
---|
425 | self.shouldFailWithSFTPError(sftp.FX_BAD_MESSAGE, "getAttrs on closed file bad", |
---|
426 | rf.getAttrs)) |
---|
427 | |
---|
428 | d2.addCallback(lambda ign: rf.close()) # should be no-op |
---|
429 | return d2 |
---|
430 | d.addCallback(_read_small) |
---|
431 | |
---|
432 | # repeat for a large file |
---|
433 | gross = u"gro\u00DF".encode("utf-8") |
---|
434 | d.addCallback(lambda ign: self.handler.openFile(gross, sftp.FXF_READ, {})) |
---|
435 | def _read_gross(rf): |
---|
436 | d2 = rf.readChunk(0, 10) |
---|
437 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"0123456789")) |
---|
438 | |
---|
439 | d2.addCallback(lambda ign: rf.readChunk(2, 6)) |
---|
440 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"234567")) |
---|
441 | |
---|
442 | d2.addCallback(lambda ign: rf.readChunk(1, 0)) |
---|
443 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"")) |
---|
444 | |
---|
445 | d2.addCallback(lambda ign: rf.readChunk(1008, 4)) # read that starts before EOF is OK |
---|
446 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"89")) |
---|
447 | |
---|
448 | d2.addCallback(lambda ign: |
---|
449 | self.shouldFailWithSFTPError(sftp.FX_EOF, "readChunk starting at EOF (0-byte)", |
---|
450 | rf.readChunk, 1010, 0)) |
---|
451 | d2.addCallback(lambda ign: |
---|
452 | self.shouldFailWithSFTPError(sftp.FX_EOF, "readChunk starting at EOF", |
---|
453 | rf.readChunk, 1010, 1)) |
---|
454 | d2.addCallback(lambda ign: |
---|
455 | self.shouldFailWithSFTPError(sftp.FX_EOF, "readChunk starting after EOF", |
---|
456 | rf.readChunk, 1011, 1)) |
---|
457 | |
---|
458 | d2.addCallback(lambda ign: rf.getAttrs()) |
---|
459 | d2.addCallback(lambda attrs: self._compareAttributes(attrs, {'permissions': S_IFREG | 0o666, 'size': 1010})) |
---|
460 | |
---|
461 | d2.addCallback(lambda ign: self.handler.getAttrs(gross, followLinks=0)) |
---|
462 | d2.addCallback(lambda attrs: self._compareAttributes(attrs, {'permissions': S_IFREG | 0o666, 'size': 1010})) |
---|
463 | |
---|
464 | d2.addCallback(lambda ign: |
---|
465 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "writeChunk on read-only handle denied", |
---|
466 | rf.writeChunk, 0, b"a")) |
---|
467 | d2.addCallback(lambda ign: |
---|
468 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "setAttrs on read-only handle denied", |
---|
469 | rf.setAttrs, {})) |
---|
470 | |
---|
471 | d2.addCallback(lambda ign: rf.close()) |
---|
472 | |
---|
473 | d2.addCallback(lambda ign: |
---|
474 | self.shouldFailWithSFTPError(sftp.FX_BAD_MESSAGE, "readChunk on closed file", |
---|
475 | rf.readChunk, 0, 1)) |
---|
476 | d2.addCallback(lambda ign: |
---|
477 | self.shouldFailWithSFTPError(sftp.FX_BAD_MESSAGE, "getAttrs on closed file", |
---|
478 | rf.getAttrs)) |
---|
479 | |
---|
480 | d2.addCallback(lambda ign: rf.close()) # should be no-op |
---|
481 | return d2 |
---|
482 | d.addCallback(_read_gross) |
---|
483 | |
---|
484 | # reading an existing small file via uri/ should succeed |
---|
485 | d.addCallback(lambda ign: self.handler.openFile(b"uri/"+self.small_uri, sftp.FXF_READ, {})) |
---|
486 | def _read_small_uri(rf): |
---|
487 | d2 = rf.readChunk(0, 10) |
---|
488 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"0123456789")) |
---|
489 | d2.addCallback(lambda ign: rf.close()) |
---|
490 | return d2 |
---|
491 | d.addCallback(_read_small_uri) |
---|
492 | |
---|
493 | # repeat for a large file |
---|
494 | d.addCallback(lambda ign: self.handler.openFile(b"uri/"+self.gross_uri, sftp.FXF_READ, {})) |
---|
495 | def _read_gross_uri(rf): |
---|
496 | d2 = rf.readChunk(0, 10) |
---|
497 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"0123456789")) |
---|
498 | d2.addCallback(lambda ign: rf.close()) |
---|
499 | return d2 |
---|
500 | d.addCallback(_read_gross_uri) |
---|
501 | |
---|
502 | # repeat for a mutable file |
---|
503 | d.addCallback(lambda ign: self.handler.openFile(b"uri/"+self.mutable_uri, sftp.FXF_READ, {})) |
---|
504 | def _read_mutable_uri(rf): |
---|
505 | d2 = rf.readChunk(0, 100) |
---|
506 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"mutable file contents")) |
---|
507 | d2.addCallback(lambda ign: rf.close()) |
---|
508 | return d2 |
---|
509 | d.addCallback(_read_mutable_uri) |
---|
510 | |
---|
511 | # repeat for a file within a directory referenced by URI |
---|
512 | d.addCallback(lambda ign: self.handler.openFile(b"uri/"+self.tiny_lit_dir_uri+b"/short", sftp.FXF_READ, {})) |
---|
513 | def _read_short(rf): |
---|
514 | d2 = rf.readChunk(0, 100) |
---|
515 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"The end.")) |
---|
516 | d2.addCallback(lambda ign: rf.close()) |
---|
517 | return d2 |
---|
518 | d.addCallback(_read_short) |
---|
519 | |
---|
520 | # check that failed downloads cause failed reads. Note that this |
---|
521 | # trashes the grid (by deleting all shares), so this must be at the |
---|
522 | # end of the test function. |
---|
523 | d.addCallback(lambda ign: self.handler.openFile(b"uri/"+self.gross_uri, sftp.FXF_READ, {})) |
---|
524 | def _read_broken(rf): |
---|
525 | d2 = defer.succeed(None) |
---|
526 | d2.addCallback(lambda ign: self.g.nuke_from_orbit()) |
---|
527 | d2.addCallback(lambda ign: |
---|
528 | self.shouldFailWithSFTPError(sftp.FX_FAILURE, "read broken", |
---|
529 | rf.readChunk, 0, 100)) |
---|
530 | # close shouldn't fail |
---|
531 | d2.addCallback(lambda ign: rf.close()) |
---|
532 | d2.addCallback(lambda res: self.failUnlessReallyEqual(res, None)) |
---|
533 | return d2 |
---|
534 | d.addCallback(_read_broken) |
---|
535 | |
---|
536 | d.addCallback(lambda ign: self.failUnlessEqual(sftpd.all_heisenfiles, {})) |
---|
537 | d.addCallback(lambda ign: self.failUnlessEqual(self.handler._heisenfiles, {})) |
---|
538 | return d |
---|
539 | |
---|
540 | def test_openFile_read_error(self): |
---|
541 | # The check at the end of openFile_read tested this for large files, |
---|
542 | # but it trashed the grid in the process, so this needs to be a |
---|
543 | # separate test. |
---|
544 | small = upload.Data(b"0123456789"*10, None) |
---|
545 | d = self._set_up("openFile_read_error") |
---|
546 | d.addCallback(lambda ign: self.root.add_file(u"small", small)) |
---|
547 | d.addCallback(lambda n: self.handler.openFile(b"/uri/"+n.get_uri(), sftp.FXF_READ, {})) |
---|
548 | def _read_broken(rf): |
---|
549 | d2 = defer.succeed(None) |
---|
550 | d2.addCallback(lambda ign: self.g.nuke_from_orbit()) |
---|
551 | d2.addCallback(lambda ign: |
---|
552 | self.shouldFailWithSFTPError(sftp.FX_FAILURE, "read broken", |
---|
553 | rf.readChunk, 0, 100)) |
---|
554 | # close shouldn't fail |
---|
555 | d2.addCallback(lambda ign: rf.close()) |
---|
556 | d2.addCallback(lambda res: self.failUnlessReallyEqual(res, None)) |
---|
557 | return d2 |
---|
558 | d.addCallback(_read_broken) |
---|
559 | |
---|
560 | d.addCallback(lambda ign: self.failUnlessEqual(sftpd.all_heisenfiles, {})) |
---|
561 | d.addCallback(lambda ign: self.failUnlessEqual(self.handler._heisenfiles, {})) |
---|
562 | return d |
---|
563 | |
---|
564 | def test_openFile_write(self): |
---|
565 | d = self._set_up("openFile_write") |
---|
566 | d.addCallback(lambda ign: self._set_up_tree()) |
---|
567 | |
---|
568 | # '' is an invalid filename |
---|
569 | d.addCallback(lambda ign: |
---|
570 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "openFile '' WRITE|CREAT|TRUNC nosuch", |
---|
571 | self.handler.openFile, b"", sftp.FXF_WRITE | sftp.FXF_CREAT | sftp.FXF_TRUNC, {})) |
---|
572 | |
---|
573 | # TRUNC is not valid without CREAT if the file does not already exist |
---|
574 | d.addCallback(lambda ign: |
---|
575 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "openFile newfile WRITE|TRUNC nosuch", |
---|
576 | self.handler.openFile, b"newfile", sftp.FXF_WRITE | sftp.FXF_TRUNC, {})) |
---|
577 | |
---|
578 | # EXCL is not valid without CREAT |
---|
579 | d.addCallback(lambda ign: |
---|
580 | self.shouldFailWithSFTPError(sftp.FX_BAD_MESSAGE, "openFile small WRITE|EXCL bad", |
---|
581 | self.handler.openFile, b"small", sftp.FXF_WRITE | sftp.FXF_EXCL, {})) |
---|
582 | |
---|
583 | # cannot write to an existing directory |
---|
584 | d.addCallback(lambda ign: |
---|
585 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile tiny_lit_dir WRITE denied", |
---|
586 | self.handler.openFile, b"tiny_lit_dir", sftp.FXF_WRITE, {})) |
---|
587 | |
---|
588 | # cannot write to an existing unknown |
---|
589 | d.addCallback(lambda ign: |
---|
590 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile unknown WRITE denied", |
---|
591 | self.handler.openFile, b"unknown", sftp.FXF_WRITE, {})) |
---|
592 | |
---|
593 | # cannot create a child of an unknown |
---|
594 | d.addCallback(lambda ign: |
---|
595 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile unknown/newfile WRITE|CREAT denied", |
---|
596 | self.handler.openFile, b"unknown/newfile", |
---|
597 | sftp.FXF_WRITE | sftp.FXF_CREAT, {})) |
---|
598 | |
---|
599 | # cannot write to a new file in an immutable directory |
---|
600 | d.addCallback(lambda ign: |
---|
601 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile tiny_lit_dir/newfile WRITE|CREAT|TRUNC denied", |
---|
602 | self.handler.openFile, b"tiny_lit_dir/newfile", |
---|
603 | sftp.FXF_WRITE | sftp.FXF_CREAT | sftp.FXF_TRUNC, {})) |
---|
604 | |
---|
605 | # cannot write to an existing immutable file in an immutable directory (with or without CREAT and EXCL) |
---|
606 | d.addCallback(lambda ign: |
---|
607 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile tiny_lit_dir/short WRITE denied", |
---|
608 | self.handler.openFile, b"tiny_lit_dir/short", sftp.FXF_WRITE, {})) |
---|
609 | d.addCallback(lambda ign: |
---|
610 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile tiny_lit_dir/short WRITE|CREAT denied", |
---|
611 | self.handler.openFile, b"tiny_lit_dir/short", |
---|
612 | sftp.FXF_WRITE | sftp.FXF_CREAT, {})) |
---|
613 | |
---|
614 | # cannot write to a mutable file via a readonly cap (by path or uri) |
---|
615 | d.addCallback(lambda ign: |
---|
616 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile readonly WRITE denied", |
---|
617 | self.handler.openFile, b"readonly", sftp.FXF_WRITE, {})) |
---|
618 | d.addCallback(lambda ign: |
---|
619 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile readonly uri WRITE denied", |
---|
620 | self.handler.openFile, b"uri/"+self.readonly_uri, sftp.FXF_WRITE, {})) |
---|
621 | |
---|
622 | # cannot create a file with the EXCL flag if it already exists |
---|
623 | d.addCallback(lambda ign: |
---|
624 | self.shouldFailWithSFTPError(sftp.FX_FAILURE, "openFile small WRITE|CREAT|EXCL failure", |
---|
625 | self.handler.openFile, b"small", |
---|
626 | sftp.FXF_WRITE | sftp.FXF_CREAT | sftp.FXF_EXCL, {})) |
---|
627 | d.addCallback(lambda ign: |
---|
628 | self.shouldFailWithSFTPError(sftp.FX_FAILURE, "openFile mutable WRITE|CREAT|EXCL failure", |
---|
629 | self.handler.openFile, b"mutable", |
---|
630 | sftp.FXF_WRITE | sftp.FXF_CREAT | sftp.FXF_EXCL, {})) |
---|
631 | d.addCallback(lambda ign: |
---|
632 | self.shouldFailWithSFTPError(sftp.FX_FAILURE, "openFile mutable uri WRITE|CREAT|EXCL failure", |
---|
633 | self.handler.openFile, b"uri/"+self.mutable_uri, |
---|
634 | sftp.FXF_WRITE | sftp.FXF_CREAT | sftp.FXF_EXCL, {})) |
---|
635 | d.addCallback(lambda ign: |
---|
636 | self.shouldFailWithSFTPError(sftp.FX_FAILURE, "openFile tiny_lit_dir/short WRITE|CREAT|EXCL failure", |
---|
637 | self.handler.openFile, b"tiny_lit_dir/short", |
---|
638 | sftp.FXF_WRITE | sftp.FXF_CREAT | sftp.FXF_EXCL, {})) |
---|
639 | |
---|
640 | # cannot write to an immutable file if we don't have its parent (with or without CREAT, TRUNC, or EXCL) |
---|
641 | d.addCallback(lambda ign: |
---|
642 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile small uri WRITE denied", |
---|
643 | self.handler.openFile, b"uri/"+self.small_uri, sftp.FXF_WRITE, {})) |
---|
644 | d.addCallback(lambda ign: |
---|
645 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile small uri WRITE|CREAT denied", |
---|
646 | self.handler.openFile, b"uri/"+self.small_uri, |
---|
647 | sftp.FXF_WRITE | sftp.FXF_CREAT, {})) |
---|
648 | d.addCallback(lambda ign: |
---|
649 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile small uri WRITE|CREAT|TRUNC denied", |
---|
650 | self.handler.openFile, b"uri/"+self.small_uri, |
---|
651 | sftp.FXF_WRITE | sftp.FXF_CREAT | sftp.FXF_TRUNC, {})) |
---|
652 | d.addCallback(lambda ign: |
---|
653 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "openFile small uri WRITE|CREAT|EXCL denied", |
---|
654 | self.handler.openFile, b"uri/"+self.small_uri, |
---|
655 | sftp.FXF_WRITE | sftp.FXF_CREAT | sftp.FXF_EXCL, {})) |
---|
656 | |
---|
657 | # test creating a new file with truncation and extension |
---|
658 | d.addCallback(lambda ign: |
---|
659 | self.handler.openFile(b"newfile", sftp.FXF_WRITE | sftp.FXF_CREAT | sftp.FXF_TRUNC, {})) |
---|
660 | def _write(wf): |
---|
661 | d2 = wf.writeChunk(0, b"0123456789") |
---|
662 | d2.addCallback(lambda res: self.failUnlessReallyEqual(res, None)) |
---|
663 | |
---|
664 | d2.addCallback(lambda ign: wf.writeChunk(8, b"0123")) |
---|
665 | d2.addCallback(lambda ign: wf.writeChunk(13, b"abc")) |
---|
666 | |
---|
667 | d2.addCallback(lambda ign: wf.getAttrs()) |
---|
668 | d2.addCallback(lambda attrs: self._compareAttributes(attrs, {'permissions': S_IFREG | 0o666, 'size': 16})) |
---|
669 | |
---|
670 | d2.addCallback(lambda ign: self.handler.getAttrs(b"newfile", followLinks=0)) |
---|
671 | d2.addCallback(lambda attrs: self._compareAttributes(attrs, {'permissions': S_IFREG | 0o666, 'size': 16})) |
---|
672 | |
---|
673 | d2.addCallback(lambda ign: wf.setAttrs({})) |
---|
674 | |
---|
675 | d2.addCallback(lambda ign: |
---|
676 | self.shouldFailWithSFTPError(sftp.FX_BAD_MESSAGE, "setAttrs with negative size bad", |
---|
677 | wf.setAttrs, {'size': -1})) |
---|
678 | |
---|
679 | d2.addCallback(lambda ign: wf.setAttrs({'size': 14})) |
---|
680 | d2.addCallback(lambda ign: wf.getAttrs()) |
---|
681 | d2.addCallback(lambda attrs: self.failUnlessReallyEqual(attrs['size'], 14)) |
---|
682 | |
---|
683 | d2.addCallback(lambda ign: wf.setAttrs({'size': 14})) |
---|
684 | d2.addCallback(lambda ign: wf.getAttrs()) |
---|
685 | d2.addCallback(lambda attrs: self.failUnlessReallyEqual(attrs['size'], 14)) |
---|
686 | |
---|
687 | d2.addCallback(lambda ign: wf.setAttrs({'size': 17})) |
---|
688 | d2.addCallback(lambda ign: wf.getAttrs()) |
---|
689 | d2.addCallback(lambda attrs: self.failUnlessReallyEqual(attrs['size'], 17)) |
---|
690 | d2.addCallback(lambda ign: self.handler.getAttrs(b"newfile", followLinks=0)) |
---|
691 | d2.addCallback(lambda attrs: self.failUnlessReallyEqual(attrs['size'], 17)) |
---|
692 | |
---|
693 | d2.addCallback(lambda ign: |
---|
694 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "readChunk on write-only handle denied", |
---|
695 | wf.readChunk, 0, 1)) |
---|
696 | |
---|
697 | d2.addCallback(lambda ign: wf.close()) |
---|
698 | |
---|
699 | d2.addCallback(lambda ign: |
---|
700 | self.shouldFailWithSFTPError(sftp.FX_BAD_MESSAGE, "writeChunk on closed file bad", |
---|
701 | wf.writeChunk, 0, b"a")) |
---|
702 | d2.addCallback(lambda ign: |
---|
703 | self.shouldFailWithSFTPError(sftp.FX_BAD_MESSAGE, "setAttrs on closed file bad", |
---|
704 | wf.setAttrs, {'size': 0})) |
---|
705 | |
---|
706 | d2.addCallback(lambda ign: wf.close()) # should be no-op |
---|
707 | return d2 |
---|
708 | d.addCallback(_write) |
---|
709 | d.addCallback(lambda ign: self.root.get(u"newfile")) |
---|
710 | d.addCallback(lambda node: download_to_data(node)) |
---|
711 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"012345670123\x00a\x00\x00\x00")) |
---|
712 | |
---|
713 | # test APPEND flag, and also replacing an existing file ("newfile" created by the previous test) |
---|
714 | d.addCallback(lambda ign: |
---|
715 | self.handler.openFile(b"newfile", sftp.FXF_WRITE | sftp.FXF_CREAT | |
---|
716 | sftp.FXF_TRUNC | sftp.FXF_APPEND, {})) |
---|
717 | def _write_append(wf): |
---|
718 | d2 = wf.writeChunk(0, b"0123456789") |
---|
719 | d2.addCallback(lambda ign: wf.writeChunk(8, b"0123")) |
---|
720 | |
---|
721 | d2.addCallback(lambda ign: wf.setAttrs({'size': 17})) |
---|
722 | d2.addCallback(lambda ign: wf.getAttrs()) |
---|
723 | d2.addCallback(lambda attrs: self.failUnlessReallyEqual(attrs['size'], 17)) |
---|
724 | |
---|
725 | d2.addCallback(lambda ign: wf.writeChunk(0, b"z")) |
---|
726 | d2.addCallback(lambda ign: wf.close()) |
---|
727 | return d2 |
---|
728 | d.addCallback(_write_append) |
---|
729 | d.addCallback(lambda ign: self.root.get(u"newfile")) |
---|
730 | d.addCallback(lambda node: download_to_data(node)) |
---|
731 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"01234567890123\x00\x00\x00z")) |
---|
732 | |
---|
733 | # test WRITE | TRUNC without CREAT, when the file already exists |
---|
734 | # This is invalid according to section 6.3 of the SFTP spec, but required for interoperability, |
---|
735 | # since POSIX does allow O_WRONLY | O_TRUNC. |
---|
736 | d.addCallback(lambda ign: |
---|
737 | self.handler.openFile(b"newfile", sftp.FXF_WRITE | sftp.FXF_TRUNC, {})) |
---|
738 | def _write_trunc(wf): |
---|
739 | d2 = wf.writeChunk(0, b"01234") |
---|
740 | d2.addCallback(lambda ign: wf.close()) |
---|
741 | return d2 |
---|
742 | d.addCallback(_write_trunc) |
---|
743 | d.addCallback(lambda ign: self.root.get(u"newfile")) |
---|
744 | d.addCallback(lambda node: download_to_data(node)) |
---|
745 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"01234")) |
---|
746 | |
---|
747 | # test WRITE | TRUNC with permissions: 0 |
---|
748 | d.addCallback(lambda ign: |
---|
749 | self.handler.openFile(b"newfile", sftp.FXF_WRITE | sftp.FXF_TRUNC, {'permissions': 0})) |
---|
750 | d.addCallback(_write_trunc) |
---|
751 | d.addCallback(lambda ign: self.root.get(u"newfile")) |
---|
752 | d.addCallback(lambda node: download_to_data(node)) |
---|
753 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"01234")) |
---|
754 | d.addCallback(lambda ign: self.root.get_metadata_for(u"newfile")) |
---|
755 | d.addCallback(lambda metadata: self.failIf(metadata.get('no-write', False), metadata)) |
---|
756 | |
---|
757 | # test EXCL flag |
---|
758 | d.addCallback(lambda ign: |
---|
759 | self.handler.openFile(b"excl", sftp.FXF_WRITE | sftp.FXF_CREAT | |
---|
760 | sftp.FXF_TRUNC | sftp.FXF_EXCL, {})) |
---|
761 | def _write_excl(wf): |
---|
762 | d2 = self.root.get(u"excl") |
---|
763 | d2.addCallback(lambda node: download_to_data(node)) |
---|
764 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"")) |
---|
765 | |
---|
766 | d2.addCallback(lambda ign: wf.writeChunk(0, b"0123456789")) |
---|
767 | d2.addCallback(lambda ign: wf.close()) |
---|
768 | return d2 |
---|
769 | d.addCallback(_write_excl) |
---|
770 | d.addCallback(lambda ign: self.root.get(u"excl")) |
---|
771 | d.addCallback(lambda node: download_to_data(node)) |
---|
772 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"0123456789")) |
---|
773 | |
---|
774 | # test that writing a zero-length file with EXCL only updates the directory once |
---|
775 | d.addCallback(lambda ign: |
---|
776 | self.handler.openFile(b"zerolength", sftp.FXF_WRITE | sftp.FXF_CREAT | |
---|
777 | sftp.FXF_EXCL, {})) |
---|
778 | def _write_excl_zerolength(wf): |
---|
779 | d2 = self.root.get(u"zerolength") |
---|
780 | d2.addCallback(lambda node: download_to_data(node)) |
---|
781 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"")) |
---|
782 | |
---|
783 | # FIXME: no API to get the best version number exists (fix as part of #993) |
---|
784 | """ |
---|
785 | d2.addCallback(lambda ign: self.root.get_best_version_number()) |
---|
786 | def _check_version(version): |
---|
787 | d3 = wf.close() |
---|
788 | d3.addCallback(lambda ign: self.root.get_best_version_number()) |
---|
789 | d3.addCallback(lambda new_version: self.failUnlessReallyEqual(new_version, version)) |
---|
790 | return d3 |
---|
791 | d2.addCallback(_check_version) |
---|
792 | """ |
---|
793 | d2.addCallback(lambda ign: wf.close()) |
---|
794 | return d2 |
---|
795 | d.addCallback(_write_excl_zerolength) |
---|
796 | d.addCallback(lambda ign: self.root.get(u"zerolength")) |
---|
797 | d.addCallback(lambda node: download_to_data(node)) |
---|
798 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"")) |
---|
799 | |
---|
800 | # test WRITE | CREAT | EXCL | APPEND |
---|
801 | d.addCallback(lambda ign: |
---|
802 | self.handler.openFile(b"exclappend", sftp.FXF_WRITE | sftp.FXF_CREAT | |
---|
803 | sftp.FXF_EXCL | sftp.FXF_APPEND, {})) |
---|
804 | def _write_excl_append(wf): |
---|
805 | d2 = self.root.get(u"exclappend") |
---|
806 | d2.addCallback(lambda node: download_to_data(node)) |
---|
807 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"")) |
---|
808 | |
---|
809 | d2.addCallback(lambda ign: wf.writeChunk(10, b"0123456789")) |
---|
810 | d2.addCallback(lambda ign: wf.writeChunk(5, b"01234")) |
---|
811 | d2.addCallback(lambda ign: wf.close()) |
---|
812 | return d2 |
---|
813 | d.addCallback(_write_excl_append) |
---|
814 | d.addCallback(lambda ign: self.root.get(u"exclappend")) |
---|
815 | d.addCallback(lambda node: download_to_data(node)) |
---|
816 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"012345678901234")) |
---|
817 | |
---|
818 | # test WRITE | CREAT | APPEND when the file does not already exist |
---|
819 | d.addCallback(lambda ign: |
---|
820 | self.handler.openFile(b"creatappend", sftp.FXF_WRITE | sftp.FXF_CREAT | |
---|
821 | sftp.FXF_APPEND, {})) |
---|
822 | def _write_creat_append_new(wf): |
---|
823 | d2 = wf.writeChunk(10, b"0123456789") |
---|
824 | d2.addCallback(lambda ign: wf.writeChunk(5, b"01234")) |
---|
825 | d2.addCallback(lambda ign: wf.close()) |
---|
826 | return d2 |
---|
827 | d.addCallback(_write_creat_append_new) |
---|
828 | d.addCallback(lambda ign: self.root.get(u"creatappend")) |
---|
829 | d.addCallback(lambda node: download_to_data(node)) |
---|
830 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"012345678901234")) |
---|
831 | |
---|
832 | # ... and when it does exist |
---|
833 | d.addCallback(lambda ign: |
---|
834 | self.handler.openFile(b"creatappend", sftp.FXF_WRITE | sftp.FXF_CREAT | |
---|
835 | sftp.FXF_APPEND, {})) |
---|
836 | def _write_creat_append_existing(wf): |
---|
837 | d2 = wf.writeChunk(5, b"01234") |
---|
838 | d2.addCallback(lambda ign: wf.close()) |
---|
839 | return d2 |
---|
840 | d.addCallback(_write_creat_append_existing) |
---|
841 | d.addCallback(lambda ign: self.root.get(u"creatappend")) |
---|
842 | d.addCallback(lambda node: download_to_data(node)) |
---|
843 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"01234567890123401234")) |
---|
844 | |
---|
845 | # test WRITE | CREAT without TRUNC, when the file does not already exist |
---|
846 | d.addCallback(lambda ign: |
---|
847 | self.handler.openFile(b"newfile2", sftp.FXF_WRITE | sftp.FXF_CREAT, {})) |
---|
848 | def _write_creat_new(wf): |
---|
849 | d2 = wf.writeChunk(0, b"0123456789") |
---|
850 | d2.addCallback(lambda ign: wf.close()) |
---|
851 | return d2 |
---|
852 | d.addCallback(_write_creat_new) |
---|
853 | d.addCallback(lambda ign: self.root.get(u"newfile2")) |
---|
854 | d.addCallback(lambda node: download_to_data(node)) |
---|
855 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"0123456789")) |
---|
856 | |
---|
857 | # ... and when it does exist |
---|
858 | d.addCallback(lambda ign: |
---|
859 | self.handler.openFile(b"newfile2", sftp.FXF_WRITE | sftp.FXF_CREAT, {})) |
---|
860 | def _write_creat_existing(wf): |
---|
861 | d2 = wf.writeChunk(0, b"abcde") |
---|
862 | d2.addCallback(lambda ign: wf.close()) |
---|
863 | return d2 |
---|
864 | d.addCallback(_write_creat_existing) |
---|
865 | d.addCallback(lambda ign: self.root.get(u"newfile2")) |
---|
866 | d.addCallback(lambda node: download_to_data(node)) |
---|
867 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"abcde56789")) |
---|
868 | |
---|
869 | d.addCallback(lambda ign: self.root.set_node(u"mutable2", self.mutable)) |
---|
870 | |
---|
871 | # test writing to a mutable file |
---|
872 | d.addCallback(lambda ign: |
---|
873 | self.handler.openFile(b"mutable", sftp.FXF_WRITE, {})) |
---|
874 | def _write_mutable(wf): |
---|
875 | d2 = wf.writeChunk(8, b"new!") |
---|
876 | d2.addCallback(lambda ign: wf.close()) |
---|
877 | return d2 |
---|
878 | d.addCallback(_write_mutable) |
---|
879 | d.addCallback(lambda ign: self.root.get(u"mutable")) |
---|
880 | def _check_same_file(node): |
---|
881 | self.failUnless(node.is_mutable()) |
---|
882 | self.failIf(node.is_readonly()) |
---|
883 | self.failUnlessReallyEqual(node.get_uri(), self.mutable_uri) |
---|
884 | return node.download_best_version() |
---|
885 | d.addCallback(_check_same_file) |
---|
886 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"mutable new! contents")) |
---|
887 | |
---|
888 | # ... and with permissions, which should be ignored |
---|
889 | d.addCallback(lambda ign: |
---|
890 | self.handler.openFile(b"mutable", sftp.FXF_WRITE, {'permissions': 0})) |
---|
891 | d.addCallback(_write_mutable) |
---|
892 | d.addCallback(lambda ign: self.root.get(u"mutable")) |
---|
893 | d.addCallback(_check_same_file) |
---|
894 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"mutable new! contents")) |
---|
895 | |
---|
896 | # ... and with a setAttrs call that diminishes the parent link to read-only, first by path |
---|
897 | d.addCallback(lambda ign: |
---|
898 | self.handler.openFile(b"mutable", sftp.FXF_WRITE, {})) |
---|
899 | def _write_mutable_setattr(wf): |
---|
900 | d2 = wf.writeChunk(8, b"read-only link from parent") |
---|
901 | |
---|
902 | d2.addCallback(lambda ign: self.handler.setAttrs(b"mutable", {'permissions': 0o444})) |
---|
903 | |
---|
904 | d2.addCallback(lambda ign: self.root.get(u"mutable")) |
---|
905 | d2.addCallback(lambda node: self.failUnless(node.is_readonly())) |
---|
906 | |
---|
907 | d2.addCallback(lambda ign: wf.getAttrs()) |
---|
908 | d2.addCallback(lambda attrs: self.failUnlessReallyEqual(attrs['permissions'], S_IFREG | 0o666)) |
---|
909 | d2.addCallback(lambda ign: self.handler.getAttrs(b"mutable", followLinks=0)) |
---|
910 | d2.addCallback(lambda attrs: self.failUnlessReallyEqual(attrs['permissions'], S_IFREG | 0o444)) |
---|
911 | |
---|
912 | d2.addCallback(lambda ign: wf.close()) |
---|
913 | return d2 |
---|
914 | d.addCallback(_write_mutable_setattr) |
---|
915 | d.addCallback(lambda ign: self.root.get(u"mutable")) |
---|
916 | def _check_readonly_file(node): |
---|
917 | self.failUnless(node.is_mutable()) |
---|
918 | self.failUnless(node.is_readonly()) |
---|
919 | self.failUnlessReallyEqual(node.get_write_uri(), None) |
---|
920 | self.failUnlessReallyEqual(node.get_storage_index(), self.mutable.get_storage_index()) |
---|
921 | return node.download_best_version() |
---|
922 | d.addCallback(_check_readonly_file) |
---|
923 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"mutable read-only link from parent")) |
---|
924 | |
---|
925 | # ... and then by handle |
---|
926 | d.addCallback(lambda ign: |
---|
927 | self.handler.openFile(b"mutable2", sftp.FXF_WRITE, {})) |
---|
928 | def _write_mutable2_setattr(wf): |
---|
929 | d2 = wf.writeChunk(7, b"2") |
---|
930 | |
---|
931 | d2.addCallback(lambda ign: wf.setAttrs({'permissions': 0o444, 'size': 8})) |
---|
932 | |
---|
933 | # The link isn't made read-only until the file is closed. |
---|
934 | d2.addCallback(lambda ign: self.root.get(u"mutable2")) |
---|
935 | d2.addCallback(lambda node: self.failIf(node.is_readonly())) |
---|
936 | |
---|
937 | d2.addCallback(lambda ign: wf.getAttrs()) |
---|
938 | d2.addCallback(lambda attrs: self.failUnlessReallyEqual(attrs['permissions'], S_IFREG | 0o444)) |
---|
939 | d2.addCallback(lambda ign: self.handler.getAttrs(b"mutable2", followLinks=0)) |
---|
940 | d2.addCallback(lambda attrs: self.failUnlessReallyEqual(attrs['permissions'], S_IFREG | 0o666)) |
---|
941 | |
---|
942 | d2.addCallback(lambda ign: wf.close()) |
---|
943 | return d2 |
---|
944 | d.addCallback(_write_mutable2_setattr) |
---|
945 | d.addCallback(lambda ign: self.root.get(u"mutable2")) |
---|
946 | d.addCallback(_check_readonly_file) # from above |
---|
947 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"mutable2")) |
---|
948 | |
---|
949 | # test READ | WRITE without CREAT or TRUNC |
---|
950 | d.addCallback(lambda ign: |
---|
951 | self.handler.openFile(b"small", sftp.FXF_READ | sftp.FXF_WRITE, {})) |
---|
952 | def _read_write(rwf): |
---|
953 | d2 = rwf.writeChunk(8, b"0123") |
---|
954 | # test immediate read starting after the old end-of-file |
---|
955 | d2.addCallback(lambda ign: rwf.readChunk(11, 1)) |
---|
956 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"3")) |
---|
957 | d2.addCallback(lambda ign: rwf.readChunk(0, 100)) |
---|
958 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"012345670123")) |
---|
959 | d2.addCallback(lambda ign: rwf.close()) |
---|
960 | return d2 |
---|
961 | d.addCallback(_read_write) |
---|
962 | d.addCallback(lambda ign: self.root.get(u"small")) |
---|
963 | d.addCallback(lambda node: download_to_data(node)) |
---|
964 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"012345670123")) |
---|
965 | |
---|
966 | # test WRITE and rename while still open |
---|
967 | d.addCallback(lambda ign: |
---|
968 | self.handler.openFile(b"small", sftp.FXF_WRITE, {})) |
---|
969 | def _write_rename(wf): |
---|
970 | d2 = wf.writeChunk(0, b"abcd") |
---|
971 | d2.addCallback(lambda ign: self.handler.renameFile(b"small", b"renamed")) |
---|
972 | d2.addCallback(lambda ign: wf.writeChunk(4, b"efgh")) |
---|
973 | d2.addCallback(lambda ign: wf.close()) |
---|
974 | return d2 |
---|
975 | d.addCallback(_write_rename) |
---|
976 | d.addCallback(lambda ign: self.root.get(u"renamed")) |
---|
977 | d.addCallback(lambda node: download_to_data(node)) |
---|
978 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"abcdefgh0123")) |
---|
979 | d.addCallback(lambda ign: |
---|
980 | self.shouldFail(NoSuchChildError, "rename small while open", "small", |
---|
981 | self.root.get, u"small")) |
---|
982 | |
---|
983 | # test WRITE | CREAT | EXCL and rename while still open |
---|
984 | d.addCallback(lambda ign: |
---|
985 | self.handler.openFile(b"newexcl", sftp.FXF_WRITE | sftp.FXF_CREAT | sftp.FXF_EXCL, {})) |
---|
986 | def _write_creat_excl_rename(wf): |
---|
987 | d2 = wf.writeChunk(0, b"abcd") |
---|
988 | d2.addCallback(lambda ign: self.handler.renameFile(b"newexcl", b"renamedexcl")) |
---|
989 | d2.addCallback(lambda ign: wf.writeChunk(4, b"efgh")) |
---|
990 | d2.addCallback(lambda ign: wf.close()) |
---|
991 | return d2 |
---|
992 | d.addCallback(_write_creat_excl_rename) |
---|
993 | d.addCallback(lambda ign: self.root.get(u"renamedexcl")) |
---|
994 | d.addCallback(lambda node: download_to_data(node)) |
---|
995 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"abcdefgh")) |
---|
996 | d.addCallback(lambda ign: |
---|
997 | self.shouldFail(NoSuchChildError, "rename newexcl while open", "newexcl", |
---|
998 | self.root.get, u"newexcl")) |
---|
999 | |
---|
1000 | # it should be possible to rename even before the open has completed |
---|
1001 | def _open_and_rename_race(ign): |
---|
1002 | slow_open = defer.Deferred() |
---|
1003 | reactor.callLater(1, slow_open.callback, None) |
---|
1004 | d2 = self.handler.openFile(b"new", sftp.FXF_WRITE | sftp.FXF_CREAT, {}, delay=slow_open) |
---|
1005 | |
---|
1006 | # deliberate race between openFile and renameFile |
---|
1007 | d3 = self.handler.renameFile(b"new", b"new2") |
---|
1008 | d3.addErrback(lambda err: self.fail("renameFile failed: %r" % (err,))) |
---|
1009 | return d2 |
---|
1010 | d.addCallback(_open_and_rename_race) |
---|
1011 | def _write_rename_race(wf): |
---|
1012 | d2 = wf.writeChunk(0, b"abcd") |
---|
1013 | d2.addCallback(lambda ign: wf.close()) |
---|
1014 | return d2 |
---|
1015 | d.addCallback(_write_rename_race) |
---|
1016 | d.addCallback(lambda ign: self.root.get(u"new2")) |
---|
1017 | d.addCallback(lambda node: download_to_data(node)) |
---|
1018 | d.addCallback(lambda data: self.failUnlessReallyEqual(data, b"abcd")) |
---|
1019 | d.addCallback(lambda ign: |
---|
1020 | self.shouldFail(NoSuchChildError, "rename new while open", "new", |
---|
1021 | self.root.get, u"new")) |
---|
1022 | |
---|
1023 | # check that failed downloads cause failed reads and failed close, |
---|
1024 | # when open for writing. Note that this trashes the grid (by deleting |
---|
1025 | # all shares), so this must be at the end of the test function. |
---|
1026 | gross = u"gro\u00DF".encode("utf-8") |
---|
1027 | d.addCallback(lambda ign: self.handler.openFile(gross, sftp.FXF_READ | sftp.FXF_WRITE, {})) |
---|
1028 | def _read_write_broken(rwf): |
---|
1029 | d2 = rwf.writeChunk(0, b"abcdefghij") |
---|
1030 | d2.addCallback(lambda ign: self.g.nuke_from_orbit()) |
---|
1031 | |
---|
1032 | # reading should fail (reliably if we read past the written chunk) |
---|
1033 | d2.addCallback(lambda ign: |
---|
1034 | self.shouldFailWithSFTPError(sftp.FX_FAILURE, "read/write broken", |
---|
1035 | rwf.readChunk, 0, 100)) |
---|
1036 | # close should fail in this case |
---|
1037 | d2.addCallback(lambda ign: |
---|
1038 | self.shouldFailWithSFTPError(sftp.FX_FAILURE, "read/write broken close", |
---|
1039 | rwf.close)) |
---|
1040 | return d2 |
---|
1041 | d.addCallback(_read_write_broken) |
---|
1042 | |
---|
1043 | d.addCallback(lambda ign: self.failUnlessEqual(sftpd.all_heisenfiles, {})) |
---|
1044 | d.addCallback(lambda ign: self.failUnlessEqual(self.handler._heisenfiles, {})) |
---|
1045 | return d |
---|
1046 | |
---|
1047 | def test_removeFile(self): |
---|
1048 | d = self._set_up("removeFile") |
---|
1049 | d.addCallback(lambda ign: self._set_up_tree()) |
---|
1050 | |
---|
1051 | d.addCallback(lambda ign: |
---|
1052 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "removeFile nofile", |
---|
1053 | self.handler.removeFile, b"nofile")) |
---|
1054 | d.addCallback(lambda ign: |
---|
1055 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "removeFile nofile", |
---|
1056 | self.handler.removeFile, b"nofile")) |
---|
1057 | d.addCallback(lambda ign: |
---|
1058 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "removeFile nodir/file", |
---|
1059 | self.handler.removeFile, b"nodir/file")) |
---|
1060 | d.addCallback(lambda ign: |
---|
1061 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "removefile ''", |
---|
1062 | self.handler.removeFile, b"")) |
---|
1063 | |
---|
1064 | # removing a directory should fail |
---|
1065 | d.addCallback(lambda ign: |
---|
1066 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "removeFile tiny_lit_dir", |
---|
1067 | self.handler.removeFile, b"tiny_lit_dir")) |
---|
1068 | |
---|
1069 | # removing a file should succeed |
---|
1070 | d.addCallback(lambda ign: self.root.get(u"gro\u00DF")) |
---|
1071 | d.addCallback(lambda ign: self.handler.removeFile(u"gro\u00DF".encode('utf-8'))) |
---|
1072 | d.addCallback(lambda ign: |
---|
1073 | self.shouldFail(NoSuchChildError, "removeFile gross", "gro", |
---|
1074 | self.root.get, u"gro\u00DF")) |
---|
1075 | |
---|
1076 | # removing an unknown should succeed |
---|
1077 | d.addCallback(lambda ign: self.root.get(u"unknown")) |
---|
1078 | d.addCallback(lambda ign: self.handler.removeFile(b"unknown")) |
---|
1079 | d.addCallback(lambda ign: |
---|
1080 | self.shouldFail(NoSuchChildError, "removeFile unknown", "unknown", |
---|
1081 | self.root.get, u"unknown")) |
---|
1082 | |
---|
1083 | # removing a link to an open file should not prevent it from being read |
---|
1084 | d.addCallback(lambda ign: self.handler.openFile(b"small", sftp.FXF_READ, {})) |
---|
1085 | def _remove_and_read_small(rf): |
---|
1086 | d2 = self.handler.removeFile(b"small") |
---|
1087 | d2.addCallback(lambda ign: |
---|
1088 | self.shouldFail(NoSuchChildError, "removeFile small", "small", |
---|
1089 | self.root.get, u"small")) |
---|
1090 | d2.addCallback(lambda ign: rf.readChunk(0, 10)) |
---|
1091 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"0123456789")) |
---|
1092 | d2.addCallback(lambda ign: rf.close()) |
---|
1093 | return d2 |
---|
1094 | d.addCallback(_remove_and_read_small) |
---|
1095 | |
---|
1096 | # removing a link to a created file should prevent it from being created |
---|
1097 | d.addCallback(lambda ign: self.handler.openFile(b"tempfile", sftp.FXF_READ | sftp.FXF_WRITE | |
---|
1098 | sftp.FXF_CREAT, {})) |
---|
1099 | def _write_remove(rwf): |
---|
1100 | d2 = rwf.writeChunk(0, b"0123456789") |
---|
1101 | d2.addCallback(lambda ign: self.handler.removeFile(b"tempfile")) |
---|
1102 | d2.addCallback(lambda ign: rwf.readChunk(0, 10)) |
---|
1103 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"0123456789")) |
---|
1104 | d2.addCallback(lambda ign: rwf.close()) |
---|
1105 | return d2 |
---|
1106 | d.addCallback(_write_remove) |
---|
1107 | d.addCallback(lambda ign: |
---|
1108 | self.shouldFail(NoSuchChildError, "removeFile tempfile", "tempfile", |
---|
1109 | self.root.get, u"tempfile")) |
---|
1110 | |
---|
1111 | # ... even if the link is renamed while open |
---|
1112 | d.addCallback(lambda ign: self.handler.openFile(b"tempfile2", sftp.FXF_READ | sftp.FXF_WRITE | |
---|
1113 | sftp.FXF_CREAT, {})) |
---|
1114 | def _write_rename_remove(rwf): |
---|
1115 | d2 = rwf.writeChunk(0, b"0123456789") |
---|
1116 | d2.addCallback(lambda ign: self.handler.renameFile(b"tempfile2", b"tempfile3")) |
---|
1117 | d2.addCallback(lambda ign: self.handler.removeFile(b"tempfile3")) |
---|
1118 | d2.addCallback(lambda ign: rwf.readChunk(0, 10)) |
---|
1119 | d2.addCallback(lambda data: self.failUnlessReallyEqual(data, b"0123456789")) |
---|
1120 | d2.addCallback(lambda ign: rwf.close()) |
---|
1121 | return d2 |
---|
1122 | d.addCallback(_write_rename_remove) |
---|
1123 | d.addCallback(lambda ign: |
---|
1124 | self.shouldFail(NoSuchChildError, "removeFile tempfile2", "tempfile2", |
---|
1125 | self.root.get, u"tempfile2")) |
---|
1126 | d.addCallback(lambda ign: |
---|
1127 | self.shouldFail(NoSuchChildError, "removeFile tempfile3", "tempfile3", |
---|
1128 | self.root.get, u"tempfile3")) |
---|
1129 | |
---|
1130 | d.addCallback(lambda ign: self.failUnlessEqual(sftpd.all_heisenfiles, {})) |
---|
1131 | d.addCallback(lambda ign: self.failUnlessEqual(self.handler._heisenfiles, {})) |
---|
1132 | return d |
---|
1133 | |
---|
1134 | def test_removeDirectory(self): |
---|
1135 | d = self._set_up("removeDirectory") |
---|
1136 | d.addCallback(lambda ign: self._set_up_tree()) |
---|
1137 | |
---|
1138 | d.addCallback(lambda ign: |
---|
1139 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "removeDirectory nodir", |
---|
1140 | self.handler.removeDirectory, b"nodir")) |
---|
1141 | d.addCallback(lambda ign: |
---|
1142 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "removeDirectory nodir/nodir", |
---|
1143 | self.handler.removeDirectory, b"nodir/nodir")) |
---|
1144 | d.addCallback(lambda ign: |
---|
1145 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "removeDirectory ''", |
---|
1146 | self.handler.removeDirectory, b"")) |
---|
1147 | |
---|
1148 | # removing a file should fail |
---|
1149 | d.addCallback(lambda ign: |
---|
1150 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "removeDirectory gross", |
---|
1151 | self.handler.removeDirectory, u"gro\u00DF".encode('utf-8'))) |
---|
1152 | |
---|
1153 | # removing a directory should succeed |
---|
1154 | d.addCallback(lambda ign: self.root.get(u"tiny_lit_dir")) |
---|
1155 | d.addCallback(lambda ign: self.handler.removeDirectory(b"tiny_lit_dir")) |
---|
1156 | d.addCallback(lambda ign: |
---|
1157 | self.shouldFail(NoSuchChildError, "removeDirectory tiny_lit_dir", "tiny_lit_dir", |
---|
1158 | self.root.get, u"tiny_lit_dir")) |
---|
1159 | |
---|
1160 | # removing an unknown should succeed |
---|
1161 | d.addCallback(lambda ign: self.root.get(u"unknown")) |
---|
1162 | d.addCallback(lambda ign: self.handler.removeDirectory(b"unknown")) |
---|
1163 | d.addCallback(lambda err: |
---|
1164 | self.shouldFail(NoSuchChildError, "removeDirectory unknown", "unknown", |
---|
1165 | self.root.get, u"unknown")) |
---|
1166 | |
---|
1167 | d.addCallback(lambda ign: self.failUnlessEqual(sftpd.all_heisenfiles, {})) |
---|
1168 | d.addCallback(lambda ign: self.failUnlessEqual(self.handler._heisenfiles, {})) |
---|
1169 | return d |
---|
1170 | |
---|
1171 | def test_renameFile(self): |
---|
1172 | d = self._set_up("renameFile") |
---|
1173 | d.addCallback(lambda ign: self._set_up_tree()) |
---|
1174 | |
---|
1175 | # renaming a non-existent file should fail |
---|
1176 | d.addCallback(lambda ign: |
---|
1177 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "renameFile nofile newfile", |
---|
1178 | self.handler.renameFile, b"nofile", b"newfile")) |
---|
1179 | d.addCallback(lambda ign: |
---|
1180 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "renameFile '' newfile", |
---|
1181 | self.handler.renameFile, b"", b"newfile")) |
---|
1182 | |
---|
1183 | # renaming a file to a non-existent path should fail |
---|
1184 | d.addCallback(lambda ign: |
---|
1185 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "renameFile small nodir/small", |
---|
1186 | self.handler.renameFile, b"small", b"nodir/small")) |
---|
1187 | |
---|
1188 | # renaming a file to an invalid UTF-8 name should fail |
---|
1189 | d.addCallback(lambda ign: |
---|
1190 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "renameFile small invalid", |
---|
1191 | self.handler.renameFile, b"small", b"\xFF")) |
---|
1192 | |
---|
1193 | # renaming a file to or from an URI should fail |
---|
1194 | d.addCallback(lambda ign: |
---|
1195 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "renameFile small from uri", |
---|
1196 | self.handler.renameFile, b"uri/"+self.small_uri, b"new")) |
---|
1197 | d.addCallback(lambda ign: |
---|
1198 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "renameFile small to uri", |
---|
1199 | self.handler.renameFile, b"small", b"uri/fake_uri")) |
---|
1200 | |
---|
1201 | # renaming a file onto an existing file, directory or unknown should fail |
---|
1202 | # The SFTP spec isn't clear about what error should be returned, but sshfs depends on |
---|
1203 | # it being FX_PERMISSION_DENIED. |
---|
1204 | d.addCallback(lambda ign: |
---|
1205 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "renameFile small small2", |
---|
1206 | self.handler.renameFile, b"small", b"small2")) |
---|
1207 | d.addCallback(lambda ign: |
---|
1208 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "renameFile small tiny_lit_dir", |
---|
1209 | self.handler.renameFile, b"small", b"tiny_lit_dir")) |
---|
1210 | d.addCallback(lambda ign: |
---|
1211 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "renameFile small unknown", |
---|
1212 | self.handler.renameFile, b"small", b"unknown")) |
---|
1213 | |
---|
1214 | # renaming a file onto a heisenfile should fail, even if the open hasn't completed |
---|
1215 | def _rename_onto_heisenfile_race(wf): |
---|
1216 | slow_open = defer.Deferred() |
---|
1217 | reactor.callLater(1, slow_open.callback, None) |
---|
1218 | |
---|
1219 | d2 = self.handler.openFile(b"heisenfile", sftp.FXF_WRITE | sftp.FXF_CREAT, {}, delay=slow_open) |
---|
1220 | |
---|
1221 | # deliberate race between openFile and renameFile |
---|
1222 | d3 = self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "renameFile small heisenfile", |
---|
1223 | self.handler.renameFile, b"small", b"heisenfile") |
---|
1224 | d2.addCallback(lambda wf: wf.close()) |
---|
1225 | return deferredutil.gatherResults([d2, d3]) |
---|
1226 | d.addCallback(_rename_onto_heisenfile_race) |
---|
1227 | |
---|
1228 | # renaming a file to a correct path should succeed |
---|
1229 | d.addCallback(lambda ign: self.handler.renameFile(b"small", b"new_small")) |
---|
1230 | d.addCallback(lambda ign: self.root.get(u"new_small")) |
---|
1231 | d.addCallback(lambda node: self.failUnlessReallyEqual(node.get_uri(), self.small_uri)) |
---|
1232 | |
---|
1233 | # renaming a file into a subdirectory should succeed (also tests Unicode names) |
---|
1234 | d.addCallback(lambda ign: self.handler.renameFile(u"gro\u00DF".encode('utf-8'), |
---|
1235 | u"loop/neue_gro\u00DF".encode('utf-8'))) |
---|
1236 | d.addCallback(lambda ign: self.root.get(u"neue_gro\u00DF")) |
---|
1237 | d.addCallback(lambda node: self.failUnlessReallyEqual(node.get_uri(), self.gross_uri)) |
---|
1238 | |
---|
1239 | # renaming a directory to a correct path should succeed |
---|
1240 | d.addCallback(lambda ign: self.handler.renameFile(b"tiny_lit_dir", b"new_tiny_lit_dir")) |
---|
1241 | d.addCallback(lambda ign: self.root.get(u"new_tiny_lit_dir")) |
---|
1242 | d.addCallback(lambda node: self.failUnlessReallyEqual(node.get_uri(), self.tiny_lit_dir_uri)) |
---|
1243 | |
---|
1244 | # renaming an unknown to a correct path should succeed |
---|
1245 | d.addCallback(lambda ign: self.handler.renameFile(b"unknown", b"new_unknown")) |
---|
1246 | d.addCallback(lambda ign: self.root.get(u"new_unknown")) |
---|
1247 | d.addCallback(lambda node: self.failUnlessReallyEqual(node.get_uri(), self.unknown_uri)) |
---|
1248 | |
---|
1249 | d.addCallback(lambda ign: self.failUnlessEqual(sftpd.all_heisenfiles, {})) |
---|
1250 | d.addCallback(lambda ign: self.failUnlessEqual(self.handler._heisenfiles, {})) |
---|
1251 | return d |
---|
1252 | |
---|
1253 | def test_renameFile_posix(self): |
---|
1254 | def _renameFile(fromPathstring, toPathstring): |
---|
1255 | extData = (struct.pack('>L', len(fromPathstring)) + fromPathstring + |
---|
1256 | struct.pack('>L', len(toPathstring)) + toPathstring) |
---|
1257 | |
---|
1258 | d2 = self.handler.extendedRequest(b'posix-rename@openssh.com', extData) |
---|
1259 | def _check(res): |
---|
1260 | res.trap(sftp.SFTPError) |
---|
1261 | if res.value.code == sftp.FX_OK: |
---|
1262 | return None |
---|
1263 | return res |
---|
1264 | d2.addCallbacks(lambda res: self.fail("posix-rename request was supposed to " |
---|
1265 | "raise an SFTPError, not get '%r'" % (res,)), |
---|
1266 | _check) |
---|
1267 | return d2 |
---|
1268 | |
---|
1269 | d = self._set_up("renameFile_posix") |
---|
1270 | d.addCallback(lambda ign: self._set_up_tree()) |
---|
1271 | |
---|
1272 | d.addCallback(lambda ign: self.root.set_node(u"loop2", self.root)) |
---|
1273 | d.addCallback(lambda ign: self.root.set_node(u"unknown2", self.unknown)) |
---|
1274 | |
---|
1275 | # POSIX-renaming a non-existent file should fail |
---|
1276 | d.addCallback(lambda ign: |
---|
1277 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "renameFile_posix nofile newfile", |
---|
1278 | _renameFile, b"nofile", b"newfile")) |
---|
1279 | d.addCallback(lambda ign: |
---|
1280 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "renameFile_posix '' newfile", |
---|
1281 | _renameFile, b"", b"newfile")) |
---|
1282 | |
---|
1283 | # POSIX-renaming a file to a non-existent path should fail |
---|
1284 | d.addCallback(lambda ign: |
---|
1285 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "renameFile_posix small nodir/small", |
---|
1286 | _renameFile, b"small", b"nodir/small")) |
---|
1287 | |
---|
1288 | # POSIX-renaming a file to an invalid UTF-8 name should fail |
---|
1289 | d.addCallback(lambda ign: |
---|
1290 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "renameFile_posix small invalid", |
---|
1291 | _renameFile, b"small", b"\xFF")) |
---|
1292 | |
---|
1293 | # POSIX-renaming a file to or from an URI should fail |
---|
1294 | d.addCallback(lambda ign: |
---|
1295 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "renameFile_posix small from uri", |
---|
1296 | _renameFile, b"uri/"+self.small_uri, b"new")) |
---|
1297 | d.addCallback(lambda ign: |
---|
1298 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "renameFile_posix small to uri", |
---|
1299 | _renameFile, b"small", b"uri/fake_uri")) |
---|
1300 | |
---|
1301 | # POSIX-renaming a file onto an existing file, directory or unknown should succeed |
---|
1302 | d.addCallback(lambda ign: _renameFile(b"small", b"small2")) |
---|
1303 | d.addCallback(lambda ign: self.root.get(u"small2")) |
---|
1304 | d.addCallback(lambda node: self.failUnlessReallyEqual(node.get_uri(), self.small_uri)) |
---|
1305 | |
---|
1306 | d.addCallback(lambda ign: _renameFile(b"small2", b"loop2")) |
---|
1307 | d.addCallback(lambda ign: self.root.get(u"loop2")) |
---|
1308 | d.addCallback(lambda node: self.failUnlessReallyEqual(node.get_uri(), self.small_uri)) |
---|
1309 | |
---|
1310 | d.addCallback(lambda ign: _renameFile(b"loop2", b"unknown2")) |
---|
1311 | d.addCallback(lambda ign: self.root.get(u"unknown2")) |
---|
1312 | d.addCallback(lambda node: self.failUnlessReallyEqual(node.get_uri(), self.small_uri)) |
---|
1313 | |
---|
1314 | # POSIX-renaming a file to a correct new path should succeed |
---|
1315 | d.addCallback(lambda ign: _renameFile(b"unknown2", b"new_small")) |
---|
1316 | d.addCallback(lambda ign: self.root.get(u"new_small")) |
---|
1317 | d.addCallback(lambda node: self.failUnlessReallyEqual(node.get_uri(), self.small_uri)) |
---|
1318 | |
---|
1319 | # POSIX-renaming a file into a subdirectory should succeed (also tests Unicode names) |
---|
1320 | d.addCallback(lambda ign: _renameFile(u"gro\u00DF".encode('utf-8'), |
---|
1321 | u"loop/neue_gro\u00DF".encode('utf-8'))) |
---|
1322 | d.addCallback(lambda ign: self.root.get(u"neue_gro\u00DF")) |
---|
1323 | d.addCallback(lambda node: self.failUnlessReallyEqual(node.get_uri(), self.gross_uri)) |
---|
1324 | |
---|
1325 | # POSIX-renaming a directory to a correct path should succeed |
---|
1326 | d.addCallback(lambda ign: _renameFile(b"tiny_lit_dir", b"new_tiny_lit_dir")) |
---|
1327 | d.addCallback(lambda ign: self.root.get(u"new_tiny_lit_dir")) |
---|
1328 | d.addCallback(lambda node: self.failUnlessReallyEqual(node.get_uri(), self.tiny_lit_dir_uri)) |
---|
1329 | |
---|
1330 | # POSIX-renaming an unknown to a correct path should succeed |
---|
1331 | d.addCallback(lambda ign: _renameFile(b"unknown", b"new_unknown")) |
---|
1332 | d.addCallback(lambda ign: self.root.get(u"new_unknown")) |
---|
1333 | d.addCallback(lambda node: self.failUnlessReallyEqual(node.get_uri(), self.unknown_uri)) |
---|
1334 | |
---|
1335 | d.addCallback(lambda ign: self.failUnlessEqual(sftpd.all_heisenfiles, {})) |
---|
1336 | d.addCallback(lambda ign: self.failUnlessEqual(self.handler._heisenfiles, {})) |
---|
1337 | return d |
---|
1338 | |
---|
1339 | def test_makeDirectory(self): |
---|
1340 | d = self._set_up("makeDirectory") |
---|
1341 | d.addCallback(lambda ign: self._set_up_tree()) |
---|
1342 | |
---|
1343 | # making a directory at a correct path should succeed |
---|
1344 | d.addCallback(lambda ign: self.handler.makeDirectory(b"newdir", {'ext_foo': 'bar', 'ctime': 42})) |
---|
1345 | |
---|
1346 | d.addCallback(lambda ign: self.root.get_child_and_metadata(u"newdir")) |
---|
1347 | def _got(child_and_metadata): |
---|
1348 | (child, metadata) = child_and_metadata |
---|
1349 | self.failUnless(IDirectoryNode.providedBy(child)) |
---|
1350 | self.failUnless(child.is_mutable()) |
---|
1351 | # FIXME |
---|
1352 | #self.failUnless('ctime' in metadata, metadata) |
---|
1353 | #self.failUnlessReallyEqual(metadata['ctime'], 42) |
---|
1354 | #self.failUnless('ext_foo' in metadata, metadata) |
---|
1355 | #self.failUnlessReallyEqual(metadata['ext_foo'], 'bar') |
---|
1356 | # TODO: child should be empty |
---|
1357 | d.addCallback(_got) |
---|
1358 | |
---|
1359 | # making intermediate directories should also succeed |
---|
1360 | d.addCallback(lambda ign: self.handler.makeDirectory(b"newparent/newchild", {})) |
---|
1361 | |
---|
1362 | d.addCallback(lambda ign: self.root.get(u"newparent")) |
---|
1363 | def _got_newparent(newparent): |
---|
1364 | self.failUnless(IDirectoryNode.providedBy(newparent)) |
---|
1365 | self.failUnless(newparent.is_mutable()) |
---|
1366 | return newparent.get(u"newchild") |
---|
1367 | d.addCallback(_got_newparent) |
---|
1368 | |
---|
1369 | def _got_newchild(newchild): |
---|
1370 | self.failUnless(IDirectoryNode.providedBy(newchild)) |
---|
1371 | self.failUnless(newchild.is_mutable()) |
---|
1372 | d.addCallback(_got_newchild) |
---|
1373 | |
---|
1374 | d.addCallback(lambda ign: |
---|
1375 | self.shouldFailWithSFTPError(sftp.FX_NO_SUCH_FILE, "makeDirectory invalid UTF-8", |
---|
1376 | self.handler.makeDirectory, b"\xFF", {})) |
---|
1377 | |
---|
1378 | # should fail because there is an existing file "small" |
---|
1379 | d.addCallback(lambda ign: |
---|
1380 | self.shouldFailWithSFTPError(sftp.FX_FAILURE, "makeDirectory small", |
---|
1381 | self.handler.makeDirectory, b"small", {})) |
---|
1382 | |
---|
1383 | # directories cannot be created read-only via SFTP |
---|
1384 | d.addCallback(lambda ign: |
---|
1385 | self.shouldFailWithSFTPError(sftp.FX_PERMISSION_DENIED, "makeDirectory newdir2 permissions:0444 denied", |
---|
1386 | self.handler.makeDirectory, b"newdir2", |
---|
1387 | {'permissions': 0o444})) |
---|
1388 | |
---|
1389 | d.addCallback(lambda ign: self.failUnlessEqual(sftpd.all_heisenfiles, {})) |
---|
1390 | d.addCallback(lambda ign: self.failUnlessEqual(self.handler._heisenfiles, {})) |
---|
1391 | return d |
---|
1392 | |
---|
1393 | def test_execCommand_and_openShell(self): |
---|
1394 | class MockProtocol(object): |
---|
1395 | def __init__(self): |
---|
1396 | self.output = "" |
---|
1397 | self.error = "" |
---|
1398 | self.reason = None |
---|
1399 | |
---|
1400 | def write(self, data): |
---|
1401 | return self.outReceived(data) |
---|
1402 | |
---|
1403 | def outReceived(self, data): |
---|
1404 | self.output += data |
---|
1405 | return defer.succeed(None) |
---|
1406 | |
---|
1407 | def errReceived(self, data): |
---|
1408 | self.error += data |
---|
1409 | return defer.succeed(None) |
---|
1410 | |
---|
1411 | def processEnded(self, reason): |
---|
1412 | self.reason = reason |
---|
1413 | return defer.succeed(None) |
---|
1414 | |
---|
1415 | def _lines_end_in_crlf(s): |
---|
1416 | return s.replace('\r\n', '').find('\n') == -1 and s.endswith('\r\n') |
---|
1417 | |
---|
1418 | d = self._set_up("execCommand_and_openShell") |
---|
1419 | |
---|
1420 | d.addCallback(lambda ign: conch_interfaces.ISession(self.handler)) |
---|
1421 | def _exec_df(session): |
---|
1422 | protocol = MockProtocol() |
---|
1423 | d2 = session.execCommand(protocol, "df -P -k /") |
---|
1424 | d2.addCallback(lambda ign: self.failUnlessIn("1024-blocks", protocol.output)) |
---|
1425 | d2.addCallback(lambda ign: self.failUnless(_lines_end_in_crlf(protocol.output), protocol.output)) |
---|
1426 | d2.addCallback(lambda ign: self.failUnlessEqual(protocol.error, "")) |
---|
1427 | d2.addCallback(lambda ign: self.failUnless(isinstance(protocol.reason.value, ProcessDone))) |
---|
1428 | d2.addCallback(lambda ign: session.eofReceived()) |
---|
1429 | d2.addCallback(lambda ign: session.closed()) |
---|
1430 | return d2 |
---|
1431 | d.addCallback(_exec_df) |
---|
1432 | |
---|
1433 | def _check_unsupported(protocol): |
---|
1434 | d2 = defer.succeed(None) |
---|
1435 | d2.addCallback(lambda ign: self.failUnlessEqual(protocol.output, "")) |
---|
1436 | d2.addCallback(lambda ign: self.failUnlessIn("only the SFTP protocol", protocol.error)) |
---|
1437 | d2.addCallback(lambda ign: self.failUnless(_lines_end_in_crlf(protocol.error), protocol.error)) |
---|
1438 | d2.addCallback(lambda ign: self.failUnless(isinstance(protocol.reason.value, ProcessTerminated))) |
---|
1439 | d2.addCallback(lambda ign: self.failUnlessEqual(protocol.reason.value.exitCode, 1)) |
---|
1440 | return d2 |
---|
1441 | |
---|
1442 | d.addCallback(lambda ign: conch_interfaces.ISession(self.handler)) |
---|
1443 | def _exec_error(session): |
---|
1444 | protocol = MockProtocol() |
---|
1445 | d2 = session.execCommand(protocol, "error") |
---|
1446 | d2.addCallback(lambda ign: session.windowChanged(None)) |
---|
1447 | d2.addCallback(lambda ign: _check_unsupported(protocol)) |
---|
1448 | d2.addCallback(lambda ign: session.closed()) |
---|
1449 | return d2 |
---|
1450 | d.addCallback(_exec_error) |
---|
1451 | |
---|
1452 | d.addCallback(lambda ign: conch_interfaces.ISession(self.handler)) |
---|
1453 | def _openShell(session): |
---|
1454 | protocol = MockProtocol() |
---|
1455 | d2 = session.openShell(protocol) |
---|
1456 | d2.addCallback(lambda ign: _check_unsupported(protocol)) |
---|
1457 | d2.addCallback(lambda ign: session.closed()) |
---|
1458 | return d2 |
---|
1459 | d.addCallback(_openShell) |
---|
1460 | |
---|
1461 | return d |
---|
1462 | |
---|
1463 | def test_extendedRequest(self): |
---|
1464 | d = self._set_up("extendedRequest") |
---|
1465 | |
---|
1466 | d.addCallback(lambda ign: self.handler.extendedRequest(b"statvfs@openssh.com", b"/")) |
---|
1467 | def _check(res): |
---|
1468 | self.failUnless(isinstance(res, bytes)) |
---|
1469 | self.failUnlessEqual(len(res), 8*11) |
---|
1470 | d.addCallback(_check) |
---|
1471 | |
---|
1472 | d.addCallback(lambda ign: |
---|
1473 | self.shouldFailWithSFTPError(sftp.FX_OP_UNSUPPORTED, "extendedRequest foo bar", |
---|
1474 | self.handler.extendedRequest, b"foo", b"bar")) |
---|
1475 | |
---|
1476 | d.addCallback(lambda ign: |
---|
1477 | self.shouldFailWithSFTPError(sftp.FX_BAD_MESSAGE, "extendedRequest posix-rename@openssh.com invalid 1", |
---|
1478 | self.handler.extendedRequest, b'posix-rename@openssh.com', b'')) |
---|
1479 | d.addCallback(lambda ign: |
---|
1480 | self.shouldFailWithSFTPError(sftp.FX_BAD_MESSAGE, "extendedRequest posix-rename@openssh.com invalid 2", |
---|
1481 | self.handler.extendedRequest, b'posix-rename@openssh.com', b'\x00\x00\x00\x01')) |
---|
1482 | d.addCallback(lambda ign: |
---|
1483 | self.shouldFailWithSFTPError(sftp.FX_BAD_MESSAGE, "extendedRequest posix-rename@openssh.com invalid 3", |
---|
1484 | self.handler.extendedRequest, b'posix-rename@openssh.com', b'\x00\x00\x00\x01_\x00\x00\x00\x01')) |
---|
1485 | |
---|
1486 | return d |
---|