[cts] test installed code path with reboot

Test that an installed app is still installed after reboot,
and the code path in PackageInfo is consistent with the path in
dumpsys.

Test: atest CtsCodePathHostTestCases
Change-Id: Ia353c176cc0a5e7473e84471a3a9b6dd8fbf473f
diff --git a/hostsidetests/packagemanager/OWNERS b/hostsidetests/packagemanager/OWNERS
new file mode 100644
index 0000000..5ee2e6d
--- /dev/null
+++ b/hostsidetests/packagemanager/OWNERS
@@ -0,0 +1,2 @@
+# Bug component: 36137
+pm-review@google.com
\ No newline at end of file
diff --git a/hostsidetests/packagemanager/codepath/Android.bp b/hostsidetests/packagemanager/codepath/Android.bp
new file mode 100644
index 0000000..7426f7c
--- /dev/null
+++ b/hostsidetests/packagemanager/codepath/Android.bp
@@ -0,0 +1,30 @@
+// Copyright (C) 2020 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.
+
+java_test_host {
+    name: "CtsCodePathHostTestCases",
+    defaults: ["cts_defaults"],
+    srcs: ["src/**/*.java"],
+    // tag this module as a cts test artifact
+    test_suites: [
+        "cts",
+        "vts",
+        "general-tests",
+    ],
+    libs: [
+        "cts-tradefed",
+        "tradefed",
+        "compatibility-host-util",
+    ],
+}
diff --git a/hostsidetests/packagemanager/codepath/AndroidTest.xml b/hostsidetests/packagemanager/codepath/AndroidTest.xml
new file mode 100644
index 0000000..2eb68ce8
--- /dev/null
+++ b/hostsidetests/packagemanager/codepath/AndroidTest.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2020 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.
+  -->
+<configuration description="Config for CTS package installation code path host test cases">
+    <option name="test-suite-tag" value="cts" />
+    <option name="config-descriptor:metadata" key="component" value="framework" />
+    <option name="config-descriptor:metadata" key="parameter" value="multi_abi" />
+    <option name="config-descriptor:metadata" key="parameter" value="not_instant_app" />
+    <option name="config-descriptor:metadata" key="parameter" value="secondary_user" />
+    <test class="com.android.compatibility.common.tradefed.testtype.JarHostTest" >
+        <option name="jar" value="CtsCodePathHostTestCases.jar" />
+    </test>
+</configuration>
+
diff --git a/hostsidetests/packagemanager/codepath/app/Android.bp b/hostsidetests/packagemanager/codepath/app/Android.bp
new file mode 100644
index 0000000..40bb86f
--- /dev/null
+++ b/hostsidetests/packagemanager/codepath/app/Android.bp
@@ -0,0 +1,28 @@
+// Copyright (C) 2020 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.
+
+
+android_test_helper_app {
+    name: "CodePathTestApp",
+    defaults: ["cts_defaults"],
+    sdk_version: "current",
+    srcs: ["src/**/*.java",],
+    test_suites: [
+        "cts",
+        "vts",
+        "general-tests",
+    ],
+    static_libs: ["androidx.test.rules"],
+    compile_multilib: "both",
+}
diff --git a/hostsidetests/packagemanager/codepath/app/AndroidManifest.xml b/hostsidetests/packagemanager/codepath/app/AndroidManifest.xml
new file mode 100644
index 0000000..a9d5a03
--- /dev/null
+++ b/hostsidetests/packagemanager/codepath/app/AndroidManifest.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!-- Copyright (C) 2020 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+          package="com.android.tests.codepath.app" >
+    <application>
+        <uses-library android:name="android.test.runner" />
+    </application>
+
+    <instrumentation
+        android:name="androidx.test.runner.AndroidJUnitRunner"
+        android:targetPackage="com.android.tests.codepath.app" />
+</manifest>
diff --git a/hostsidetests/packagemanager/codepath/app/src/com/android/tests/codepath/app/CodePathDeviceTest.java b/hostsidetests/packagemanager/codepath/app/src/com/android/tests/codepath/app/CodePathDeviceTest.java
new file mode 100644
index 0000000..dd909c8
--- /dev/null
+++ b/hostsidetests/packagemanager/codepath/app/src/com/android/tests/codepath/app/CodePathDeviceTest.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2020 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.tests.codepath.app;
+
+import android.content.pm.PackageInfo;
+
+import androidx.test.InstrumentationRegistry;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(AndroidJUnit4.class)
+public class CodePathDeviceTest {
+    @Test
+    public void testCodePathMatchesExpected() throws Exception {
+        String expectedCodePath =
+                InstrumentationRegistry.getArguments().getString("expectedCodePath");
+        String packageName =
+                InstrumentationRegistry.getInstrumentation().getContext().getPackageName();
+        PackageInfo pi = InstrumentationRegistry.getInstrumentation().getContext()
+                .getPackageManager().getPackageInfo(packageName, 0);
+        String apkPath = pi.applicationInfo.sourceDir;
+        Assert.assertTrue(apkPath.startsWith(expectedCodePath));
+    }
+}
diff --git a/hostsidetests/packagemanager/codepath/src/com/android/tests/codepath/host/CodePathTest.java b/hostsidetests/packagemanager/codepath/src/com/android/tests/codepath/host/CodePathTest.java
new file mode 100644
index 0000000..32763dd
--- /dev/null
+++ b/hostsidetests/packagemanager/codepath/src/com/android/tests/codepath/host/CodePathTest.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2020 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.tests.codepath.host;
+
+import android.platform.test.annotations.AppModeFull;
+
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.device.PackageInfo;
+import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
+import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@RunWith(DeviceJUnit4ClassRunner.class)
+public class CodePathTest extends BaseHostJUnit4Test {
+    private static final String TEST_APK = "CodePathTestApp.apk";
+    private static final String TEST_PACKAGE = "com.android.tests.codepath.app";
+    private static final String TEST_CLASS = TEST_PACKAGE + "." + "CodePathDeviceTest";
+    private static final String TEST_METHOD = "testCodePathMatchesExpected";
+    private static final String CODE_PATH_ROOT = "/data/app";
+    private static final long DEFAULT_TIMEOUT = 10 * 60 * 1000L;
+
+    /** Uninstall apps after tests. */
+    @After
+    public void cleanUp() throws Exception {
+        uninstallPackage(getDevice(), TEST_PACKAGE);
+        Assert.assertFalse(isPackageInstalled(TEST_PACKAGE));
+    }
+
+    @Test
+    @AppModeFull
+    public void testAppInstallsWithReboot() throws Exception {
+        installPackage(TEST_APK);
+        Assert.assertTrue(isPackageInstalled(TEST_PACKAGE));
+        final String codePathFromDumpsys = getCodePathFromDumpsys(TEST_PACKAGE);
+        Assert.assertTrue(codePathFromDumpsys.startsWith(CODE_PATH_ROOT));
+        runDeviceTest(codePathFromDumpsys);
+        // Code paths should still be valid after reboot
+        getDevice().reboot();
+        final String codePathFromDumpsysAfterReboot = getCodePathFromDumpsys(TEST_PACKAGE);
+        Assert.assertEquals(codePathFromDumpsys, codePathFromDumpsysAfterReboot);
+        runDeviceTest(codePathFromDumpsys);
+    }
+
+    private String getCodePathFromDumpsys(String packageName)
+            throws DeviceNotAvailableException {
+        PackageInfo packageInfo = getDevice().getAppPackageInfo(packageName);
+        return packageInfo.getCodePath();
+    }
+
+    private void runDeviceTest(String codePathToMatch) throws Exception {
+        final Map<String, String> testArgs = new HashMap<>();
+        testArgs.put("expectedCodePath", codePathToMatch);
+        runDeviceTests(getDevice(), null, TEST_PACKAGE, TEST_CLASS, TEST_METHOD,
+                null, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT,
+                0L, true, false, testArgs);
+    }
+}
\ No newline at end of file