Browse Source

Exclude invalid url-encoded strings from randomized tests (#71085)

Dan Hermann 4 years ago
parent
commit
370ce5d516

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

@@ -127,12 +127,23 @@ public abstract class AbstractStringProcessorTestCase<T> extends ESTestCase {
     }
 
     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;
+    }
 }

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

@@ -10,6 +10,7 @@ package org.elasticsearch.ingest.common;
 
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
+import java.util.List;
 
 public class URLDecodeProcessorTests extends AbstractStringProcessorTestCase<String> {
     @Override
@@ -30,4 +31,30 @@ public class URLDecodeProcessorTests extends AbstractStringProcessorTestCase<Str
             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;
+        }
+    }
 }