summaryrefslogtreecommitdiffstats
path: root/ipatests/test_ipapython/test_ssh.py
blob: 817ef057ecd822d841aac26d8f2a23e0a99c93d8 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Authors:
#   Jan Cholasta <jcholast@redhat.com>
#
# Copyright (C) 2011  Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
Test the `ipapython/ssh.py` module.
"""

import base64

import six
import pytest

from ipapython import ssh

if six.PY3:
    unicode = str

pytestmark = pytest.mark.tier0


def make_public_key_checker(pk, out):
    def check_public_key():
        try:
            parsed = ssh.SSHPublicKey(pk)
            assert parsed.openssh() == out
        except Exception as e:
            assert type(e) is out
    check_public_key.description = "Test SSH public key parsing (%s)" % repr(pk)
    return check_public_key

def test_public_key_parsing():
    b64 = 'AAAAB3NzaC1yc2EAAAADAQABAAABAQDGAX3xAeLeaJggwTqMjxNwa6XHBUAikXPGMzEpVrlLDCZtv00djsFTBi38PkgxBJVkgRWMrcBsr/35lq7P6w8KGIwA8GI48Z0qBS2NBMJ2u9WQ2hjLN6GdMlo77O0uJY3251p12pCVIS/bHRSq8kHO2No8g7KA9fGGcagPfQH+ee3t7HUkpbQkFTmbPPN++r3V8oVUk5LxbryB3UIIVzNmcSIn3JrXynlvui4MixvrtX6zx+O/bBo68o8/eZD26QrahVbA09fivrn/4h3TM019Eu/c2jOdckfU3cHUV/3Tno5d6JicibyaoDDK7S/yjdn5jhaz8MSEayQvFkZkiF0L'
    raw = base64.b64decode(b64)
    openssh = 'ssh-rsa %s' % b64

    pks = [
        (b'\xff',                   UnicodeDecodeError),
        (u'\xff',                   ValueError),

        (raw,                       openssh),
        (b'\0\0\0\x04none',         u'none AAAABG5vbmU='),
        (b'\0\0\0',                 ValueError),
        (b'\0\0\0\0',               ValueError),
        (b'\0\0\0\x01',             ValueError),
        (b'\0\0\0\x01\xff',         ValueError),

        (u'\0\0\0\x04none',         ValueError),
        (u'\0\0\0',                 ValueError),
        (u'\0\0\0\0',               ValueError),
        (u'\0\0\0\x01',             ValueError),
        (u'\0\0\0\x01\xff',         ValueError),

        (b64,                       openssh),
        (unicode(b64),              openssh),
        (b64.encode('ascii'),       openssh),
        (u'\n%s\n\n' % b64,         openssh),
        (u'AAAABG5vbmU=',           u'none AAAABG5vbmU='),
        (u'AAAAB',                  ValueError),

        (openssh,                   openssh),
        (unicode(openssh),          openssh),
        (openssh.encode('ascii'),   openssh),
        (u'none AAAABG5vbmU=',      u'none AAAABG5vbmU='),
        (u'\t \t ssh-rsa \t \t%s\t \tthis is a comment\t \t ' % b64,
                                    u'%s this is a comment' % openssh),
        (u'opt3,opt2="\tx ",opt1,opt2="\\"x " %s comment ' % openssh,
                                    u'opt1,opt2="\\"x ",opt3 %s comment' % openssh),
        (u'ssh-rsa\n%s' % b64,      ValueError),
        (u'ssh-rsa\t%s' % b64,      ValueError),
        (u'vanitas %s' % b64,       ValueError),
        (u'@opt %s' % openssh,      ValueError),
        (u'opt=val %s' % openssh,   ValueError),
        (u'opt, %s' % openssh,      ValueError),
    ]

    for pk in pks:
        yield make_public_key_checker(*pk)