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(), }
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)
def test_validate_tuple(self): val = DigestValue(b'salt', b'digest', hashlib.md5) field = ChallengeField('md5') assert field._validate(None, val) is val
def test_valid_default(self): val = ChallengeField('md5', default=DigestValue.create('digest', hashlib.md5))
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
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)
def test_create_no_salt(self): msg = 'message' val = DigestValue.create(msg, hashlib.md5) assert len(val.salt) == hashlib.md5().digest_size
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')
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')
def test_parse_invalid(self): with pytest.raises(ValueError): DigestValue.parse('asdf', hashlib.md5)
def test_parse(self): val = DigestValue(b'salt', b'digest', hashlib.sha256) val2 = DigestValue.parse(str(val), hashlib.sha256) assert val == val2
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
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())