Esempio n. 1
0
        h = md4(b("abc"))

        h2 = h.copy()
        h2.update(b("def"))
        self.assertEqual(h2.hexdigest(), "804e7f1c2586e50b49ac65db5b645131")

        h.update(b("ghi"))
        self.assertEqual(h.hexdigest(), "c5225580bfe176f6deeee33dee98732c")


# create subclasses to test with and without native backend
class MD4_SSL_Test(_MD4_Test):
    descriptionPrefix = "MD4 (ssl version)"


MD4_SSL_TEST = skipUnless(has_native_md4, "hashlib lacks ssl support")(MD4_SSL_Test)


class MD4_Builtin_Test(_MD4_Test):
    descriptionPrefix = "MD4 (builtin version)"
    _disable_native = True


MD4_Builtin_Test = skipUnless(TEST_MODE("full") or not has_native_md4, "skipped under current test mode")(
    MD4_Builtin_Test
)

# =============================================================================
# test PBKDF1 support
# =============================================================================
class Pbkdf1_Test(TestCase):
Esempio n. 2
0
            # django 1.5 tests expect empty django_des_crypt salt field
            if DJANGO_VERSION > (1,4):
                from passlib.hash import django_des_crypt
                patchAttr(self, django_des_crypt, "use_duplicate_salt", False)

            # hack: need password_context to keep up to date with hasher.iterations
            if DJANGO_VERSION >= (1,6):
                def update_hook(self):
                    rounds = _thmod.get_hasher("pbkdf2_sha256").iterations
                    self.update(
                        django_pbkdf2_sha256__min_rounds=rounds,
                        django_pbkdf2_sha256__default_rounds=rounds,
                        django_pbkdf2_sha256__max_rounds=rounds,
                    )
                patchAttr(self, password_context, "__class__", ContextWithHook)
                patchAttr(self, password_context, "update_hook", update_hook)

        # omitting this test, since it depends on updated to django hasher settings
        test_pbkdf2_upgrade_new_hasher = lambda self: self.skipTest("omitted by passlib")

        def tearDown(self):
            self.unload_extension()
            super(HashersTest, self).tearDown()

    HashersTest = skipUnless(TEST_MODE("default"),
                             "requires >= 'default' test mode")(HashersTest)

#=============================================================================
# eof
#=============================================================================
        h = md4(b('abc'))

        h2 = h.copy()
        h2.update(b('def'))
        self.assertEqual(h2.hexdigest(), '804e7f1c2586e50b49ac65db5b645131')

        h.update(b('ghi'))
        self.assertEqual(h.hexdigest(), 'c5225580bfe176f6deeee33dee98732c')


# create subclasses to test with and without native backend
class MD4_SSL_Test(_MD4_Test):
    descriptionPrefix = "MD4 (ssl version)"


MD4_SSL_TEST = skipUnless(has_native_md4,
                          "hashlib lacks ssl support")(MD4_SSL_Test)


class MD4_Builtin_Test(_MD4_Test):
    descriptionPrefix = "MD4 (builtin version)"
    _disable_native = True


MD4_Builtin_Test = skipUnless(
    TEST_MODE("full") or not has_native_md4,
    "skipped under current test mode")(MD4_Builtin_Test)


#=============================================================================
# test PBKDF1 support
#=============================================================================
        self.assertEqual(run(first_name='staff', is_staff=True), 1000)
        self.assertEqual(run(first_name='superuser', is_superuser=True), 1000)

        # test bad value
        self.assertRaises(TypeError, self.load_extension, PASSLIB_CONTEXT=config,
                          PASSLIB_GET_CATEGORY='x')

    #=========================================================
    # eoc
    #=========================================================

# hack up the some of the real django tests to run w/ extension loaded,
# to ensure we mimic their behavior.
if has_django14:
    from django.contrib.auth.tests.hashers import TestUtilsHashPass as _TestHashers
    class HashersTest(_TestHashers, _ExtensionSupport):
        def setUp(self):
            # omitted orig setup, loading hashers our own way
            self.load_extension(PASSLIB_CONTEXT=stock_config, check=False)
        def tearDown(self):
            self.unload_extension()
            super(HashersTest, self).tearDown()
    del _TestHashers

    HashersTest = skipUnless(TEST_MODE("default"),
                             "requires >= 'default' test mode")(HashersTest)

#=========================================================
#eof
#=========================================================
    # NOTE: the following have been cloned from _bcrypt_test()

    def populate_settings(self, kwds):
        # speed up test w/ lower rounds
        kwds.setdefault("rounds", 4)
        super(django_bcrypt_test, self).populate_settings(kwds)

    def fuzz_setting_rounds(self):
        # decrease default rounds for fuzz testing to speed up volume.
        return randintgauss(5, 8, 6, 1)

    def fuzz_setting_ident(self):
        # omit multi-ident tests, only $2a$ counts for this class
        return None

django_bcrypt_test = skipUnless(hash.bcrypt.has_backend(),
                                "no bcrypt backends available")(django_bcrypt_test)

class django_bcrypt_sha256_test(HandlerCase, _DjangoHelper):
    """test django_bcrypt_sha256"""
    handler = hash.django_bcrypt_sha256
    min_django_version = (1,6)
    forbidden_characters = None
    fuzz_salts_need_bcrypt_repair = True

    known_correct_hashes = [
        #
        # custom - generated via django 1.6 hasher
        #
        ('',
            'bcrypt_sha256$$2a$06$/3OeRpbOf8/l6nPPRdZPp.nRiyYqPobEZGdNRBWihQhiFDh1ws1tu'),
        (UPASS_LETMEIN,
    def populate_settings(self, kwds):
        # speed up test w/ lower rounds
        kwds.setdefault("rounds", 4)
        super(django_bcrypt_test, self).populate_settings(kwds)

    def fuzz_setting_rounds(self):
        # decrease default rounds for fuzz testing to speed up volume.
        return randintgauss(5, 8, 6, 1)

    def fuzz_setting_ident(self):
        # omit multi-ident tests, only $2a$ counts for this class
        return None


django_bcrypt_test = skipUnless(
    hash.bcrypt.has_backend(),
    "no bcrypt backends available")(django_bcrypt_test)


class django_bcrypt_sha256_test(HandlerCase, _DjangoHelper):
    """test django_bcrypt_sha256"""
    handler = hash.django_bcrypt_sha256
    min_django_version = (1, 6)
    forbidden_characters = None
    fuzz_salts_need_bcrypt_repair = True

    known_correct_hashes = [
        #
        # custom - generated via django 1.6 hasher
        #
        ('',