Skip to content

Commit a8034b2

Browse files
committed
Exclude invalid url-encoded strings from randomized tests (elastic#71085)
1 parent 70de640 commit a8034b2

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AbstractStringProcessorTestCase.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,23 @@ public void testNonStringValueWithIgnoreMissing() throws Exception {
127127
}
128128

129129
public void testTargetField() throws Exception {
130-
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap());
131-
String fieldValue = RandomDocumentPicks.randomString(random());
132-
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, modifyInput(fieldValue));
130+
IngestDocument ingestDocument;
131+
String fieldValue;
132+
String fieldName;
133+
boolean ignoreMissing;
134+
do {
135+
ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap());
136+
fieldValue = RandomDocumentPicks.randomString(random());
137+
fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, modifyInput(fieldValue));
138+
ignoreMissing = randomBoolean();
139+
} while (isSupportedValue(ingestDocument.getFieldValue(fieldName, Object.class, ignoreMissing)) == false);
133140
String targetFieldName = fieldName + "foo";
134-
Processor processor = newProcessor(fieldName, randomBoolean(), targetFieldName);
141+
Processor processor = newProcessor(fieldName, ignoreMissing, targetFieldName);
135142
processor.execute(ingestDocument);
136143
assertThat(ingestDocument.getFieldValue(targetFieldName, expectedResultType()), equalTo(expectedResult(fieldValue)));
137144
}
145+
146+
protected boolean isSupportedValue(Object value) {
147+
return true;
148+
}
138149
}

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/URLDecodeProcessorTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.io.UnsupportedEncodingException;
1212
import java.net.URLDecoder;
13+
import java.util.List;
1314

1415
public class URLDecodeProcessorTests extends AbstractStringProcessorTestCase<String> {
1516
@Override
@@ -30,4 +31,30 @@ protected String expectedResult(String input) {
3031
throw new IllegalArgumentException("invalid");
3132
}
3233
}
34+
35+
@Override
36+
protected boolean isSupportedValue(Object value) {
37+
// some random strings produced by the randomized test framework contain invalid URL encodings
38+
if (value instanceof String) {
39+
return isValidUrlEncodedString((String) value);
40+
} else if (value instanceof List) {
41+
for (Object o : (List) value) {
42+
if ((o instanceof String) == false || isValidUrlEncodedString((String) o) == false) {
43+
return false;
44+
}
45+
}
46+
return true;
47+
} else {
48+
throw new IllegalArgumentException("unexpected type");
49+
}
50+
}
51+
52+
private static boolean isValidUrlEncodedString(String s) {
53+
try {
54+
URLDecoder.decode(s, "UTF-8");
55+
return true;
56+
} catch (Exception e) {
57+
return false;
58+
}
59+
}
3360
}

0 commit comments

Comments
 (0)