From dd7679bd89c072e1cea7147db1eeb790c8967150 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Mon, 20 Feb 2017 18:05:34 -0800 Subject: Test proving #853, fails on Python 3 but not 2 --- tests/test_pkey.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'tests') diff --git a/tests/test_pkey.py b/tests/test_pkey.py index 2181dd91..56b0bd62 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -107,6 +107,8 @@ L4QLcT5aND0EHZLB2fAUDXiWIb2j4rg1mwPlBMiBXA== x1234 = b'\x01\x02\x03\x04' +TEST_KEY_BYTES = b'\x00\x00\x00\x07ssh-rsa\x00\x00\x00\x01#\x00\x00\x00\x81\x00\xd3\x8fV\xea\x07\x85\xa6k%\x8d<\x1f\xbc\x8dT\x98\xa5\x96$\xf3E#\xbe>\xbc\xd2\x93\x93\x87f\xceD\x18\xdb \x0c\xb3\xa1a\x96\xf8e#\xcc\xacS\x8a#\xefVlE\x83\x1epv\xc1o\x17M\xef\xdf\x89DUXL\xa6\x8b\xaa<\x06\x10\xd7\x93w\xec\xaf\xe2\xaf\x95\xd8\xfb\xd9\xbfw\xcb\x9f0)#y{\x10\x90\xaa\x85l\tPru\x8c\t\x19\xce\xa0\xf1\xd2\xdc\x8e/\x8b\xa8f\x9c0\xdey\x84\xd2F\xf7\xcbmm\x1f\x87' + class KeyTest (unittest.TestCase): @@ -427,3 +429,7 @@ class KeyTest (unittest.TestCase): self.assertEqual(key, key2) finally: os.remove(newfile) + + def test_stringification(self): + key = RSAKey.from_private_key_file(test_path('test_rsa.key')) + self.assertEqual(str(key), TEST_KEY_BYTES) -- cgit v1.2.3 From c0f4238eda90e884d52717ef0cb0abbc0f4ed871 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Mon, 20 Feb 2017 20:57:59 -0800 Subject: This is awful and I hate my life --- paramiko/rsakey.py | 12 +++++++++++- tests/test_pkey.py | 9 ++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/paramiko/rsakey.py b/paramiko/rsakey.py index bc9053f5..fa2b4c44 100644 --- a/paramiko/rsakey.py +++ b/paramiko/rsakey.py @@ -27,6 +27,7 @@ from cryptography.hazmat.primitives.asymmetric import rsa, padding from paramiko.message import Message from paramiko.pkey import PKey +from paramiko.py3compat import PY2 from paramiko.ssh_exception import SSHException @@ -76,7 +77,16 @@ class RSAKey(PKey): return m.asbytes() def __str__(self): - return self.asbytes() + # NOTE: as per inane commentary in #853, this appears to be the least + # crummy way to get a representation that prints identical to Python + # 2's previous behavior, on both interpreters. + # TODO: replace with a nice clean fingerprint display or something + if PY2: + # Can't just return the .decode below for Py2 because stuff still + # tries stuffing it into ASCII for whatever godforsaken reason + return self.asbytes() + else: + return self.asbytes().decode('utf8', errors='ignore') def __hash__(self): h = hash(self.get_name()) diff --git a/tests/test_pkey.py b/tests/test_pkey.py index 56b0bd62..24d78c3e 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # Copyright (C) 2003-2009 Robey Pointer # # This file is part of paramiko. @@ -27,7 +28,7 @@ from hashlib import md5 import base64 from paramiko import RSAKey, DSSKey, ECDSAKey, Message, util -from paramiko.py3compat import StringIO, byte_chr, b, bytes +from paramiko.py3compat import StringIO, byte_chr, b, bytes, PY2 from tests.util import test_path @@ -107,7 +108,8 @@ L4QLcT5aND0EHZLB2fAUDXiWIb2j4rg1mwPlBMiBXA== x1234 = b'\x01\x02\x03\x04' -TEST_KEY_BYTES = b'\x00\x00\x00\x07ssh-rsa\x00\x00\x00\x01#\x00\x00\x00\x81\x00\xd3\x8fV\xea\x07\x85\xa6k%\x8d<\x1f\xbc\x8dT\x98\xa5\x96$\xf3E#\xbe>\xbc\xd2\x93\x93\x87f\xceD\x18\xdb \x0c\xb3\xa1a\x96\xf8e#\xcc\xacS\x8a#\xefVlE\x83\x1epv\xc1o\x17M\xef\xdf\x89DUXL\xa6\x8b\xaa<\x06\x10\xd7\x93w\xec\xaf\xe2\xaf\x95\xd8\xfb\xd9\xbfw\xcb\x9f0)#y{\x10\x90\xaa\x85l\tPru\x8c\t\x19\xce\xa0\xf1\xd2\xdc\x8e/\x8b\xa8f\x9c0\xdey\x84\xd2F\xf7\xcbmm\x1f\x87' +TEST_KEY_BYTESTR_2 = '\x00\x00\x00\x07ssh-rsa\x00\x00\x00\x01#\x00\x00\x00\x81\x00\xd3\x8fV\xea\x07\x85\xa6k%\x8d<\x1f\xbc\x8dT\x98\xa5\x96$\xf3E#\xbe>\xbc\xd2\x93\x93\x87f\xceD\x18\xdb \x0c\xb3\xa1a\x96\xf8e#\xcc\xacS\x8a#\xefVlE\x83\x1epv\xc1o\x17M\xef\xdf\x89DUXL\xa6\x8b\xaa<\x06\x10\xd7\x93w\xec\xaf\xe2\xaf\x95\xd8\xfb\xd9\xbfw\xcb\x9f0)#y{\x10\x90\xaa\x85l\tPru\x8c\t\x19\xce\xa0\xf1\xd2\xdc\x8e/\x8b\xa8f\x9c0\xdey\x84\xd2F\xf7\xcbmm\x1f\x87' +TEST_KEY_BYTESTR_3 = '\x00\x00\x00\x07ssh-rsa\x00\x00\x00\x01#\x00\x00\x00\x00ӏV\x07k%<\x1fT$E#>ғfD\x18 \x0cae#̬S#VlE\x1epvo\x17M߉DUXL<\x06\x10דw\u2bd5ٿw˟0)#y{\x10l\tPru\t\x19Π\u070e/f0yFmm\x1f' class KeyTest (unittest.TestCase): @@ -432,4 +434,5 @@ class KeyTest (unittest.TestCase): def test_stringification(self): key = RSAKey.from_private_key_file(test_path('test_rsa.key')) - self.assertEqual(str(key), TEST_KEY_BYTES) + comparable = TEST_KEY_BYTESTR_2 if PY2 else TEST_KEY_BYTESTR_3 + self.assertEqual(str(key), comparable) -- cgit v1.2.3