示例#1
0
  def test_find_known_host(self):
    content = '''
example.com.172.16.1.1 ecdsa-sha2-nistp256 key1
bitbucket.org,18.205.93.2 ssh-rsa key2
198.168.1.1 ssh-rsa key3
'''
    tmp_file = self.make_temp_file(content = content)
    c = ssh_known_hosts_file(tmp_file)
    self.assertEqual( ( [ 'bitbucket.org', '18.205.93.2' ], 'ssh-rsa', 'key2', None ),
                      c.find_known_host('bitbucket.org') )
示例#2
0
  def test_basic(self):
    content = '''
example.com.172.16.1.1 ecdsa-sha2-nistp256 key1
bitbucket.org,18.205.93.2 ssh-rsa key2
198.168.1.1 ssh-rsa key3
'''
    tmp_file = self.make_temp_file(content = content)
    c = ssh_known_hosts_file(tmp_file)

    self.assertMultiLineEqual( content.strip(), str(c).strip() )
示例#3
0
  def test_malformed_file(self):
    content = '''
example.com.172.16.1.1 ecdsa-sha2-nistp256 key1
wtf
bitbucket.org,18.205.93.2 ssh-rsa key2
198.168.1.1 ssh-rsa key3
'''
    tmp_file = self.make_temp_file(content = content)
    c = ssh_known_hosts_file(tmp_file)
    with self.assertRaises(ssh_config_error) as ctx:
      c.find_known_host('bitbucket.org')
示例#4
0
  def test_nonexistent_file(self):
    tmp_file = self.make_temp_file()
    file_util.remove(tmp_file)
    c = ssh_known_hosts_file(tmp_file)
    c.add_known_host(ssh_known_host([ 'foo.com', '192.168.2.2' ], 'ssh-rsa', 'key4'))

    expected = '''
foo.com,192.168.2.2 ssh-rsa key4
'''
    
    self.assertMultiLineEqual( expected.strip(), file_util.read(tmp_file, codec = 'utf-8').strip() )
示例#5
0
  def test_comment_preservation(self):
    content = '''
# kiwi
example.com.172.16.1.1 ecdsa-sha2-nistp256 key1
# apple
bitbucket.org,18.205.93.2 ssh-rsa key2
# lemon
198.168.1.1 ssh-rsa key3
'''
    tmp_file = self.make_temp_file(content = content)
    c = ssh_known_hosts_file(tmp_file)

    self.assertMultiLineEqual( content.strip(), str(c).strip() )
示例#6
0
  def test_add_known_host(self):
    content = '''
example.com.172.16.1.1 ecdsa-sha2-nistp256 key1
bitbucket.org,18.205.93.2 ssh-rsa key2
198.168.1.1 ssh-rsa key3
'''
    tmp_file = self.make_temp_file(content = content)
    c = ssh_known_hosts_file(tmp_file)
    c.add_known_host(ssh_known_host([ 'foo.com', '192.168.2.2' ], 'ssh-rsa', 'key4'))

    expected = '''
example.com.172.16.1.1 ecdsa-sha2-nistp256 key1
bitbucket.org,18.205.93.2 ssh-rsa key2
198.168.1.1 ssh-rsa key3
foo.com,192.168.2.2 ssh-rsa key4
'''
    
    self.assertMultiLineEqual( expected.strip(), file_util.read(tmp_file, codec = 'utf-8').strip() )