|
| 1 | +from datetime import date, datetime |
| 2 | +from decimal import Decimal |
| 3 | +from uuid import uuid4 |
| 4 | + |
| 5 | +from django.db import connection |
| 6 | +from django.utils.encoding import force_bytes |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from example_app import models |
| 11 | + |
| 12 | + |
| 13 | +def _fetch_raw_bytes(model_cls, field_name, pk): |
| 14 | + with connection.cursor() as cursor: |
| 15 | + cursor.execute( |
| 16 | + f"SELECT {field_name} FROM {model_cls._meta.db_table} WHERE id = %s", |
| 17 | + [pk], |
| 18 | + ) |
| 19 | + value = cursor.fetchone()[0] |
| 20 | + return bytes(value) |
| 21 | + |
| 22 | + |
| 23 | +def _update_raw_bytes(model_cls, field_name, pk, raw_bytes): |
| 24 | + with connection.cursor() as cursor: |
| 25 | + cursor.execute( |
| 26 | + f"UPDATE {model_cls._meta.db_table} SET {field_name} = %s WHERE id = %s", |
| 27 | + [raw_bytes, pk], |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.django_db |
| 32 | +def test_round_trip_and_ciphertext_at_rest(): |
| 33 | + payload = b"binary payload \x00\x01\x02" |
| 34 | + instance = models.EncryptedSample.objects.create( |
| 35 | + name="Alice", |
| 36 | + bio="Encrypted bio", |
| 37 | + |
| 38 | + count=42, |
| 39 | + birth_date=date(1990, 1, 1), |
| 40 | + created_at=datetime(2024, 1, 1, 12, 0, 0), |
| 41 | + payload=payload, |
| 42 | + ) |
| 43 | + instance.refresh_from_db() |
| 44 | + |
| 45 | + assert instance.name == "Alice" |
| 46 | + assert instance.bio == "Encrypted bio" |
| 47 | + assert instance. email == "[email protected]" |
| 48 | + assert instance.count == 42 |
| 49 | + assert instance.birth_date == date(1990, 1, 1) |
| 50 | + assert instance.created_at == datetime(2024, 1, 1, 12, 0, 0) |
| 51 | + assert instance.payload == payload |
| 52 | + |
| 53 | + raw_name = _fetch_raw_bytes(models.EncryptedSample, "name", instance.id) |
| 54 | + raw_payload = _fetch_raw_bytes(models.EncryptedSample, "payload", instance.id) |
| 55 | + assert raw_name != force_bytes("Alice") |
| 56 | + assert raw_payload != payload |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.django_db |
| 60 | +def test_tamper_detection(): |
| 61 | + instance = models.EncryptedSample.objects.create( |
| 62 | + name="Tamper", |
| 63 | + bio="Test", |
| 64 | + |
| 65 | + count=7, |
| 66 | + birth_date=date(2000, 2, 2), |
| 67 | + created_at=datetime(2024, 2, 2, 10, 0, 0), |
| 68 | + ) |
| 69 | + raw_name = _fetch_raw_bytes(models.EncryptedSample, "name", instance.id) |
| 70 | + tampered = bytearray(raw_name) |
| 71 | + tampered[0] = (tampered[0] + 1) % 256 |
| 72 | + _update_raw_bytes(models.EncryptedSample, "name", instance.id, bytes(tampered)) |
| 73 | + |
| 74 | + with pytest.raises(Exception): |
| 75 | + instance.refresh_from_db() |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.django_db |
| 79 | +def test_deterministic_ciphertext_and_lookup(): |
| 80 | + first = models.DeterministicSample.objects.create( |
| 81 | + keyword="repeat", |
| 82 | + |
| 83 | + count=5, |
| 84 | + ) |
| 85 | + second = models.DeterministicSample.objects.create( |
| 86 | + keyword="repeat", |
| 87 | + |
| 88 | + count=5, |
| 89 | + ) |
| 90 | + raw_first = _fetch_raw_bytes(models.DeterministicSample, "keyword", first.id) |
| 91 | + raw_second = _fetch_raw_bytes(models.DeterministicSample, "keyword", second.id) |
| 92 | + assert raw_first == raw_second |
| 93 | + |
| 94 | + matches = models.DeterministicSample.objects.filter(keyword="repeat") |
| 95 | + assert matches.count() == 2 |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.django_db |
| 99 | +def test_aad_enforced(): |
| 100 | + instance = models.EncryptedWithAad.objects.create(secret="aad-secret") |
| 101 | + raw_secret = _fetch_raw_bytes(models.EncryptedWithAad, "secret", instance.id) |
| 102 | + |
| 103 | + field = models.EncryptedWithAad._meta.get_field("secret") |
| 104 | + with pytest.raises(Exception): |
| 105 | + field._keyset_manager.aead_primitive.decrypt(raw_secret, b"wrong-aad") |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.django_db |
| 109 | +def test_extended_fields_round_trip(): |
| 110 | + token = uuid4() |
| 111 | + payload = {"alpha": 1, "beta": {"gamma": "delta"}, "items": [1, 2, 3]} |
| 112 | + instance = models.ExtendedEncryptedSample.objects.create( |
| 113 | + flag=True, |
| 114 | + ratio=3.14, |
| 115 | + amount=Decimal("1234.50"), |
| 116 | + token=token, |
| 117 | + payload=payload, |
| 118 | + url="https://example.com/alpha", |
| 119 | + ) |
| 120 | + instance.refresh_from_db() |
| 121 | + |
| 122 | + assert instance.flag is True |
| 123 | + assert instance.ratio == 3.14 |
| 124 | + assert instance.amount == Decimal("1234.50") |
| 125 | + assert instance.token == token |
| 126 | + assert instance.payload == payload |
| 127 | + assert instance.url == "https://example.com/alpha" |
| 128 | + |
| 129 | + raw_payload = _fetch_raw_bytes(models.ExtendedEncryptedSample, "payload", instance.id) |
| 130 | + assert raw_payload != force_bytes(payload) |
| 131 | + |
| 132 | + |
| 133 | +@pytest.mark.django_db |
| 134 | +def test_deterministic_uuid_ciphertext(): |
| 135 | + token = uuid4() |
| 136 | + first = models.DeterministicExtendedSample.objects.create(token=token) |
| 137 | + second = models.DeterministicExtendedSample.objects.create(token=token) |
| 138 | + |
| 139 | + raw_first = _fetch_raw_bytes(models.DeterministicExtendedSample, "token", first.id) |
| 140 | + raw_second = _fetch_raw_bytes(models.DeterministicExtendedSample, "token", second.id) |
| 141 | + assert raw_first == raw_second |
0 commit comments