]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
SFTP: add tests for more combinations of open flags.
authordavid-sarah <david-sarah@jacaranda.org>
Wed, 19 May 2010 05:39:33 +0000 (22:39 -0700)
committerdavid-sarah <david-sarah@jacaranda.org>
Wed, 19 May 2010 05:39:33 +0000 (22:39 -0700)
src/allmydata/frontends/sftpd.py
src/allmydata/test/test_sftp.py

index 68fb5192ed2d009793679cdd4457c3b7ea95a725..70cd859518857f043b72b8d7d9b9ba39c118fadf 100644 (file)
@@ -876,10 +876,9 @@ class SFTPUserHandler(ConchUser, PrefixingLogMixin):
             raise SFTPError(FX_BAD_MESSAGE,
                             "invalid file open flags: at least one of FXF_READ and FXF_WRITE must be set")
 
-        if not (flags & FXF_CREAT):
-            if flags & FXF_EXCL:
-                raise SFTPError(FX_BAD_MESSAGE,
-                                "invalid file open flags: FXF_EXCL cannot be set without FXF_CREAT")
+        if (flags & FXF_EXCL) and not (flags & FXF_CREAT):
+            raise SFTPError(FX_BAD_MESSAGE,
+                            "invalid file open flags: FXF_EXCL cannot be set without FXF_CREAT")
 
         path = self._path_from_string(pathstring)
         if not path:
@@ -993,7 +992,7 @@ class SFTPUserHandler(ConchUser, PrefixingLogMixin):
 
                         metadata['readonly'] = _is_readonly(parent_readonly, filenode)
                         return _make_sftp_file(self.check_abort, flags, self._convergence, parent=parent,
-                                              childname=childname, filenode=filenode, metadata=metadata)
+                                               childname=childname, filenode=filenode, metadata=metadata)
                     def _no_child(f):
                         if noisy: self.log("_no_child(%r)" % (f,), level=NOISY)
                         f.trap(NoSuchChildError)
@@ -1006,7 +1005,7 @@ class SFTPUserHandler(ConchUser, PrefixingLogMixin):
                                             "cannot create a file when the parent directory is read-only")
 
                         return _make_sftp_file(self.check_abort, flags, self._convergence, parent=parent,
-                                              childname=childname)
+                                               childname=childname)
                     d3.addCallbacks(_got_child, _no_child)
                     return d3
 
index eb9eb9ee2edb051828e905c13c6c7768413367c1..609e8fcd2370b0d103e891ac85d055aa616cec00 100644 (file)
@@ -747,18 +747,57 @@ class Handler(GridTestMixin, ShouldFailMixin, unittest.TestCase):
         d.addCallback(lambda node: download_to_data(node))
         d.addCallback(lambda data: self.failUnlessReallyEqual(data, "012345678901234"))
 
-        # test WRITE | CREAT without TRUNC
+        # test WRITE | CREAT | APPEND when the file does not already exist
+        d.addCallback(lambda ign:
+                      self.handler.openFile("creatappend", sftp.FXF_WRITE | sftp.FXF_CREAT |
+                                                           sftp.FXF_APPEND, {}))
+        def _write_creat_append_new(wf):
+            d2 = wf.writeChunk(10, "0123456789")
+            d2.addCallback(lambda ign: wf.writeChunk(5, "01234"))
+            d2.addCallback(lambda ign: wf.close())
+            return d2
+        d.addCallback(_write_creat_append_new)
+        d.addCallback(lambda ign: self.root.get(u"creatappend"))
+        d.addCallback(lambda node: download_to_data(node))
+        d.addCallback(lambda data: self.failUnlessReallyEqual(data, "012345678901234"))
+
+        # ... and when it does exist
+        d.addCallback(lambda ign:
+                      self.handler.openFile("creatappend", sftp.FXF_WRITE | sftp.FXF_CREAT |
+                                                           sftp.FXF_APPEND, {}))
+        def _write_creat_append_existing(wf):
+            d2 = wf.writeChunk(5, "01234")
+            d2.addCallback(lambda ign: wf.close())
+            return d2
+        d.addCallback(_write_creat_append_existing)
+        d.addCallback(lambda ign: self.root.get(u"creatappend"))
+        d.addCallback(lambda node: download_to_data(node))
+        d.addCallback(lambda data: self.failUnlessReallyEqual(data, "01234567890123401234"))
+
+        # test WRITE | CREAT without TRUNC, when the file does not already exist
         d.addCallback(lambda ign:
                       self.handler.openFile("newfile2", sftp.FXF_WRITE | sftp.FXF_CREAT, {}))
-        def _write_notrunc(wf):
+        def _write_creat_new(wf):
             d2 =  wf.writeChunk(0, "0123456789")
             d2.addCallback(lambda ign: wf.close())
             return d2
-        d.addCallback(_write_notrunc)
+        d.addCallback(_write_creat_new)
         d.addCallback(lambda ign: self.root.get(u"newfile2"))
         d.addCallback(lambda node: download_to_data(node))
         d.addCallback(lambda data: self.failUnlessReallyEqual(data, "0123456789"))
 
+        # ... and when it does exist
+        d.addCallback(lambda ign:
+                      self.handler.openFile("newfile2", sftp.FXF_WRITE | sftp.FXF_CREAT, {}))
+        def _write_creat_existing(wf):
+            d2 =  wf.writeChunk(0, "abcde")
+            d2.addCallback(lambda ign: wf.close())
+            return d2
+        d.addCallback(_write_creat_existing)
+        d.addCallback(lambda ign: self.root.get(u"newfile2"))
+        d.addCallback(lambda node: download_to_data(node))
+        d.addCallback(lambda data: self.failUnlessReallyEqual(data, "abcde56789"))
+
         # test writing to a mutable file
         d.addCallback(lambda ign:
                       self.handler.openFile("mutable", sftp.FXF_WRITE, {}))