Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed PseudoFuncInput and PseudoFuncOutput to only carry a single value #38

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<github.repository>statisticsnorway/dapla-dlp-pseudo-core</github.repository>

<!-- Dependency versions -->
<dapla-dlp-pseudo-func.version>1.3.0</dapla-dlp-pseudo-func.version>
<dapla-dlp-pseudo-func.version>1.3.1-SNAPSHOT</dapla-dlp-pseudo-func.version>
<guava.version>32.0.0-jre</guava.version>
<jsonassert.version>1.5.1</jsonassert.version>
<logback.version>1.4.6</logback.version>
Expand Down
19 changes: 10 additions & 9 deletions src/test/java/no/ssb/dlp/pseudo/core/func/DaeadFuncTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package no.ssb.dlp.pseudo.core.func;

import com.google.common.collect.ImmutableList;
import com.google.crypto.tink.CleartextKeysetHandle;
import com.google.crypto.tink.DeterministicAead;
import com.google.crypto.tink.JsonKeysetReader;
import com.google.crypto.tink.KeysetHandle;
import com.google.crypto.tink.daead.DeterministicAeadConfig;
import no.ssb.dapla.dlp.pseudo.func.*;
import no.ssb.dapla.dlp.pseudo.func.PseudoFunc;
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncConfig;
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncFactory;
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncInput;
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncOutput;
import no.ssb.dapla.dlp.pseudo.func.tink.daead.TinkDaeadFuncConfig;
import no.ssb.dapla.dlp.pseudo.func.tink.fpe.TinkFpeFuncConfig;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.*;

public class DaeadFuncTest {

Expand Down Expand Up @@ -45,13 +48,11 @@ private PseudoFunc f(String funcDecl) throws Exception {
return PseudoFuncFactory.create(config);
}

private void transformAndRestore(Object originalVal, Object expectedVal, PseudoFunc func) {
Iterable expectedElements = (expectedVal instanceof Iterable) ? (Iterable) expectedVal : ImmutableList.of(expectedVal);
Iterable originalElements = (originalVal instanceof Iterable) ? (Iterable) originalVal : ImmutableList.of(originalVal);
private void transformAndRestore(String originalVal, String expectedVal, PseudoFunc func) {
PseudoFuncOutput pseudonymized = func.apply(PseudoFuncInput.of(originalVal));
assertThat(pseudonymized.getValues()).containsExactlyElementsOf(expectedElements);
PseudoFuncOutput depseudonymized = func.restore(PseudoFuncInput.of(pseudonymized.getValues()));
assertThat(depseudonymized.getValues()).containsExactlyElementsOf(originalElements);
assertThat(pseudonymized.getValue()).isEqualTo(expectedVal);
PseudoFuncOutput depseudonymized = func.restore(PseudoFuncInput.of(pseudonymized.getValue()));
assertThat(depseudonymized.getValue()).isEqualTo(originalVal);
}

@Test
Expand Down
28 changes: 14 additions & 14 deletions src/test/java/no/ssb/dlp/pseudo/core/func/Ff31FuncTest.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package no.ssb.dlp.pseudo.core.func;

import com.google.common.collect.ImmutableList;
import com.google.crypto.tink.CleartextKeysetHandle;
import com.google.crypto.tink.JsonKeysetReader;
import com.google.crypto.tink.KeysetHandle;
import no.ssb.crypto.tink.fpe.Fpe;
import no.ssb.crypto.tink.fpe.FpeConfig;
import no.ssb.crypto.tink.fpe.IncompatiblePlaintextException;
import no.ssb.dapla.dlp.pseudo.func.*;
import no.ssb.dapla.dlp.pseudo.func.PseudoFunc;
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncConfig;
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncFactory;
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncInput;
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncOutput;
import no.ssb.dapla.dlp.pseudo.func.tink.fpe.TinkFpeFuncConfig;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.*;

public class Ff31FuncTest {

Expand Down Expand Up @@ -46,13 +48,11 @@ private PseudoFunc f(String funcDecl) throws Exception {
return PseudoFuncFactory.create(config);
}

private void transformAndRestore(Object originalVal, Object expectedVal, PseudoFunc func) {
Iterable expectedElements = (expectedVal instanceof Iterable) ? (Iterable) expectedVal : ImmutableList.of(expectedVal);
Iterable originalElements = (originalVal instanceof Iterable) ? (Iterable) originalVal : ImmutableList.of(originalVal);
private void transformAndRestore(String originalVal, String expectedVal, PseudoFunc func) {
PseudoFuncOutput pseudonymized = func.apply(PseudoFuncInput.of(originalVal));
assertThat(pseudonymized.getValues()).containsExactlyElementsOf(expectedElements);
PseudoFuncOutput depseudonymized = func.restore(PseudoFuncInput.of(pseudonymized.getValues()));
assertThat(depseudonymized.getValues()).containsExactlyElementsOf(originalElements);
assertThat(pseudonymized.getValue()).isEqualTo(expectedVal);
PseudoFuncOutput depseudonymized = func.restore(PseudoFuncInput.of(pseudonymized.getValue()));
assertThat(depseudonymized.getValue()).isEqualTo(originalVal);
}

@Test
Expand Down Expand Up @@ -82,10 +82,10 @@ void givenText_ff31Delete_shouldEncryptAndDecrypt() throws Exception {
PseudoFunc func = f(funcDeclStr);

PseudoFuncOutput pseudonymized = func.apply(PseudoFuncInput.of("Ken sent me..."));
assertThat(pseudonymized.getFirstValue()).isEqualTo("6DyNHKvig");
assertThat(pseudonymized.getValue()).isEqualTo("6DyNHKvig");

PseudoFuncOutput depseudonymized = func.restore(PseudoFuncInput.of("6DyNHKvig"));
assertThat(depseudonymized.getFirstValue()).isEqualTo("Kensentme");
assertThat(depseudonymized.getValue()).isEqualTo("Kensentme");
}

@Test
Expand All @@ -94,10 +94,10 @@ void givenText_ff31Redact_shouldEncryptAndDecrypt() throws Exception {
PseudoFunc func = f(funcDeclStr);

PseudoFuncOutput pseudonymized = func.apply(PseudoFuncInput.of("Ken sent me..."));
assertThat(pseudonymized.getFirstValue()).isEqualTo("3WD8UlZRDER1z5");
assertThat(pseudonymized.getValue()).isEqualTo("3WD8UlZRDER1z5");

PseudoFuncOutput depseudonymized = func.restore(PseudoFuncInput.of("3WD8UlZRDER1z5"));
assertThat(depseudonymized.getFirstValue()).isEqualTo("KenZsentZmeZZZ");
assertThat(depseudonymized.getValue()).isEqualTo("KenZsentZmeZZZ");
}


Expand Down
30 changes: 15 additions & 15 deletions src/test/java/no/ssb/dlp/pseudo/core/func/LegacyFpeFuncTest.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
package no.ssb.dlp.pseudo.core.func;

import com.google.common.collect.ImmutableList;
import no.ssb.dapla.dlp.pseudo.func.*;
import no.ssb.dapla.dlp.pseudo.func.PseudoFunc;
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncConfig;
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncFactory;
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncInput;
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncOutput;
import no.ssb.dapla.dlp.pseudo.func.fpe.FpeFunc;
import no.ssb.dapla.dlp.pseudo.func.fpe.FpeFuncConfig;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.*;

public class LegacyFpeFuncTest {

private final static Map<String, String> KEYSETS = Map.of(
"secret1", "C5sn7B4YtwcilAwuVx6NuAsMWLusOSA/ldia40ZugDI="
);

private PseudoFunc f(String funcDecl) throws Exception {
private PseudoFunc f(String funcDecl) {
PseudoFuncConfig config = PseudoFuncConfigFactory.get(funcDecl);
String keyId = config.getRequired(FpeFuncConfig.Param.KEY_ID, String.class);
config.add(FpeFuncConfig.Param.KEY_DATA, KEYSETS.get(keyId));
return PseudoFuncFactory.create(config);
}

private void transformAndRestore(Object originalVal, Object expectedVal, PseudoFunc func) {
Iterable expectedElements = (expectedVal instanceof Iterable) ? (Iterable) expectedVal : ImmutableList.of(expectedVal);
Iterable originalElements = (originalVal instanceof Iterable) ? (Iterable) originalVal : ImmutableList.of(originalVal);
private void transformAndRestore(String originalVal, String expectedVal, PseudoFunc func) {
PseudoFuncOutput pseudonymized = func.apply(PseudoFuncInput.of(originalVal));
assertThat(pseudonymized.getValues()).containsExactlyElementsOf(expectedElements);
PseudoFuncOutput depseudonymized = func.restore(PseudoFuncInput.of(pseudonymized.getValues()));
assertThat(depseudonymized.getValues()).containsExactlyElementsOf(originalElements);
assertThat(pseudonymized.getValue()).isEqualTo(expectedVal);
PseudoFuncOutput depseudonymized = func.restore(PseudoFuncInput.of(pseudonymized.getValue()));
assertThat(depseudonymized.getValue()).isEqualTo(originalVal);
}

@Test
void givenText_fpeAnychar_shouldEncryptAndDecrypt() throws Exception {
void givenText_fpeAnychar_shouldEncryptAndDecrypt() {
String funcDeclStr = "fpe-anychar(keyId=secret1)";
transformAndRestore("Something", "-Æ'GÕT@«L", f(funcDeclStr));
}

@Test
void givenText_fpeCustomAlphabet_shouldEncryptAndDecrypt() throws Exception {
void givenText_fpeCustomAlphabet_shouldEncryptAndDecrypt() {
String funcDeclStr = "fpe-abcdefghij(keyId=secret1)";
transformAndRestore("abcdef", "djcjbf", f(funcDeclStr));
}

@Test
void givenNonAlphabetText_fpeCustomAlphabet_shouldFail() throws Exception {
void givenNonAlphabetText_fpeCustomAlphabet_shouldFail() {
String funcDeclStr = "fpe-abcdefghij(keyId=secret1, replaceIllegalChars=false, replaceIllegalCharsWith=X)";
assertThatThrownBy(() -> {
f(funcDeclStr).apply(PseudoFuncInput.of("abcHELLO"));
Expand All @@ -56,7 +56,7 @@ void givenNonAlphabetText_fpeCustomAlphabet_shouldFail() throws Exception {
}

@Test
void givenDigits_fpeDigits_shouldEncryptAndDecrypt() throws Exception {
void givenDigits_fpeDigits_shouldEncryptAndDecrypt() {
String funcDeclStr = "fpe-digits(keyId=secret1)";
transformAndRestore("1234567890", "7830880047", f(funcDeclStr));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public String getAlgorithm() {

@Override
public PseudoFuncOutput apply(PseudoFuncInput input) {
return new PseudoFuncOutput();
return new PseudoFuncOutput(null);
}

@Override
public PseudoFuncOutput restore(PseudoFuncInput input) {
return new PseudoFuncOutput();
return new PseudoFuncOutput(null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private PseudoFunc f(String funcDecl) {
}

static void assertEqual(PseudoFuncOutput out, Object expected) {
assertThat(out.getFirstValue()).isEqualTo(expected);
assertThat(out.getValue()).isEqualTo(expected);
}

@Test
Expand Down
Loading