From 4ec181c1403670702c2f163062b92de4dec3d2cc Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 31 May 2015 13:12:01 +1200 Subject: [PATCH] Move version check to netlib, unit test it. --- netlib/version_check.py | 49 ++++++++++++++++++++++++++++++++++++++ test/test_version_check.py | 22 +++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 netlib/version_check.py create mode 100644 test/test_version_check.py diff --git a/netlib/version_check.py b/netlib/version_check.py new file mode 100644 index 000000000..09dc23ae4 --- /dev/null +++ b/netlib/version_check.py @@ -0,0 +1,49 @@ +from __future__ import print_function, absolute_import +import sys +import inspect +import os.path + +import OpenSSL +from . import version + +PYOPENSSL_MIN_VERSION = (0, 15) + + +def version_check( + mitmproxy_version, + pyopenssl_min_version=PYOPENSSL_MIN_VERSION, + fp=sys.stderr): + """ + Having installed a wrong version of pyOpenSSL or netlib is unfortunately a + very common source of error. Check before every start that both versions + are somewhat okay. + """ + # We don't introduce backward-incompatible changes in patch versions. Only + # consider major and minor version. + if version.IVERSION[:2] != mitmproxy_version[:2]: + print( + "You are using mitmproxy %s with netlib %s. " + "Most likely, that won't work - please upgrade!" % ( + mitmproxy_version, version.VERSION + ), + file=fp + ) + sys.exit(1) + v = tuple([int(x) for x in OpenSSL.__version__.split(".")][:2]) + if v < pyopenssl_min_version: + print( + "You are using an outdated version of pyOpenSSL:" + " mitmproxy requires pyOpenSSL %x or greater." % + pyopenssl_min_version, + file=fp + ) + # Some users apparently have multiple versions of pyOpenSSL installed. + # Report which one we got. + pyopenssl_path = os.path.dirname(inspect.getfile(OpenSSL)) + print( + "Your pyOpenSSL %s installation is located at %s" % ( + OpenSSL.__version__, pyopenssl_path + ), + file=fp + ) + sys.exit(1) diff --git a/test/test_version_check.py b/test/test_version_check.py new file mode 100644 index 000000000..bf6ad1f5b --- /dev/null +++ b/test/test_version_check.py @@ -0,0 +1,22 @@ +import cStringIO +import mock +from netlib import version_check, version + + +@mock.patch("sys.exit") +def test_version_check(sexit): + fp = cStringIO.StringIO() + version_check.version_check(version.IVERSION, fp=fp) + assert not sexit.called + + b = (version.IVERSION[0] - 1, version.IVERSION[1]) + version_check.version_check(b, fp=fp) + assert sexit.called + + sexit.reset_mock() + version_check.version_check( + version.IVERSION, + pyopenssl_min_version=(9999,), + fp=fp + ) + assert sexit.called