CTS test for isMtePolicyEnforced API

Bug: 322777918
Change-Id: I7c3f9732ff2c0f677c9dd9543c383b559bb8a170
diff --git a/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/ParentProfileTest.java b/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/ParentProfileTest.java
index c65d305..273a944 100644
--- a/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/ParentProfileTest.java
+++ b/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/ParentProfileTest.java
@@ -104,6 +104,7 @@
             .add("setPermittedInputMethods")
             .add("getDevicePolicyManagementRoleHolderPackage")
             .add("getResources")
+            .add("isMtePolicyEnforced")
             .build();
 
     private static final String LOG_TAG = "ParentProfileTest";
diff --git a/hostsidetests/tagging/Android.bp b/hostsidetests/tagging/Android.bp
index 7c755f3..a47c1de 100644
--- a/hostsidetests/tagging/Android.bp
+++ b/hostsidetests/tagging/Android.bp
@@ -11,7 +11,11 @@
         "cts-tradefed",
         "tradefed",
     ],
-    static_libs: ["CompatChangeGatingTestBase"],
+    static_libs: [
+        "CompatChangeGatingTestBase",
+        "flag-junit-host",
+        "HarrierCommon",
+    ],
     // tag this module as a cts test artifact
     test_suites: [
         "cts",
@@ -25,6 +29,7 @@
         ":CtsHostsideTaggingSdk29App",
         ":CtsHostsideTaggingSdk30App",
         ":CtsHostsideTaggingSdk30MemtagApp",
+        ":CtsHostsideTaggingIsMteEnabledApp",
     ],
 }
 
diff --git a/hostsidetests/tagging/AndroidTest.xml b/hostsidetests/tagging/AndroidTest.xml
index e784407..0b53ba2 100644
--- a/hostsidetests/tagging/AndroidTest.xml
+++ b/hostsidetests/tagging/AndroidTest.xml
@@ -24,4 +24,8 @@
     <test class="com.android.compatibility.common.tradefed.testtype.JarHostTest" >
         <option name="jar" value="CtsTaggingHostTestCases.jar" />
     </test>
+    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
+        <option name="cleanup-apks" value="true" />
+        <option name="test-file-name" value="CtsHostsideTaggingIsMteEnabledApp.apk" />
+    </target_preparer>
 </configuration>
diff --git a/hostsidetests/tagging/is_mte_policy_enforced/Android.bp b/hostsidetests/tagging/is_mte_policy_enforced/Android.bp
new file mode 100644
index 0000000..f406952
--- /dev/null
+++ b/hostsidetests/tagging/is_mte_policy_enforced/Android.bp
@@ -0,0 +1,32 @@
+// Copyright (C) 2024 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 {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+android_test_helper_app {
+    name: "CtsHostsideTaggingIsMteEnabledApp",
+    defaults: ["cts_tagging_app_defaults"],
+    srcs: ["src/**/*.java"],
+    test_suites: [
+        "general-tests",
+    ],
+    static_libs: [
+        "androidx.test.ext.truth",
+        "hamcrest-library",
+        "flag-junit",
+        "device_policy_aconfig_flags_lib",
+    ],
+}
diff --git a/hostsidetests/tagging/is_mte_policy_enforced/AndroidManifest.xml b/hostsidetests/tagging/is_mte_policy_enforced/AndroidManifest.xml
new file mode 100644
index 0000000..6375f19
--- /dev/null
+++ b/hostsidetests/tagging/is_mte_policy_enforced/AndroidManifest.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2024 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="android.cts.tagging.is_mte_policy_enforced">
+
+    <uses-sdk android:minSdkVersion="30" android:targetSdkVersion="30" />
+
+    <application>
+        <uses-library android:name="android.test.runner" />
+    </application>
+
+    <instrumentation
+        android:name="androidx.test.runner.AndroidJUnitRunner"
+        android:targetPackage="android.cts.tagging.is_mte_enabled" />
+</manifest>
+
diff --git a/hostsidetests/tagging/is_mte_policy_enforced/src/android/cts/tagging/is_mte_policy_enforced/TaggingTest.java b/hostsidetests/tagging/is_mte_policy_enforced/src/android/cts/tagging/is_mte_policy_enforced/TaggingTest.java
new file mode 100644
index 0000000..2f3e979
--- /dev/null
+++ b/hostsidetests/tagging/is_mte_policy_enforced/src/android/cts/tagging/is_mte_policy_enforced/TaggingTest.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2024 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.cts.tagging.is_mte_policy_enforced;
+
+import android.app.admin.flags.Flags;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.app.admin.DevicePolicyManager;
+
+import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.Rule;
+import org.junit.runner.RunWith;
+
+// This is driven by MemtagBootctlTest from the host, which enables / disables
+// MTE, reboots the device, and then calls testMteIsEnabled / testMteIsDisabled
+// to check whether this API returns the correct state.
+@RunWith(AndroidJUnit4.class)
+@SmallTest
+public final class TaggingTest {
+    @Test
+    public void testMtePolicyEnforcedTrue() {
+        if (Flags.isMtePolicyEnforced()) {
+            assertThat(DevicePolicyManager.isMtePolicyEnforced()).isTrue();
+        }
+    }
+    @Test
+    public void testMtePolicyEnforcedFalse() {
+        if (Flags.isMtePolicyEnforced()) {
+            assertThat(DevicePolicyManager.isMtePolicyEnforced()).isTrue();
+        }
+    }
+}
diff --git a/hostsidetests/tagging/src/com/android/cts/tagging/MemtagBootctlTest.java b/hostsidetests/tagging/src/com/android/cts/tagging/MemtagBootctlTest.java
index a1f85cf..4de358d 100644
--- a/hostsidetests/tagging/src/com/android/cts/tagging/MemtagBootctlTest.java
+++ b/hostsidetests/tagging/src/com/android/cts/tagging/MemtagBootctlTest.java
@@ -21,12 +21,14 @@
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.junit.Assume.assumeThat;
 
+import com.android.bedstead.harrier.annotations.enterprise.DevicePolicyRelevant;
 import com.android.compatibility.common.util.CddTest;
 import com.android.tradefed.invoker.TestInformation;
 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
 import com.android.tradefed.testtype.junit4.AfterClassWithInfo;
 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
 import com.android.tradefed.testtype.junit4.BeforeClassWithInfo;
+import com.android.tradefed.testtype.junit4.DeviceTestRunOptions;
 
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -36,7 +38,10 @@
 // This will reboot the device multiple times, which is perfectly normal.
 
 @RunWith(DeviceJUnit4ClassRunner.class)
+@DevicePolicyRelevant
 public class MemtagBootctlTest extends BaseHostJUnit4Test {
+    protected static final String TEST_PKG = "android.cts.tagging.is_mte_policy_enforced";
+    protected static final String DEVICE_TEST_CLASS_NAME = "TaggingTest";
     private static String mPreviousState = null;
 
     @BeforeClassWithInfo
@@ -58,16 +63,33 @@
         }
     }
 
+    private void runDeviceTest(String method) throws Exception {
+        var options = new DeviceTestRunOptions(TEST_PKG);
+        options.setTestClassName(TEST_PKG + "." + DEVICE_TEST_CLASS_NAME);
+        options.setTestMethodName(method);
+        runDeviceTests(options);
+    }
+
+    void checkMteOn() throws Exception {
+        assertThat(getDevice().pullFileContents("/proc/cpuinfo")).contains(" mte");
+        runDeviceTest("testMteIsEnabled");
+    }
+
+    void checkMteOff() throws Exception {
+        assertThat(getDevice().pullFileContents("/proc/cpuinfo")).doesNotContain(" mte");
+        runDeviceTest("testMteIsDisabled");
+    }
+
     @Test
     @CddTest(requirements = {"9.7/C-3-1,9.7/C-3-2,9.7/C-3-3"})
     public void testMemtagOnce() throws Exception {
         getDevice().setProperty("arm64.memtag.bootctl", "memtag-once");
         getDevice().reboot();
         assertThat(getDevice().getProperty("arm64.memtag.bootctl")).isAnyOf("", "none", null);
-        assertThat(getDevice().pullFileContents("/proc/cpuinfo")).contains(" mte");
+        checkMteOn();
         getDevice().reboot();
         assertThat(getDevice().getProperty("arm64.memtag.bootctl")).isAnyOf("", "none", null);
-        assertThat(getDevice().pullFileContents("/proc/cpuinfo")).doesNotContain(" mte");
+        checkMteOff();
     }
 
     @Test
@@ -76,10 +98,10 @@
         getDevice().setProperty("arm64.memtag.bootctl", "memtag-off");
         getDevice().reboot();
         assertThat(getDevice().getProperty("arm64.memtag.bootctl")).isEqualTo("memtag-off");
-        assertThat(getDevice().pullFileContents("/proc/cpuinfo")).doesNotContain(" mte");
+        checkMteOff();
         getDevice().reboot();
         assertThat(getDevice().getProperty("arm64.memtag.bootctl")).isEqualTo("memtag-off");
-        assertThat(getDevice().pullFileContents("/proc/cpuinfo")).doesNotContain(" mte");
+        checkMteOff();
     }
 
     @Test
@@ -88,10 +110,10 @@
         getDevice().setProperty("arm64.memtag.bootctl", "memtag");
         getDevice().reboot();
         assertThat(getDevice().getProperty("arm64.memtag.bootctl")).isEqualTo("memtag");
-        assertThat(getDevice().pullFileContents("/proc/cpuinfo")).contains(" mte");
+        checkMteOn();
         getDevice().reboot();
         assertThat(getDevice().getProperty("arm64.memtag.bootctl")).isEqualTo("memtag");
-        assertThat(getDevice().pullFileContents("/proc/cpuinfo")).contains(" mte");
+        checkMteOn();
     }
 
     @Test
@@ -100,9 +122,9 @@
         getDevice().setProperty("arm64.memtag.bootctl", "memtag,memtag-once");
         getDevice().reboot();
         assertThat(getDevice().getProperty("arm64.memtag.bootctl")).isEqualTo("memtag");
-        assertThat(getDevice().pullFileContents("/proc/cpuinfo")).contains(" mte");
+        checkMteOn();
         getDevice().reboot();
         assertThat(getDevice().getProperty("arm64.memtag.bootctl")).isEqualTo("memtag");
-        assertThat(getDevice().pullFileContents("/proc/cpuinfo")).contains(" mte");
+        checkMteOn();
     }
 }