refactor: подтянуть Legal NER, переформатировать код и добавить тесты
Слияние с 685ec97 (третья ступень NER для юридических реквизитов),
код приведён к google-java-format, добавлены юнит-тесты
AdaptiveConcurrencyLimiter/SystemsConfig/PayloadCipher.
This commit is contained in:
@@ -1,50 +1,54 @@
|
||||
package ru.pdguard;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import ru.pdguard.core.PayloadCipher;
|
||||
import ru.pdguard.core.PayloadStore;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
/** Шифрование персональных данных в хранилище. */
|
||||
class PayloadCipherTest {
|
||||
|
||||
/** 32 байта в hex — валидный AES-256 ключ. */
|
||||
private static final String KEY = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
|
||||
/** 32 байта в hex — валидный AES-256 ключ. */
|
||||
private static final String KEY =
|
||||
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
|
||||
|
||||
@Test
|
||||
void encryptDecryptRoundTrip() {
|
||||
PayloadCipher cipher = new PayloadCipher(KEY);
|
||||
String original = "Клиент Иванов Иван Иванович, паспорт 4509 123456";
|
||||
String encrypted = cipher.encrypt(original);
|
||||
assertNotEquals(original, encrypted, "шифротекст не должен совпадать с исходником");
|
||||
assertEquals(original, cipher.decrypt(encrypted), "должно расшифроваться обратно");
|
||||
}
|
||||
@Test
|
||||
void encryptDecryptRoundTrip() {
|
||||
PayloadCipher cipher = new PayloadCipher(KEY);
|
||||
String original = "Клиент Иванов Иван Иванович, паспорт 4509 123456";
|
||||
String encrypted = cipher.encrypt(original);
|
||||
assertNotEquals(original, encrypted, "шифротекст не должен совпадать с исходником");
|
||||
assertEquals(original, cipher.decrypt(encrypted), "должно расшифроваться обратно");
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledCipherPassesThrough() {
|
||||
PayloadCipher cipher = PayloadCipher.disabled();
|
||||
String original = "Клиент Иванов";
|
||||
assertEquals(original, cipher.encrypt(original), "без ключа шифрование выключено");
|
||||
assertEquals(original, cipher.decrypt(original), "без ключа дешифрование выключено");
|
||||
}
|
||||
@Test
|
||||
void disabledCipherPassesThrough() {
|
||||
PayloadCipher cipher = PayloadCipher.disabled();
|
||||
String original = "Клиент Иванов";
|
||||
assertEquals(original, cipher.encrypt(original), "без ключа шифрование выключено");
|
||||
assertEquals(original, cipher.decrypt(original), "без ключа дешифрование выключено");
|
||||
}
|
||||
|
||||
@Test
|
||||
void storeStoresEncryptedButReturnsPlaintext() {
|
||||
PayloadCipher cipher = new PayloadCipher(KEY);
|
||||
PayloadStore store = new PayloadStore(1_000_000L, 30, ru.pdguard.core.SharedIndex.disabled(), cipher);
|
||||
@Test
|
||||
void storeStoresEncryptedButReturnsPlaintext() {
|
||||
PayloadCipher cipher = new PayloadCipher(KEY);
|
||||
PayloadStore store =
|
||||
new PayloadStore(1_000_000L, 30, ru.pdguard.core.SharedIndex.disabled(), cipher);
|
||||
|
||||
String original = "Клиент Иванов Иван Иванович, паспорт 4509 123456";
|
||||
String masked = "Клиент И. И. И., паспорт 45** ****56";
|
||||
store.put("test", "id-1", original, masked);
|
||||
String original = "Клиент Иванов Иван Иванович, паспорт 4509 123456";
|
||||
String masked = "Клиент И. И. И., паспорт 45** ****56";
|
||||
store.put("test", "id-1", original, masked);
|
||||
|
||||
// Чтение по id возвращает исходный текст.
|
||||
PayloadStore.Entry entry = store.byId("test", "id-1");
|
||||
assertEquals(original, entry.original(), "чтение по id должно вернуть исходный текст");
|
||||
// Чтение по id возвращает исходный текст.
|
||||
PayloadStore.Entry entry = store.byId("test", "id-1");
|
||||
assertEquals(original, entry.original(), "чтение по id должно вернуть исходный текст");
|
||||
|
||||
// Чтение по маске возвращает исходный текст.
|
||||
assertEquals(original, store.originalForMask("test", masked),
|
||||
"чтение по маске должно вернуть исходный текст");
|
||||
}
|
||||
}
|
||||
// Чтение по маске возвращает исходный текст.
|
||||
assertEquals(
|
||||
original,
|
||||
store.originalForMask("test", masked),
|
||||
"чтение по маске должно вернуть исходный текст");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user