Source

django-scrypt / tests / test_subclassing.py

# -*- coding: utf-8 -*-

from __future__ import with_statement
import time
from django.conf.global_settings import PASSWORD_HASHERS as default_hashers
from django.contrib.auth.hashers import (
    is_password_usable, check_password, make_password,
    get_hasher, load_hashers, UNUSABLE_PASSWORD)
from django.utils.unittest import TestCase, skipUnless

from django_scrypt.hashers import ScryptPasswordHasher

try:
    import scrypt
except ImportError:
    scrypt = None


@skipUnless(scrypt, "Uninstalled scrypt module needed to generate hash")
class TestBigMemNScryptHasher(TestCase):

    def setUp(self):
        scrypt_hashers = ("tests.test_subclassing.BigMemNScryptHasher",
                          "django_scrypt.hashers.ScryptPasswordHasher") + default_hashers
        load_hashers(password_hashers=scrypt_hashers)
        self.password = 'letmein'

    def test_BigMemScryptHasher(self):
        """Test operation of the extended bigmem hasher"""
        encoded = make_password(self.password, hasher='scrypt_N')
        self.assertTrue(check_password(self.password, encoded))


@skipUnless(scrypt, "Uninstalled scrypt module needed to generate hash")
class TestBigMemScryptHasher(TestCase):

    def setUp(self):
        scrypt_hashers = ("tests.test_subclassing.BigMemScryptHasher",
                          "django_scrypt.hashers.ScryptPasswordHasher") + default_hashers
        load_hashers(password_hashers=scrypt_hashers)
        self.password = 'letmein'

    def test_BigMemScryptHasher(self):
        """Test operation of the extended bigmem hasher"""
        encoded = make_password(self.password, hasher='scrypt_r')
        self.assertTrue(check_password(self.password, encoded))


@skipUnless(scrypt, "Uninstalled scrypt module needed to generate hash")
class TestLongTimeScryptHasher(TestCase):

    def setUp(self):
        scrypt_hashers = ("tests.test_subclassing.LongTimeScryptHasher",
                          "django_scrypt.hashers.ScryptPasswordHasher") + default_hashers
        load_hashers(password_hashers=scrypt_hashers)
        self.password = 'letmein'

    def test_LongTimeScryptHasher(self):
        """Test operation of the extended time hasher that uses 16MB"""
        encoded = make_password(self.password, hasher='scrypt_p')
        self.assertTrue(check_password(self.password, encoded))

    def test_LongTimeScryptHasher_takes_long_time(self):
        """Test operation of the extended time hasher uses more time than default"""
        start = time.clock()
        encoded = make_password(self.password, hasher='scrypt')
        self.assertTrue(check_password(self.password, encoded))
        elapsed1 = (time.clock() - start)

        start = time.clock()
        encoded = make_password(self.password, hasher='scrypt_p')
        self.assertTrue(check_password(self.password, encoded))
        elapsed2 = (time.clock() - start)

        self.assertTrue(elapsed1 < elapsed2)


class LongTimeScryptHasher(ScryptPasswordHasher):
    """This hasher is tuned for longer duration"""
    algorithm = "scrypt_p"
    p = 3


class BigMemScryptHasher(ScryptPasswordHasher):
    """This hasher is tuned to use lots of memory
    (128 * 2 ** 14 * 18) ==  37748736 or ~36mb
    """
    algorithm = "scrypt_r"
    r = 18


class BigMemNScryptHasher(ScryptPasswordHasher):
    """This hasher is tuned to use lots of memory
    (128 * 2 ** 15 * 8) == 33554432 or ~32mb
    """
    algorithm = "scrypt_N"
    N = 15
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.