Exemplo n.º 1
0
 def test_to_basic(self):
     field = ChallengeField('md5')
     assert field.to_basic(
         MockConfig(), DigestValue(b'salt', b'digest', hashlib.md5)) == {
             'salt': base64.b64encode(b'salt').decode(),
             'digest': base64.b64encode(b'digest').decode(),
         }
Exemplo n.º 2
0
 def test_create_trunc_salt(self):
     msg = 'message'
     digest_size = hashlib.sha1().digest_size
     salt = b'x' * (digest_size + 10)
     val = DigestValue.create(msg, hashlib.sha1, salt=salt)
     assert len(val.salt) == digest_size
     assert val.salt == (b'x' * digest_size)
Exemplo n.º 3
0
 def test_validate_tuple(self):
     val = DigestValue(b'salt', b'digest', hashlib.md5)
     field = ChallengeField('md5')
     assert field._validate(None, val) is val
Exemplo n.º 4
0
 def test_valid_default(self):
     val = ChallengeField('md5', default=DigestValue.create('digest', hashlib.md5))
Exemplo n.º 5
0
 def test_create_salt_exact(self):
     msg = 'message'
     digest_size = hashlib.sha256().digest_size
     salt = b'x' * digest_size
     val = DigestValue.create(msg, hashlib.sha256, salt=salt)
     assert val.salt == salt
Exemplo n.º 6
0
 def test_create_short_salt(self):
     msg = b'message'
     digest_size = hashlib.sha256().digest_size
     salt = b'x' * (digest_size - 1)
     with pytest.raises(TypeError):
         DigestValue.create(msg, hashlib.sha256, salt=salt)
Exemplo n.º 7
0
 def test_create_no_salt(self):
     msg = 'message'
     val = DigestValue.create(msg, hashlib.md5)
     assert len(val.salt) == hashlib.md5().digest_size
Exemplo n.º 8
0
 def test_challenge_failed(self):
     salt = b'salt'
     digest = hashlib.md5(b'message').digest()
     val = DigestValue(salt, digest, hashlib.md5)
     with pytest.raises(ValueError):
         val.challenge(b'm3ssage')
Exemplo n.º 9
0
 def test_challenge_success(self):
     salt = b'salt'
     digest = hashlib.md5(b'saltmessage').digest()
     val = DigestValue(salt, digest, hashlib.md5)
     val.challenge('message')
     val.challenge(b'message')
Exemplo n.º 10
0
 def test_parse_invalid(self):
     with pytest.raises(ValueError):
         DigestValue.parse('asdf', hashlib.md5)
Exemplo n.º 11
0
    def test_parse(self):
        val = DigestValue(b'salt', b'digest', hashlib.sha256)
        val2 = DigestValue.parse(str(val), hashlib.sha256)

        assert val == val2
Exemplo n.º 12
0
 def test_set_default_tuple(self):
     sdt = DigestValue.create('hello', hashlib.md5)
     field = ChallengeField('md5', key='test', default=sdt)
     cfg = BaseConfig(BaseSchema())
     field.__setdefault__(cfg)
     assert cfg._data['test'] is sdt
Exemplo n.º 13
0
 def test_str(self):
     salt = base64.b64encode(b'salt')
     digest = base64.b64encode(b'digest')
     val = DigestValue(b'salt', b'digest', hashlib.sha256)
     assert str(val) == "%s:%s" % (salt.decode(), digest.decode())