Skip to content

Commit

Permalink
Added number masking fuzzing testing against Jackson
Browse files Browse the repository at this point in the history
  • Loading branch information
Breus committed Dec 30, 2023
1 parent f1c528d commit d4d36c4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/test/java/dev/blaauwendraad/masker/json/FuzzingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

final class FuzzingTest {
private static final Set<String> DEFAULT_TARGET_KEYS = Set.of("targetKey1", "targetKey2", "targetKey3");
private static final int SECONDS_FOR_EACH_TEST_TO_RUN = 2;
private static final int SECONDS_FOR_EACH_TEST_TO_RUN = 3;

@ParameterizedTest
@MethodSource("jsonMaskingConfigs")
Expand All @@ -32,22 +32,31 @@ void fuzzingAgainstParseAndMaskUsingJackson(JsonMaskingConfig jsonMaskingConfig)
String randomJsonNodeString = randomJsonNode.toPrettyString();
String keyContainsOutput = masker.mask(randomJsonNodeString);
String jacksonMaskingOutput = ParseAndMaskUtil.mask(randomJsonNode, jsonMaskingConfig).toPrettyString();
Assertions.assertEquals(jacksonMaskingOutput,
keyContainsOutput,
"Failed for input: " + randomJsonNodeString
Assertions.assertEquals(
jacksonMaskingOutput,
keyContainsOutput,
"Failed for input: " + randomJsonNodeString
);
randomTestExecuted++;
}
System.out.printf("Executed %d randomly generated test scenarios in %d seconds%n",
randomTestExecuted,
SECONDS_FOR_EACH_TEST_TO_RUN
System.out.printf(
"Executed %d randomly generated test scenarios in %d seconds%n",
randomTestExecuted,
SECONDS_FOR_EACH_TEST_TO_RUN
);
}

@Nonnull
private static Stream<JsonMaskingConfig> jsonMaskingConfigs() {
return Stream.of(JsonMaskingConfig.getDefault(DEFAULT_TARGET_KEYS),
JsonMaskingConfig.custom(DEFAULT_TARGET_KEYS, JsonMaskingConfig.TargetKeyMode.ALLOW).build()
return Stream.of(
JsonMaskingConfig.getDefault(DEFAULT_TARGET_KEYS),
JsonMaskingConfig.custom(DEFAULT_TARGET_KEYS, JsonMaskingConfig.TargetKeyMode.MASK)
.maskNumericValuesWith(1)
.build(),
JsonMaskingConfig.custom(DEFAULT_TARGET_KEYS, JsonMaskingConfig.TargetKeyMode.ALLOW).build(),
JsonMaskingConfig.custom(DEFAULT_TARGET_KEYS, JsonMaskingConfig.TargetKeyMode.ALLOW)
.maskNumericValuesWith(2)
.build()
);
}
}
16 changes: 16 additions & 0 deletions src/test/java/dev/blaauwendraad/masker/json/ParseAndMaskUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.BigIntegerNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.NumericNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import dev.blaauwendraad.masker.json.config.JsonMaskingConfig;

import javax.annotation.Nonnull;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.Set;

Expand Down Expand Up @@ -57,6 +60,7 @@ static JsonNode mask(JsonNode jsonNode, JsonMaskingConfig jsonMaskingConfig) {
private static JsonNode maskJsonValue(JsonNode jsonNode, JsonMaskingConfig jsonMaskingConfig) {
return switch (jsonNode.getNodeType()) {
case STRING -> maskTextNode((TextNode) jsonNode);
case NUMBER -> maskNumericNode((NumericNode) jsonNode, jsonMaskingConfig);
case ARRAY -> maskArrayNodeValue((ArrayNode) jsonNode, jsonMaskingConfig);
case OBJECT -> maskObjectNodeValue((ObjectNode) jsonNode, jsonMaskingConfig);
default -> jsonNode;
Expand All @@ -68,6 +72,18 @@ private static TextNode maskTextNode(TextNode textNode) {
return new TextNode(maskText(textNode.textValue()));
}

@Nonnull
private static NumericNode maskNumericNode(NumericNode numericNode, JsonMaskingConfig jsonMaskingConfig) {
if (!jsonMaskingConfig.isNumberMaskingEnabled()) {
return numericNode;
}
String text = numericNode.asText();
int numericLength = text.length();
int theNumber = jsonMaskingConfig.getMaskNumericValuesWith();
String repeatingNumber = String.valueOf(theNumber).repeat(numericLength);
return new BigIntegerNode(new BigInteger(repeatingNumber));
}

@Nonnull
private static ArrayNode maskArrayNodeValue(ArrayNode arrayNode, JsonMaskingConfig jsonMaskingConfig) {
ArrayNode maskedArrayNode = JsonNodeFactory.instance.arrayNode();
Expand Down

0 comments on commit d4d36c4

Please sign in to comment.