def test_parse_new(self): """In new format, parsing is same for encryped and un-encrypted.""" key_str = open(_support("test_ed25519.key")).read() pkformat, typ, headers, data = PKey._parse_openssh_pkey(key_str) self.assertEqual(pkformat, PKey.FORMAT_OPENSSH) self.assertEqual(typ, "OPENSSH") self.assertEqual(headers, {}) self.assertIsInstance(data, bytes) self.assertEqual(len(data), 266)
def test_parse_old_password(self): """Old format with headers""" pkformat, typ, headers, data = PKey._parse_openssh_pkey(self.ecdsa_str) self.assertEqual(pkformat, PKey.FORMAT_ORIGINAL) self.assertEqual(typ, ECDSAKey.LEGACY_TYPE) self.assertEqual( headers, { "dek-info": "AES-128-CBC,EEB56BC745EDB2DE04FC3FE1F8DA387E", "proc-type": "4,ENCRYPTED" }) self.assertIsInstance(data, bytes) self.assertEqual(len(data), 128)
def test_parse_empty(self): with self.assertRaises(SSHException) as ctx: PKey._parse_openssh_pkey("") self.assertEqual(str(ctx.exception), "not a valid private key file")
def test_bad_base64(self): with self.assertRaises(SSHException) as ctx: PKey._parse_openssh_pkey(INVALID_BASE64_KEY) self.assertEqual(str(ctx.exception), "base64 decoding error: Incorrect padding")
def test_parse_end_line(self): """END tag must be on a line by itself""" key_str = self.ecdsa_str.strip() + "BLA" with self.assertRaises(SSHException): PKey._parse_openssh_pkey(key_str)
def test_parse_begin_line(self): """BEGIN tag must be on a line by itself""" key_str = "BLA" + self.ecdsa_str with self.assertRaises(SSHException): PKey._parse_openssh_pkey(key_str)
def test_parse_crlf(self): """Test handling of Windows newlines""" key_str = self.ecdsa_str key_str = key_str.replace("\n", "\r\n") self.assertEqual(PKey._parse_openssh_pkey(key_str), self.ecdsa_parsed)
def test_parse_whitespace(self): """In some places, extraneous whitespace should not affect parsing""" key_str = self.ecdsa_str key_str = key_str.replace("\n", " \t\n") key_str = key_str.replace(": ", ": \t ") self.assertEqual(PKey._parse_openssh_pkey(key_str), self.ecdsa_parsed)
def setUpClass(cls): # Several tests rely on these fields. cls.ecdsa_str = open( _support("test_ecdsa_password_256.key")).read().strip() cls.ecdsa_parsed = PKey._parse_openssh_pkey(cls.ecdsa_str)