~cloud-init-dev/cloud-init/trunk

« back to all changes in this revision

Viewing changes to tests/unittests/test_sshutil.py

  • Committer: Scott Moser
  • Date: 2016-08-10 15:06:15 UTC
  • Revision ID: smoser@ubuntu.com-20160810150615-ma2fv107w3suy1ma
README: Mention move of revision control to git.

cloud-init development has moved its revision control to git.
It is available at 
  https://code.launchpad.net/cloud-init

Clone with 
  git clone https://git.launchpad.net/cloud-init
or
  git clone git+ssh://git.launchpad.net/cloud-init

For more information see
  https://git.launchpad.net/cloud-init/tree/HACKING.rst

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from mock import patch
2
 
 
3
 
from . import helpers as test_helpers
4
 
from cloudinit import ssh_util
5
 
 
6
 
 
7
 
VALID_CONTENT = {
8
 
    'dsa': (
9
 
        "AAAAB3NzaC1kc3MAAACBAIrjOQSlSea19bExXBMBKBvcLhBoVvNBjCppNzllipF"
10
 
        "W4jgIOMcNanULRrZGjkOKat6MWJNetSbV1E6IOFDQ16rQgsh/OvYU9XhzM8seLa"
11
 
        "A21VszZuhIV7/2DE3vxu7B54zVzueG1O1Deq6goQCRGWBUnqO2yluJiG4HzrnDa"
12
 
        "jzRAAAAFQDMPO96qXd4F5A+5b2f2MO7SpVomQAAAIBpC3K2zIbDLqBBs1fn7rsv"
13
 
        "KcJvwihdlVjG7UXsDB76P2GNqVG+IlYPpJZ8TO/B/fzTMtrdXp9pSm9OY1+BgN4"
14
 
        "REsZ2WNcvfgY33aWaEM+ieCcQigvxrNAF2FTVcbUIIxAn6SmHuQSWrLSfdHc8H7"
15
 
        "hsrgeUPPdzjBD/cv2ZmqwZ1AAAAIAplIsScrJut5wJMgyK1JG0Kbw9JYQpLe95P"
16
 
        "obB069g8+mYR8U0fysmTEdR44mMu0VNU5E5OhTYoTGfXrVrkR134LqFM2zpVVbE"
17
 
        "JNDnIqDHxTkc6LY2vu8Y2pQ3/bVnllZZOda2oD5HQ7ovygQa6CH+fbaZHbdDUX/"
18
 
        "5z7u2rVAlDw=="
19
 
    ),
20
 
    'ecdsa': (
21
 
        "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBITrGBB3cgJ"
22
 
        "J7fPxvtMW9H3oRisNpJ3OAslxZeyP7I0A9BPAW0RQIwHVtVnM7zrp4nI+JLZov/"
23
 
        "Ql7lc2leWL7CY="
24
 
    ),
25
 
    'rsa': (
26
 
        "AAAAB3NzaC1yc2EAAAABIwAAAQEA3I7VUf2l5gSn5uavROsc5HRDpZdQueUq5oz"
27
 
        "emNSj8T7enqKHOEaFoU2VoPgGEWC9RyzSQVeyD6s7APMcE82EtmW4skVEgEGSbD"
28
 
        "c1pvxzxtchBj78hJP6Cf5TCMFSXw+Fz5rF1dR23QDbN1mkHs7adr8GW4kSWqU7Q"
29
 
        "7NDwfIrJJtO7Hi42GyXtvEONHbiRPOe8stqUly7MvUoN+5kfjBM8Qqpfl2+FNhT"
30
 
        "YWpMfYdPUnE7u536WqzFmsaqJctz3gBxH9Ex7dFtrxR4qiqEr9Qtlu3xGn7Bw07"
31
 
        "/+i1D+ey3ONkZLN+LQ714cgj8fRS4Hj29SCmXp5Kt5/82cD/VN3NtHw=="
32
 
    ),
33
 
}
34
 
 
35
 
TEST_OPTIONS = (
36
 
    "no-port-forwarding,no-agent-forwarding,no-X11-forwarding,"
37
 
    'command="echo \'Please login as the user \"ubuntu\" rather than the'
38
 
    'user \"root\".\';echo;sleep 10"')
39
 
 
40
 
 
41
 
class TestAuthKeyLineParser(test_helpers.TestCase):
42
 
    def test_simple_parse(self):
43
 
        # test key line with common 3 fields (keytype, base64, comment)
44
 
        parser = ssh_util.AuthKeyLineParser()
45
 
        for ktype in ['rsa', 'ecdsa', 'dsa']:
46
 
            content = VALID_CONTENT[ktype]
47
 
            comment = 'user-%s@host' % ktype
48
 
            line = ' '.join((ktype, content, comment,))
49
 
            key = parser.parse(line)
50
 
 
51
 
            self.assertEqual(key.base64, content)
52
 
            self.assertFalse(key.options)
53
 
            self.assertEqual(key.comment, comment)
54
 
            self.assertEqual(key.keytype, ktype)
55
 
 
56
 
    def test_parse_no_comment(self):
57
 
        # test key line with key type and base64 only
58
 
        parser = ssh_util.AuthKeyLineParser()
59
 
        for ktype in ['rsa', 'ecdsa', 'dsa']:
60
 
            content = VALID_CONTENT[ktype]
61
 
            line = ' '.join((ktype, content,))
62
 
            key = parser.parse(line)
63
 
 
64
 
            self.assertEqual(key.base64, content)
65
 
            self.assertFalse(key.options)
66
 
            self.assertFalse(key.comment)
67
 
            self.assertEqual(key.keytype, ktype)
68
 
 
69
 
    def test_parse_with_keyoptions(self):
70
 
        # test key line with options in it
71
 
        parser = ssh_util.AuthKeyLineParser()
72
 
        options = TEST_OPTIONS
73
 
        for ktype in ['rsa', 'ecdsa', 'dsa']:
74
 
            content = VALID_CONTENT[ktype]
75
 
            comment = 'user-%s@host' % ktype
76
 
            line = ' '.join((options, ktype, content, comment,))
77
 
            key = parser.parse(line)
78
 
 
79
 
            self.assertEqual(key.base64, content)
80
 
            self.assertEqual(key.options, options)
81
 
            self.assertEqual(key.comment, comment)
82
 
            self.assertEqual(key.keytype, ktype)
83
 
 
84
 
    def test_parse_with_options_passed_in(self):
85
 
        # test key line with key type and base64 only
86
 
        parser = ssh_util.AuthKeyLineParser()
87
 
 
88
 
        baseline = ' '.join(("rsa", VALID_CONTENT['rsa'], "user@host"))
89
 
        myopts = "no-port-forwarding,no-agent-forwarding"
90
 
 
91
 
        key = parser.parse("allowedopt" + " " + baseline)
92
 
        self.assertEqual(key.options, "allowedopt")
93
 
 
94
 
        key = parser.parse("overridden_opt " + baseline, options=myopts)
95
 
        self.assertEqual(key.options, myopts)
96
 
 
97
 
    def test_parse_invalid_keytype(self):
98
 
        parser = ssh_util.AuthKeyLineParser()
99
 
        key = parser.parse(' '.join(["badkeytype", VALID_CONTENT['rsa']]))
100
 
 
101
 
        self.assertFalse(key.valid())
102
 
 
103
 
 
104
 
class TestParseSSHConfig(test_helpers.TestCase):
105
 
 
106
 
    def setUp(self):
107
 
        self.load_file_patch = patch('cloudinit.ssh_util.util.load_file')
108
 
        self.load_file = self.load_file_patch.start()
109
 
        self.isfile_patch = patch('cloudinit.ssh_util.os.path.isfile')
110
 
        self.isfile = self.isfile_patch.start()
111
 
        self.isfile.return_value = True
112
 
 
113
 
    def tearDown(self):
114
 
        self.load_file_patch.stop()
115
 
        self.isfile_patch.stop()
116
 
 
117
 
    def test_not_a_file(self):
118
 
        self.isfile.return_value = False
119
 
        self.load_file.side_effect = IOError
120
 
        ret = ssh_util.parse_ssh_config('not a real file')
121
 
        self.assertEqual([], ret)
122
 
 
123
 
    def test_empty_file(self):
124
 
        self.load_file.return_value = ''
125
 
        ret = ssh_util.parse_ssh_config('some real file')
126
 
        self.assertEqual([], ret)
127
 
 
128
 
    def test_comment_line(self):
129
 
        comment_line = '# This is a comment'
130
 
        self.load_file.return_value = comment_line
131
 
        ret = ssh_util.parse_ssh_config('some real file')
132
 
        self.assertEqual(1, len(ret))
133
 
        self.assertEqual(comment_line, ret[0].line)
134
 
 
135
 
    def test_blank_lines(self):
136
 
        lines = ['', '\t', ' ']
137
 
        self.load_file.return_value = '\n'.join(lines)
138
 
        ret = ssh_util.parse_ssh_config('some real file')
139
 
        self.assertEqual(len(lines), len(ret))
140
 
        for line in ret:
141
 
            self.assertEqual('', line.line)
142
 
 
143
 
    def test_lower_case_config(self):
144
 
        self.load_file.return_value = 'foo bar'
145
 
        ret = ssh_util.parse_ssh_config('some real file')
146
 
        self.assertEqual(1, len(ret))
147
 
        self.assertEqual('foo', ret[0].key)
148
 
        self.assertEqual('bar', ret[0].value)
149
 
 
150
 
    def test_upper_case_config(self):
151
 
        self.load_file.return_value = 'Foo Bar'
152
 
        ret = ssh_util.parse_ssh_config('some real file')
153
 
        self.assertEqual(1, len(ret))
154
 
        self.assertEqual('foo', ret[0].key)
155
 
        self.assertEqual('Bar', ret[0].value)
156
 
 
157
 
    def test_lower_case_with_equals(self):
158
 
        self.load_file.return_value = 'foo=bar'
159
 
        ret = ssh_util.parse_ssh_config('some real file')
160
 
        self.assertEqual(1, len(ret))
161
 
        self.assertEqual('foo', ret[0].key)
162
 
        self.assertEqual('bar', ret[0].value)
163
 
 
164
 
    def test_upper_case_with_equals(self):
165
 
        self.load_file.return_value = 'Foo=bar'
166
 
        ret = ssh_util.parse_ssh_config('some real file')
167
 
        self.assertEqual(1, len(ret))
168
 
        self.assertEqual('foo', ret[0].key)
169
 
        self.assertEqual('bar', ret[0].value)
170
 
 
171
 
# vi: ts=4 expandtab