Skip to content

Commit

Permalink
Exclude invalid url-encoded strings from randomized tests (#71085) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
danhermann committed May 20, 2021
1 parent cb290c6 commit 5f1333d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,23 @@ public void testNonStringValueWithIgnoreMissing() throws Exception {
}

public void testTargetField() throws Exception {
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap());
String fieldValue = RandomDocumentPicks.randomString(random());
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, modifyInput(fieldValue));
IngestDocument ingestDocument;
String fieldValue;
String fieldName;
boolean ignoreMissing;
do {
ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap());
fieldValue = RandomDocumentPicks.randomString(random());
fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, modifyInput(fieldValue));
ignoreMissing = randomBoolean();
} while (isSupportedValue(ingestDocument.getFieldValue(fieldName, Object.class, ignoreMissing)) == false);
String targetFieldName = fieldName + "foo";
Processor processor = newProcessor(fieldName, randomBoolean(), targetFieldName);
Processor processor = newProcessor(fieldName, ignoreMissing, targetFieldName);
processor.execute(ingestDocument);
assertThat(ingestDocument.getFieldValue(targetFieldName, expectedResultType()), equalTo(expectedResult(fieldValue)));
}

protected boolean isSupportedValue(Object value) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

public class URLDecodeProcessorTests extends AbstractStringProcessorTestCase<String> {
@Override
Expand All @@ -30,4 +31,30 @@ protected String expectedResult(String input) {
throw new IllegalArgumentException("invalid");
}
}

@Override
protected boolean isSupportedValue(Object value) {
// some random strings produced by the randomized test framework contain invalid URL encodings
if (value instanceof String) {
return isValidUrlEncodedString((String) value);
} else if (value instanceof List) {
for (Object o : (List) value) {
if ((o instanceof String) == false || isValidUrlEncodedString((String) o) == false) {
return false;
}
}
return true;
} else {
throw new IllegalArgumentException("unexpected type");
}
}

private static boolean isValidUrlEncodedString(String s) {
try {
URLDecoder.decode(s, "UTF-8");
return true;
} catch (Exception e) {
return false;
}
}
}

0 comments on commit 5f1333d

Please sign in to comment.