Skip to content

Commit

Permalink
Fix issue occuring for NullValuesInFieldsFuzzer that was treating nul…
Browse files Browse the repository at this point in the history
…l as string null rather than null value
  • Loading branch information
en-milie committed May 23, 2022
1 parent be7ac26 commit abc322e
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected void process(FuzzingData data, String fuzzedField, FuzzingStrategy fuz
if (this.isFuzzingPossible(data, fuzzedField, fuzzingStrategy)) {
logger.debug("Fuzzing possible...");
FuzzingResult fuzzingResult = catsUtil.replaceField(data.getPayload(), fuzzedField, fuzzingStrategy);
boolean isFuzzedValueMatchingPattern = this.isFuzzedValueMatchingPattern(String.valueOf(fuzzingResult.getFuzzedValue()), data, fuzzedField);
boolean isFuzzedValueMatchingPattern = this.isFuzzedValueMatchingPattern(fuzzingResult.getFuzzedValue(), data, fuzzedField);

ServiceData serviceData = ServiceData.builder().relativePath(data.getPath())
.headers(data.getHeaders()).payload(fuzzingResult.getJson()).httpMethod(data.getMethod())
Expand Down Expand Up @@ -159,14 +159,14 @@ private FuzzingConstraints createFuzzingConstraints(FuzzingData data, FuzzingStr
* @param fuzzedField the name of the field being fuzzed
* @return true if the fuzzed value matches the pattern, false otherwise
*/
private boolean isFuzzedValueMatchingPattern(String fieldValue, FuzzingData data, String fuzzedField) {
private boolean isFuzzedValueMatchingPattern(Object fieldValue, FuzzingData data, String fuzzedField) {
Schema<?> fieldSchema = data.getRequestPropertyTypes().get(fuzzedField);
if (fieldSchema.getPattern() == null || fieldSchema instanceof ByteArraySchema) {
return true;
}
Pattern pattern = Pattern.compile(fieldSchema.getPattern());

return fieldValue == null || pattern.matcher(fieldValue).matches();
return fieldValue == null || pattern.matcher(String.valueOf(fieldValue)).matches();
}

private boolean hasMinValue(FuzzingData data, String fuzzedField) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/endava/cats/model/FuzzingResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@AllArgsConstructor
public class FuzzingResult {
private final String json;
private final String fuzzedValue;
private final Object fuzzedValue;

public static FuzzingResult empty() {
return new FuzzingResult("", "");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/endava/cats/util/CatsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public FuzzingResult replaceField(String payload, String jsonPropertyForReplacem
}
context.set(JsonUtils.sanitizeToJsonPath(jsonPropertyForReplacement), valueToSet);

return new FuzzingResult(context.jsonString(), String.valueOf(valueToSet));
return new FuzzingResult(context.jsonString(), valueToSet);
}
return FuzzingResult.empty();
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/endava/cats/util/CatsUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void shouldReturnEmptyFuzzingResultWhenEmptyJson() {
FuzzingStrategy strategy = FuzzingStrategy.replace().withData("fuzzed");
FuzzingResult result = catsUtil.replaceField("", "test", strategy);

Assertions.assertThat(result.getFuzzedValue()).isEmpty();
Assertions.assertThat(result.getFuzzedValue()).asString().isEmpty();
Assertions.assertThat(result.getJson()).isEmpty();
}

Expand Down

0 comments on commit abc322e

Please sign in to comment.