CTS verifying unbindBackupAgent() is not callable
Test: atest AgentBindingTest
Bug: 79700331
Change-Id: Ic151ae153f81889b340d055b1014db65075e819c
diff --git a/tests/backup/Android.mk b/tests/backup/Android.mk
index e12f2c4..985f372 100644
--- a/tests/backup/Android.mk
+++ b/tests/backup/Android.mk
@@ -27,7 +27,12 @@
android.test.base.stubs \
-LOCAL_STATIC_JAVA_LIBRARIES := compatibility-device-util ctstestrunner ctstestserver mockito-target-minus-junit4
+LOCAL_STATIC_JAVA_LIBRARIES := \
+ compatibility-device-util \
+ ctstestrunner \
+ ctstestserver \
+ mockito-target-minus-junit4 \
+ testng
LOCAL_SRC_FILES := $(call all-java-files-under, src)
diff --git a/tests/backup/src/android/backup/cts/AgentBindingTest.java b/tests/backup/src/android/backup/cts/AgentBindingTest.java
new file mode 100644
index 0000000..cfd97c7
--- /dev/null
+++ b/tests/backup/src/android/backup/cts/AgentBindingTest.java
@@ -0,0 +1,84 @@
+/*
+ * 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 android.backup.cts;
+
+import static android.support.test.InstrumentationRegistry.getInstrumentation;
+
+import static org.junit.Assume.assumeTrue;
+import static org.testng.Assert.expectThrows;
+
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.os.IBinder;
+
+import java.lang.reflect.Method;
+
+public class AgentBindingTest extends BaseBackupCtsTest {
+ private Context mContext;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ mContext = getInstrumentation().getTargetContext();
+ }
+
+ public void testUnbindBackupAgent_isNotCallableFromCts() throws Exception {
+ if (!isBackupSupported()) {
+ return;
+ }
+ expectThrows(Exception.class, () -> unbindBackupAgent(mContext.getApplicationInfo()));
+ }
+
+ public void testBindBackupAgent_isNotCallableFromCts() throws Exception {
+ if (!isBackupSupported()) {
+ return;
+ }
+ expectThrows(Exception.class, () -> bindBackupAgent(mContext.getPackageName(), 0, 0));
+ }
+
+ private static void unbindBackupAgent(ApplicationInfo applicationInfo) throws Exception {
+ callActivityManagerMethod(
+ "unbindBackupAgent",
+ new Class<?>[] {ApplicationInfo.class},
+ new Object[] {applicationInfo});
+ }
+
+ private static void bindBackupAgent(String packageName, int backupRestoreMode, int userId)
+ throws Exception {
+ callActivityManagerMethod(
+ "bindBackupAgent",
+ new Class<?>[] {String.class, int.class, int.class},
+ new Object[] {packageName, backupRestoreMode, userId});
+ }
+
+ private static void callActivityManagerMethod(
+ String methodName, Class<?>[] types, Object[] args) throws Exception {
+ Class<?> activityManagerClass = Class.forName("android.app.IActivityManager");
+ Object activityManager = getActivityManager();
+ Method bindBackupAgentMethod = activityManagerClass.getMethod(methodName, types);
+ bindBackupAgentMethod.invoke(activityManager, args);
+ }
+
+ private static Object getActivityManager() throws Exception {
+ Class<?> serviceManagerClass = Class.forName("android.os.ServiceManager");
+ Class<?> stubClass = Class.forName("android.app.IActivityManager$Stub");
+ Method asInterfaceMethod = stubClass.getMethod("asInterface", IBinder.class);
+ Method getServiceMethod = serviceManagerClass.getMethod("getService", String.class);
+ return asInterfaceMethod.invoke(
+ null, (IBinder) getServiceMethod.invoke(serviceManagerClass, "activity"));
+ }
+}