Skip to content

Exclude invalid url-encoded strings from randomized tests #71085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}