From c7fe7efac10b8711c1afd99a7b1bf63830aab31b Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Mon, 29 Jun 2015 09:29:38 -0400 Subject: [PATCH] Add mocking for securitymanager environment. This is a standalone wrapper around Mockito, it allows us to use it without granting dangerous permissions to all of our code. See https://github.com/rmuir/securemock --- securemock/pom.xml | 107 +++++ .../java/org/elasticsearch/mock/Mockito.java | 367 ++++++++++++++++++ 2 files changed, 474 insertions(+) create mode 100644 securemock/pom.xml create mode 100644 securemock/src/main/java/org/elasticsearch/mock/Mockito.java diff --git a/securemock/pom.xml b/securemock/pom.xml new file mode 100644 index 00000000000..0b7491cff25 --- /dev/null +++ b/securemock/pom.xml @@ -0,0 +1,107 @@ + + 4.0.0 + org.elasticsearch + securemock + 1.0-SNAPSHOT + jar + securemock + Allows creating mocks in tests without having to grant dangerous permissions to all of your code. + 2015 + + + org.sonatype.oss + oss-parent + 7 + + + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + scm:git:git@github.com:elastic/elasticsearch.git + scm:git:git@github.com:elastic/elasticsearch.git + http://github.com/elastic/elasticsearch/securemock + + + + UTF-8 + 1.7 + 1.7 + + + + + oss-snapshots + Sonatype OSS Snapshots + https://oss.sonatype.org/content/repositories/snapshots/ + + + + + + org.mockito + mockito-core + 1.9.5 + + + org.objenesis + objenesis + 2.1 + + + junit + junit + 4.11 + test + + + + + + + org.apache.maven.plugins + maven-shade-plugin + 2.4 + + + package + + shade + + + + + junit:junit + org.hamcrest:hamcrest-core + + + + + + org.mockito + org.elasticsearch.mock.orig + + org.mockito.Mockito + + + + org.elasticsearch.mock + org.mockito + + org.elasticsearch.mock.Mockito* + + + + + + + + + + diff --git a/securemock/src/main/java/org/elasticsearch/mock/Mockito.java b/securemock/src/main/java/org/elasticsearch/mock/Mockito.java new file mode 100644 index 00000000000..b5abe867d16 --- /dev/null +++ b/securemock/src/main/java/org/elasticsearch/mock/Mockito.java @@ -0,0 +1,367 @@ +/* + * 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.mock; + +import org.mockito.InOrder; +import org.mockito.Matchers; +import org.mockito.MockSettings; +import org.mockito.MockingDetails; +import org.mockito.ReturnValues; +import org.mockito.stubbing.Answer; +import org.mockito.stubbing.DeprecatedOngoingStubbing; +import org.mockito.stubbing.OngoingStubbing; +import org.mockito.stubbing.Stubber; +import org.mockito.stubbing.VoidMethodStubbable; +import org.mockito.verification.VerificationMode; +import org.mockito.verification.VerificationWithTimeout; + +import java.security.AccessController; +import java.security.PrivilegedAction; + +/** + * Wraps Mockito API with calls to AccessController. + *

+ * This is useful if you want to mock in a securitymanager environment, + * but contain the permissions to only mocking test libraries. + *

+ * Instead of: + *

+ * grant {
+ *   permission java.lang.RuntimePermission "reflectionFactoryAccess";
+ * };
+ * 
+ * You can just change maven dependencies to use securemock.jar, and then: + *
+ * grant codeBase "/url/to/securemock.jar" {
+ *   permission java.lang.RuntimePermission "reflectionFactoryAccess";
+ * };
+ * 
+ */ +public class Mockito extends Matchers { + + public static final Answer RETURNS_DEFAULTS = org.mockito.Mockito.RETURNS_DEFAULTS; + public static final Answer RETURNS_SMART_NULLS = org.mockito.Mockito.RETURNS_SMART_NULLS; + public static final Answer RETURNS_MOCKS = org.mockito.Mockito.RETURNS_MOCKS; + public static final Answer RETURNS_DEEP_STUBS = org.mockito.Mockito.RETURNS_DEEP_STUBS; + public static final Answer CALLS_REAL_METHODS = org.mockito.Mockito.CALLS_REAL_METHODS; + + public static T mock(final Class classToMock) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public T run() { + return org.mockito.Mockito.mock(classToMock); + } + }); + } + + public static T mock(final Class classToMock, final String name) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public T run() { + return org.mockito.Mockito.mock(classToMock, name); + } + }); + } + + public static MockingDetails mockingDetails(final Object toInspect) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public MockingDetails run() { + return org.mockito.Mockito.mockingDetails(toInspect); + } + }); + } + + @Deprecated + public static T mock(final Class classToMock, final ReturnValues returnValues) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public T run() { + return org.mockito.Mockito.mock(classToMock, returnValues); + } + }); + } + + public static T mock(final Class classToMock, final Answer defaultAnswer) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public T run() { + return org.mockito.Mockito.mock(classToMock, defaultAnswer); + } + }); + } + + public static T mock(final Class classToMock, final MockSettings mockSettings) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public T run() { + return org.mockito.Mockito.mock(classToMock, mockSettings); + } + }); + } + + public static T spy(final T object) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public T run() { + return org.mockito.Mockito.spy(object); + } + }); + } + + public static DeprecatedOngoingStubbing stub(final T methodCall) { + return AccessController.doPrivileged(new PrivilegedAction>() { + @Override + public DeprecatedOngoingStubbing run() { + return org.mockito.Mockito.stub(methodCall); + } + }); + } + + public static OngoingStubbing when(final T methodCall) { + return AccessController.doPrivileged(new PrivilegedAction>() { + @Override + public OngoingStubbing run() { + return org.mockito.Mockito.when(methodCall); + } + }); + } + + public static T verify(final T mock) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public T run() { + return org.mockito.Mockito.verify(mock); + } + }); + } + + public static T verify(final T mock, final VerificationMode mode) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public T run() { + return org.mockito.Mockito.verify(mock, mode); + } + }); + } + + public static void reset(final T ... mocks) { + AccessController.doPrivileged(new PrivilegedAction() { + @Override + public Void run() { + org.mockito.Mockito.reset(mocks); + return null; + } + }); + } + + public static void verifyNoMoreInteractions(final Object... mocks) { + AccessController.doPrivileged(new PrivilegedAction() { + @Override + public Void run() { + org.mockito.Mockito.verifyNoMoreInteractions(mocks); + return null; + } + }); + } + + public static void verifyZeroInteractions(final Object... mocks) { + AccessController.doPrivileged(new PrivilegedAction() { + @Override + public Void run() { + org.mockito.Mockito.verifyZeroInteractions(mocks); + return null; + } + }); + } + + @Deprecated + public static VoidMethodStubbable stubVoid(final T mock) { + return AccessController.doPrivileged(new PrivilegedAction>() { + @Override + public VoidMethodStubbable run() { + return org.mockito.Mockito.stubVoid(mock); + } + }); + } + + public static Stubber doThrow(final Throwable toBeThrown) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public Stubber run() { + return org.mockito.Mockito.doThrow(toBeThrown); + } + }); + } + + public static Stubber doThrow(final Class toBeThrown) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public Stubber run() { + return org.mockito.Mockito.doThrow(toBeThrown); + } + }); + } + + public static Stubber doCallRealMethod() { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public Stubber run() { + return org.mockito.Mockito.doCallRealMethod(); + } + }); + } + + public static Stubber doAnswer(final Answer answer) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public Stubber run() { + return org.mockito.Mockito.doAnswer(answer); + } + }); + } + + public static Stubber doNothing() { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public Stubber run() { + return org.mockito.Mockito.doNothing(); + } + }); + } + + public static Stubber doReturn(final Object toBeReturned) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public Stubber run() { + return org.mockito.Mockito.doReturn(toBeReturned); + } + }); + } + + public static InOrder inOrder(final Object... mocks) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public InOrder run() { + return org.mockito.Mockito.inOrder(mocks); + } + }); + } + + public static Object[] ignoreStubs(final Object... mocks) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public Object[] run() { + return org.mockito.Mockito.ignoreStubs(mocks); + } + }); + } + + public static VerificationMode times(final int wantedNumberOfInvocations) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public VerificationMode run() { + return org.mockito.Mockito.times(wantedNumberOfInvocations); + } + }); + } + + public static VerificationMode never() { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public VerificationMode run() { + return org.mockito.Mockito.never(); + } + }); + } + + public static VerificationMode atLeastOnce() { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public VerificationMode run() { + return org.mockito.Mockito.atLeastOnce(); + } + }); + } + + public static VerificationMode atLeast(final int minNumberOfInvocations) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public VerificationMode run() { + return org.mockito.Mockito.atLeast(minNumberOfInvocations); + } + }); + } + + public static VerificationMode atMost(final int maxNumberOfInvocations) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public VerificationMode run() { + return org.mockito.Mockito.atMost(maxNumberOfInvocations); + } + }); + } + + public static VerificationMode calls(final int wantedNumberOfInvocations) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public VerificationMode run() { + return org.mockito.Mockito.calls(wantedNumberOfInvocations); + } + }); + } + + public static VerificationMode only() { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public VerificationMode run() { + return org.mockito.Mockito.only(); + } + }); + } + + public static VerificationWithTimeout timeout(final int millis) { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public VerificationWithTimeout run() { + return org.mockito.Mockito.timeout(millis); + } + }); + } + + public static void validateMockitoUsage() { + AccessController.doPrivileged(new PrivilegedAction() { + @Override + public Void run() { + org.mockito.Mockito.validateMockitoUsage(); + return null; + } + }); + } + + public static MockSettings withSettings() { + return AccessController.doPrivileged(new PrivilegedAction() { + @Override + public MockSettings run() { + return org.mockito.Mockito.withSettings(); + } + }); + } +}