# test_paramiko_vendor.py
#
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
# General Public License as public by the Free Software Foundation; version 2.0
# or (at your option) any later version. You can redistribute it and/or
# modify it under the terms of either of these two licenses.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# You should have received a copy of the licenses; if not, see
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
# License, Version 2.0.
#

"""Tests for paramiko_vendor."""

import socket
import threading

from dulwich.tests import TestCase

from io import StringIO
from unittest import skipIf

try:
    import paramiko
except ImportError:
    has_paramiko = False
else:
    has_paramiko = True
    from dulwich.contrib.paramiko_vendor import ParamikoSSHVendor

    class Server(paramiko.ServerInterface):
        """http://docs.paramiko.org/en/2.4/api/server.html"""
        def __init__(self, commands, *args, **kwargs):
            super(Server, self).__init__(*args, **kwargs)
            self.commands = commands

        def check_channel_exec_request(self, channel, command):
            self.commands.append(command)
            return True

        def check_auth_password(self, username, password):
            if username == USER and password == PASSWORD:
                return paramiko.AUTH_SUCCESSFUL
            return paramiko.AUTH_FAILED

        def check_auth_publickey(self, username, key):
            pubkey = paramiko.RSAKey.from_private_key(StringIO(CLIENT_KEY))
            if username == USER and key == pubkey:
                return paramiko.AUTH_SUCCESSFUL
            return paramiko.AUTH_FAILED

        def check_channel_request(self, kind, chanid):
            if kind == "session":
                return paramiko.OPEN_SUCCEEDED
            return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED

        def get_allowed_auths(self, username):
            return "password,publickey"


USER = 'testuser'
PASSWORD = 'test'
SERVER_KEY = """\
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAy/L1sSYAzxsMprtNXW4u/1jGXXkQmQ2xtmKVlR+RlIL3a1BH
bzTpPlZyjltAAwzIP8XR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@skipIf(not has_paramiko, "paramiko is not installed")
class ParamikoSSHVendorTests(TestCase):

    def setUp(self):
        import paramiko.transport

        # re-enable server functionality for tests
        if hasattr(paramiko.transport, "SERVER_DISABLED_BY_GENTOO"):
            paramiko.transport.SERVER_DISABLED_BY_GENTOO = False

        self.commands = []
        socket.setdefaulttimeout(10)
        self.addCleanup(socket.setdefaulttimeout, None)
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.bind(('127.0.0.1', 0))
        self.socket.listen(5)
        self.addCleanup(self.socket.close)
        self.port = self.socket.getsockname()[1]
        self.thread = threading.Thread(target=self._run)
        self.thread.start()

    def tearDown(self):
        self.thread.join()

    def _run(self):
        try:
            conn, addr = self.socket.accept()
        except socket.error:
            return False
        self.transport = paramiko.Transport(conn)
        self.addCleanup(self.transport.close)
        host_key = paramiko.RSAKey.from_private_key(StringIO(SERVER_KEY))
        self.transport.add_server_key(host_key)
        server = Server(self.commands)
        self.transport.start_server(server=server)

    def test_run_command_password(self):
        vendor = ParamikoSSHVendor(allow_agent=False, look_for_keys=False,)
        vendor.run_command(
            '127.0.0.1', 'test_run_command_password',
            username=USER, port=self.port, password=PASSWORD)

        self.assertIn(b'test_run_command_password', self.commands)

    def test_run_command_with_privkey(self):
        key = paramiko.RSAKey.from_private_key(StringIO(CLIENT_KEY))

        vendor = ParamikoSSHVendor(allow_agent=False, look_for_keys=False,)
        vendor.run_command(
            '127.0.0.1', 'test_run_command_with_privkey',
            username=USER, port=self.port, pkey=key)

        self.assertIn(b'test_run_command_with_privkey', self.commands)

    def test_run_command_data_transfer(self):
        vendor = ParamikoSSHVendor(allow_agent=False, look_for_keys=False,)
        con = vendor.run_command(
            '127.0.0.1', 'test_run_command_data_transfer',
            username=USER, port=self.port, password=PASSWORD)

        self.assertIn(b'test_run_command_data_transfer', self.commands)

        channel = self.transport.accept(5)
        channel.send(b'stdout\n')
        channel.send_stderr(b'stderr\n')
        channel.close()

        # Fixme: it's return false
        # self.assertTrue(con.can_read())

        self.assertEqual(b'stdout\n', con.read(4096))

        # Fixme: it's return empty string
        # self.assertEqual(b'stderr\n', con.read_stderr(4096))
