def test_crypto_cache_entry_setattr():
    entry = CryptoMaterialsCacheEntry(**_VALID_KWARGS["CryptoMaterialsCacheEntry"])

    with pytest.raises(NotSupportedError) as excinfo:
        entry.bytes_encrypted = 0

    excinfo.match(r"Attributes may not be set on CryptoMaterialsCacheEntry objects")
def test_crypto_cache_entry_is_too_old_no_lifetime_hint(patch_time):
    kwargs = _VALID_KWARGS['CryptoMaterialsCacheEntry'].copy()
    kwargs['hints'] = MagicMock(__class__=CryptoMaterialsCacheEntryHints,
                                lifetime=None)
    entry = CryptoMaterialsCacheEntry(**kwargs)

    assert not entry.is_too_old()
def test_crypto_cache_entry_invalidate():
    entry = CryptoMaterialsCacheEntry(**_VALID_KWARGS["CryptoMaterialsCacheEntry"])

    assert entry.valid

    entry.invalidate()

    assert not entry.valid
def test_crypto_cache_entry_update_with_message_bytes_encrypted():
    entry = CryptoMaterialsCacheEntry(**_VALID_KWARGS["CryptoMaterialsCacheEntry"])
    super(CryptoMaterialsCacheEntry, entry).__setattr__("messages_encrypted", 10)
    super(CryptoMaterialsCacheEntry, entry).__setattr__("bytes_encrypted", 10)

    entry._update_with_message_bytes_encrypted(50)

    assert entry.messages_encrypted == 11
    assert entry.bytes_encrypted == 60
def test_crypto_cache_entry_is_too_old(patch_time, age_modifier, result):
    lifetime = patch_time + age_modifier
    kwargs = _VALID_KWARGS['CryptoMaterialsCacheEntry'].copy()
    kwargs['hints'] = MagicMock(__class__=CryptoMaterialsCacheEntryHints,
                                lifetime=lifetime)
    entry = CryptoMaterialsCacheEntry(**kwargs)

    if result:
        assert entry.is_too_old()
    else:
        assert not entry.is_too_old()
def test_crypto_cache_entry_is_too_old(patch_time, age_modifier, result):
    mock_creation_time = 1
    aws_encryption_sdk.caches.time.time.return_value = mock_creation_time
    lifetime = mock_creation_time + age_modifier
    kwargs = _VALID_KWARGS["CryptoMaterialsCacheEntry"].copy()
    kwargs["hints"] = MagicMock(__class__=CryptoMaterialsCacheEntryHints, lifetime=lifetime)
    entry = CryptoMaterialsCacheEntry(**kwargs)
    assert entry.creation_time == mock_creation_time

    aws_encryption_sdk.caches.time.time.return_value = mock_creation_time + 1
    if result:
        assert entry.is_too_old()
    else:
        assert not entry.is_too_old()
def test_crypto_cache_entry_age(patch_time):
    mock_creation_time = 5
    mock_check_time = 10
    aws_encryption_sdk.caches.time.time.return_value = mock_creation_time
    entry = CryptoMaterialsCacheEntry(**_VALID_KWARGS["CryptoMaterialsCacheEntry"])
    aws_encryption_sdk.caches.time.time.return_value = mock_check_time

    assert entry.age == mock_check_time - mock_creation_time
예제 #8
0
def test_put_decryption_materials():
    cache_key = b'ex_cache_key'
    value = MagicMock(__class__=DecryptionMaterials)
    check_value = CryptoMaterialsCacheEntry(cache_key=cache_key, value=value)
    cache = NullCryptoMaterialsCache()

    test = cache.put_decryption_materials(cache_key=cache_key,
                                          decryption_materials=value)

    assert test == check_value
예제 #9
0
def test_put_encryption_materials():
    cache_key = b'ex_cache_key'
    value = MagicMock(__class__=EncryptionMaterials)
    check_value = CryptoMaterialsCacheEntry(cache_key=cache_key, value=value)
    cache = NullCryptoMaterialsCache()

    test = cache.put_encryption_materials(cache_key=cache_key,
                                          encryption_materials=value,
                                          plaintext_length=0,
                                          entry_hints=None)

    assert test == check_value
def test_crypto_cache_entry_init(patch_time):
    entry = CryptoMaterialsCacheEntry(
        cache_key=b'ex_cache_key',
        value=MagicMock(__class__=EncryptionMaterials),
        hints=CryptoMaterialsCacheEntryHints(lifetime=10.0))

    assert entry.creation_time == 3.0
    assert entry.bytes_encrypted == 0
    assert entry.messages_encrypted == 0
    assert entry.valid
    # Because Lock is a helper function that returns whatever native primitive is best,
    #  we cannot test for type.  Instead, testing for interface.
    assert hasattr(entry._lock, 'acquire')
    assert callable(entry._lock.acquire)
    assert hasattr(entry._lock, 'release')
    assert callable(entry._lock.release)
def test_crypto_cache_entry_age(patch_time):
    entry = CryptoMaterialsCacheEntry(
        **_VALID_KWARGS['CryptoMaterialsCacheEntry'])

    assert entry.age == patch_time
def test_crypto_cache_entry_defaults():
    entry = CryptoMaterialsCacheEntry(
        cache_key=b'ex_cache_key',
        value=MagicMock(__class__=EncryptionMaterials))

    assert entry.hints == CryptoMaterialsCacheEntryHints()
def test_crypto_cache_entry_valid_attributes(valid_kwargs_overrides):
    kwargs = _VALID_KWARGS['CryptoMaterialsCacheEntry'].copy()
    kwargs.update(valid_kwargs_overrides)
    CryptoMaterialsCacheEntry(**kwargs)