# 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)
# 
# PUB_FILE = "%s.pub" % (PKEY_FILENAME,)


import os
from base64 import b64encode
from ssh2.knownhost import LIBSSH2_KNOWNHOST_TYPE_PLAIN, \
    LIBSSH2_KNOWNHOST_KEYENC_RAW, LIBSSH2_KNOWNHOST_KEY_SSHRSA, \
    LIBSSH2_KNOWNHOST_KEYENC_BASE64, LIBSSH2_KNOWNHOST_TYPE_SHA1, \
    LIBSSH2_KNOWNHOST_KEY_SSHDSS
from ssh2.session import LIBSSH2_HOSTKEY_HASH_SHA1, LIBSSH2_HOSTKEY_TYPE_RSA
from ssh2.exceptions import KnownHostDeleteError, KnownHostCheckError
from .base_test import SSH2TestCase, PUB_FILE
        try:
            kh = self.session.knownhost_init()
            self.assertEqual(kh.readfile(write_file), 1)
            keys = kh.get()
            self.assertEqual(len(keys), 1)
            entry = keys[0]
            self.assertEqual(entry.key, key)
        finally:
            try:
                os.unlink(write_file)
            except Exception:
                pass

    def test_readline_writeline(self):
        line = b"""10.0.0.1 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA8ZzI0FP2Q5DOAL0q/T8QPWmnz6sMxDOVQ34u4NUo4GZZ948RpeSIVA57hmLYWkq+Hx4idsCLyyvfkEzEieY4Zm5wDtjX5uInP695TrZHd2SmPJwwq3PYwdZfyCvkDyLC8VnKe1iKGdJ4sSiD83gIXLsvHzL6nZ4TiX+N4MXpxFHIh6yA6aSmYy5FIB8Gn5veU+XunmXxUQdwIhNRkQiK5laA3X3paDFlUAWFyeS40+B+IVAQnA655+7Nxza86Xe6PROmJvLefZlqR8W1EI73ea7AKSfZijqHzxbIJYCQIh3FfQXfiaC15xKnnslEZNTOkfCblawav5jL9sIuw3wO3w=="""
        self.assertEqual(self._auth(), 0)
        kh = self.session.knownhost_init()
        kh.readline(line)
        keys = kh.get()
        self.assertEqual(len(keys), 1)
        entry = keys[0]
        self.assertTrue(b64encode(entry.key) in line)
        written_line = kh.writeline(entry).strip()
        self.assertEqual(line, written_line)

    def test_check(self):
        self.assertEqual(self._auth(), 0)
        kh = self.session.knownhost_init()
        host_key, key_type = self.session.hostkey()
        key_type = LIBSSH2_KNOWNHOST_KEY_SSHRSA \

# 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)
# 
# PUB_FILE = "%s.pub" % (PKEY_FILENAME,)


# Path: tests/test_knownhost.py
import os
from base64 import b64encode
from ssh2.knownhost import LIBSSH2_KNOWNHOST_TYPE_PLAIN, \
    LIBSSH2_KNOWNHOST_KEYENC_RAW, LIBSSH2_KNOWNHOST_KEY_SSHRSA, \
    LIBSSH2_KNOWNHOST_KEYENC_BASE64, LIBSSH2_KNOWNHOST_TYPE_SHA1, \
    LIBSSH2_KNOWNHOST_KEY_SSHDSS
from ssh2.session import LIBSSH2_HOSTKEY_HASH_SHA1, LIBSSH2_HOSTKEY_TYPE_RSA
from ssh2.exceptions import KnownHostDeleteError, KnownHostCheckError
from .base_test import SSH2TestCase, PUB_FILE

        try:
            kh = self.session.knownhost_init()
            self.assertEqual(kh.readfile(write_file), 1)
            keys = kh.get()
            self.assertEqual(len(keys), 1)
            entry = keys[0]
            self.assertEqual(entry.key, key)
        finally:
            try:
                os.unlink(write_file)
            except Exception:
                pass

    def test_readline_writeline(self):
        line = b"""10.0.0.1 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA8ZzI0FP2Q5DOAL0q/T8QPWmnz6sMxDOVQ34u4NUo4GZZ948RpeSIVA57hmLYWkq+Hx4idsCLyyvfkEzEieY4Zm5wDtjX5uInP695TrZHd2SmPJwwq3PYwdZfyCvkDyLC8VnKe1iKGdJ4sSiD83gIXLsvHzL6nZ4TiX+N4MXpxFHIh6yA6aSmYy5FIB8Gn5veU+XunmXxUQdwIhNRkQiK5laA3X3paDFlUAWFyeS40+B+IVAQnA655+7Nxza86Xe6PROmJvLefZlqR8W1EI73ea7AKSfZijqHzxbIJYCQIh3FfQXfiaC15xKnnslEZNTOkfCblawav5jL9sIuw3wO3w=="""
        self.assertEqual(self._auth(), 0)
        kh = self.session.knownhost_init()
        kh.readline(line)
        keys = kh.get()
        self.assertEqual(len(keys), 1)
        entry = keys[0]
        self.assertTrue(b64encode(entry.key) in line)
        written_line = kh.writeline(entry).strip()
        self.assertEqual(line, written_line)

    def test_check(self):
        self.assertEqual(self._auth(), 0)
        kh = self.session.knownhost_init()
        host_key, key_type = self.session.hostkey()
        key_type = LIBSSH2_KNOWNHOST_KEY_SSHRSA \

                   if key_type == LIBSSH2_HOSTKEY_TYPE_RSA \