Debian Codegraph @

tests/test_hostkeys.py

raw
1 # Copyright (C) 2006-2007 Robey Pointer <[email protected]>
2 #
3 # This file is part of paramiko.
4 #
5 # Paramiko is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation; either version 2.1 of the License, or (at your option)
8 # any later version.
9 #
10 # Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with Paramiko; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 """
20 Some unit tests for HostKeys.
21 """
22
23 from base64 import decodebytes
24 from binascii import hexlify
25 import os
26 import unittest
27
28 import paramiko
29
30 from ._util import _support
31
32
33 test_hosts_file = """\
34 secure.example.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA1PD6U2/TVxET6lkpKhOk5r\
35 9q/kAYG6sP9f5zuUYP8i7FOFp/6ncCEbbtg/lB+A3iidyxoSWl+9jtoyyDOOVX4UIDV9G11Ml8om3\
36 D+jrpI9cycZHqilK0HmxDeCuxbwyMuaCygU9gS2qoRvNLWZk70OpIKSSpBo0Wl3/XUmz9uhc=
37 broken.example.com ssh-rsa AAAA
38 happy.example.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA8bP1ZA7DCZDB9J0s50l31M\
39 BGQ3GQ/Fc7SX6gkpXkwcZryoi4kNFhHu5LvHcZPdxXV1D+uTMfGS1eyd2Yz/DoNWXNAl8TI0cAsW\
40 5ymME3bQ4J/k1IKxCtz/bAlAqFgKoc+EolMziDYqWIATtW0rYTJvzGAzTmMj80/QpsFH+Pc2M=
41 modern.example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKHEChAIxsh2hr8Q\
42 +Ea1AAHZyfEB2elEc2YgduVzBtp+
43 curvy.example.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlz\
44 dHAyNTYAAABBBAa+pY7djSpbg5viAcZhPt56AO3U3Sd7h7dnlUp0EjfDgyYHYQxl2QZ4JGgfwR5iv9\
45 T9iRZjQzvJd5s+kBAZtpk=
46 """
47
48 test_hosts_file_tabs = """\
49 secure.example.com\tssh-rsa\tAAAAB3NzaC1yc2EAAAABIwAAAIEA1PD6U2/TVxET6lkpKhOk5r\
50 9q/kAYG6sP9f5zuUYP8i7FOFp/6ncCEbbtg/lB+A3iidyxoSWl+9jtoyyDOOVX4UIDV9G11Ml8om3\
51 D+jrpI9cycZHqilK0HmxDeCuxbwyMuaCygU9gS2qoRvNLWZk70OpIKSSpBo0Wl3/XUmz9uhc=
52 happy.example.com\tssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA8bP1ZA7DCZDB9J0s50l31M\
53 BGQ3GQ/Fc7SX6gkpXkwcZryoi4kNFhHu5LvHcZPdxXV1D+uTMfGS1eyd2Yz/DoNWXNAl8TI0cAsW\
54 5ymME3bQ4J/k1IKxCtz/bAlAqFgKoc+EolMziDYqWIATtW0rYTJvzGAzTmMj80/QpsFH+Pc2M=
55 modern.example.com\tssh-ed25519\tAAAAC3NzaC1lZDI1NTE5AAAAIKHEChAIxsh2hr8Q\
56 +Ea1AAHZyfEB2elEc2YgduVzBtp+
57 curvy.example.com\tecdsa-sha2-nistp256\tAAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbml\
58 zdHAyNTYAAABBBAa+pY7djSpbg5viAcZhPt56AO3U3Sd7h7dnlUp0EjfDgyYHYQxl2QZ4JGgfwR5iv\
59 9T9iRZjQzvJd5s+kBAZtpk=
60 """
61
62 keyblob = b"""\
63 AAAAB3NzaC1yc2EAAAABIwAAAIEA8bP1ZA7DCZDB9J0s50l31MBGQ3GQ/Fc7SX6gkpXkwcZryoi4k\
64 NFhHu5LvHcZPdxXV1D+uTMfGS1eyd2Yz/DoNWXNAl8TI0cAsW5ymME3bQ4J/k1IKxCtz/bAlAqFgK\
65 oc+EolMziDYqWIATtW0rYTJvzGAzTmMj80/QpsFH+Pc2M="""
66
67
68 class HostKeysTest(unittest.TestCase):
69 def setUp(self):
70 with open("hostfile.temp", "w") as f:
71 f.write(test_hosts_file)
72
73 def tearDown(self):
74 os.unlink("hostfile.temp")
75
76 def test_load(self):
77 hostdict = paramiko.HostKeys("hostfile.temp")
78 assert len(hostdict) == 4
79 self.assertEqual(1, len(list(hostdict.values())[0]))
80 self.assertEqual(1, len(list(hostdict.values())[1]))
81 fp = hexlify(
82 hostdict["secure.example.com"]["ssh-rsa"].get_fingerprint()
83 ).upper()
84 self.assertEqual(b"E6684DB30E109B67B70FF1DC5C7F1363", fp)
85
86 def test_add(self):
87 hostdict = paramiko.HostKeys("hostfile.temp")
88 hh = "|1|BMsIC6cUIP2zBuXR3t2LRcJYjzM=|hpkJMysjTk/+zzUUzxQEa2ieq6c="
89 key = paramiko.RSAKey(data=decodebytes(keyblob))
90 hostdict.add(hh, "ssh-rsa", key)
91 assert len(hostdict) == 5
92 x = hostdict["foo.example.com"]
93 fp = hexlify(x["ssh-rsa"].get_fingerprint()).upper()
94 self.assertEqual(b"7EC91BB336CB6D810B124B1353C32396", fp)
95 self.assertTrue(hostdict.check("foo.example.com", key))
96
97 def test_dict(self):
98 hostdict = paramiko.HostKeys("hostfile.temp")
99 self.assertTrue("secure.example.com" in hostdict)
100 self.assertTrue("not.example.com" not in hostdict)
101 self.assertTrue("secure.example.com" in hostdict)
102 self.assertTrue("not.example.com" not in hostdict)
103 x = hostdict.get("secure.example.com", None)
104 self.assertTrue(x is not None)
105 fp = hexlify(x["ssh-rsa"].get_fingerprint()).upper()
106 self.assertEqual(b"E6684DB30E109B67B70FF1DC5C7F1363", fp)
107 assert list(hostdict) == hostdict.keys()
108 assert len(list(hostdict)) == len(hostdict.keys()) == 4
109
110 def test_dict_set(self):
111 hostdict = paramiko.HostKeys("hostfile.temp")
112 key = paramiko.RSAKey(data=decodebytes(keyblob))
113 key_ed25519 = paramiko.Ed25519Key.from_private_key_file(
114 _support("ed25519.key")
115 )
116 hostdict["secure.example.com"] = {
117 "ssh-rsa": key,
118 "ssh-ed25519": key_ed25519,
119 }
120 hostdict["fake.example.com"] = {}
121 hostdict["fake.example.com"]["ssh-rsa"] = key
122
123 assert len(hostdict) == 5
124 self.assertEqual(2, len(list(hostdict.values())[0]))
125 self.assertEqual(1, len(list(hostdict.values())[1]))
126 self.assertEqual(1, len(list(hostdict.values())[2]))
127 fp = hexlify(
128 hostdict["secure.example.com"]["ssh-rsa"].get_fingerprint()
129 ).upper()
130 self.assertEqual(b"7EC91BB336CB6D810B124B1353C32396", fp)
131 fp = hexlify(
132 hostdict["secure.example.com"]["ssh-ed25519"].get_fingerprint()
133 ).upper()
134 self.assertEqual(b"B3D522AAF9755EE8CD0EEA02B929A280", fp)
135
136 def test_delitem(self):
137 hostdict = paramiko.HostKeys("hostfile.temp")
138 target = "happy.example.com"
139 hostdict[target] # will KeyError if not present
140 del hostdict[target]
141 try:
142 hostdict[target]
143 except KeyError:
144 pass # Good
145 else:
146 assert False, "Entry was not deleted from HostKeys on delitem!"
147
148 def test_entry_delitem(self):
149 hostdict = paramiko.HostKeys("hostfile.temp")
150 target = "happy.example.com"
151 entry = hostdict[target]
152 key_type_list = [key_type for key_type in entry]
153 for key_type in key_type_list:
154 del entry[key_type]
155
156 # will KeyError if not present
157 for key_type in key_type_list:
158 try:
159 del entry[key_type]
160 except KeyError:
161 pass # Good
162 else:
163 assert False, "Key was not deleted from Entry on delitem!"
164
165
166 class HostKeysTabsTest(HostKeysTest):
167 def setUp(self):
168 with open("hostfile.temp", "w") as f:
169 f.write(test_hosts_file_tabs)