summaryrefslogtreecommitdiffhomepage
path: root/tests/test_agent.py
blob: a00011bf10fe5c16f5dd1bfd3da11606ff07e218 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import unittest

from paramiko.message import Message
from paramiko.agent import (
    SSH2_AGENT_SIGN_RESPONSE,
    cSSH2_AGENTC_SIGN_REQUEST,
    SSH_AGENT_RSA_SHA2_256,
    SSH_AGENT_RSA_SHA2_512,
    AgentKey,
)
from paramiko.util import b


class ChaosAgent:
    def _send_message(self, msg):
        self._sent_message = msg
        sig = Message()
        sig.add_string(b("lol"))
        sig.rewind()
        return SSH2_AGENT_SIGN_RESPONSE, sig


class AgentTests(unittest.TestCase):
    def _sign_with_agent(self, kwargs, expectation):
        agent = ChaosAgent()
        key = AgentKey(agent, b("secret!!!"))
        result = key.sign_ssh_data(b("token"), **kwargs)
        assert result == b("lol")
        msg = agent._sent_message
        msg.rewind()
        assert msg.get_byte() == cSSH2_AGENTC_SIGN_REQUEST
        assert msg.get_string() == b("secret!!!")
        assert msg.get_string() == b("token")
        assert msg.get_int() == expectation

    def test_agent_signing_defaults_to_0_for_flags_field(self):
        # No algorithm kwarg at all
        self._sign_with_agent(kwargs=dict(), expectation=0)

    def test_agent_signing_is_2_for_SHA256(self):
        self._sign_with_agent(
            kwargs=dict(algorithm="rsa-sha2-256"),
            expectation=SSH_AGENT_RSA_SHA2_256,
        )

    def test_agent_signing_is_2_for_SHA512(self):
        self._sign_with_agent(
            kwargs=dict(algorithm="rsa-sha2-512"),
            expectation=SSH_AGENT_RSA_SHA2_512,
        )

    def test_agent_key_str_kinda_fixed(self):
        # Tests for a missed spot in Python 3 upgrades: AgentKey.__str__ was
        # returning bytes, as if under Python 2. When bug present, this
        # explodes with "__str__ returned non-string".
        key = AgentKey(ChaosAgent(), b("secret!!!"))
        assert str(key) == repr(key)