# Path: pysecure/adapters/sftpa.py
# class SftpFile(object):
#     def __init__(self, sftp_session, filepath, access_type_om='r', 
#                  create_mode=DEFAULT_CREATE_MODE):
# 
#         at_im = self.__at_om_to_im(access_type_om)
# 
#         self.__sftp_session_int = getattr(sftp_session, 
#                                           'session_id', 
#                                           sftp_session)
# 
#         self.__filepath = filepath
#         self.__access_type = at_im
#         self.__create_mode = create_mode
# 
#     def __repr__(self):
#         return ('<SFTP_FILE [%s] \"%s\">' % 
#                 (self.__access_type[0], self.__filepath))
# 
#     def __at_om_to_im(self, om):
#         """Convert an "outer" access mode to an "inner" access mode.
#         Returns a tuple of:
# 
#             (<system access mode>, <is append>, <is universal newlines>).
#         """
# 
#         original_om = om
#         
#         if om[0] == 'U':
#             om = om[1:]
#             is_um = True
#         else:
#             is_um = False
# 
#         if om == 'r':
#             return (original_om, O_RDONLY, False, is_um)
#         elif om == 'w':
#             return (original_om, O_WRONLY | O_CREAT | O_TRUNC, False, is_um)
#         elif om == 'a':
#             return (original_om, O_WRONLY | O_CREAT, False, is_um)
#         elif om == 'r+':
#             return (original_om, O_RDWR | O_CREAT, False, is_um)
#         elif om == 'w+':
#             return (original_om, O_RDWR | O_CREAT | O_TRUNC, False, is_um)
#         elif om == 'a+':
#             return (original_om, O_RDWR | O_CREAT, True, is_um)
#         else:
#             raise Exception("Outer access mode [%s] is invalid." % 
#                             (original_om))
# 
#     def __enter__(self):
#         return self.open()
#         
#     def open(self):
#         """This is the only way to open a file resource."""
# 
#         self.__sf = _sftp_open(self.__sftp_session_int, 
#                                self.__filepath, 
#                                self.access_type_int, 
#                                self.__create_mode)
# 
#         if self.access_type_is_append is True:
#             self.seek(self.filesize)
# 
#         return SftpFileObject(self)
# 
#     def __exit__(self, e_type, e_value, e_tb):
#         self.close()
# 
#     def close(self):
#         _sftp_close(self.__sf)
# 
#     def write(self, buffer_):
#         return _sftp_write(self.__sf, buffer_)
# 
#     def seek(self, position):
#         return _sftp_seek(self.__sf, position)
# 
#     def read(self, size):
#         """Read a length of bytes. Return empty on EOF."""
# 
#         return _sftp_read(self.__sf, size)
# 
#     def fstat(self):
#         return _sftp_fstat(self.__sf)
#         
#     def rewind(self):
#         return _sftp_rewind(self.__sf)
# 
#     @property
#     def sf(self):
#         return self.__sf
# 
#     @property
#     def position(self):
#         return _sftp_tell(self.__sf)
# 
#     @property
#     def filesize(self):
#         return self.fstat().size
# 
#     @property
#     def filepath(self):
#         return self.__filepath
# 
#     @property
#     def access_type_str(self):
#         return self.__access_type[0]
# 
#     @property
#     def access_type_int(self):
#         return self.__access_type[1]
# 
#     @property
#     def access_type_is_append(self):
#         return self.__access_type[2]
#     
#     @property
#     def access_type_has_universal_nl(self):
#         return self.__access_type[3]
# 
# Path: pysecure/test/test_base.py
# def connect_sftp_test(sftp_cb):
#     print("Connecting SFTP with key: %s" % (key_filepath))
#     auth_cb = get_key_auth_cb(key_filepath)
#     connect_sftp_with_cb(sftp_cb, user, host, auth_cb, verbosity=verbosity)


from unittest import TestCase
from pysecure.adapters.sftpa import SftpFile
from pysecure.test.test_base import connect_sftp_test


class BinaryReadTest(TestCase):
    def __sftp_cb(self, ssh, sftp):
#        print("Opening file.")
    
        with SftpFile(sftp, 'test_libgksu2.so.0', 'r') as sf:
            buffer_ = sf.read()

            with open('/tmp/sftp_dump', 'wb') as f:
                f.write(buffer_)

#            print("Read (%d) bytes." % (len(buffer_)))

    def test_binary_read(self):

# Path: pysecure/adapters/sftpa.py
# class SftpFile(object):
#     def __init__(self, sftp_session, filepath, access_type_om='r', 
#                  create_mode=DEFAULT_CREATE_MODE):
# 
#         at_im = self.__at_om_to_im(access_type_om)
# 
#         self.__sftp_session_int = getattr(sftp_session, 
#                                           'session_id', 
#                                           sftp_session)
# 
#         self.__filepath = filepath
#         self.__access_type = at_im
#         self.__create_mode = create_mode
# 
#     def __repr__(self):
#         return ('<SFTP_FILE [%s] \"%s\">' % 
#                 (self.__access_type[0], self.__filepath))
# 
#     def __at_om_to_im(self, om):
#         """Convert an "outer" access mode to an "inner" access mode.
#         Returns a tuple of:
# 
#             (<system access mode>, <is append>, <is universal newlines>).
#         """
# 
#         original_om = om
#         
#         if om[0] == 'U':
#             om = om[1:]
#             is_um = True
#         else:
#             is_um = False
# 
#         if om == 'r':
#             return (original_om, O_RDONLY, False, is_um)
#         elif om == 'w':
#             return (original_om, O_WRONLY | O_CREAT | O_TRUNC, False, is_um)
#         elif om == 'a':
#             return (original_om, O_WRONLY | O_CREAT, False, is_um)
#         elif om == 'r+':
#             return (original_om, O_RDWR | O_CREAT, False, is_um)
#         elif om == 'w+':
#             return (original_om, O_RDWR | O_CREAT | O_TRUNC, False, is_um)
#         elif om == 'a+':
#             return (original_om, O_RDWR | O_CREAT, True, is_um)
#         else:
#             raise Exception("Outer access mode [%s] is invalid." % 
#                             (original_om))
# 
#     def __enter__(self):
#         return self.open()
#         
#     def open(self):
#         """This is the only way to open a file resource."""
# 
#         self.__sf = _sftp_open(self.__sftp_session_int, 
#                                self.__filepath, 
#                                self.access_type_int, 
#                                self.__create_mode)
# 
#         if self.access_type_is_append is True:
#             self.seek(self.filesize)
# 
#         return SftpFileObject(self)
# 
#     def __exit__(self, e_type, e_value, e_tb):
#         self.close()
# 
#     def close(self):
#         _sftp_close(self.__sf)
# 
#     def write(self, buffer_):
#         return _sftp_write(self.__sf, buffer_)
# 
#     def seek(self, position):
#         return _sftp_seek(self.__sf, position)
# 
#     def read(self, size):
#         """Read a length of bytes. Return empty on EOF."""
# 
#         return _sftp_read(self.__sf, size)
# 
#     def fstat(self):
#         return _sftp_fstat(self.__sf)
#         
#     def rewind(self):
#         return _sftp_rewind(self.__sf)
# 
#     @property
#     def sf(self):
#         return self.__sf
# 
#     @property
#     def position(self):
#         return _sftp_tell(self.__sf)
# 
#     @property
#     def filesize(self):
#         return self.fstat().size
# 
#     @property
#     def filepath(self):
#         return self.__filepath
# 
#     @property
#     def access_type_str(self):
#         return self.__access_type[0]
# 
#     @property
#     def access_type_int(self):
#         return self.__access_type[1]
# 
#     @property
#     def access_type_is_append(self):
#         return self.__access_type[2]
#     
#     @property
#     def access_type_has_universal_nl(self):
#         return self.__access_type[3]
# 
# Path: pysecure/test/test_base.py
# def connect_sftp_test(sftp_cb):
#     print("Connecting SFTP with key: %s" % (key_filepath))
#     auth_cb = get_key_auth_cb(key_filepath)
#     connect_sftp_with_cb(sftp_cb, user, host, auth_cb, verbosity=verbosity)


# Path: pysecure/test/binary_read.py
from unittest import TestCase
from pysecure.adapters.sftpa import SftpFile
from pysecure.test.test_base import connect_sftp_test



class BinaryReadTest(TestCase):
    def __sftp_cb(self, ssh, sftp):
#        print("Opening file.")
    
        with SftpFile(sftp, 'test_libgksu2.so.0', 'r') as sf:
            buffer_ = sf.read()

            with open('/tmp/sftp_dump', 'wb') as f:
                f.write(buffer_)

#            print("Read (%d) bytes." % (len(buffer_)))

    def test_binary_read(self):

        connect_sftp_test(self.__sftp_cb)