From abc322ebd70057cad4b59f141619c6962c51cc0d Mon Sep 17 00:00:00 2001 From: Madalin Ilie Date: Mon, 23 May 2022 21:32:43 +0300 Subject: [PATCH] Fix issue occuring for NullValuesInFieldsFuzzer that was treating null as string null rather than null value --- .../endava/cats/fuzzer/fields/base/BaseFieldsFuzzer.java | 6 +++--- src/main/java/com/endava/cats/model/FuzzingResult.java | 2 +- src/main/java/com/endava/cats/util/CatsUtil.java | 2 +- src/test/java/com/endava/cats/util/CatsUtilTest.java | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/endava/cats/fuzzer/fields/base/BaseFieldsFuzzer.java b/src/main/java/com/endava/cats/fuzzer/fields/base/BaseFieldsFuzzer.java index c500efa37..a9575655d 100644 --- a/src/main/java/com/endava/cats/fuzzer/fields/base/BaseFieldsFuzzer.java +++ b/src/main/java/com/endava/cats/fuzzer/fields/base/BaseFieldsFuzzer.java @@ -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()) @@ -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) { diff --git a/src/main/java/com/endava/cats/model/FuzzingResult.java b/src/main/java/com/endava/cats/model/FuzzingResult.java index 3d830cfa5..7b785c0fe 100755 --- a/src/main/java/com/endava/cats/model/FuzzingResult.java +++ b/src/main/java/com/endava/cats/model/FuzzingResult.java @@ -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("", ""); diff --git a/src/main/java/com/endava/cats/util/CatsUtil.java b/src/main/java/com/endava/cats/util/CatsUtil.java index 0788ba47c..f4534f22e 100644 --- a/src/main/java/com/endava/cats/util/CatsUtil.java +++ b/src/main/java/com/endava/cats/util/CatsUtil.java @@ -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(); } diff --git a/src/test/java/com/endava/cats/util/CatsUtilTest.java b/src/test/java/com/endava/cats/util/CatsUtilTest.java index 0ea438288..61f9336f7 100644 --- a/src/test/java/com/endava/cats/util/CatsUtilTest.java +++ b/src/test/java/com/endava/cats/util/CatsUtilTest.java @@ -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(); }