def test_fsetstat(self, mock_request): filename = b'services' attrs = { b'size': 10**2, b'perm': 0o100600, b'atime': 1415626110, b'mtime': 1415626120, } 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'' etc_services = open('/etc/services', 'rb').read() self.server.input_queue = sftpcmd( SSH2_FXP_WRITE, sftpstring(handle), sftpint64(0), sftpstring(etc_services) ) self.server.process() self.server.output_queue = b'' self.server.input_queue = sftpcmd( SSH2_FXP_FSETSTAT, sftpstring(handle), sftpint( SSH2_FILEXFER_ATTR_SIZE | SSH2_FILEXFER_ATTR_PERMISSIONS | SSH2_FILEXFER_ATTR_ACMODTIME ), sftpint64(attrs[b'size']), sftpint(attrs[b'perm']), sftpint(attrs[b'atime']), sftpint(attrs[b'mtime']), ) 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/fsetstat', auth=None, data={ 'method': 'fsetstat', 'filename': filename, 'attrs': attrs}), mock.ANY, # close ]) os.unlink(filename)
def test_fsetstat(self): filename = b'services' attrs = { b'size': 10**2, b'perm': 0o100600, b'atime': 1415626110, b'mtime': 1415626120, } 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'' etc_services = open('/etc/services', 'rb').read() self.server.input_queue = sftpcmd( SSH2_FXP_WRITE, sftpstring(handle), sftpint64(0), sftpstring(etc_services) ) self.server.process() self.server.output_queue = b'' self.server.input_queue = sftpcmd( SSH2_FXP_FSETSTAT, sftpstring(handle), sftpint( SSH2_FILEXFER_ATTR_SIZE | SSH2_FILEXFER_ATTR_PERMISSIONS | SSH2_FILEXFER_ATTR_ACMODTIME ), sftpint64(attrs[b'size']), sftpint(attrs[b'perm']), sftpint(attrs[b'atime']), sftpint(attrs[b'mtime']), ) 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('fsetstat', 'filename'), filename) self.assertEqual( pickle.loads(self.hook.get_result('fsetstat', 'attrs')), attrs) os.unlink(filename)
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)
def test_fsetstat(self, mock_request): filename = b'services' attrs = { b'size': 10**2, b'perm': 0o100600, b'atime': 1415626110, b'mtime': 1415626120, } 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'' etc_services = open('/etc/services', 'rb').read() self.server.input_queue = sftpcmd(SSH2_FXP_WRITE, sftpstring(handle), sftpint64(0), sftpstring(etc_services)) self.server.process() self.server.output_queue = b'' self.server.input_queue = sftpcmd( SSH2_FXP_FSETSTAT, sftpstring(handle), sftpint(SSH2_FILEXFER_ATTR_SIZE | SSH2_FILEXFER_ATTR_PERMISSIONS | SSH2_FILEXFER_ATTR_ACMODTIME), sftpint64(attrs[b'size']), sftpint(attrs[b'perm']), sftpint(attrs[b'atime']), sftpint(attrs[b'mtime']), ) 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/fsetstat', auth=None, data={ 'method': 'fsetstat', 'filename': filename, 'attrs': attrs }), mock.ANY, # close ]) os.unlink(filename)
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)
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)
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_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_fsetstat(self): atime = 1415626110 mtime = 1415626120 size = 10**2 self.server.input_queue = sftpcmd( SSH2_FXP_OPEN, sftpstring(b'services'), sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE), sftpint(0)) 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() self.server.input_queue = sftpcmd(SSH2_FXP_WRITE, sftpstring(handle), sftpint64(0), sftpstring(etc_services)) self.server.process() self.server.output_queue = b'' self.server.input_queue = sftpcmd( SSH2_FXP_FSETSTAT, sftpstring(handle), sftpint(SSH2_FILEXFER_ATTR_SIZE | SSH2_FILEXFER_ATTR_PERMISSIONS | SSH2_FILEXFER_ATTR_ACMODTIME), sftpint64(size), # 1000 bytes sftpint(33152), # 0o100600 sftpint(atime), sftpint(mtime)) self.server.process() self.server.output_queue = b'' self.server.input_queue = sftpcmd(SSH2_FXP_CLOSE, sftpstring(handle)) self.server.process() self.assertEqual(0o600, stat_lib.S_IMODE(os.lstat('services').st_mode)) self.assertEqual(atime, os.lstat('services').st_atime) self.assertEqual(mtime, os.lstat('services').st_mtime) self.assertEqual(size, os.lstat('services').st_size) os.unlink('services')
def test_fsetstat(self): filename = b'services' attrs = { b'size': 10**2, b'perm': 0o100600, b'atime': 1415626110, b'mtime': 1415626120, } 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'' etc_services = open('/etc/services', 'rb').read() self.server.input_queue = sftpcmd(SSH2_FXP_WRITE, sftpstring(handle), sftpint64(0), sftpstring(etc_services)) self.server.process() self.server.output_queue = b'' self.server.input_queue = sftpcmd( SSH2_FXP_FSETSTAT, sftpstring(handle), sftpint(SSH2_FILEXFER_ATTR_SIZE | SSH2_FILEXFER_ATTR_PERMISSIONS | SSH2_FILEXFER_ATTR_ACMODTIME), sftpint64(attrs[b'size']), sftpint(attrs[b'perm']), sftpint(attrs[b'atime']), sftpint(attrs[b'mtime']), ) 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('fsetstat', 'filename'), filename) self.assertEqual( pickle.loads(self.hook.get_result('fsetstat', 'attrs')), attrs) os.unlink(filename)
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_fsetstat(self): atime = 1415626110 mtime = 1415626120 size = 10**2 self.server.input_queue = sftpcmd( SSH2_FXP_OPEN, sftpstring(b'services'), sftpint(SSH2_FXF_CREAT | SSH2_FXF_WRITE), sftpint(0) ) 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() self.server.input_queue = sftpcmd( SSH2_FXP_WRITE, sftpstring(handle), sftpint64(0), sftpstring(etc_services) ) self.server.process() self.server.output_queue = b'' self.server.input_queue = sftpcmd( SSH2_FXP_FSETSTAT, sftpstring(handle), sftpint( SSH2_FILEXFER_ATTR_SIZE | SSH2_FILEXFER_ATTR_PERMISSIONS | SSH2_FILEXFER_ATTR_ACMODTIME ), sftpint64(size), # 1000 bytes sftpint(33152), # 0o100600 sftpint(atime), sftpint(mtime) ) self.server.process() self.server.output_queue = b'' self.server.input_queue = sftpcmd( SSH2_FXP_CLOSE, sftpstring(handle) ) self.server.process() self.assertEqual( 0o600, stat_lib.S_IMODE(os.lstat('services').st_mode) ) self.assertEqual( atime, os.lstat('services').st_atime ) self.assertEqual( mtime, os.lstat('services').st_mtime ) self.assertEqual( size, os.lstat('services').st_size ) os.unlink('services')