aboutsummaryrefslogtreecommitdiff
path: root/dexmaker-mockito-tests/src/androidTest/java/com/android/dx
diff options
context:
space:
mode:
authorandroid-build-team Robot <android-build-team-robot@google.com>2018-05-23 07:22:00 +0000
committerandroid-build-team Robot <android-build-team-robot@google.com>2018-05-23 07:22:00 +0000
commit41739f1fc403c235dcfba6bceb766103d649f8ce (patch)
tree3b848189d1bc1a2238caf1d1285953232c35806a /dexmaker-mockito-tests/src/androidTest/java/com/android/dx
parenta72b97b7fbaa0f112313fd4b0e3d3c2a7a804467 (diff)
parent314cb2efb7b1d8d9b584a6e0bd82727168cfd181 (diff)
downloaddexmaker-41739f1fc403c235dcfba6bceb766103d649f8ce.tar.gz
Snap for 4799153 from 314cb2efb7b1d8d9b584a6e0bd82727168cfd181 to pi-releaseandroid-wear-9.0.0_r9android-wear-9.0.0_r8android-wear-9.0.0_r7android-wear-9.0.0_r6android-wear-9.0.0_r5android-wear-9.0.0_r4android-wear-9.0.0_r34android-wear-9.0.0_r33android-wear-9.0.0_r32android-wear-9.0.0_r31android-wear-9.0.0_r30android-wear-9.0.0_r3android-wear-9.0.0_r29android-wear-9.0.0_r28android-wear-9.0.0_r27android-wear-9.0.0_r26android-wear-9.0.0_r25android-wear-9.0.0_r24android-wear-9.0.0_r23android-wear-9.0.0_r22android-wear-9.0.0_r21android-wear-9.0.0_r20android-wear-9.0.0_r2android-wear-9.0.0_r19android-wear-9.0.0_r18android-wear-9.0.0_r17android-wear-9.0.0_r16android-wear-9.0.0_r15android-wear-9.0.0_r14android-wear-9.0.0_r13android-wear-9.0.0_r12android-wear-9.0.0_r11android-wear-9.0.0_r10android-wear-9.0.0_r1android-vts-9.0_r9android-vts-9.0_r8android-vts-9.0_r7android-vts-9.0_r6android-vts-9.0_r5android-vts-9.0_r4android-security-9.0.0_r76android-security-9.0.0_r75android-security-9.0.0_r74android-security-9.0.0_r73android-security-9.0.0_r72android-security-9.0.0_r71android-security-9.0.0_r70android-security-9.0.0_r69android-security-9.0.0_r68android-security-9.0.0_r67android-security-9.0.0_r66android-security-9.0.0_r65android-security-9.0.0_r64android-security-9.0.0_r63android-security-9.0.0_r62android-cts-9.0_r5android-cts-9.0_r4android-cts-9.0_r3android-cts-9.0_r2android-cts-9.0_r1android-9.0.0_r9android-9.0.0_r8android-9.0.0_r7android-9.0.0_r61android-9.0.0_r60android-9.0.0_r6android-9.0.0_r59android-9.0.0_r58android-9.0.0_r57android-9.0.0_r56android-9.0.0_r55android-9.0.0_r54android-9.0.0_r53android-9.0.0_r52android-9.0.0_r51android-9.0.0_r50android-9.0.0_r5android-9.0.0_r49android-9.0.0_r48android-9.0.0_r3android-9.0.0_r2android-9.0.0_r18android-9.0.0_r17android-9.0.0_r10android-9.0.0_r1security-pi-releasepie-security-releasepie-s2-releasepie-release-2pie-releasepie-r2-s2-releasepie-r2-s1-releasepie-r2-release
Change-Id: If84e54ee1e30915790a9cb4c21008e55843acad6
Diffstat (limited to 'dexmaker-mockito-tests/src/androidTest/java/com/android/dx')
-rw-r--r--dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/BlacklistedApis.java161
1 files changed, 161 insertions, 0 deletions
diff --git a/dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/BlacklistedApis.java b/dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/BlacklistedApis.java
new file mode 100644
index 0000000..ffe55fb
--- /dev/null
+++ b/dexmaker-mockito-tests/src/androidTest/java/com/android/dx/mockito/tests/BlacklistedApis.java
@@ -0,0 +1,161 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed 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 com.android.dx.mockito.tests;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
+import static org.mockito.Mockito.doCallRealMethod;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.provider.Settings;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.runner.AndroidJUnit4;
+import android.view.View;
+import android.widget.FrameLayout;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Simplified versions of bugs seen in the past
+ */
+@RunWith(AndroidJUnit4.class)
+public class BlacklistedApis {
+ /**
+ * Check if the application is marked as {@code android:debuggable} in the manifest
+ *
+ * @return {@code true} iff it is marked as such
+ */
+ private boolean isDebuggable() throws PackageManager.NameNotFoundException {
+ Context context = InstrumentationRegistry.getTargetContext();
+ PackageInfo packageInfo = context.getPackageManager().getPackageInfo(
+ context.getPackageName(), 0);
+
+ return (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
+ }
+
+ @Test
+ public void callBlacklistedPublicMethodRealMethod() throws Exception {
+ Context targetContext = InstrumentationRegistry.getTargetContext();
+
+ FrameLayout child = new FrameLayout(targetContext);
+ FrameLayout parent = spy(new FrameLayout(targetContext));
+
+ if (isDebuggable()) {
+ // This calls a blacklisted public method.
+ // Since Android P these methods are not callable from outside of the Android framework
+ // anymore:
+ //
+ // https://android-developers.googleblog.com/2018/02/
+ // improving-stability-by-reducing-usage.html
+ //
+ // Hence if we use a subclass mock this will fail. Inline mocking does not have this
+ // problem as the mock class is the same as the mocked class.
+ parent.addView(child);
+ } else {
+ try {
+ parent.addView(child);
+ fail();
+ } catch (NoSuchMethodError expected) {
+ // expected
+ }
+ }
+ }
+
+ @Test
+ public void copyBlacklistedFields() throws Exception {
+ assumeTrue(isDebuggable());
+
+ Context targetContext = InstrumentationRegistry.getTargetContext();
+
+ FrameLayout child = new FrameLayout(targetContext);
+ FrameLayout parent = spy(new FrameLayout(targetContext));
+
+ parent.addView(child);
+
+ // During cloning of the parent spy, all fields are copied. This accesses a blacklisted
+ // fields. Since Android P these fields are not visible from outside of the Android
+ // framework anymore:
+ //
+ // https://android-developers.googleblog.com/2018/02/
+ // improving-stability-by-reducing-usage.html
+ //
+ // As 'measure' requires the fields to be initialized, this fails if the fields are not
+ // copied.
+ parent.measure(100, 100);
+ }
+
+ @Test
+ public void cannotCallBlackListedAfterSpying() {
+ // Spying and mocking might change the View class's byte code
+ spy(new View(InstrumentationRegistry.getTargetContext(), null));
+ mock(View.class);
+
+ // View#setNotifyAutofillManagerOnClick is a blacklisted method. Resolving it should fail
+ try {
+ View.class.getDeclaredMethod("setNotifyAutofillManagerOnClick", Boolean.TYPE);
+ fail();
+ } catch (NoSuchMethodException expected) {
+ // expected
+ }
+ }
+
+ public class CallBlackListedMethod {
+ public boolean callingBlacklistedMethodCausesException() {
+ // Settings.Global#isValidZenMode is a blacklisted method. Resolving it should fail
+ try {
+ Settings.Global.class.getDeclaredMethod("isValidZenMode", Integer.TYPE);
+ return false;
+ } catch (NoSuchMethodException expected) {
+ return true;
+ }
+ }
+ }
+
+ @Test
+ public void spiesCannotBeUsedToCallHiddenMethods() {
+ CallBlackListedMethod t = spy(new CallBlackListedMethod());
+ assertTrue(t.callingBlacklistedMethodCausesException());
+ }
+
+ public abstract class CallBlacklistedMethodAbstract {
+ public boolean callingBlacklistedMethodCausesException() {
+ // Settings.Global#isValidZenMode is a blacklisted method. Resolving it should fail
+ try {
+ Settings.Global.class.getDeclaredMethod("isValidZenMode", Integer.TYPE);
+ return false;
+ } catch (NoSuchMethodException expected) {
+ return true;
+ }
+ }
+
+ public abstract void unused();
+ }
+
+ @Test
+ public void mocksOfAbstractClassesCannotBeUsedToCallHiddenMethods() {
+ CallBlacklistedMethodAbstract t = mock(CallBlacklistedMethodAbstract.class);
+ doCallRealMethod().when(t).callingBlacklistedMethodCausesException();
+ assertTrue(t.callingBlacklistedMethodCausesException());
+ }
+}