diff options
| author | Eli Collins <elic@assurancetechnologies.com> | 2020-05-11 14:14:50 -0400 |
|---|---|---|
| committer | Eli Collins <elic@assurancetechnologies.com> | 2020-05-11 14:14:50 -0400 |
| commit | a81474193a4c28a3ecd2c0e028de16454bb32913 (patch) | |
| tree | 107f511aa62b6ca36e50c592827f358e9d7765ff | |
| parent | 972ff330923bf3c9ac38544d488736fdfff41454 (diff) | |
| download | passlib-a81474193a4c28a3ecd2c0e028de16454bb32913.tar.gz | |
passlib.tests: fixed bug where HandlerCase.test_82_crypt_support() wasn't
being run on systems where "os_crypt" backend detected as missing.
This was because of conflict with create_backend_case(), which marks
ALL methods as skipped if backend isn't present. Reworked so certain methods
can be marked as "run anyways". In particular, we want platform_crypt_support
checks to always run, since they're there as sanity check that our "os_crypt"
detection routines are actually working in the first place :)
Also rewrote test_82_crypt_support() contents to add some code comments.
| -rw-r--r-- | docs/history/1.7.rst | 3 | ||||
| -rw-r--r-- | passlib/tests/test_handlers_bcrypt.py | 4 | ||||
| -rw-r--r-- | passlib/tests/utils.py | 88 |
3 files changed, 72 insertions, 23 deletions
diff --git a/docs/history/1.7.rst b/docs/history/1.7.rst index 5dd63a5..49b452a 100644 --- a/docs/history/1.7.rst +++ b/docs/history/1.7.rst @@ -44,6 +44,9 @@ Bugfixes * :mod:`passlib.ext.django`: fixed lru_cache import (django 3 compatibility) +* :mod:`!passlib.tests`: fixed bug where :meth:`HandlerCase.test_82_crypt_support` wasn't + being run on systems lacking support for hasher being tested. Now runs regardless of support. + Other Changes ------------- diff --git a/passlib/tests/test_handlers_bcrypt.py b/passlib/tests/test_handlers_bcrypt.py index 6b6e562..699b2d7 100644 --- a/passlib/tests/test_handlers_bcrypt.py +++ b/passlib/tests/test_handlers_bcrypt.py @@ -155,8 +155,8 @@ class _bcrypt_test(HandlerCase): platform_crypt_support = [ ("freedbsd|openbsd|netbsd", True), ("darwin", False), - # linux - may be present via addon, e.g. debian's libpam-unix2 - # solaris - depends on policy + ("linux", None), # may be present via addon, e.g. debian's libpam-unix2 + ("solaris", None), # depends on system policy ] #=================================================================== diff --git a/passlib/tests/utils.py b/passlib/tests/utils.py index f509a9d..8ef9a63 100644 --- a/passlib/tests/utils.py +++ b/passlib/tests/utils.py @@ -669,8 +669,20 @@ class TestCase(_TestCase): #============================================================================= # other unittest helpers #============================================================================= + RESERVED_BACKEND_NAMES = ["any", "default"] + +def doesnt_require_backend(func): + """ + decorator for HandlerCase.create_backend_case() -- + used to decorate methods that should be run even if backend isn't present + (by default, full test suite is skipped when backend is missing) + """ + func._doesnt_require_backend = True + return func + + class HandlerCase(TestCase): """base class for testing password hash handlers (esp passlib.utils.handlers subclasses) @@ -913,7 +925,8 @@ class HandlerCase(TestCase): # and other backend helpers #--------------------------------------------------------------- - BACKEND_NOT_AVAILABLE = "backend not available" + #: default message used by _get_skip_backend_reason() + _BACKEND_NOT_AVAILABLE = "backend not available" @classmethod def _get_skip_backend_reason(cls, backend): @@ -926,7 +939,7 @@ class HandlerCase(TestCase): return "only default backend is being tested" if handler.has_backend(backend): return None - return cls.BACKEND_NOT_AVAILABLE + return cls._BACKEND_NOT_AVAILABLE @classmethod def create_backend_case(cls, backend): @@ -943,18 +956,34 @@ class HandlerCase(TestCase): dict( descriptionPrefix="%s (%s backend)" % (name, backend), backend=backend, + _skip_backend_reason=cls._get_skip_backend_reason(backend), __module__=cls.__module__, ) ) - skip_reason = cls._get_skip_backend_reason(backend) - if skip_reason: - subcls = skip(skip_reason)(subcls) return subcls + #: flag for setUp() indicating this class is disabled due to backend issue; + #: this is only set for dynamic subclasses generated by create_backend_case() + _skip_backend_reason = None + + def _test_requires_backend(self): + """ + check if current test method decorated with doesnt_require_backend() helper + """ + meth = getattr(self, self._testMethodName, None) + return not getattr(meth, "_doesnt_require_backend", False) + #=================================================================== # setup #=================================================================== def setUp(self): + + # check if test is disabled due to missing backend; + # and that it wasn't exempted via @doesnt_require_backend() decorator + skip_backend_reason = self._skip_backend_reason + if skip_backend_reason and self._test_requires_backend(): + raise self.skipTest(skip_backend_reason) + super(HandlerCase, self).setUp() # if needed, select specific backend for duration of test @@ -3227,30 +3256,47 @@ class OsCryptMixin(HandlerCase): self.assertRaises(MissingBackendError, self.do_genhash, 'stub', hash) self.assertRaises(MissingBackendError, self.do_verify, 'stub', hash) + @doesnt_require_backend def test_82_crypt_support(self): - """test platform-specific crypt() support detection""" - # NOTE: this is mainly just a sanity check to ensure the runtime - # detection is functioning correctly on some known platforms, - # so that I can feel more confident it'll work right on unknown ones. + """ + test platform-specific crypt() support detection + + NOTE: this is mainly just a sanity check to ensure the runtime + detection is functioning correctly on some known platforms, + so that we can feel more confident it'll work right on unknown ones. + """ + + # skip wrapper handlers, won't ever have crypt support if hasattr(self.handler, "orig_prefix"): raise self.skipTest("not applicable to wrappers") + + # look for first entry that matches current system + # XXX: append "/" + platform.release() to string? + using_backend = not self.using_patched_crypt + name = self.handler.name platform = sys.platform - for pattern, state in self.platform_crypt_support: + for pattern, expected in self.platform_crypt_support: if re.match(pattern, platform): break else: - raise self.skipTest("no data for %r platform" % platform) - if state is None: - # e.g. platform='freebsd8' ... sha256_crypt not added until 8.3 - raise self.skipTest("varied support on %r platform" % platform) - elif state != self.using_patched_crypt: - return - elif state: - self.fail("expected %r platform would have native support " - "for %r" % (platform, self.handler.name)) + raise self.skipTest("no data for %r platform (current host support = %r)" % + (platform, using_backend)) + + # rules can use "state=None" to signal varied support; + # e.g. platform='freebsd8' ... sha256_crypt not added until 8.3 + if expected is None: + raise self.skipTest("varied support on %r platform (current host support = %r)" % + (platform, using_backend)) + + # compare expectation vs reality + if expected == using_backend: + pass + elif expected: + self.fail("expected %r platform would have native support for %r" % + (platform, name)) else: - self.fail("did not expect %r platform would have native support " - "for %r" % (platform, self.handler.name)) + self.fail("did not expect %r platform would have native support for %r" % + (platform, name)) #=================================================================== # fuzzy verified support -- add new verified that uses os crypt() |
