lucene.security.IndexSearcherTest.java Source code

Java tutorial

Introduction

Here is the source code for lucene.security.IndexSearcherTest.java

Source

/**
 * 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 lucene.security;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import lucene.security.index.AccessControlFactory;
import lucene.security.index.AccessControlWriter;
import lucene.security.index.FilterAccessControlFactory;
import lucene.security.index.SecureAtomicReader;
import lucene.security.search.SecureIndexSearcher;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

public class IndexSearcherTest {

    private static final Collection<String> EMPTY = Arrays.asList(new String[] {});
    private AccessControlFactory _accessControlFactory = new FilterAccessControlFactory();

    @Test
    public void test1() throws ParseException, IOException {
        runTest(1, list("d", "a", "b"));
    }

    @Test
    public void test2() throws ParseException, IOException {
        runTest(2, list("c", "a", "b"));
    }

    @Test
    public void test3() throws ParseException, IOException {
        runTest(0, list("x"));
    }

    @Test
    public void test4() throws ParseException, IOException {
        runTest(3, list("c", "a", "b"), list("c", "a", "b"), list("_read_", "_discover_"));
    }

    @Test
    public void test5() throws ParseException, IOException {
        runTest(3, list("c", "a", "b"), list("c", "a", "b"), list("_read_", "_discover_"));
    }

    private void runTest(int expected, Collection<String> readAuthorizations) throws IOException, ParseException {
        runTest(expected, readAuthorizations, EMPTY, EMPTY);
    }

    private void runTest(int expected, Collection<String> readAuthorizations,
            Collection<String> discoverAuthorizations, Collection<String> discoverableFields)
            throws IOException, ParseException {
        IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_43, new StandardAnalyzer(Version.LUCENE_43));
        Directory dir = new RAMDirectory();
        IndexWriter writer = new IndexWriter(dir, conf);
        writer.addDocument(getEmpty());
        writer.commit();
        writer.addDocument(getDoc("(a&b)|d", null, "f1", "f2"));
        writer.addDocument(getDoc("a&b&c", null, "f1", "f2"));
        writer.addDocument(getDoc("a&b&c&e", "a&b&c", "f1", "f2"));
        writer.addDocument(getDoc(null, null, "f1", "f2"));// can't find
        writer.close();

        DirectoryReader reader = DirectoryReader.open(dir);
        List<AtomicReaderContext> leaves = reader.leaves();
        assertEquals(2, leaves.size());
        SecureIndexSearcher searcher = new SecureIndexSearcher(reader, getAccessControlFactory(),
                readAuthorizations, discoverAuthorizations, toSet(discoverableFields));

        String queryStr = "text";
        Query query = new QueryParser(Version.LUCENE_43, "text", new StandardAnalyzer(Version.LUCENE_43))
                .parse(queryStr);
        TopDocs topDocs = searcher.search(query, 10);

        assertEquals(expected, topDocs.totalHits);
        DocumentAuthorizations readDocumentAuthorizations = new DocumentAuthorizations(readAuthorizations);
        DocumentAuthorizations discoverDocumentAuthorizations = new DocumentAuthorizations(discoverAuthorizations);
        DocumentVisibilityEvaluator readVisibilityEvaluator = new DocumentVisibilityEvaluator(
                readDocumentAuthorizations);
        DocumentVisibilityEvaluator discoverVisibilityEvaluator = new DocumentVisibilityEvaluator(
                discoverDocumentAuthorizations);
        for (int i = 0; i < topDocs.totalHits & i < topDocs.scoreDocs.length; i++) {
            Document doc = searcher.doc(topDocs.scoreDocs[i].doc);
            String read = doc.get("_read_");
            String discover = doc.get("_discover_");
            if (read != null && discover != null) {
                DocumentVisibility readVisibility = new DocumentVisibility(read);
                DocumentVisibility discoverVisibility = new DocumentVisibility(discover);
                assertTrue(readVisibilityEvaluator.evaluate(readVisibility)
                        || discoverVisibilityEvaluator.evaluate(discoverVisibility));
            } else if (read != null) {
                DocumentVisibility readVisibility = new DocumentVisibility(read);
                assertTrue(readVisibilityEvaluator.evaluate(readVisibility));
            } else if (discover != null) {
                DocumentVisibility discoverVisibility = new DocumentVisibility(discover);
                assertTrue(discoverVisibilityEvaluator.evaluate(discoverVisibility));
                // Since this document is only discoverable validate fields that are
                // being returned.
                validateDiscoverFields(doc, discoverableFields);
            } else {
                fail("Should not fetch empty document.");
            }
        }
        searcher.search(query, new Collector() {

            @Override
            public void setScorer(Scorer scorer) throws IOException {
            }

            @Override
            public void setNextReader(AtomicReaderContext context) throws IOException {
                assertTrue(context.reader() instanceof SecureAtomicReader);
            }

            @Override
            public void collect(int doc) throws IOException {

            }

            @Override
            public boolean acceptsDocsOutOfOrder() {
                return false;
            }
        });
    }

    private Iterable<? extends IndexableField> getEmpty() {
        return new Document();
    }

    private void validateDiscoverFields(Document doc, Collection<String> discoverableFields) {
        Set<String> fields = new HashSet<String>(discoverableFields);
        for (IndexableField indexableField : doc.getFields()) {
            assertTrue(fields.contains(indexableField.name()));
        }
    }

    private Set<String> toSet(Collection<String> col) {
        if (col == null) {
            return null;
        }
        return new HashSet<String>(col);
    }

    private AccessControlFactory getAccessControlFactory() {
        return _accessControlFactory;
    }

    private Iterable<? extends IndexableField> getDoc(String read, String discover, String field1, String field2) {
        Document doc = new Document();
        AccessControlWriter writer = _accessControlFactory.getWriter();
        doc.add(new StringField("f1", field1, Store.YES));
        doc.add(new StringField("f2", field2, Store.YES));
        doc.add(new TextField("text", "constant text", Store.YES));
        Iterable<IndexableField> fields = doc;
        if (read != null) {
            fields = writer.addReadVisiblity(read, doc);
        }
        if (discover != null) {
            fields = writer.addDiscoverVisiblity(discover, fields);
        }
        return fields;
    }

    private List<String> list(String... strs) {
        return Arrays.asList(strs);
    }
}