1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vi:ts=4:et
from . import util
import setup as pycurl_setup
import unittest
import os, os.path, sys
import functools
try:
# Python 2
from StringIO import StringIO
except ImportError:
# Python 3
from io import StringIO
def set_env(key, new_value):
old_value = os.environ.get(key)
if new_value is not None:
os.environ[key] = new_value
elif old_value is not None:
del os.environ[key]
else:
# new and old values are None which mean the variable is not set
pass
return old_value
def reset_env(key, old_value):
# empty string means environment variable was empty
# None means it was not set
if old_value is not None:
os.environ[key] = old_value
elif key in os.environ:
del os.environ[key]
def using_curl_config(path, ssl_library=None):
path = os.path.join(os.path.dirname(__file__), 'fake-curl', path)
def decorator(fn):
@functools.wraps(fn)
def decorated(*args, **kwargs):
old_path = set_env('PYCURL_CURL_CONFIG', path)
old_ssl_library = set_env('PYCURL_SSL_LIBRARY', ssl_library)
try:
return fn(*args, **kwargs)
finally:
reset_env('PYCURL_CURL_CONFIG', old_path)
reset_env('PYCURL_SSL_LIBRARY', old_ssl_library)
return decorated
return decorator
def min_python_version(*spec):
def decorator(fn):
@functools.wraps(fn)
def decorated(*args, **kwargs):
if sys.version_info < spec:
raise unittest.SkipTest('Minimum Python version %s required' % spec.join('.'))
return fn(*args, **kwargs)
return decorated
return decorator
class SetupTest(unittest.TestCase):
@util.only_unix
def test_sanity_check(self):
config = pycurl_setup.ExtensionConfiguration()
# we should link against libcurl, one would expect
assert 'curl' in config.libraries
@util.only_unix
def test_valid_option_consumes_argv(self):
argv = ['', '--with-nss']
pycurl_setup.ExtensionConfiguration(argv)
self.assertEqual([''], argv)
@util.only_unix
def test_invalid_option_not_consumed(self):
argv = ['', '--bogus']
pycurl_setup.ExtensionConfiguration(argv)
self.assertEqual(['', '--bogus'], argv)
@util.only_unix
def test_invalid_option_suffix_not_consumed(self):
argv = ['', '--with-nss-bogus']
pycurl_setup.ExtensionConfiguration(argv)
self.assertEqual(['', '--with-nss-bogus'], argv)
@util.only_unix
@using_curl_config('curl-config-empty')
def test_no_ssl(self):
config = pycurl_setup.ExtensionConfiguration()
# do not expect anything to do with ssl
assert 'crypto' not in config.libraries
@util.only_unix
@using_curl_config('curl-config-libs-and-static-libs')
def test_does_not_use_static_libs(self):
config = pycurl_setup.ExtensionConfiguration()
# should not link against any libraries from --static-libs if
# --libs succeeded
assert 'flurby' in config.libraries
assert 'kzzert' not in config.libraries
@util.only_unix
@using_curl_config('curl-config-ssl-in-libs')
def test_ssl_in_libs(self):
config = pycurl_setup.ExtensionConfiguration()
# should link against openssl
assert 'crypto' in config.libraries
@util.only_unix
@using_curl_config('curl-config-ssl-in-static-libs')
def test_ssl_in_static_libs(self):
config = pycurl_setup.ExtensionConfiguration()
# should link against openssl
assert 'crypto' in config.libraries
@util.only_unix
@using_curl_config('curl-config-empty')
def test_no_ssl_define(self):
config = pycurl_setup.ExtensionConfiguration()
# ssl define should be off
assert 'HAVE_CURL_SSL' not in config.define_symbols
@util.only_unix
@using_curl_config('curl-config-ssl-in-libs')
def test_ssl_in_libs_sets_ssl_define(self):
config = pycurl_setup.ExtensionConfiguration()
# ssl define should be on
assert 'HAVE_CURL_SSL' in config.define_symbols
@util.only_unix
@using_curl_config('curl-config-ssl-in-static-libs')
def test_ssl_in_static_libs_sets_ssl_define(self):
config = pycurl_setup.ExtensionConfiguration()
# ssl define should be on
assert 'HAVE_CURL_SSL' in config.define_symbols
@util.only_unix
@using_curl_config('curl-config-ssl-in-libs')
def test_ssl_feature_sets_ssl_define(self):
config = pycurl_setup.ExtensionConfiguration()
# ssl define should be on
assert 'HAVE_CURL_SSL' in config.define_symbols
@util.only_unix
@using_curl_config('curl-config-ssl-feature-only')
def test_ssl_feature_only(self):
saved_stderr = sys.stderr
sys.stderr = captured_stderr = StringIO()
try:
config = pycurl_setup.ExtensionConfiguration()
finally:
sys.stderr = saved_stderr
# ssl define should be on
assert 'HAVE_CURL_SSL' in config.define_symbols
# and a warning message
assert 'Warning: libcurl is configured to use SSL, but we have \
not been able to determine which SSL backend it is using.' in captured_stderr.getvalue()
@util.only_unix
@using_curl_config('curl-config-ssl-feature-only')
def test_libcurl_ssl_openssl(self):
sopath = os.path.join(os.path.dirname(__file__), 'fake-curl', 'libcurl', 'with_openssl.so')
config = pycurl_setup.ExtensionConfiguration(['',
'--libcurl-dll=' + sopath])
# openssl should be detected
assert 'HAVE_CURL_SSL' in config.define_symbols
assert 'HAVE_CURL_OPENSSL' in config.define_symbols
assert 'crypto' in config.libraries
assert 'HAVE_CURL_GNUTLS' not in config.define_symbols
assert 'HAVE_CURL_NSS' not in config.define_symbols
@util.only_unix
@using_curl_config('curl-config-ssl-feature-only')
def test_libcurl_ssl_gnutls(self):
sopath = os.path.join(os.path.dirname(__file__), 'fake-curl', 'libcurl', 'with_gnutls.so')
config = pycurl_setup.ExtensionConfiguration(['',
'--libcurl-dll=' + sopath])
# gnutls should be detected
assert 'HAVE_CURL_SSL' in config.define_symbols
assert 'HAVE_CURL_GNUTLS' in config.define_symbols
assert 'gnutls' in config.libraries
assert 'HAVE_CURL_OPENSSL' not in config.define_symbols
assert 'HAVE_CURL_NSS' not in config.define_symbols
@util.only_unix
@using_curl_config('curl-config-ssl-feature-only')
def test_libcurl_ssl_nss(self):
sopath = os.path.join(os.path.dirname(__file__), 'fake-curl', 'libcurl', 'with_nss.so')
config = pycurl_setup.ExtensionConfiguration(['',
'--libcurl-dll=' + sopath])
# nss should be detected
assert 'HAVE_CURL_SSL' in config.define_symbols
assert 'HAVE_CURL_NSS' in config.define_symbols
assert 'ssl3' in config.libraries
assert 'HAVE_CURL_OPENSSL' not in config.define_symbols
assert 'HAVE_CURL_GNUTLS' not in config.define_symbols
@util.only_unix
@using_curl_config('curl-config-empty')
def test_libcurl_ssl_unrecognized(self):
sopath = os.path.join(os.path.dirname(__file__), 'fake-curl', 'libcurl', 'with_unknown.so')
config = pycurl_setup.ExtensionConfiguration(['',
'--libcurl-dll=' + sopath])
assert 'HAVE_CURL_SSL' not in config.define_symbols
assert 'HAVE_CURL_OPENSSL' not in config.define_symbols
assert 'HAVE_CURL_GNUTLS' not in config.define_symbols
assert 'HAVE_CURL_NSS' not in config.define_symbols
@util.only_unix
@using_curl_config('curl-config-ssl-feature-only')
def test_with_ssl_library(self):
config = pycurl_setup.ExtensionConfiguration(['',
'--with-ssl'])
assert 'HAVE_CURL_SSL' in config.define_symbols
assert 'HAVE_CURL_OPENSSL' in config.define_symbols
assert 'crypto' in config.libraries
assert 'HAVE_CURL_GNUTLS' not in config.define_symbols
assert 'HAVE_CURL_NSS' not in config.define_symbols
@util.only_unix
@using_curl_config('curl-config-ssl-feature-only')
def test_with_openssl_library(self):
config = pycurl_setup.ExtensionConfiguration(['',
'--with-openssl'])
assert 'HAVE_CURL_SSL' in config.define_symbols
assert 'HAVE_CURL_OPENSSL' in config.define_symbols
assert 'crypto' in config.libraries
assert 'HAVE_CURL_GNUTLS' not in config.define_symbols
assert 'HAVE_CURL_NSS' not in config.define_symbols
@util.only_unix
@using_curl_config('curl-config-ssl-feature-only')
def test_with_gnutls_library(self):
config = pycurl_setup.ExtensionConfiguration(['',
'--with-gnutls'])
assert 'HAVE_CURL_SSL' in config.define_symbols
assert 'HAVE_CURL_GNUTLS' in config.define_symbols
assert 'gnutls' in config.libraries
assert 'HAVE_CURL_OPENSSL' not in config.define_symbols
assert 'HAVE_CURL_NSS' not in config.define_symbols
@util.only_unix
@using_curl_config('curl-config-ssl-feature-only')
def test_with_nss_library(self):
config = pycurl_setup.ExtensionConfiguration(['',
'--with-nss'])
assert 'HAVE_CURL_SSL' in config.define_symbols
assert 'HAVE_CURL_NSS' in config.define_symbols
assert 'ssl3' in config.libraries
assert 'HAVE_CURL_OPENSSL' not in config.define_symbols
assert 'HAVE_CURL_GNUTLS' not in config.define_symbols
@util.only_unix
@using_curl_config('curl-config-empty')
def test_no_ssl_feature_with_libcurl_dll(self):
sopath = os.path.join(os.path.dirname(__file__), 'fake-curl', 'libcurl', 'with_openssl.so')
config = pycurl_setup.ExtensionConfiguration(['',
'--libcurl-dll=' + sopath])
# openssl should not be detected
assert 'HAVE_CURL_SSL' not in config.define_symbols
assert 'HAVE_CURL_OPENSSL' not in config.define_symbols
assert 'crypto' not in config.libraries
@util.only_unix
@using_curl_config('curl-config-empty')
def test_no_ssl_feature_with_ssl(self):
old_stderr = sys.stderr
sys.stderr = captured_stderr = StringIO()
try:
config = pycurl_setup.ExtensionConfiguration(['',
'--with-ssl'])
# openssl should not be detected
assert 'HAVE_CURL_SSL' not in config.define_symbols
assert 'HAVE_CURL_OPENSSL' not in config.define_symbols
assert 'crypto' not in config.libraries
finally:
sys.stderr = old_stderr
self.assertEqual("Warning: SSL backend specified manually but libcurl does not use SSL",
captured_stderr.getvalue().strip())
|