# Path: tests/base_test.py
# class SSH2TestCase(unittest.TestCase):
# 
#     @classmethod
#     def setUpClass(cls):
#         _mask = int('0600') if version_info <= (2,) else 0o600
#         os.chmod(PKEY_FILENAME, _mask)
#         cls.server = OpenSSHServer()
#         cls.server.start_server()
# 
#     @classmethod
#     def tearDownClass(cls):
#         cls.server.stop()
#         del cls.server
# 
#     def setUp(self):
#         self.host = '127.0.0.1'
#         self.port = 2222
#         self.cmd = 'echo me'
#         self.resp = u'me'
#         self.user_key = PKEY_FILENAME
#         self.user_pub_key = PUB_FILE
#         self.user = pwd.getpwuid(os.geteuid()).pw_name
#         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#         sock.connect((self.host, self.port))
#         self.sock = sock
#         self.session = Session()
#         self.session.handshake(self.sock)
# 
#     def tearDown(self):
#         self.session.disconnect()
#         self.sock.close()
# 
#     def _auth(self):
#         return self.session.userauth_publickey_fromfile(
#             self.user, self.user_key)


import os
import socket
from unittest import skipUnless
from .base_test import SSH2TestCase
from ssh2.session import Session, LIBSSH2_HOSTKEY_HASH_MD5, \
    LIBSSH2_HOSTKEY_HASH_SHA1, LIBSSH2_METHOD_KEX, LIBSSH2_METHOD_CRYPT_CS
from ssh2.sftp import SFTP
from ssh2.channel import Channel
from ssh2.error_codes import LIBSSH2_ERROR_EAGAIN
from ssh2.exceptions import AuthenticationError, AgentAuthenticationError, \
    SCPProtocolError, RequestDeniedError, InvalidRequestError, \
    SocketSendError, FileError, PublickeyUnverifiedError, MethodNotSupported
from ssh2.utils import wait_socket
from ssh2.listener import Listener
            raise
        try:
            total = 0
            size, data = file_chan.read(size=fileinfo.st_size)
            total += size
            while total < fileinfo.st_size:
                total += size
                size, data = file_chan.read()
            self.assertEqual(total, fileinfo.st_size)
        except Exception:
            raise
        finally:
            os.unlink(remote_filename)
        self.assertRaises(SCPProtocolError, self.session.scp_recv, remote_filename)

    def test_scp_send(self):
        self.assertEqual(self._auth(), 0)
        test_data = b"data"
        remote_filename = os.sep.join([os.path.dirname(__file__),
                                       "remote_test_file"])
        to_copy = os.sep.join([os.path.dirname(__file__),
                               "copied"])
        with open(remote_filename, 'wb') as fh:
            fh.write(test_data)
        fileinfo = os.stat(remote_filename)
        try:
            chan = self.session.scp_send(
                to_copy, fileinfo.st_mode & 777, fileinfo.st_size)
            with open(remote_filename, 'rb') as local_fh:
                for data in local_fh:

# Path: tests/base_test.py
# class SSH2TestCase(unittest.TestCase):
# 
#     @classmethod
#     def setUpClass(cls):
#         _mask = int('0600') if version_info <= (2,) else 0o600
#         os.chmod(PKEY_FILENAME, _mask)
#         cls.server = OpenSSHServer()
#         cls.server.start_server()
# 
#     @classmethod
#     def tearDownClass(cls):
#         cls.server.stop()
#         del cls.server
# 
#     def setUp(self):
#         self.host = '127.0.0.1'
#         self.port = 2222
#         self.cmd = 'echo me'
#         self.resp = u'me'
#         self.user_key = PKEY_FILENAME
#         self.user_pub_key = PUB_FILE
#         self.user = pwd.getpwuid(os.geteuid()).pw_name
#         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#         sock.connect((self.host, self.port))
#         self.sock = sock
#         self.session = Session()
#         self.session.handshake(self.sock)
# 
#     def tearDown(self):
#         self.session.disconnect()
#         self.sock.close()
# 
#     def _auth(self):
#         return self.session.userauth_publickey_fromfile(
#             self.user, self.user_key)


# Path: tests/test_session.py
import os
import socket
from unittest import skipUnless
from .base_test import SSH2TestCase
from ssh2.session import Session, LIBSSH2_HOSTKEY_HASH_MD5, \
    LIBSSH2_HOSTKEY_HASH_SHA1, LIBSSH2_METHOD_KEX, LIBSSH2_METHOD_CRYPT_CS
from ssh2.sftp import SFTP
from ssh2.channel import Channel
from ssh2.error_codes import LIBSSH2_ERROR_EAGAIN
from ssh2.exceptions import AuthenticationError, AgentAuthenticationError, \
    SCPProtocolError, RequestDeniedError, InvalidRequestError, \
    SocketSendError, FileError, PublickeyUnverifiedError, MethodNotSupported
from ssh2.utils import wait_socket
from ssh2.listener import Listener

            raise
        try:
            total = 0
            size, data = file_chan.read(size=fileinfo.st_size)
            total += size
            while total < fileinfo.st_size:
                total += size
                size, data = file_chan.read()
            self.assertEqual(total, fileinfo.st_size)
        except Exception:
            raise
        finally:
            os.unlink(remote_filename)
        self.assertRaises(SCPProtocolError, self.session.scp_recv, remote_filename)

    def test_scp_send(self):
        self.assertEqual(self._auth(), 0)
        test_data = b"data"
        remote_filename = os.sep.join([os.path.dirname(__file__),
                                       "remote_test_file"])
        to_copy = os.sep.join([os.path.dirname(__file__),
                               "copied"])
        with open(remote_filename, 'wb') as fh:
            fh.write(test_data)
        fileinfo = os.stat(remote_filename)
        try:
            chan = self.session.scp_send(
                to_copy, fileinfo.st_mode & 777, fileinfo.st_size)
            with open(remote_filename, 'rb') as local_fh:
                for data in local_fh:

                    chan.write(data)