Debian Codegraph @

ansible_collections/community/hrobot/tests/unit/plugins/module_utils/test_ssh.py

raw
1 # Copyright (c) 2021 Felix Fontein <[email protected]>
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import (absolute_import, division, print_function)
6 __metaclass__ = type
7
8
9 import pytest
10
11 from ansible_collections.community.hrobot.plugins.module_utils.ssh import (
12 FingerprintError,
13 normalize_fingerprint,
14 extract_fingerprint,
15 )
16
17 from ..data import (
18 SSH_PUBLIC_KEY_1,
19 SSH_FINGERPRINT_1,
20 )
21
22
23 def test_normalize_fingerprint():
24 assert normalize_fingerprint(SSH_FINGERPRINT_1) == SSH_FINGERPRINT_1
25 assert normalize_fingerprint('F5:7e:4f:d8:ab:20:b8:5B:8b:2f:7a:4:47:fd:96:73') == (
26 'f5:7e:4f:d8:ab:20:b8:5b:8b:2f:7a:04:47:fd:96:73'
27 )
28 assert normalize_fingerprint('F57e4fd8ab20b85B8b2f7a0447fd9673') == (
29 'f5:7e:4f:d8:ab:20:b8:5b:8b:2f:7a:04:47:fd:96:73'
30 )
31 assert normalize_fingerprint('Fe:F', size=2) == 'fe:0f'
32
33 with pytest.raises(FingerprintError) as exc:
34 normalize_fingerprint('')
35 print(exc.value.args[0])
36 assert exc.value.args[0] == 'Fingerprint must consist of 16 8-bit hex numbers: got 0 8-bit hex numbers instead'
37 with pytest.raises(FingerprintError) as exc:
38 normalize_fingerprint('1:2:3')
39 print(exc.value.args[0])
40 assert exc.value.args[0] == 'Fingerprint must consist of 16 8-bit hex numbers: got 3 8-bit hex numbers instead'
41 with pytest.raises(FingerprintError) as exc:
42 normalize_fingerprint('01023')
43 print(exc.value.args[0])
44 assert exc.value.args[0] == 'Fingerprint must consist of 16 8-bit hex numbers: got 3 8-bit hex numbers instead'
45
46 with pytest.raises(FingerprintError) as exc:
47 normalize_fingerprint('A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P')
48 print(exc.value.args[0])
49 assert exc.value.args[0] == 'Fingerprint must consist of 16 8-bit hex numbers: number 7 is invalid: "G"'
50 with pytest.raises(FingerprintError) as exc:
51 normalize_fingerprint('fee:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P')
52 print(exc.value.args[0])
53 assert exc.value.args[0] == 'Fingerprint must consist of 16 8-bit hex numbers: number 1 is invalid: "fee"'
54
55
56 def test_extract_fingerprint():
57 assert extract_fingerprint(SSH_PUBLIC_KEY_1) == SSH_FINGERPRINT_1
58 assert extract_fingerprint(' %s foo@ bar ' % SSH_PUBLIC_KEY_1.replace(' ', ' ')) == SSH_FINGERPRINT_1
59
60 key = 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGGdztn98LzAZkwHzSNa2HpTERPzBZdrdMt9u++0qQ+U'
61 assert extract_fingerprint(key) == 'f5:7e:4f:d8:ab:20:b8:5b:8b:2f:7a:04:47:fd:96:73'
62 print(extract_fingerprint(key, alg='sha256', size=32))
63 assert extract_fingerprint(key, alg='sha256', size=32) == (
64 '64:94:70:47:7a:bd:79:99:95:9f:3b:d3:37:8c:2c:fa:33:a7:d1:93:95:56:1b:f7:f6:52:31:34:0b:4a:fc:67'
65 )
66
67 key = (
68 'ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDEVarUR'
69 'tu+DmCvn0OkHC+gCOQ6Bxkolfh9NvWr4f8SPfQJ/yOUO6RZ+m3RhvnDEWAvA1BG/lCNqui6/kuZiyVk='
70 )
71 assert extract_fingerprint(key) == 'f4:b7:43:14:fe:8b:43:4b:cc:b3:63:dc:cf:23:bb:cb'
72 print(extract_fingerprint(key, alg='sha256', size=32))
73 assert extract_fingerprint(key, alg='sha256', size=32) == (
74 '88:c2:a3:0f:2a:cf:60:73:7c:52:e0:41:40:25:c3:d4:5d:32:37:a9:46:48:3e:37:34:f1:aa:0d:4d:69:15:d7'
75 )
76
77 with pytest.raises(FingerprintError) as exc:
78 extract_fingerprint(' adsf ')
79 print(exc.value.args[0])
80 assert exc.value.args[0] == 'Error while extracting fingerprint from public key data: cannot split public key into at least two parts'
81
82 with pytest.raises(FingerprintError) as exc:
83 extract_fingerprint('a b')
84 print(exc.value.args[0])
85 assert exc.value.args[0] in (
86 'Error while extracting fingerprint from public key data: Invalid base64-encoded string:'
87 ' number of data characters (1) cannot be 1 more than a multiple of 4',
88 'Error while extracting fingerprint from public key data: Incorrect padding',
89 )
90 with pytest.raises(FingerprintError) as exc:
91 extract_fingerprint('a ab=f')
92 print(exc.value.args[0])
93 assert exc.value.args[0] == 'Error while extracting fingerprint from public key data: Incorrect padding'
94 with pytest.raises(FingerprintError) as exc:
95 extract_fingerprint('a ab==', alg='foo bar')
96 print(exc.value.args[0])
97 assert exc.value.args[0] == 'Hash algorithm FOO BAR is not available. Possibly running in FIPS mode.'