|
| 1 | +# This file is part of ssh-python. |
| 2 | +# Copyright (C) 2018 Panos Kittenis |
| 3 | +# |
| 4 | +# This library is free software; you can redistribute it and/or |
| 5 | +# modify it under the terms of the GNU Lesser General Public |
| 6 | +# License as published by the Free Software Foundation, version 2.1. |
| 7 | +# |
| 8 | +# This library is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | +# Lesser General Public License for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU Lesser General Public |
| 14 | +# License along with this library; if not, write to the Free Software |
| 15 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-130 |
| 16 | + |
| 17 | +import unittest |
| 18 | +import os |
| 19 | +import platform |
| 20 | + |
| 21 | +from ssh.sftp import SFTP |
| 22 | +from ssh.sftp_handles import SFTPDir, SFTPFile |
| 23 | +from ssh.sftp_attributes import SFTPAttributes |
| 24 | +from ssh.exceptions import InvalidAPIUse, SFTPHandleError |
| 25 | + |
| 26 | +from .base_test import SSHTestCase |
| 27 | + |
| 28 | + |
| 29 | +class SFTPTest(SSHTestCase): |
| 30 | + |
| 31 | + def test_sftp_init(self): |
| 32 | + self._auth() |
| 33 | + sftp = self.session.sftp_new() |
| 34 | + self.assertIsInstance(sftp, SFTP) |
| 35 | + self.assertEqual(sftp.init(), 0) |
| 36 | + |
| 37 | + def test_sftp_fail(self): |
| 38 | + self.assertRaises(InvalidAPIUse, self.session.sftp_new) |
| 39 | + self._auth() |
| 40 | + sftp = self.session.sftp_new() |
| 41 | + self.assertRaises(SFTPHandleError, sftp.opendir, '.') |
| 42 | + |
| 43 | + def test_sftp_dir(self): |
| 44 | + self._auth() |
| 45 | + sftp = self.session.sftp_new() |
| 46 | + sftp.init() |
| 47 | + _dir = sftp.opendir('.') |
| 48 | + self.assertIsInstance(_dir, SFTPDir) |
| 49 | + self.assertFalse(_dir.eof()) |
| 50 | + self.assertEqual(_dir.closedir(), 0) |
| 51 | + # dir handle from context manager |
| 52 | + with sftp.opendir('.') as _dir: |
| 53 | + for attr in _dir: |
| 54 | + self.assertIsInstance(attr, SFTPAttributes) |
| 55 | + self.assertIsNotNone(attr.name) |
| 56 | + self.assertTrue(_dir.eof()) |
| 57 | + |
| 58 | + def test_sftp_readdir(self): |
| 59 | + self._auth() |
| 60 | + sftp = self.session.sftp_new() |
| 61 | + sftp.init() |
| 62 | + with sftp.opendir('.') as _dir: |
| 63 | + attrs = _dir.readdir() |
| 64 | + self.assertIsInstance(attrs, SFTPAttributes) |
| 65 | + self.assertIsNotNone(attrs.uid) |
| 66 | + self.assertIsNotNone(attrs.gid) |
| 67 | + self.assertIsNotNone(attrs.owner) |
| 68 | + self.assertIsNotNone(attrs.group) |
| 69 | + self.assertTrue(attrs.size > 0) |
| 70 | + self.assertEqual(attrs.name, b'.') |
| 71 | + self.assertTrue(len(attrs.longname) > 1) |
| 72 | + |
| 73 | + def test_sftp_file(self): |
| 74 | + self._auth() |
| 75 | + sftp = self.session.sftp_new() |
| 76 | + sftp.init() |
| 77 | + if int(platform.python_version_tuple()[0]) >= 3: |
| 78 | + test_file_data = b'test' + bytes(os.linesep, 'utf-8') |
| 79 | + else: |
| 80 | + test_file_data = b'test' + os.linesep |
| 81 | + remote_filename = os.sep.join([os.path.dirname(__file__), |
| 82 | + 'remote_test_file']) |
| 83 | + with open(remote_filename, 'wb') as test_fh: |
| 84 | + test_fh.write(test_file_data) |
| 85 | + try: |
| 86 | + remote_fh = sftp.open(remote_filename, os.O_RDONLY, 0) |
| 87 | + self.assertIsInstance(remote_fh, SFTPFile) |
| 88 | + finally: |
| 89 | + os.unlink(remote_filename) |
| 90 | + # with sftp.open(remote_filename, 0, 0) as remote_fh: |
| 91 | + # try: |
| 92 | + # self.assertTrue(remote_fh is not None) |
| 93 | + # remote_data = b"" |
| 94 | + # for rc, data in remote_fh: |
| 95 | + # remote_data += data |
| 96 | + # self.assertEqual(remote_fh.close(), 0) |
| 97 | + # self.assertEqual(remote_data, test_file_data) |
| 98 | + # finally: |
| 99 | + # os.unlink(remote_filename) |
0 commit comments