diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2018-03-18 21:50:38 +0100 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2018-03-19 12:25:50 +0100 |
commit | b977909fc93caa599e52943be25b7f6042a4c70b (patch) | |
tree | 75b02a848786f7c7cdb8c4220e7f5bc39befd8a6 /tests | |
parent | d15ceaa99cbe560052c7fe9ecbbc77ecfd3d18b6 (diff) | |
download | m2crypto-b977909fc93caa599e52943be25b7f6042a4c70b.tar.gz |
Replace util.py3{bytes,str} with six.ensure_{binary,text}
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/test_authcookie.py | 6 | ||||
-rw-r--r-- | tests/test_obj.py | 13 | ||||
-rw-r--r-- | tests/test_ssl.py | 16 | ||||
-rw-r--r-- | tests/test_util.py | 20 |
4 files changed, 29 insertions, 26 deletions
diff --git a/tests/test_authcookie.py b/tests/test_authcookie.py index c53a448..a2dae16 100755 --- a/tests/test_authcookie.py +++ b/tests/test_authcookie.py @@ -11,7 +11,7 @@ try: except ImportError: import unittest -from M2Crypto import EVP, Rand, util +from M2Crypto import EVP, Rand, six, util from M2Crypto.AuthCookie import AuthCookie, AuthCookieJar, mix, unmix, unmix3 from M2Crypto.six.moves.http_cookies import SimpleCookie # pylint: disable=no-name-in-module,import-error @@ -58,7 +58,7 @@ class AuthCookieTestCase(unittest.TestCase): # Peek inside the cookie jar... key = self.jar._key # pylint: disable=protected-access mac = util.bin_to_hex( - EVP.hmac(key, util.py3bytes(mix(self.exp, self.data)), 'sha1')) + EVP.hmac(key, six.ensure_binary(mix(self.exp, self.data)), 'sha1')) self.assertEqual(c.mac(), mac) # Ok, stop peeking now. cookie_str = self._format % (self.exp, self.data, mac) @@ -111,7 +111,7 @@ class AuthCookieTestCase(unittest.TestCase): self.assertAlmostEqual(exp, self.exp, places=4) key = self.jar._key # pylint: disable=protected-access mac = util.bin_to_hex( - EVP.hmac(key, util.py3bytes(mix(self.exp, self.data)), 'sha1')) + EVP.hmac(key, six.ensure_binary(mix(self.exp, self.data)), 'sha1')) self.assertEqual(digest, mac) def test_cookie_str(self): diff --git a/tests/test_obj.py b/tests/test_obj.py index a30e767..7cdada0 100644 --- a/tests/test_obj.py +++ b/tests/test_obj.py @@ -8,7 +8,7 @@ try: except ImportError: import unittest -from M2Crypto import ASN1, BIO, Rand, X509, m2, util +from M2Crypto import ASN1, BIO, Rand, X509, m2, six """ These functions must be cleaned up and moved to some python module @@ -26,15 +26,18 @@ def x509_name_entry2tuple(entry): bio = BIO.MemoryBuffer() m2.asn1_string_print(bio._ptr(), m2.x509_name_entry_get_data(entry._ptr())) return ( - util.py3str(m2.obj_obj2txt( + six.ensure_text(m2.obj_obj2txt( m2.x509_name_entry_get_object(entry._ptr()), 0)), - util.py3str(bio.getvalue())) + six.ensure_text(bio.getvalue())) def tuple2x509_name_entry(tup): obj, data = tup - _x509_ne = m2.x509_name_entry_create_by_txt(None, obj, ASN1.MBSTRING_ASC, - data, len(data)) + # TODO This is evil, isn't it? Shouldn't we use only official API? + # Something like X509.X509_Name.add_entry_by_txt() + _x509_ne = m2.x509_name_entry_create_by_txt(None, six.ensure_str(obj), + ASN1.MBSTRING_ASC, + six.ensure_str(data), len(data)) if not _x509_ne: raise ValueError("Invalid object indentifier: %s" % obj) return X509.X509_Name_Entry(_x509_ne, _pyfree=1) # Prevent memory leaks diff --git a/tests/test_ssl.py b/tests/test_ssl.py index 459531a..7c683dc 100644 --- a/tests/test_ssl.py +++ b/tests/test_ssl.py @@ -36,7 +36,7 @@ except ImportError: import unittest from M2Crypto import (Err, Rand, SSL, X509, ftpslib, httpslib, m2, m2urllib, - m2urllib2, m2xmlrpclib, six, util) + m2urllib2, m2xmlrpclib, py27plus, six) from tests.fips import fips_mode log = logging.getLogger('test_SSL') @@ -119,7 +119,7 @@ class BaseSSLClientTestCase(unittest.TestCase): def stop_server(self, pid): pid.terminate() out, err = pid.communicate() - return util.py3str(out), util.py3str(err) + return six.ensure_text(out), six.ensure_text(err) def http_get(self, s): s.send(b'GET / HTTP/1.0\n\n') @@ -132,7 +132,7 @@ class BaseSSLClientTestCase(unittest.TestCase): except SSL.SSLError: # s_server throws an 'unexpected eof'... break resp = resp + r - return util.py3str(resp) + return six.ensure_text(resp) def setUp(self): self.srv_host = srv_host @@ -167,7 +167,7 @@ class HttpslibSSLClientTestCase(BaseSSLClientTestCase): c.close() finally: self.stop_server(pid) - self.assertIn('s_server -quiet -www', util.py3str(data)) + self.assertIn('s_server -quiet -www', six.ensure_text(data)) def test_HTTPSConnection_resume_session(self): pid = self.start_server(self.args) @@ -198,7 +198,7 @@ class HttpslibSSLClientTestCase(BaseSSLClientTestCase): c2.request('GET', '/') ses2 = c2.get_session() t2 = ses2.as_text() - data = util.py3str(c2.getresponse().read()) + data = six.ensure_text(c2.getresponse().read()) c.close() c2.close() self.assertEqual(t, t2, "Sessions did not match") @@ -215,7 +215,7 @@ class HttpslibSSLClientTestCase(BaseSSLClientTestCase): c = httpslib.HTTPSConnection(srv_host, self.srv_port, ssl_context=self.ctx) c.request('GET', '/') - data = util.py3str(c.getresponse().read()) + data = six.ensure_text(c.getresponse().read()) c.close() finally: self.stop_server(pid) @@ -1025,7 +1025,7 @@ class Urllib2TEChunkedSSLClientTestCase(BaseSSLClientTestCase): self.stop_server(pid) -@unittest.skipUnless(util.py27plus, +@unittest.skipUnless(py27plus, "Twisted doesn't test well with Python 2.6") class TwistedSSLClientTestCase(BaseSSLClientTestCase): @@ -1191,7 +1191,7 @@ def suite(): suite.addTest(unittest.makeSuite(MiscSSLClientTestCase)) suite.addTest(unittest.makeSuite(FtpslibTestCase)) try: - if util.py27plus: + if py27plus: import M2Crypto.SSL.TwistedProtocolWrapper as wrapper # noqa suite.addTest(unittest.makeSuite(TwistedSSLClientTestCase)) except ImportError: diff --git a/tests/test_util.py b/tests/test_util.py index 7053655..c4b3c0e 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -18,36 +18,36 @@ from M2Crypto import util, six class UtilTestCase(unittest.TestCase): def test_py3bytes(self): - self.assertIsInstance(util.py3bytes('test'), bytes) + self.assertIsInstance(six.ensure_binary('test'), bytes) def test_py3str(self): - self.assertIsInstance(util.py3str('test'), str) + self.assertIsInstance(six.ensure_text('test'), str) def test_py3bytes_str(self): - self.assertIsInstance(util.py3bytes(u'test'), bytes) + self.assertIsInstance(six.ensure_binary(u'test'), bytes) def test_py3str_str(self): - self.assertIsInstance(util.py3str(u'test'), six.string_types) + self.assertIsInstance(six.ensure_text(u'test'), six.string_types) def test_py3bytes_bytes(self): - self.assertIsInstance(util.py3bytes(b'test'), bytes) + self.assertIsInstance(six.ensure_binary(b'test'), bytes) def test_py3str_bytes(self): - self.assertIsInstance(util.py3str(b'test'), str) + self.assertIsInstance(six.ensure_text(b'test'), str) def test_py3bytes_bytearray(self): - self.assertIsInstance(util.py3bytes(bytearray(b'test')), bytearray) + self.assertIsInstance(six.ensure_binary(bytearray(b'test')), bytearray) def test_py3str_bytearray(self): - self.assertIsInstance(util.py3str(bytearray(b'test')), str) + self.assertIsInstance(six.ensure_text(bytearray(b'test')), str) def test_py3bytes_None(self): with self.assertRaises(TypeError): - util.py3bytes(None) + six.ensure_binary(None) def test_py3str_None(self): with self.assertRaises(TypeError): - util.py3str(None) + six.ensure_text(None) def suite(): |