def test_stat(self, mock_request): """Additionally tests multiple urls.""" self.server.hook = UrlRequestHook( 'test_url', urls_mapping={'stat': ['test_url_1', 'test_url_2']}) filename = b'services' with open('/etc/services') as f: with open(filename, 'a') as f_bis: f_bis.write(f.read()) self.server.output_queue = b'' self.server.input_queue = sftpcmd(SSH2_FXP_STAT, sftpstring(filename)) self.server.process() mock_request.assert_has_calls([ mock.call('POST', 'test_url_1/stat', auth=None, data={ 'method': 'stat', 'filename': filename }), mock.call('POST', 'test_url_2/stat', auth=None, data={ 'method': 'stat', 'filename': filename }), ]) os.unlink(filename)
def setUp(self): os.chdir(t_path()) self.home = 'home' if not os.path.isdir(self.home): os.mkdir(self.home) self.server = SFTPServer(SFTPServerStorage(self.home), hook=UrlRequestHook('test_url'), logfile=t_path('log'), raise_on_error=True)
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 })
def test_lstat(self, mock_request): """Additionally tests skipping mapping for different server action.""" self.server.hook = UrlRequestHook( 'test_url', urls_mapping={'open': ['test_url_1', 'test_url_2']}) linkname = b'link' os.symlink('foo', linkname) self.server.output_queue = b'' self.server.input_queue = sftpcmd(SSH2_FXP_LSTAT, sftpstring(linkname)) self.server.process() mock_request.assert_called_once_with('POST', 'test_url/lstat', auth=None, data={ 'method': 'lstat', 'filename': linkname }) os.unlink(linkname)
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)
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_opendir(self, mock_request): """Additionally tests single url and multiple paths.""" self.server.hook = UrlRequestHook( 'test_url', paths_mapping={ 'opendir': ['test_path_1', 'test_path_2', 'test_path_3'] }) dirname = b'foo' os.mkdir(dirname) self.server.input_queue = sftpcmd(SSH2_FXP_OPENDIR, sftpstring(dirname)) 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/test_path_1', auth=None, data={ 'method': 'opendir', 'filename': dirname }), mock.call('POST', 'test_url/test_path_2', auth=None, data={ 'method': 'opendir', 'filename': dirname }), mock.call('POST', 'test_url/test_path_3', auth=None, data={ 'method': 'opendir', 'filename': dirname }), mock.ANY, # close ]) rmtree(dirname)
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)