-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
breus
committed
Dec 17, 2023
1 parent
f74a687
commit 3319f14
Showing
8 changed files
with
276 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/test/java/dev/blaauwendraad/masker/json/NoFailingExecutionFuzzingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package dev.blaauwendraad.masker.json; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import dev.blaauwendraad.masker.json.config.JsonMaskingConfig; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import randomgen.json.RandomJsonGenerator; | ||
import randomgen.json.RandomJsonGeneratorConfig; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.ParametersAreNonnullByDefault; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.Set; | ||
import java.util.concurrent.*; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* This class contains fuzzing tests which are meant to spot infinite loops and program failures for all combination | ||
* of {@link JsonMasker} and {@link JsonMaskingConfig}. | ||
* <p> | ||
* For each {@link JsonMaskingConfig}, random JSON inputs are generated against which the masker runs and the only thing | ||
* that is tested it doesn't cause an exception or gets stuck in a loop. | ||
*/ | ||
@ParametersAreNonnullByDefault | ||
final class NoFailingExecutionFuzzingTest { | ||
private static final Duration DEFAULT_TEST_INSTANCE_DURATION = Duration.ofSeconds(3); | ||
|
||
@ParameterizedTest | ||
@MethodSource("failureFuzzingConfigurations") | ||
// duration in seconds the tests runs for | ||
void defaultJsonMasker(JsonMaskingConfig jsonMaskingConfig, Duration durationToRunEachTest) throws InterruptedException { | ||
Instant startTime = Instant.now(); | ||
AtomicInteger randomTestExecuted = new AtomicInteger(); | ||
AtomicReference<String> lastExecutedJson = new AtomicReference<>(); | ||
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); | ||
threadPoolExecutor.execute(() -> { | ||
while (Instant.ofEpochMilli(System.currentTimeMillis()).isBefore(startTime.plus(durationToRunEachTest))) { | ||
KeyContainsMasker keyContainsMasker = new KeyContainsMasker(jsonMaskingConfig); | ||
RandomJsonGenerator randomJsonGenerator = | ||
new RandomJsonGenerator(RandomJsonGeneratorConfig.builder().setMaxArraySize(3).setMaxNodeDepth(3).setMaxObjectKeys(2).createConfig()); | ||
JsonNode randomJsonNode = randomJsonGenerator.createRandomJsonNode(); | ||
String jsonString = randomJsonNode.toPrettyString(); | ||
lastExecutedJson.set(jsonString); | ||
Assertions.assertDoesNotThrow( | ||
() -> keyContainsMasker.mask(jsonString), | ||
randomJsonNode.toPrettyString() | ||
); | ||
randomTestExecuted.incrementAndGet(); | ||
} | ||
}); | ||
threadPoolExecutor.awaitTermination(4, TimeUnit.SECONDS); | ||
System.out.printf( | ||
"Executed %d randomly generated test scenarios in %d seconds%n", | ||
randomTestExecuted.get(), | ||
durationToRunEachTest.toSeconds() | ||
); | ||
// This is created to see on which input the program gets stuck in a loop | ||
System.out.printf("Last executed JSON:\n%s", lastExecutedJson.toString()); | ||
} | ||
|
||
|
||
@Nonnull | ||
private static Stream<Arguments> failureFuzzingConfigurations() { | ||
Set<String> targetKeys = Set.of("targetKey1", "targetKey2"); | ||
return Stream.of( | ||
Arguments.of( | ||
JsonMaskingConfig.getDefault(targetKeys), DEFAULT_TEST_INSTANCE_DURATION | ||
), | ||
Arguments.of( | ||
JsonMaskingConfig.custom(targetKeys, JsonMaskingConfig.TargetKeyMode.MASK) | ||
.caseSensitiveTargetKeys().build(), DEFAULT_TEST_INSTANCE_DURATION | ||
), | ||
Arguments.of( | ||
JsonMaskingConfig.custom(targetKeys, JsonMaskingConfig.TargetKeyMode.MASK) | ||
.disableArrayValueMasking().build(), DEFAULT_TEST_INSTANCE_DURATION | ||
), | ||
Arguments.of( | ||
JsonMaskingConfig.custom(targetKeys, JsonMaskingConfig.TargetKeyMode.MASK) | ||
.build(), DEFAULT_TEST_INSTANCE_DURATION | ||
) | ||
); | ||
} | ||
|
||
|
||
} |
26 changes: 26 additions & 0 deletions
26
src/test/java/dev/blaauwendraad/masker/json/NoObjectValueMaskingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package dev.blaauwendraad.masker.json; | ||
|
||
import dev.blaauwendraad.masker.json.config.JsonMaskerAlgorithmType; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; | ||
import java.io.IOException; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
@ParametersAreNonnullByDefault | ||
final class NoObjectValueMaskingTest { | ||
@ParameterizedTest | ||
@MethodSource("noObjectMaskingFile") | ||
void multiTargetKey(JsonMaskerTestInstance testInstance) { | ||
Assertions.assertEquals(testInstance.expectedOutput(), testInstance.jsonMasker().mask(testInstance.input())); | ||
} | ||
|
||
private static Stream<JsonMaskerTestInstance> noObjectMaskingFile() throws IOException { | ||
return JsonMaskerTestUtil.getJsonMaskerTestInstancesFromFile("test-no-object-value-masking.json", Set.of( | ||
JsonMaskerAlgorithmType.values())).stream(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
[ | ||
{ | ||
"maskerConfig": { | ||
"objectValueMasking": false | ||
}, | ||
"targetKeys": [ | ||
"maskMe", | ||
"andMaskMe" | ||
], | ||
"input": { | ||
"someKey": { | ||
"maskMe": [ | ||
null, | ||
true, | ||
123, | ||
"yes", | ||
"no" | ||
], | ||
"dontMaskMe": { | ||
"andMaskMe": "hello" | ||
} | ||
} | ||
}, | ||
"expectedOutput": { | ||
"someKey": { | ||
"maskMe": [ | ||
null, | ||
true, | ||
123, | ||
"***", | ||
"**" | ||
], | ||
"dontMaskMe": { | ||
"andMaskMe": "*****" | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"maskerConfig": { | ||
"objectValueMasking": false | ||
}, | ||
"targetKeys": [ | ||
"targetKey1", | ||
"targetKey2" | ||
], | ||
"input": { | ||
"targetKey1": { | ||
"targetKey2": "\u001C" | ||
}, | ||
"targetKey2": "W" | ||
}, | ||
"expectedOutput": { | ||
"targetKey1": { | ||
"targetKey2": "*" | ||
}, | ||
"targetKey2": "*" | ||
} | ||
}, | ||
{ | ||
"maskerConfig": { | ||
"objectValueMasking": false | ||
}, | ||
"targetKeys": [ | ||
"targetKey1", | ||
"targetKey2" | ||
], | ||
"input": { | ||
"targetKey1": { | ||
"targetKey2": "hey", | ||
"targetKey3": "\u001C" | ||
}, | ||
"targetKey2": true, | ||
"targetKey4": null | ||
}, | ||
"expectedOutput": { | ||
"targetKey1": { | ||
"targetKey2": "***", | ||
"targetKey3": "\u001C" | ||
}, | ||
"targetKey2": true, | ||
"targetKey4": null | ||
} | ||
}, | ||
{ | ||
"maskerConfig": { | ||
"objectValueMasking": false | ||
}, | ||
"targetKeys": [ | ||
"targetKey1", | ||
"targetKey2" | ||
], | ||
"input": { | ||
"targetKey1": { | ||
"targetKey2": [ | ||
{}, | ||
null, | ||
{ | ||
"targetKey1": "mask" | ||
} | ||
], | ||
"targetKey3": "\u001C" | ||
}, | ||
"targetKey2": true, | ||
"targetKey4": null | ||
}, | ||
"expectedOutput": { | ||
"targetKey1": { | ||
"targetKey2": [ | ||
{}, | ||
null, | ||
{ | ||
"targetKey1": "****" | ||
} | ||
], | ||
"targetKey3": "\u001C" | ||
}, | ||
"targetKey2": true, | ||
"targetKey4": null | ||
} | ||
}, | ||
{ | ||
"maskerConfig": { | ||
"objectValueMasking": false | ||
}, | ||
"targetKeys": [ | ||
"targetKey1" | ||
], | ||
"input": { | ||
"targetKey1": [ | ||
{ | ||
"pbNGs還": {} | ||
} | ||
] | ||
}, | ||
"expectedOutput": { | ||
"targetKey1": [ | ||
{ | ||
"pbNGs還": {} | ||
} | ||
] | ||
} | ||
} | ||
] |