You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by th...@apache.org on 2022/03/30 15:06:47 UTC

[jackrabbit-oak] branch OAK-9741 created (now 1fb413f)

This is an automated email from the ASF dual-hosted git repository.

thomasm pushed a change to branch OAK-9741
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git.


      at 1fb413f  OAK-9741 Test cases for invalid index definitions

This branch includes the following new commits:

     new 1fb413f  OAK-9741 Test cases for invalid index definitions

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[jackrabbit-oak] 01/01: OAK-9741 Test cases for invalid index definitions

Posted by th...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

thomasm pushed a commit to branch OAK-9741
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git

commit 1fb413f5a04c9a8aa32dc637d7e86e42887e5053
Author: Thomas Mueller <th...@apache.org>
AuthorDate: Wed Mar 30 17:06:32 2022 +0200

    OAK-9741 Test cases for invalid index definitions
---
 .../invalidData/InvalidIndexDefinitionTest.java    | 259 +++++++++++++++++++++
 .../oak/plugins/index/search/IndexDefinition.java  |   3 +-
 2 files changed, 260 insertions(+), 2 deletions(-)

diff --git a/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/invalidData/InvalidIndexDefinitionTest.java b/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/invalidData/InvalidIndexDefinitionTest.java
new file mode 100644
index 0000000..b781979
--- /dev/null
+++ b/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/invalidData/InvalidIndexDefinitionTest.java
@@ -0,0 +1,259 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.jackrabbit.oak.plugins.index.lucene.invalidData;
+
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.INDEX_DEFINITIONS_NAME;
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.INDEX_DEFINITIONS_NODE_TYPE;
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.TYPE_PROPERTY_NAME;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import java.util.List;
+import java.util.regex.PatternSyntaxException;
+
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.InitialContentHelper;
+import org.apache.jackrabbit.oak.Oak;
+import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.api.ContentRepository;
+import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexConstants;
+import org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexEditorProvider;
+import org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexProvider;
+import org.apache.jackrabbit.oak.plugins.index.nodetype.NodeTypeIndexProvider;
+import org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexEditorProvider;
+import org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants;
+import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore;
+import org.apache.jackrabbit.oak.query.AbstractQueryTest;
+import org.apache.jackrabbit.oak.spi.commit.Observer;
+import org.apache.jackrabbit.oak.spi.filter.PathFilter;
+import org.apache.jackrabbit.oak.spi.query.QueryIndexProvider;
+import org.apache.jackrabbit.oak.spi.security.OpenSecurityProvider;
+import org.apache.jackrabbit.oak.spi.state.NodeStore;
+import org.apache.lucene.codecs.Codec;
+import org.junit.Test;
+
+import com.google.common.collect.Lists;
+
+public class InvalidIndexDefinitionTest extends AbstractQueryTest {
+
+    private LuceneIndexEditorProvider editorProvider;
+
+    private NodeStore nodeStore;
+    
+    @Override
+    protected ContentRepository createRepository() {
+        editorProvider = new LuceneIndexEditorProvider();
+        LuceneIndexProvider provider = new LuceneIndexProvider();
+        nodeStore = new MemoryNodeStore(InitialContentHelper.INITIAL_CONTENT);
+        return new Oak(nodeStore)
+                .with(new OpenSecurityProvider())
+                .with((QueryIndexProvider) provider)
+                .with((Observer) provider)
+                .with(editorProvider)
+                .with(new PropertyIndexEditorProvider())
+                .with(new NodeTypeIndexProvider())
+                .createContentRepository();
+    }
+    
+    @Test
+    public void allFine() throws CommitFailedException {
+        createIndexNodeAndData();
+        root.commit();
+        String query = "select [jcr:path] from [nt:base] where isdescendantnode('/tmp') and upper([test]) = 'HELLO'";
+        assertThat(explain(query), containsString("lucene:test"));
+        assertQuery(query, Lists.newArrayList("/tmp/testNode"));
+    }
+    
+    @Test(expected = IllegalArgumentException.class)
+    public void invalidCodec() throws CommitFailedException {
+        Tree def = createIndexNodeAndData();
+        // An incorrect value throws an exception, for example:
+        // java.lang.IllegalArgumentException: A SPI class of type org.apache.lucene.codecs.Codec with name 'Lucene46x' does not exist.
+        String codecValue = Codec.getDefault().getName() + "x";
+        def.setProperty(LuceneIndexConstants.CODEC_NAME, codecValue);
+        root.commit();
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void invalidCompatMode() throws CommitFailedException {
+        Tree def = createIndexNodeAndData();
+        // 3 results in IllegalArgumentException: Unknown version : 3
+        def.setProperty(LuceneIndexConstants.COMPAT_MODE, 3);
+        root.commit();
+    }
+    
+    @Test(expected = PatternSyntaxException.class)
+    public void invalidValueRegex() throws CommitFailedException {
+        Tree def = createIndexNodeAndData();
+        // An incorrect value, for example "[a-z", results in
+        // java.util.regex.PatternSyntaxException: Unclosed character class near index 3
+        def.setProperty(LuceneIndexConstants.PROP_VALUE_REGEX, "[a-z");
+        root.commit();
+    }
+    
+    @Test(expected = PatternSyntaxException.class)
+    public void invalidQueryFilterRegex() throws CommitFailedException {
+        Tree def = createIndexNodeAndData();
+        // An incorrect value, for example "[a-z", results in
+        // java.util.regex.PatternSyntaxException: Unclosed character class near index 3
+        def.setProperty(LuceneIndexConstants.PROP_QUERY_FILTER_REGEX, "[a-z");
+        root.commit();
+    }
+    
+    @Test(expected = IllegalArgumentException.class)
+    public void invalidBlobSize() throws CommitFailedException {
+        Tree def = createIndexNodeAndData();
+        // 1L + Integer.MAX_VALUE results in IllegalArgumentException: Out of range: 2147483648
+        def.setProperty("blobSize", 1L + Integer.MAX_VALUE);
+        root.commit();
+    }    
+
+    @Test
+    public void negativeBlobSize() throws CommitFailedException {
+        Tree def = createIndexNodeAndData();
+        // 1L + Integer.MAX_VALUE results in IllegalArgumentException: Out of range: 2147483648
+        def.setProperty("blobSize", -1);
+        root.commit();
+        String query = "select [jcr:path] from [nt:base] where isdescendantnode('/tmp') and upper([test]) = 'HELLO'";
+        assertThat(explain(query), containsString("lucene:test"));
+        assertQuery(query, Lists.newArrayList("/tmp/testNode"));
+    }    
+
+    @Test(expected = IllegalArgumentException.class)
+    public void invalidMaxFieldLength() throws CommitFailedException {
+        Tree def = createIndexNodeAndData();
+        // 1L + Integer.MAX_VALUE results in IllegalArgumentException: Out of range: 2147483648
+        def.setProperty(FulltextIndexConstants.MAX_FIELD_LENGTH, 1L + Integer.MAX_VALUE);
+        root.commit();
+    }    
+
+    @Test
+    public void invalidEvaluatePathRestriction() throws CommitFailedException {
+        Tree def = createIndexNodeAndData();
+        // errors here are ignored
+        def.setProperty(LuceneIndexConstants.EVALUATE_PATH_RESTRICTION, "abc");
+        root.commit();
+        String query = "select [jcr:path] from [nt:base] where isdescendantnode('/tmp') and upper([test]) = 'HELLO'";
+        assertThat(explain(query), containsString("lucene:test"));
+        assertQuery(query, Lists.newArrayList("/tmp/testNode"));
+    }
+
+    @Test
+    public void invalidIncludedPath() throws CommitFailedException {
+        // this will commit, but in oak-run it will not work:
+        // ERROR o.a.j.o.p.i.search.IndexDefinition - Config error for index definition at /oak:index/... . 
+        // Please correct the index definition and reindex after correction. Additional Info : No valid include provided. Includes [/tmp], Excludes [/tmp]
+        // java.lang.IllegalStateException: No valid include provided. Includes [/tmp], Excludes [/tmp]
+        Tree def = createIndexNodeAndData();
+        def.setProperty(PathFilter.PROP_INCLUDED_PATHS, List.of("/tmp/testNode"), Type.STRINGS);
+        def.setProperty(PathFilter.PROP_EXCLUDED_PATHS, List.of("/tmp"), Type.STRINGS);
+        root.commit();
+        String query = "select [jcr:path] from [nt:base] where isdescendantnode('/tmp') and upper([test]) = 'HELLO'";
+        assertThat(explain(query), containsString("traverse"));
+        assertQuery(query, Lists.newArrayList("/tmp/testNode"));
+    }
+    
+    @Test
+    public void invalidPropertyBoost() throws CommitFailedException {
+        // errors here are ignored (including Double.POSITIVE_INFINITY, NEGATIVE_INFINITY)
+        Tree def = createIndexNodeAndData();
+        Tree indexRules = def.getChild(LuceneIndexConstants.INDEX_RULES);
+        Tree ntBase = indexRules.getChild("nt:base");
+        Tree properties = ntBase.getChild(FulltextIndexConstants.PROP_NODE);
+        Tree test = properties.getChild("test");
+        test.setProperty(FulltextIndexConstants.FIELD_BOOST, Double.NEGATIVE_INFINITY);
+        root.commit();
+        String query = "select [jcr:path] from [nt:base] where isdescendantnode('/tmp') and upper([test]) = 'HELLO'";
+        assertThat(explain(query), containsString("lucene:test"));
+        assertQuery(query, Lists.newArrayList("/tmp/testNode"));
+    }    
+    
+    @Test
+    public void invalidPropertyFunction() throws CommitFailedException {
+        // errors here are ignored (including Double.POSITIVE_INFINITY, NEGATIVE_INFINITY)
+        Tree def = createIndexNodeAndData();
+        Tree indexRules = def.getChild(LuceneIndexConstants.INDEX_RULES);
+        Tree ntBase = indexRules.getChild("nt:base");
+        Tree properties = ntBase.getChild(FulltextIndexConstants.PROP_NODE);
+        Tree test = properties.getChild("test");
+        test.removeProperty(FulltextIndexConstants.PROP_NAME);
+        // errors here are ignored - just the index is not used then
+        // ("./test" is not a supported syntax)
+        test.setProperty(FulltextIndexConstants.PROP_FUNCTION, "upper([./test])");
+        root.commit();
+        String query = "select [jcr:path] from [nt:base] where isdescendantnode('/tmp') and upper([./test]) = 'HELLO'";
+        assertThat(explain(query), containsString("traverse"));
+        assertQuery(query, Lists.newArrayList("/tmp/testNode"));
+    }    
+    
+    Tree createIndexNodeAndData() throws CommitFailedException {
+        Tree tmp = root.getTree("/").addChild("tmp");
+        tmp.setProperty("jcr:primaryType", "nt:unstructured", Type.NAME);
+        Tree testNode = tmp.addChild("testNode");
+        testNode.setProperty("jcr:primaryType", "nt:unstructured", Type.NAME);
+        testNode.setProperty("test", "hello");
+        root.commit();
+        
+        Tree index = root.getTree("/");
+        Tree def = index.addChild(INDEX_DEFINITIONS_NAME).addChild("test");
+        def.setProperty(JcrConstants.JCR_PRIMARYTYPE, INDEX_DEFINITIONS_NODE_TYPE, Type.NAME);
+        
+        def.setProperty(TYPE_PROPERTY_NAME, LuceneIndexConstants.TYPE_LUCENE);
+        
+        // we don't set it now to speed up testing
+        // def.setProperty(IndexConstants.ASYNC_PROPERTY_NAME, "async");
+        
+        Tree indexRules = def.addChild(LuceneIndexConstants.INDEX_RULES);
+        indexRules.setProperty(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED, Type.NAME);
+
+        // errors here are ignored
+        Tree ntBase = indexRules.addChild("nt:base");
+        ntBase.setProperty(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED, Type.NAME);
+        Tree properties = ntBase.addChild(FulltextIndexConstants.PROP_NODE);
+        properties.setProperty(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED, Type.NAME);
+        
+        Tree test = properties.addChild("test");
+        test.setProperty(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED, Type.NAME);
+
+        // use a function index
+        test.setProperty(FulltextIndexConstants.PROP_FUNCTION, "upper([test])");
+        
+        // errors here are ignored
+        test.setProperty(FulltextIndexConstants.PROP_ORDERED, true);
+        
+        // errors here are ignored
+        test.setProperty(FulltextIndexConstants.PROP_PROPERTY_INDEX, true);
+        
+        // errors here are ignored
+        test.setProperty(FulltextIndexConstants.PROP_NOT_NULL_CHECK_ENABLED, true);
+
+        // errors here are ignored
+        test.setProperty(FulltextIndexConstants.PROP_NULL_CHECK_ENABLED, true);
+
+        return def;
+    }
+    
+    protected String explain(String query){
+        String explain = "explain " + query;
+        return executeQuery(explain, "JCR-SQL2").get(0);
+    }
+
+}
\ No newline at end of file
diff --git a/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/IndexDefinition.java b/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/IndexDefinition.java
index b9e5016..705f5d0 100644
--- a/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/IndexDefinition.java
+++ b/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/IndexDefinition.java
@@ -406,8 +406,7 @@ public class IndexDefinition implements Aggregate.AggregateMapper {
             this.indexSelectionPolicy
                     = getOptionalValue(defn, IndexConstants.INDEX_SELECTION_POLICY, null);
             this.nodeTypeIndex = getOptionalValue(defn, FulltextIndexConstants.PROP_INDEX_NODE_TYPE, false);
-
-            this.blobSize = getOptionalValue(defn, BLOB_SIZE, DEFAULT_BLOB_SIZE);
+            this.blobSize = Math.max(1024, getOptionalValue(defn, BLOB_SIZE, DEFAULT_BLOB_SIZE));
 
             this.aggregates = nodeTypeIndex ? Collections.emptyMap() : collectAggregates(defn);