Remove all setAccessible in tests and forbid

This commit is contained in:
Robert Muir 2015-09-12 19:46:39 -04:00
parent 3a0dd59a15
commit b16e1569fe
9 changed files with 20 additions and 185 deletions

View File

@ -259,6 +259,10 @@ public class XAnalyzingSuggester extends Lookup {
public long ramBytesUsed() { public long ramBytesUsed() {
return fst == null ? 0 : fst.ramBytesUsed(); return fst == null ? 0 : fst.ramBytesUsed();
} }
public int getMaxAnalyzedPathsForOneInput() {
return maxAnalyzedPathsForOneInput;
}
// Replaces SEP with epsilon or remaps them if // Replaces SEP with epsilon or remaps them if
// we were asked to preserve them: // we were asked to preserve them:

View File

@ -200,22 +200,6 @@ public class SecurityTests extends ESTestCase {
} catch (IOException expected) {} } catch (IOException expected) {}
} }
/** We only grant this to special jars */
public void testUnsafeAccess() throws Exception {
assumeTrue("test requires security manager", System.getSecurityManager() != null);
try {
// class could be legitimately loaded, so we might not fail until setAccessible
Class.forName("sun.misc.Unsafe")
.getDeclaredField("theUnsafe")
.setAccessible(true);
fail("didn't get expected exception");
} catch (SecurityException expected) {
// ok
} catch (Exception somethingElse) {
assumeNoException("perhaps JVM doesn't have Unsafe?", somethingElse);
}
}
/** can't execute processes */ /** can't execute processes */
public void testProcessExecution() throws Exception { public void testProcessExecution() throws Exception {
assumeTrue("test requires security manager", System.getSecurityManager() != null); assumeTrue("test requires security manager", System.getSecurityManager() != null);

View File

@ -29,8 +29,6 @@ import org.elasticsearch.test.ESIntegTestCase.Scope;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Test; import org.junit.Test;
import java.lang.reflect.Field;
import static org.elasticsearch.indices.analysis.HunspellService.*; import static org.elasticsearch.indices.analysis.HunspellService.*;
import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.notNullValue;
@ -114,10 +112,8 @@ public class HunspellServiceIT extends ESIntegTestCase {
} }
} }
// TODO: open up a getter on Dictionary // TODO: on next upgrade of lucene, just use new getter
private void assertIgnoreCase(boolean expected, Dictionary dictionary) throws Exception { private void assertIgnoreCase(boolean expected, Dictionary dictionary) throws Exception {
Field f = Dictionary.class.getDeclaredField("ignoreCase"); // assertEquals(expected, dictionary.getIgnoreCase());
f.setAccessible(true);
assertEquals(expected, f.getBoolean(dictionary));
} }
} }

View File

@ -1,146 +0,0 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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.elasticsearch.network;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.node.Node;
import org.elasticsearch.test.ESIntegTestCase;
import org.hamcrest.Matchers;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
/**
*/
public class DirectBufferNetworkIT extends ESIntegTestCase {
@Override
protected Settings nodeSettings(int nodeOrdinal) {
return Settings.builder()
.put(Node.HTTP_ENABLED, true)
.put(super.nodeSettings(nodeOrdinal)).build();
}
/**
* This test validates that using large data sets (large docs + large API requests) don't
* cause a large direct byte buffer to be allocated internally in the sun.nio buffer cache.
* <p/>
* See {@link org.elasticsearch.common.netty.NettyUtils#DEFAULT_GATHERING} for more info.
*/
@Test
public void verifySaneDirectBufferAllocations() throws Exception {
assumeTrue("test cannot run with security manager enabled", System.getSecurityManager() == null);
createIndex("test");
int estimatedBytesSize = scaledRandomIntBetween(ByteSizeValue.parseBytesSizeValue("1.1mb", "estimatedBytesSize").bytesAsInt(),
ByteSizeValue.parseBytesSizeValue("1.5mb", "estimatedBytesSize").bytesAsInt());
byte[] data = new byte[estimatedBytesSize];
getRandom().nextBytes(data);
ByteArrayOutputStream docOut = new ByteArrayOutputStream();
// we use smile to automatically use the binary mapping
XContentBuilder doc = XContentFactory.smileBuilder(docOut).startObject().startObject("doc").field("value", data).endObject();
doc.close();
byte[] docBytes = docOut.toByteArray();
int numDocs = randomIntBetween(2, 5);
logger.info("indexing [{}] docs, each with size [{}]", numDocs, estimatedBytesSize);
IndexRequestBuilder[] builders = new IndexRequestBuilder[numDocs];
for (int i = 0; i < numDocs; ++i) {
builders[i] = client().prepareIndex("test", "type").setSource(docBytes);
}
indexRandom(true, builders);
logger.info("done indexing");
logger.info("executing random client search for all docs");
assertHitCount(client().prepareSearch("test").setFrom(0).setSize(numDocs).get(), numDocs);
logger.info("executing transport client search for all docs");
assertHitCount(internalCluster().transportClient().prepareSearch("test").setFrom(0).setSize(numDocs).get(), numDocs);
logger.info("executing HTTP search for all docs");
// simulate large HTTP call as well
httpClient().method("GET").path("/test/_search").addParam("size", Integer.toString(numDocs)).execute();
logger.info("validating large direct buffer not allocated");
validateNoLargeDirectBufferAllocated();
}
/**
* Validates that all the thread local allocated ByteBuffer in sun.nio under the Util$BufferCache
* are not greater than 1mb.
*/
private void validateNoLargeDirectBufferAllocated() throws Exception {
// Make the fields in the Thread class that store ThreadLocals
// accessible
Field threadLocalsField = Thread.class.getDeclaredField("threadLocals");
threadLocalsField.setAccessible(true);
// Make the underlying array of ThreadLoad.ThreadLocalMap.Entry objects
// accessible
Class<?> tlmClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");
Field tableField = tlmClass.getDeclaredField("table");
tableField.setAccessible(true);
for (Thread thread : Thread.getAllStackTraces().keySet()) {
if (thread == null) {
continue;
}
Object threadLocalMap = threadLocalsField.get(thread);
if (threadLocalMap == null) {
continue;
}
Object[] table = (Object[]) tableField.get(threadLocalMap);
if (table == null) {
continue;
}
for (Object entry : table) {
if (entry == null) {
continue;
}
Field valueField = entry.getClass().getDeclaredField("value");
valueField.setAccessible(true);
Object value = valueField.get(entry);
if (value == null) {
continue;
}
if (!value.getClass().getName().equals("sun.nio.ch.Util$BufferCache")) {
continue;
}
Field buffersField = value.getClass().getDeclaredField("buffers");
buffersField.setAccessible(true);
Object[] buffers = (Object[]) buffersField.get(value);
for (Object buffer : buffers) {
if (buffer == null) {
continue;
}
assertThat(((ByteBuffer) buffer).capacity(), Matchers.lessThan(1 * 1024 * 1024));
}
}
}
}
}

View File

@ -244,11 +244,9 @@ public class CompletionPostingsFormatTests extends ESTestCase {
fieldType.setProvider(currentProvider); fieldType.setProvider(currentProvider);
final CompletionFieldMapper mapper = new CompletionFieldMapper("foo", fieldType, Integer.MAX_VALUE, indexSettings, FieldMapper.MultiFields.empty(), null); final CompletionFieldMapper mapper = new CompletionFieldMapper("foo", fieldType, Integer.MAX_VALUE, indexSettings, FieldMapper.MultiFields.empty(), null);
Lookup buildAnalyzingLookup = buildAnalyzingLookup(mapper, titles, titles, weights); Lookup buildAnalyzingLookup = buildAnalyzingLookup(mapper, titles, titles, weights);
Field field = buildAnalyzingLookup.getClass().getDeclaredField("maxAnalyzedPathsForOneInput"); if (buildAnalyzingLookup instanceof XAnalyzingSuggester) {
field.setAccessible(true); assertEquals(reference.getMaxAnalyzedPathsForOneInput(), ((XAnalyzingSuggester) buildAnalyzingLookup).getMaxAnalyzedPathsForOneInput());
Field refField = reference.getClass().getDeclaredField("maxAnalyzedPathsForOneInput"); }
refField.setAccessible(true);
assertThat(refField.get(reference), equalTo(field.get(buildAnalyzingLookup)));
for (int i = 0; i < titles.length; i++) { for (int i = 0; i < titles.length; i++) {
int res = between(1, 10); int res = between(1, 10);

View File

@ -33,6 +33,7 @@ import com.carrotsearch.randomizedtesting.rules.TestRuleAdapter;
import org.apache.lucene.uninverting.UninvertingReader; import org.apache.lucene.uninverting.UninvertingReader;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.LuceneTestCase.SuppressCodecs; import org.apache.lucene.util.LuceneTestCase.SuppressCodecs;
import org.apache.lucene.util.TestRuleMarkFailure;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
import org.apache.lucene.util.TimeUnits; import org.apache.lucene.util.TimeUnits;
import org.elasticsearch.Version; import org.elasticsearch.Version;
@ -643,4 +644,9 @@ public abstract class ESTestCase extends LuceneTestCase {
sb.append("]"); sb.append("]");
assertThat(count + " files exist that should have been cleaned:\n" + sb.toString(), count, equalTo(0)); assertThat(count + " files exist that should have been cleaned:\n" + sb.toString(), count, equalTo(0));
} }
/** Returns the suite failure marker: internal use only! */
public static TestRuleMarkFailure getSuiteFailureMarker() {
return suiteFailureMarker;
}
} }

View File

@ -49,7 +49,6 @@ import org.junit.Assert;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.lang.reflect.Field;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.*;
@ -237,13 +236,7 @@ public class MockFSDirectoryService extends FsDirectoryService {
public CloseableDirectory(BaseDirectoryWrapper dir) { public CloseableDirectory(BaseDirectoryWrapper dir) {
this.dir = dir; this.dir = dir;
try { this.failureMarker = ESTestCase.getSuiteFailureMarker();
final Field suiteFailureMarker = LuceneTestCase.class.getDeclaredField("suiteFailureMarker");
suiteFailureMarker.setAccessible(true);
this.failureMarker = (TestRuleMarkFailure) suiteFailureMarker.get(LuceneTestCase.class);
} catch (Throwable e) {
throw new ElasticsearchException("foo", e);
}
} }
@Override @Override

View File

@ -107,3 +107,7 @@ com.google.common.util.concurrent.SettableFuture
com.google.common.util.concurrent.Futures com.google.common.util.concurrent.Futures
com.google.common.util.concurrent.MoreExecutors com.google.common.util.concurrent.MoreExecutors
com.google.common.collect.ImmutableSortedMap com.google.common.collect.ImmutableSortedMap
@defaultMessage Do not violate java's access system
java.lang.reflect.AccessibleObject#setAccessible(boolean)
java.lang.reflect.AccessibleObject#setAccessible(java.lang.reflect.AccessibleObject[], boolean)

View File

@ -83,7 +83,3 @@ java.util.concurrent.Future#cancel(boolean)
@defaultMessage Don't try reading from paths that are not configured in Environment, resolve from Environment instead @defaultMessage Don't try reading from paths that are not configured in Environment, resolve from Environment instead
org.elasticsearch.common.io.PathUtils#get(java.lang.String, java.lang.String[]) org.elasticsearch.common.io.PathUtils#get(java.lang.String, java.lang.String[])
org.elasticsearch.common.io.PathUtils#get(java.net.URI) org.elasticsearch.common.io.PathUtils#get(java.net.URI)
@defaultMessage Do not violate java's access system
java.lang.reflect.AccessibleObject#setAccessible(boolean)
java.lang.reflect.AccessibleObject#setAccessible(java.lang.reflect.AccessibleObject[], boolean)