| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from dns._features import ( |
| 6 |
_cache, |
| 7 |
_requirements, |
| 8 |
_tuple_from_text, |
| 9 |
_version_check, |
| 10 |
force, |
| 11 |
have, |
| 12 |
) |
| 13 |
|
| 14 |
try: |
| 15 |
import cryptography |
| 16 |
|
| 17 |
v = _tuple_from_text(cryptography.__version__) |
| 18 |
have_cryptography = v >= (42, 0, 0) |
| 19 |
except ImportError: |
| 20 |
have_cryptography = False |
| 21 |
|
| 22 |
|
| 23 |
def test_tuple_from_text(): |
| 24 |
assert _tuple_from_text("") == () |
| 25 |
assert _tuple_from_text("1") == (1,) |
| 26 |
assert _tuple_from_text("1.2") == (1, 2) |
| 27 |
assert _tuple_from_text("1.2rc1") == (1, 2) |
| 28 |
assert _tuple_from_text("1.2.junk3") == (1, 2) |
| 29 |
|
| 30 |
|
| 31 |
@pytest.mark.skipif( |
| 32 |
not have_cryptography, reason="cryptography not available or too old" |
| 33 |
) |
| 34 |
def test_version_check(): |
| 35 |
assert _version_check("cryptography>=42") |
| 36 |
assert not _version_check("cryptography>=10000") |
| 37 |
assert not _version_check("totallyboguspackagename>=10000") |
| 38 |
|
| 39 |
|
| 40 |
@pytest.mark.skipif( |
| 41 |
not have_cryptography, reason="cryptography not available or too old" |
| 42 |
) |
| 43 |
def test_have(): |
| 44 |
# ensure cache is empty; we can't just assign as our local is shadowing the |
| 45 |
# variable in dns._features |
| 46 |
while len(_cache) > 0: |
| 47 |
_cache.popitem() |
| 48 |
assert have("dnssec") |
| 49 |
assert _cache["dnssec"] == True |
| 50 |
assert not have("bogusfeature") |
| 51 |
assert _cache["bogusfeature"] == False |
| 52 |
_requirements["unavailable"] = ["bogusmodule>=10000"] |
| 53 |
try: |
| 54 |
assert not have("unavailable") |
| 55 |
finally: |
| 56 |
del _requirements["unavailable"] |
| 57 |
|
| 58 |
|
| 59 |
def test_force(): |
| 60 |
while len(_cache) > 0: |
| 61 |
_cache.popitem() |
| 62 |
assert not have("bogusfeature") |
| 63 |
assert _cache["bogusfeature"] == False |
| 64 |
force("bogusfeature", True) |
| 65 |
assert have("bogusfeature") |
| 66 |
assert _cache["bogusfeature"] == True |
| 67 |
force("bogusfeature", False) |
| 68 |
assert not have("bogusfeature") |
| 69 |
assert _cache["bogusfeature"] == False |
| 70 |
_requirements["unavailable"] = ["bogusmodule>=10000"] |
| 71 |
try: |
| 72 |
assert not have("unavailable") |
| 73 |
assert _cache["unavailable"] == False |
| 74 |
force("unavailable", True) |
| 75 |
assert _cache["unavailable"] == True |
| 76 |
finally: |
| 77 |
del _requirements["unavailable"] |