Beispiel #1
0
 def test_write(self):
     filename = b'services'
     write_offset = 5
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(filename),
         sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE | SSH2_FXF_READ),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
         sftpint(0o644),
     )
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     chunk = open('/etc/services', 'rb').read()
     self.server.input_queue = sftpcmd(
         SSH2_FXP_WRITE,
         sftpstring(handle),
         sftpint64(write_offset),
         sftpstring(chunk),
     )
     self.server.process()
     self.assertEqual(self.hook.get_result('write', 'filename'), filename)
     self.assertEqual(self.hook.get_result('write', 'offset'), write_offset)
     self.assertEqual(self.hook.get_result('write', 'chunk'), chunk)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     os.unlink(filename)
 def test_init(self):
     self.server.input_queue = sftpcmd(
         SSH2_FXP_INIT, sftpint(2), sftpint(0)
     )
     self.server.process()
     version = get_sftpint(self.server.output_queue)
     self.assertEqual(version, SSH2_FILEXFER_VERSION)
 def test_write(self, mock_request):
     filename = b'services'
     write_offset = 5
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(filename),
         sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE | SSH2_FXF_READ),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
         sftpint(0o644),
     )
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     chunk = open('/etc/services', 'rb').read()
     self.server.input_queue = sftpcmd(
         SSH2_FXP_WRITE,
         sftpstring(handle),
         sftpint64(write_offset),
         sftpstring(chunk),
     )
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     mock_request.assert_has_calls([
         mock.ANY,  # open
         mock.call(
             'POST', 'test_url/write', auth=None,
             data={
                 'method': 'write', 'filename': filename,
                 'offset': write_offset}),
         mock.ANY,  # close
     ])
     os.unlink(filename)
Beispiel #4
0
 def test_open(self, mock_request):
     filename = b'services'
     flags = SSH2_FXF_CREAT | SSH2_FXF_WRITE
     perm = 0o100600
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(filename),
         sftpint(flags),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
         sftpint(perm),
     )
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     mock_request.assert_has_calls([
         mock.call('POST',
                   'test_url/open',
                   auth=None,
                   data={
                       'method': 'open',
                       'filename': filename,
                       'flags': self.server.get_explicit_flags(flags),
                       'attrs': {
                           b'perm': perm
                       }
                   }),
         mock.ANY,  # close
     ])
     os.unlink(filename)
    def test_rename(self):
        self.server.input_queue = sftpcmd(
            SSH2_FXP_OPEN,
            sftpstring(b'services'),
            sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE),
            sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
            sftpint(0o644)
        )
        self.server.process()
        handle = get_sftphandle(self.server.output_queue)

        # reset output queue
        self.server.output_queue = b''
        self.server.input_queue = sftpcmd(
            SSH2_FXP_CLOSE,
            sftpstring(handle),
        )
        self.server.process()

        self.server.input_queue = sftpcmd(
            SSH2_FXP_RENAME,
            sftpstring(b'services'),
            sftpstring(b'other_services'),
        )
        self.server.process()
        self.assertIn('other_services', os.listdir('.'))
 def test_rm(self, mock_request):
     filename = b'services'
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(filename),
         sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
         sftpint(0o644)
     )
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(
         SSH2_FXP_REMOVE,
         sftpstring(filename),
         sftpint(0)
     )
     self.server.process()
     mock_request.assert_has_calls([
         mock.ANY,  # open
         mock.ANY,  # close
         mock.call(
             'POST', 'test_url/rm', auth=None,
             data={'method': 'rm', 'filename': filename}),
     ])
 def test_rename(self, mock_request):
     oldpath = b'services'
     newpath = b'other_services'
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(oldpath),
         sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
         sftpint(0o644)
     )
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(
         SSH2_FXP_RENAME,
         sftpstring(oldpath),
         sftpstring(newpath),
     )
     self.server.process()
     mock_request.assert_has_calls([
         mock.ANY,  # open
         mock.ANY,  # close
         mock.call(
             'POST', 'test_url/rename', auth=None,
             data={
                 'method': 'rename', 'oldpath': oldpath,
                 'newpath': newpath}),
     ])
     os.unlink(newpath)
    def test_remove(self):
        self.server.input_queue = sftpcmd(
            SSH2_FXP_OPEN,
            sftpstring(b'services'),
            sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE),
            sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
            sftpint(0o644)
        )
        self.server.process()
        handle = get_sftphandle(self.server.output_queue)

        # reset output queue
        self.server.output_queue = b''
        self.server.input_queue = sftpcmd(
            SSH2_FXP_CLOSE,
            sftpstring(handle)
        )
        self.server.process()

        self.server.input_queue = sftpcmd(
            SSH2_FXP_REMOVE,
            sftpstring(b'services'),
            sftpint(0)
        )
        self.server.process()
 def test_realpath(self, mock_request):
     """Additionally tests multiple urls and no path."""
     self.server.hook = UrlRequestHook(
         'test_url',
         urls_mapping={
             'realpath': ['test_url_1', 'test_url_2']},
         paths_mapping={
             'realpath': ''})
     filename = b'services'
     flags = SSH2_FXF_CREAT | SSH2_FXF_WRITE
     perm = 0o100600
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(filename),
         sftpint(flags),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
         sftpint(perm),
     )
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_REALPATH,
                                       sftpstring(filename))
     self.server.process()
     mock_request.assert_has_calls([
         mock.ANY,  # open
         mock.call(
             'POST', 'test_url_1/', auth=None,
             data={'method': 'realpath', 'filename': filename}),
         mock.call(
             'POST', 'test_url_2/', auth=None,
             data={'method': 'realpath', 'filename': filename}),
     ])
     os.unlink(filename)
Beispiel #10
0
 def test_rm(self, mock_request):
     filename = b'services'
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN, sftpstring(filename),
         sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS), sftpint(0o644))
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_REMOVE,
                                       sftpstring(filename), sftpint(0))
     self.server.process()
     mock_request.assert_has_calls([
         mock.ANY,  # open
         mock.ANY,  # close
         mock.call('POST',
                   'test_url/rm',
                   auth=None,
                   data={
                       'method': 'rm',
                       'filename': filename
                   }),
     ])
Beispiel #11
0
 def test_rename(self, mock_request):
     oldpath = b'services'
     newpath = b'other_services'
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN, sftpstring(oldpath),
         sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS), sftpint(0o644))
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(
         SSH2_FXP_RENAME,
         sftpstring(oldpath),
         sftpstring(newpath),
     )
     self.server.process()
     mock_request.assert_has_calls([
         mock.ANY,  # open
         mock.ANY,  # close
         mock.call('POST',
                   'test_url/rename',
                   auth=None,
                   data={
                       'method': 'rename',
                       'oldpath': oldpath,
                       'newpath': newpath
                   }),
     ])
     os.unlink(newpath)
    def test_fstat(self):
        self.server.input_queue = sftpcmd(
            SSH2_FXP_OPEN,
            sftpstring(b'services'),
            sftpint(SSH2_FXF_CREAT),
            sftpint(0)
        )
        self.server.process()
        handle = get_sftphandle(self.server.output_queue)

        self.server.output_queue = b''
        self.server.input_queue = sftpcmd(
            SSH2_FXP_FSTAT,
            sftpstring(handle)
        )
        self.server.process()
        stat = get_sftpstat(self.server.output_queue)
        self.assertEqual(stat['size'], 0)
        self.assertEqual(stat['uid'], os.getuid())

        self.server.output_queue = b''
        self.server.input_queue = sftpcmd(
            SSH2_FXP_CLOSE,
            sftpstring(handle)
        )
        self.server.process()

        os.unlink('services')
Beispiel #13
0
 def test_write(self):
     filename = b'services'
     write_offset = 5
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(filename),
         sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE | SSH2_FXF_READ),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
         sftpint(0o644),
     )
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     chunk = open('/etc/services', 'rb').read()
     self.server.input_queue = sftpcmd(
         SSH2_FXP_WRITE,
         sftpstring(handle),
         sftpint64(write_offset),
         sftpstring(chunk),
     )
     self.server.process()
     self.assertEqual(self.hook.get_result('write', 'filename'), filename)
     self.assertEqual(self.hook.get_result('write', 'offset'), write_offset)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     os.unlink(filename)
Beispiel #14
0
 def test_rename(self):
     oldpath = b'services'
     newpath = b'other_services'
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(oldpath),
         sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
         sftpint(0o644)
     )
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(
         SSH2_FXP_RENAME,
         sftpstring(oldpath),
         sftpstring(newpath),
     )
     self.server.process()
     self.assertEqual(self.hook.get_result('rename', 'oldpath'), oldpath)
     self.assertEqual(self.hook.get_result('rename', 'newpath'), newpath)
     os.unlink(newpath)
 def test_open(self, mock_request):
     filename = b'services'
     flags = SSH2_FXF_CREAT | SSH2_FXF_WRITE
     perm = 0o100600
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(filename),
         sftpint(flags),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
         sftpint(perm),
     )
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     mock_request.assert_has_calls([
         mock.call(
             'POST', 'test_url/open', auth=None,
             data={
                 'method': 'open', 'filename': filename,
                 'flags': self.server.get_explicit_flags(flags),
                 'attrs': {b'perm': perm}}),
         mock.ANY,  # close
     ])
     os.unlink(filename)
    def test_open_already_existing(self):
        self.server.input_queue = sftpcmd(
            SSH2_FXP_OPEN,
            sftpstring(b'services'),
            sftpint(SSH2_FXF_CREAT),
            sftpint(0)
        )
        self.server.process()
        handle = get_sftphandle(self.server.output_queue)

        # reset output queue
        self.server.output_queue = b''
        self.server.input_queue = sftpcmd(
            SSH2_FXP_CLOSE,
            sftpstring(handle)
        )
        self.server.process()

        self.server.input_queue = sftpcmd(
            SSH2_FXP_OPEN,
            sftpstring(b'services'),
            sftpint(SSH2_FXF_CREAT | SSH2_FXF_EXCL),
            sftpint(0)
        )
        self.assertRaises(SFTPException, self.server.process)

        os.unlink('services')
Beispiel #17
0
    def test_mkdir_forbidden(self):
        self.server.input_queue = sftpcmd(SSH2_FXP_MKDIR,
                                          sftpstring(b'../foo'), sftpint(0))
        self.assertRaises(SFTPForbidden, self.server.process)

        self.server.input_queue = sftpcmd(SSH2_FXP_MKDIR, sftpstring(b'/foo'),
                                          sftpint(0))
        self.assertRaises(SFTPForbidden, self.server.process)
Beispiel #18
0
 def test_init(self, mock_request):
     self.server.input_queue = sftpcmd(SSH2_FXP_INIT, sftpint(2),
                                       sftpint(0))
     self.server.process()
     mock_request.assert_called_once_with('POST',
                                          'test_url/init',
                                          auth=None,
                                          data={'method': 'init'})
    def test_mkdir_forbidden(self):
        self.server.input_queue = sftpcmd(
            SSH2_FXP_MKDIR, sftpstring(b'../foo'), sftpint(0))
        self.assertRaises(SFTPForbidden, self.server.process)

        self.server.input_queue = sftpcmd(
            SSH2_FXP_MKDIR, sftpstring(b'/foo'), sftpint(0))
        self.assertRaises(SFTPForbidden, self.server.process)
Beispiel #20
0
    def test_open_forbidden(self):
        self.server.input_queue = sftpcmd(SSH2_FXP_OPEN,
                                          sftpstring(b'/etc/services'),
                                          sftpint(SSH2_FXF_CREAT), sftpint(0))
        self.assertRaises(SFTPForbidden, self.server.process)

        self.server.input_queue = sftpcmd(SSH2_FXP_OPEN,
                                          sftpstring(b'../../foo'),
                                          sftpint(SSH2_FXF_CREAT), sftpint(0))
        self.assertRaises(SFTPForbidden, self.server.process)
    def test_open_forbidden(self):
        self.server.input_queue = sftpcmd(
            SSH2_FXP_OPEN, sftpstring(
                b'/etc/services'), sftpint(SSH2_FXF_CREAT), sftpint(0)
        )
        self.assertRaises(SFTPForbidden, self.server.process)

        self.server.input_queue = sftpcmd(
            SSH2_FXP_OPEN, sftpstring(
                b'../../foo'), sftpint(SSH2_FXF_CREAT), sftpint(0)
        )
        self.assertRaises(SFTPForbidden, self.server.process)
Beispiel #22
0
 def test_fstat(self):
     filename = b'services'
     self.server.input_queue = sftpcmd(SSH2_FXP_OPEN, sftpstring(filename),
                                       sftpint(SSH2_FXF_CREAT), sftpint(0))
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_FSTAT, sftpstring(handle))
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     self.assertEqual(self.hook.get_result('fstat'), filename)
     os.unlink(filename)
Beispiel #23
0
 def test_close(self):
     filename = b'services'
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(filename),
         sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE),
         sftpint(0),
     )
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     self.assertEqual(self.hook.get_result('close'), filename)
     os.unlink(filename)
Beispiel #24
0
    def test_mkdir(self):
        self.server.input_queue = sftpcmd(SSH2_FXP_MKDIR, sftpstring(b'foo'),
                                          sftpint(0))
        self.server.process()

        self.server.output_queue = b''
        self.server.input_queue = sftpcmd(SSH2_FXP_MKDIR, sftpstring(b'foo'),
                                          sftpint(0))
        self.assertRaises(SFTPException, self.server.process)

        self.server.output_queue = b''
        self.server.input_queue = sftpcmd(SSH2_FXP_RMDIR, sftpstring(b'foo'))
        self.server.process()

        self.assertRaises(OSError, os.rmdir, 'foo')
Beispiel #25
0
 def test_read(self, mock_request):
     filename = b'services'
     read_offset = 2
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(filename),
         sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE | SSH2_FXF_READ),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
         sftpint(0o644),
     )
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     chunk = open('/etc/services', 'rb').read()
     size = (os.lstat('/etc/services').st_size)
     self.server.input_queue = sftpcmd(
         SSH2_FXP_WRITE,
         sftpstring(handle),
         sftpint64(0),
         sftpstring(chunk),
     )
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(
         SSH2_FXP_READ,
         sftpstring(handle),
         sftpint64(read_offset),
         sftpint(size),
     )
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     mock_request.assert_has_calls([
         mock.ANY,  # open
         mock.ANY,  # write
         mock.call('POST',
                   'test_url/read',
                   auth=None,
                   data={
                       'method': 'read',
                       'filename': filename,
                       'offset': read_offset,
                       'size': size
                   }),
         mock.ANY,  # close
     ])
     os.unlink(filename)
Beispiel #26
0
 def test_rm(self):
     filename = b'services'
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN, sftpstring(filename),
         sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS), sftpint(0o644))
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_REMOVE,
                                       sftpstring(filename), sftpint(0))
     self.server.process()
     self.assertEqual(self.hook.get_result('rm'), filename)
 def test_readlink(self, mock_request):
     """Additionally tests multiple urls and multiple paths."""
     self.server.hook = UrlRequestHook(
         'test_url',
         urls_mapping={
             'readlink': ['test_url_1', 'test_url_2']},
         paths_mapping={
             'readlink': ['test_path_1', 'test_path_2']})
     linkpath = b'ugly'
     targetpath = b'ugliest'
     os.symlink(linkpath, targetpath)
     self.server.input_queue = sftpcmd(
         SSH2_FXP_READLINK, sftpstring(targetpath), sftpint(0))
     self.server.process()
     mock_request.assert_has_calls([
         mock.call(
             'POST', 'test_url_1/test_path_1', auth=None,
             data={'method': 'readlink', 'filename': targetpath}),
         mock.call(
             'POST', 'test_url_1/test_path_2', auth=None,
             data={'method': 'readlink', 'filename': targetpath}),
         mock.call(
             'POST', 'test_url_2/test_path_1', auth=None,
             data={'method': 'readlink', 'filename': targetpath}),
         mock.call(
             'POST', 'test_url_2/test_path_2', auth=None,
             data={'method': 'readlink', 'filename': targetpath}),
     ])
 def test_remove_forbidden(self):
     self.server.input_queue = sftpcmd(
         SSH2_FXP_REMOVE,
         sftpstring(b'/etc/services'),
         sftpint(0)
     )
     self.assertRaises(SFTPForbidden, self.server.process)
 def test_remove_notfound(self):
     self.server.input_queue = sftpcmd(
         SSH2_FXP_REMOVE,
         sftpstring(b'services'),
         sftpint(0)
     )
     self.assertRaises(SFTPNotFound, self.server.process)
Beispiel #30
0
    def test_remove(self):
        self.server.input_queue = sftpcmd(
            SSH2_FXP_OPEN, sftpstring(b'services'),
            sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE),
            sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS), sftpint(0o644))
        self.server.process()
        handle = get_sftphandle(self.server.output_queue)

        # reset output queue
        self.server.output_queue = b''
        self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
        self.server.process()

        self.server.input_queue = sftpcmd(SSH2_FXP_REMOVE,
                                          sftpstring(b'services'), sftpint(0))
        self.server.process()
Beispiel #31
0
    def test_copy_services(self):
        self.server.input_queue = sftpcmd(
            SSH2_FXP_OPEN, sftpstring(b'services'),
            sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE | SSH2_FXF_READ),
            sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS), sftpint(0o644))
        self.server.process()
        handle = get_sftphandle(self.server.output_queue)

        # reset output queue
        self.server.output_queue = b''
        etc_services = open('/etc/services', 'rb').read()
        etc_services_size = \
            os.lstat('/etc/services').st_size  # size of the whole file
        self.server.input_queue = sftpcmd(SSH2_FXP_WRITE, sftpstring(handle),
                                          sftpint64(0),
                                          sftpstring(etc_services))
        self.server.process()

        # reset output queue
        self.server.output_queue = b''
        self.server.input_queue = sftpcmd(SSH2_FXP_READ, sftpstring(handle),
                                          sftpint64(0),
                                          sftpint(etc_services_size))
        self.server.process()
        data = get_sftpdata(self.server.output_queue)

        self.server.output_queue = b''
        self.server.input_queue = sftpcmd(
            SSH2_FXP_READ,
            sftpstring(handle),
            sftpint64(etc_services_size),
            sftpint(1)  # wait for the EOF
        )
        # EOF status is raised as an exception
        self.assertRaises(SFTPException, self.server.process)

        # reset output queue
        self.server.output_queue = b''
        self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
        self.server.process()

        self.assertEqual(etc_services, open('services', 'rb').read())
        self.assertEqual(etc_services, data)
        self.assertEqual(0o644, stat_lib.S_IMODE(os.lstat('services').st_mode))
        self.assertEqual(etc_services_size, os.lstat('services').st_size)

        os.unlink('services')
    def test_mkdir(self):
        self.server.input_queue = sftpcmd(
            SSH2_FXP_MKDIR, sftpstring(b'foo'), sftpint(0))
        self.server.process()

        self.server.output_queue = b''
        self.server.input_queue = sftpcmd(
            SSH2_FXP_MKDIR, sftpstring(b'foo'), sftpint(0))
        self.assertRaises(SFTPException, self.server.process)

        self.server.output_queue = b''
        self.server.input_queue = sftpcmd(
            SSH2_FXP_RMDIR, sftpstring(b'foo')
        )
        self.server.process()

        self.assertRaises(OSError, os.rmdir, 'foo')
Beispiel #33
0
    def test_readlink(self):
        os.symlink("infound", "foo")

        self.server.input_queue = sftpcmd(SSH2_FXP_READLINK,
                                          sftpstring(b'foo'), sftpint(0))
        self.server.process()
        link = get_sftpname(self.server.output_queue)
        self.assertEqual(link, b"infound")
    def test_readlink(self):
        os.symlink("infound", "foo")

        self.server.input_queue = sftpcmd(
            SSH2_FXP_READLINK, sftpstring(b'foo'), sftpint(0))
        self.server.process()
        link = get_sftpname(self.server.output_queue)
        self.assertEqual(link, b"infound")
Beispiel #35
0
 def test_readlink(self):
     linkpath = b'ugly'
     targetpath = b'ugliest'
     os.symlink(linkpath, targetpath)
     self.server.input_queue = sftpcmd(
         SSH2_FXP_READLINK, sftpstring(targetpath), sftpint(0))
     self.server.process()
     self.assertEqual(self.hook.get_result('readlink'), targetpath)
Beispiel #36
0
 def test_readlink(self):
     linkpath = b'ugly'
     targetpath = b'ugliest'
     os.symlink(linkpath, targetpath)
     self.server.input_queue = sftpcmd(SSH2_FXP_READLINK,
                                       sftpstring(targetpath), sftpint(0))
     self.server.process()
     self.assertEqual(self.hook.get_result('readlink'), targetpath)
Beispiel #37
0
 def test_realpath(self):
     filename = b'services'
     flags = SSH2_FXF_CREAT | SSH2_FXF_WRITE
     perm = 0o100600
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(filename),
         sftpint(flags),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
         sftpint(perm),
     )
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_REALPATH,
                                       sftpstring(filename))
     self.server.process()
     self.assertEqual(self.hook.get_result('realpath'), filename)
     os.unlink(filename)
Beispiel #38
0
 def test_realpath(self):
     filename = b'services'
     flags = SSH2_FXF_CREAT | SSH2_FXF_WRITE
     perm = 0o100600
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(filename),
         sftpint(flags),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
         sftpint(perm),
     )
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_REALPATH,
                                       sftpstring(filename))
     self.server.process()
     self.assertEqual(self.hook.get_result('realpath'), filename)
     os.unlink(filename)
Beispiel #39
0
    def test_open_already_existing(self):
        self.server.input_queue = sftpcmd(SSH2_FXP_OPEN,
                                          sftpstring(b'services'),
                                          sftpint(SSH2_FXF_CREAT), sftpint(0))
        self.server.process()
        handle = get_sftphandle(self.server.output_queue)

        # reset output queue
        self.server.output_queue = b''
        self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
        self.server.process()

        self.server.input_queue = sftpcmd(
            SSH2_FXP_OPEN, sftpstring(b'services'),
            sftpint(SSH2_FXF_CREAT | SSH2_FXF_EXCL), sftpint(0))
        self.assertRaises(SFTPException, self.server.process)

        os.unlink('services')
Beispiel #40
0
 def test_rmdir(self):
     dirname = b'foo'
     # sftpint(0) means no attrs
     self.server.input_queue = sftpcmd(
         SSH2_FXP_MKDIR, sftpstring(dirname), sftpint(0))
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_RMDIR, sftpstring(dirname))
     self.server.process()
     self.assertEqual(self.hook.get_result('rmdir'), dirname)
Beispiel #41
0
 def test_symlink(self):
     linkpath = b'ugly'
     targetpath = b'ugliest'
     self.server.input_queue = sftpcmd(
         SSH2_FXP_SYMLINK, sftpstring(linkpath), sftpstring(targetpath),
         sftpint(0))
     self.server.process()
     self.assertEqual(self.hook.get_result('symlink', 'linkpath'), linkpath)
     self.assertEqual(self.hook.get_result('symlink', 'targetpath'),
                      targetpath)
Beispiel #42
0
 def test_symlink(self):
     linkpath = b'ugly'
     targetpath = b'ugliest'
     self.server.input_queue = sftpcmd(SSH2_FXP_SYMLINK,
                                       sftpstring(linkpath),
                                       sftpstring(targetpath), sftpint(0))
     self.server.process()
     self.assertEqual(self.hook.get_result('symlink', 'linkpath'), linkpath)
     self.assertEqual(self.hook.get_result('symlink', 'targetpath'),
                      targetpath)
Beispiel #43
0
 def test_rmdir(self):
     dirname = b'foo'
     # sftpint(0) means no attrs
     self.server.input_queue = sftpcmd(SSH2_FXP_MKDIR, sftpstring(dirname),
                                       sftpint(0))
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_RMDIR, sftpstring(dirname))
     self.server.process()
     self.assertEqual(self.hook.get_result('rmdir'), dirname)
Beispiel #44
0
    def test_fstat(self):
        self.server.input_queue = sftpcmd(SSH2_FXP_OPEN,
                                          sftpstring(b'services'),
                                          sftpint(SSH2_FXF_CREAT), sftpint(0))
        self.server.process()
        handle = get_sftphandle(self.server.output_queue)

        self.server.output_queue = b''
        self.server.input_queue = sftpcmd(SSH2_FXP_FSTAT, sftpstring(handle))
        self.server.process()
        stat = get_sftpstat(self.server.output_queue)
        self.assertEqual(stat['size'], 0)
        self.assertEqual(stat['uid'], os.getuid())

        self.server.output_queue = b''
        self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
        self.server.process()

        os.unlink('services')
Beispiel #45
0
 def test_mkdir(self):
     dirname = b'foo'
     # sftpint(0) means no attrs
     self.server.input_queue = sftpcmd(
         SSH2_FXP_MKDIR, sftpstring(dirname), sftpint(0))
     self.server.process()
     self.server.output_queue = b''
     self.assertEqual(self.hook.get_result('mkdir', 'filename'), dirname)
     self.assertEqual(pickle.loads(self.hook.get_result('mkdir', 'attrs')),
                      dict())
     os.rmdir(dirname)
 def test_close(self, mock_request):
     filename = b'services'
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(filename),
         sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE),
         sftpint(0),
     )
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     mock_request.assert_has_calls([
         mock.ANY,  # open
         mock.call(
             'POST', 'test_url/close', auth=None,
             data={'method': 'close', 'filename': filename}),
     ])
     os.unlink(filename)
Beispiel #47
0
 def test_mkdir(self):
     dirname = b'foo'
     # sftpint(0) means no attrs
     self.server.input_queue = sftpcmd(SSH2_FXP_MKDIR, sftpstring(dirname),
                                       sftpint(0))
     self.server.process()
     self.server.output_queue = b''
     self.assertEqual(self.hook.get_result('mkdir', 'filename'), dirname)
     self.assertEqual(pickle.loads(self.hook.get_result('mkdir', 'attrs')),
                      dict())
     os.rmdir(dirname)
    def test_symlink(self):
        self.server.input_queue = sftpcmd(
            SSH2_FXP_SYMLINK, sftpstring(b'bad/ugly'),
            sftpstring(b'bad/ugliest'), sftpint(0))
        self.assertRaises(SFTPNotFound, self.server.process)

        self.server.input_queue = sftpcmd(
            SSH2_FXP_SYMLINK, sftpstring(b'/bad/ugly'),
            sftpstring(b'bad/ugliest'), sftpint(0))
        self.assertRaises(SFTPForbidden, self.server.process)

        self.server.input_queue = sftpcmd(
            SSH2_FXP_SYMLINK, sftpstring(b'bad/ugly'),
            sftpstring(b'/bad/ugliest'), sftpint(0))
        self.assertRaises(SFTPForbidden, self.server.process)

        self.server.input_queue = sftpcmd(
            SSH2_FXP_SYMLINK, sftpstring(b'ugly'),
            sftpstring(b'ugliest'), sftpint(0))
        self.server.process()
        self.assertIn('ugly', os.listdir('.'))
Beispiel #49
0
 def test_open(self):
     filename = b'services'
     flags = SSH2_FXF_CREAT | SSH2_FXF_WRITE
     perm = 0o100600
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(filename),
         sftpint(flags),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
         sftpint(perm),
     )
     self.server.process()
     self.assertEqual(self.hook.get_result('open', 'filename'), filename)
     self.assertEqual(self.hook.get_result('open', 'flags'), flags)
     self.assertEqual(pickle.loads(self.hook.get_result('open', 'attrs')),
                      {b'perm': perm})
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     os.unlink(filename)
Beispiel #50
0
 def test_open(self):
     filename = b'services'
     flags = SSH2_FXF_CREAT | SSH2_FXF_WRITE
     perm = 0o100600
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(filename),
         sftpint(flags),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
         sftpint(perm),
     )
     self.server.process()
     self.assertEqual(self.hook.get_result('open', 'filename'), filename)
     self.assertEqual(self.hook.get_result('open', 'flags'), flags)
     self.assertEqual(pickle.loads(self.hook.get_result('open', 'attrs')),
                      {b'perm': perm})
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     os.unlink(filename)
Beispiel #51
0
 def test_realpath(self, mock_request):
     """Additionally tests multiple urls and no path."""
     self.server.hook = UrlRequestHook(
         'test_url',
         urls_mapping={'realpath': ['test_url_1', 'test_url_2']},
         paths_mapping={'realpath': ''})
     filename = b'services'
     flags = SSH2_FXF_CREAT | SSH2_FXF_WRITE
     perm = 0o100600
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(filename),
         sftpint(flags),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
         sftpint(perm),
     )
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_REALPATH,
                                       sftpstring(filename))
     self.server.process()
     mock_request.assert_has_calls([
         mock.ANY,  # open
         mock.call('POST',
                   'test_url_1/',
                   auth=None,
                   data={
                       'method': 'realpath',
                       'filename': filename
                   }),
         mock.call('POST',
                   'test_url_2/',
                   auth=None,
                   data={
                       'method': 'realpath',
                       'filename': filename
                   }),
     ])
     os.unlink(filename)
Beispiel #52
0
 def test_rename(self):
     oldpath = b'services'
     newpath = b'other_services'
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN, sftpstring(oldpath),
         sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS), sftpint(0o644))
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(
         SSH2_FXP_RENAME,
         sftpstring(oldpath),
         sftpstring(newpath),
     )
     self.server.process()
     self.assertEqual(self.hook.get_result('rename', 'oldpath'), oldpath)
     self.assertEqual(self.hook.get_result('rename', 'newpath'), newpath)
     os.unlink(newpath)
Beispiel #53
0
 def test_rm(self):
     filename = b'services'
     self.server.input_queue = sftpcmd(
         SSH2_FXP_OPEN,
         sftpstring(filename),
         sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE),
         sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS),
         sftpint(0o644)
     )
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(
         SSH2_FXP_REMOVE,
         sftpstring(filename),
         sftpint(0)
     )
     self.server.process()
     self.assertEqual(self.hook.get_result('rm'), filename)
Beispiel #54
0
 def test_fstat(self, mock_request):
     filename = b'services'
     self.server.input_queue = sftpcmd(SSH2_FXP_OPEN, sftpstring(filename),
                                       sftpint(SSH2_FXF_CREAT), sftpint(0))
     self.server.process()
     handle = get_sftphandle(self.server.output_queue)
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_FSTAT, sftpstring(handle))
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle))
     self.server.process()
     mock_request.assert_has_calls([
         mock.ANY,  # open
         mock.call('POST',
                   'test_url/fstat',
                   auth=None,
                   data={
                       'method': 'fstat',
                       'filename': filename
                   }),
     ])
     os.unlink(filename)
Beispiel #55
0
    def test_rename(self):
        self.server.input_queue = sftpcmd(
            SSH2_FXP_OPEN, sftpstring(b'services'),
            sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE),
            sftpint(SSH2_FILEXFER_ATTR_PERMISSIONS), sftpint(0o644))
        self.server.process()
        handle = get_sftphandle(self.server.output_queue)

        # reset output queue
        self.server.output_queue = b''
        self.server.input_queue = sftpcmd(
            SSH2_FXP_CLOSE,
            sftpstring(handle),
        )
        self.server.process()

        self.server.input_queue = sftpcmd(
            SSH2_FXP_RENAME,
            sftpstring(b'services'),
            sftpstring(b'other_services'),
        )
        self.server.process()
        self.assertIn('other_services', os.listdir('.'))
 def test_symlink(self, mock_request):
     """Additionally tests GET method."""
     self.server.hook = UrlRequestHook('test_url', request_method='GET')
     linkpath = b'ugly'
     targetpath = b'ugliest'
     self.server.input_queue = sftpcmd(
         SSH2_FXP_SYMLINK, sftpstring(linkpath), sftpstring(targetpath),
         sftpint(0))
     self.server.process()
     mock_request.assert_called_once_with(
         'GET', 'test_url/symlink', auth=None,
         data={
             'method': 'symlink', 'linkpath': linkpath,
             'targetpath': targetpath})
Beispiel #57
0
    def test_symlink(self):
        self.server.input_queue = sftpcmd(SSH2_FXP_SYMLINK,
                                          sftpstring(b'bad/ugly'),
                                          sftpstring(b'bad/ugliest'),
                                          sftpint(0))
        self.assertRaises(SFTPNotFound, self.server.process)

        self.server.input_queue = sftpcmd(SSH2_FXP_SYMLINK,
                                          sftpstring(b'/bad/ugly'),
                                          sftpstring(b'bad/ugliest'),
                                          sftpint(0))
        self.assertRaises(SFTPForbidden, self.server.process)

        self.server.input_queue = sftpcmd(SSH2_FXP_SYMLINK,
                                          sftpstring(b'bad/ugly'),
                                          sftpstring(b'/bad/ugliest'),
                                          sftpint(0))
        self.assertRaises(SFTPForbidden, self.server.process)

        self.server.input_queue = sftpcmd(SSH2_FXP_SYMLINK,
                                          sftpstring(b'ugly'),
                                          sftpstring(b'ugliest'), sftpint(0))
        self.server.process()
        self.assertIn('ugly', os.listdir('.'))
Beispiel #58
0
 def test_symlink(self, mock_request):
     """Additionally tests GET method."""
     self.server.hook = UrlRequestHook('test_url', request_method='GET')
     linkpath = b'ugly'
     targetpath = b'ugliest'
     self.server.input_queue = sftpcmd(SSH2_FXP_SYMLINK,
                                       sftpstring(linkpath),
                                       sftpstring(targetpath), sftpint(0))
     self.server.process()
     mock_request.assert_called_once_with('GET',
                                          'test_url/symlink',
                                          auth=None,
                                          data={
                                              'method': 'symlink',
                                              'linkpath': linkpath,
                                              'targetpath': targetpath
                                          })
Beispiel #59
0
 def test_mkdir(self, mock_request):
     """Additionally tests no path."""
     self.server.hook = UrlRequestHook('test_url',
                                       paths_mapping={'mkdir': ''})
     dirname = b'foo'
     # sftpint(0) means no attrs
     self.server.input_queue = sftpcmd(SSH2_FXP_MKDIR, sftpstring(dirname),
                                       sftpint(0))
     self.server.process()
     mock_request.assert_called_once_with('POST',
                                          'test_url/',
                                          auth=None,
                                          data={
                                              'method': 'mkdir',
                                              'filename': dirname,
                                              'attrs': dict()
                                          })
     os.rmdir(dirname)
Beispiel #60
0
 def test_rmdir(self, mock_request):
     dirname = b'foo'
     # sftpint(0) means no attrs
     self.server.input_queue = sftpcmd(SSH2_FXP_MKDIR, sftpstring(dirname),
                                       sftpint(0))
     self.server.process()
     self.server.output_queue = b''
     self.server.input_queue = sftpcmd(SSH2_FXP_RMDIR, sftpstring(dirname))
     self.server.process()
     mock_request.assert_has_calls([
         mock.ANY,  # mkdir
         mock.call('POST',
                   'test_url/rmdir',
                   auth=None,
                   data={
                       'method': 'rmdir',
                       'filename': dirname
                   }),
     ])