Add MTE Settings.

MTE is a technology to help prevent exploitation of some security bugs.
We want to offer users that want to trade off a slight reduction in
performance for higher security the option to do so from the settings
menu.

Test: make RunSettingsRoboTests
      check UI manually
Bug: 245624194
Change-Id: Ifbb76e124142ae843ce90bd604ae8417d65fcc7b
diff --git a/tests/robotests/Android.bp b/tests/robotests/Android.bp
index 7119100..26c4d19 100644
--- a/tests/robotests/Android.bp
+++ b/tests/robotests/Android.bp
@@ -80,6 +80,7 @@
         "SettingsLib-robo-testutils",
         "android-support-annotations",
         "androidx.test.core",
+        "androidx.test.rules",
         "androidx.test.runner",
         "androidx.test.ext.junit",
         "androidx.test.espresso.core",
diff --git a/tests/robotests/src/com/android/settings/security/MemtagHelperTest.java b/tests/robotests/src/com/android/settings/security/MemtagHelperTest.java
new file mode 100644
index 0000000..4ffbf5b
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/security/MemtagHelperTest.java
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2022 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.settings.security;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.os.SystemProperties;
+
+import com.android.settings.R;
+import com.android.settings.core.BasePreferenceController;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.annotation.Config;
+import org.robolectric.shadows.ShadowSystemProperties;
+
+@RunWith(RobolectricTestRunner.class)
+public class MemtagHelperTest {
+    private final String mMemtagProperty = "arm64.memtag.bootctl";
+    private final String mMemtagSupportedProperty = "ro.arm64.memtag.bootctl_supported";
+    private final String mDeviceConfigOverride =
+            "persist.device_config.memory_safety_native.bootloader_override";
+
+    @Test
+    public void isChecked_empty_isFalse() {
+        ShadowSystemProperties.override(mMemtagProperty, "");
+        assertThat(MemtagHelper.isChecked()).isFalse();
+    }
+
+    @Test
+    public void isChecked_memtag_isTrue() {
+        ShadowSystemProperties.override(mMemtagProperty, "memtag");
+        assertThat(MemtagHelper.isChecked()).isTrue();
+    }
+
+    @Test
+    public void isChecked_memtagAndKernel_isTrue() {
+        ShadowSystemProperties.override(mMemtagProperty, "memtag,memtag-kernel");
+        assertThat(MemtagHelper.isChecked()).isTrue();
+    }
+
+    @Test
+    public void isChecked_kernel_isFalse() {
+        ShadowSystemProperties.override(mMemtagProperty, "memtag-kernel");
+        assertThat(MemtagHelper.isChecked()).isFalse();
+    }
+
+    @Test
+    public void isChecked_kernelAndMemtag_isTrue() {
+        ShadowSystemProperties.override(mMemtagProperty, "memtag-kernel,memtag");
+        assertThat(MemtagHelper.isChecked()).isTrue();
+    }
+
+    @Test
+    public void SetChecked_true_isMemtag() {
+        MemtagHelper.setChecked(true);
+        assertThat(SystemProperties.get(mMemtagProperty)).isEqualTo("memtag");
+    }
+
+    @Test
+    public void SetChecked_false_isNone() {
+        MemtagHelper.setChecked(false);
+        assertThat(SystemProperties.get(mMemtagProperty)).isEqualTo("none");
+    }
+
+    @Test
+    public void getAvailabilityStatus_isForcedOff_isDISABLED_DEPENDENT_SETTING() {
+        ShadowSystemProperties.override(mDeviceConfigOverride, "force_off");
+        ShadowSystemProperties.override(mMemtagSupportedProperty, "true");
+        assertThat(MemtagHelper.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.DISABLED_DEPENDENT_SETTING);
+    }
+
+    @Test
+    public void getAvailabilityStatus_isUnsupported_isUNSUPPORTED_ON_DEVICE() {
+        ShadowSystemProperties.override(mDeviceConfigOverride, "");
+        ShadowSystemProperties.override(mMemtagSupportedProperty, "false");
+        assertThat(MemtagHelper.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_isSupported_isAVAILABLE() {
+        ShadowSystemProperties.override(mMemtagSupportedProperty, "true");
+        assertThat(MemtagHelper.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    @Config(shadows = {ZygoteShadow.class})
+    public void IsOn_zygoteSupportsMemoryTagging_isTrue() {
+        ZygoteShadow.setSupportsMemoryTagging(true);
+        assertThat(MemtagHelper.isOn()).isTrue();
+    }
+
+    @Test
+    @Config(shadows = {ZygoteShadow.class})
+    public void IsOn_noZygoteSupportsMemoryTagging_isFalse() {
+        ZygoteShadow.setSupportsMemoryTagging(false);
+        assertThat(MemtagHelper.isOn()).isFalse();
+    }
+
+    @Test
+    @Config(shadows = {ZygoteShadow.class})
+    public void getSummary_memtagAndZygoteSupportsMemoryTagging_memtag_on() {
+        ZygoteShadow.setSupportsMemoryTagging(true);
+        ShadowSystemProperties.override(mDeviceConfigOverride, "");
+        ShadowSystemProperties.override(mMemtagProperty, "memtag");
+        assertThat(MemtagHelper.getSummary()).isEqualTo(R.string.memtag_on);
+    }
+
+    @Test
+    @Config(shadows = {ZygoteShadow.class})
+    public void getSummary_noMemtagAndZygoteSupportsMemoryTagging_memtag_off_pending() {
+        ZygoteShadow.setSupportsMemoryTagging(true);
+        ShadowSystemProperties.override(mDeviceConfigOverride, "");
+        ShadowSystemProperties.override(mMemtagProperty, "");
+        assertThat(MemtagHelper.getSummary()).isEqualTo(R.string.memtag_off_pending);
+    }
+
+    @Test
+    @Config(shadows = {ZygoteShadow.class})
+    public void getSummary_noMemtagAndNoZygoteSupportsMemoryTagging_memtag_off() {
+        ZygoteShadow.setSupportsMemoryTagging(false);
+        ShadowSystemProperties.override(mDeviceConfigOverride, "");
+        ShadowSystemProperties.override(mMemtagProperty, "");
+        assertThat(MemtagHelper.getSummary()).isEqualTo(R.string.memtag_off);
+    }
+
+    @Test
+    @Config(shadows = {ZygoteShadow.class})
+    public void getSummary_memtagAndNoZygoteSupportsMemoryTagging_memtag_on_pending() {
+        ZygoteShadow.setSupportsMemoryTagging(false);
+        ShadowSystemProperties.override(mDeviceConfigOverride, "");
+        ShadowSystemProperties.override(mMemtagProperty, "memtag");
+        assertThat(MemtagHelper.getSummary()).isEqualTo(R.string.memtag_on_pending);
+    }
+
+    @Test
+    @Config(shadows = {ZygoteShadow.class})
+    public void getSummary_forceOffOverride_memtag_force_off() {
+        ZygoteShadow.setSupportsMemoryTagging(false);
+        ShadowSystemProperties.override(mDeviceConfigOverride, "force_off");
+        ShadowSystemProperties.override(mMemtagProperty, "memtag");
+        assertThat(MemtagHelper.getSummary()).isEqualTo(R.string.memtag_force_off);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/security/MemtagPageTest.java b/tests/robotests/src/com/android/settings/security/MemtagPageTest.java
new file mode 100644
index 0000000..a4fd21b
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/security/MemtagPageTest.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2022 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.settings.security;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.app.settings.SettingsEnums;
+import android.content.Context;
+
+import com.android.settings.R;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+@RunWith(RobolectricTestRunner.class)
+public class MemtagPageTest {
+
+    private MemtagPage mMemtagPage;
+    private Context mContext;
+
+    @Before
+    public void setUp() {
+        mMemtagPage = new MemtagPage();
+        mContext = RuntimeEnvironment.application;
+    }
+
+    @Test
+    public void getMetricsCategory_isSETTINGS_MEMTAG_CATEGORY() {
+        assertThat(mMemtagPage.getMetricsCategory())
+                .isEqualTo(SettingsEnums.SETTINGS_MEMTAG_CATEGORY);
+    }
+
+    @Test
+    public void getPreferenceScreenResId_isMemtag_page() {
+        assertThat(mMemtagPage.getPreferenceScreenResId()).isEqualTo(R.xml.memtag_page);
+    }
+
+    @Test
+    public void SEARCH_INDEX_DATA_PROVIDERgetPreferenceControllers_isNotEmpty() {
+        assertThat(MemtagPage.SEARCH_INDEX_DATA_PROVIDER.getPreferenceControllers(mContext))
+                .isNotEmpty();
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/security/MemtagPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/security/MemtagPreferenceControllerTest.java
new file mode 100644
index 0000000..2f8f658
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/security/MemtagPreferenceControllerTest.java
@@ -0,0 +1,161 @@
+/*
+ * Copyright (C) 2022 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.settings.security;
+
+import static androidx.test.espresso.Espresso.onView;
+import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
+import static androidx.test.espresso.matcher.RootMatchers.isDialog;
+import static androidx.test.espresso.matcher.ViewMatchers.withText;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.Context;
+import android.os.Bundle;
+
+import androidx.fragment.app.FragmentActivity;
+import androidx.fragment.app.FragmentContainerView;
+import androidx.test.rule.ActivityTestRule;
+
+import com.android.settings.R;
+import com.android.settings.testutils.shadow.ShadowDeviceConfig;
+import com.android.settingslib.testutils.shadow.ShadowInteractionJankMonitor;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+import org.robolectric.shadows.ShadowSystemProperties;
+
+@RunWith(RobolectricTestRunner.class)
+@Config(
+        shadows = {
+            ZygoteShadow.class,
+            ShadowDeviceConfig.class,
+            ShadowInteractionJankMonitor.class
+        })
+public class MemtagPreferenceControllerTest {
+    private final String mMemtagSupportedProperty = "ro.arm64.memtag.bootctl_supported";
+
+    @Rule
+    public ActivityTestRule<TestActivity> mActivityTestRule =
+            new ActivityTestRule<>(TestActivity.class);
+
+    private MemtagPage mMemtagPage;
+    private MemtagPreferenceController mController;
+    private Context mContext;
+    private TestActivity mActivity;
+
+    private static final String FRAGMENT_TAG = "memtag_page";
+
+    @Before
+    public void setUp() {
+        ShadowSystemProperties.override(mMemtagSupportedProperty, "true");
+
+        mContext = RuntimeEnvironment.application;
+        mMemtagPage = new MemtagPage();
+        mActivity = mActivityTestRule.getActivity();
+        mActivity
+                .getSupportFragmentManager()
+                .beginTransaction()
+                .add(TestActivity.CONTAINER_VIEW_ID, mMemtagPage)
+                .commit();
+        mController = new MemtagPreferenceController(mContext, FRAGMENT_TAG);
+        mController.setFragment(mMemtagPage);
+    }
+
+    @Test
+    public void getSliceHighlightMenuRes_isMenu_key_security() {
+        assertThat(mController.getSliceHighlightMenuRes()).isEqualTo(R.string.menu_key_security);
+    }
+
+    @Test
+    public void setChecked_isChecked_updatesSummary() {
+        ZygoteShadow.setSupportsMemoryTagging(true);
+        mController.setChecked(true);
+        assertThat(mController.getSummary())
+                .isEqualTo(mContext.getResources().getString(R.string.memtag_on));
+    }
+
+    @Test
+    public void setChecked_isUnchecked_updatesSummary() {
+        ZygoteShadow.setSupportsMemoryTagging(false);
+        mController.setChecked(false);
+        assertThat(mController.getSummary())
+                .isEqualTo(mContext.getResources().getString(R.string.memtag_off));
+    }
+
+    @Test
+    public void setChecked_isCheckedPending_updatesSummary() {
+        ZygoteShadow.setSupportsMemoryTagging(false);
+        mController.setChecked(true);
+        assertThat(mController.getSummary())
+                .isEqualTo(mContext.getResources().getString(R.string.memtag_on_pending));
+    }
+
+    @Test
+    public void setChecked_isUncheckedPending_updatesSummary() {
+        ZygoteShadow.setSupportsMemoryTagging(true);
+        mController.setChecked(false);
+        assertThat(mController.getSummary())
+                .isEqualTo(mContext.getResources().getString(R.string.memtag_off_pending));
+    }
+
+    @Test
+    public void setChecked_isCheckedPending_showsDialog() {
+        ZygoteShadow.setSupportsMemoryTagging(false);
+        mController.setChecked(true);
+        onView(withText(R.string.memtag_reboot_title)).inRoot(isDialog());
+    }
+
+    @Test
+    public void setChecked_isUncheckedPending_showsDialog() {
+        ZygoteShadow.setSupportsMemoryTagging(true);
+        mController.setChecked(false);
+        onView(withText(R.string.memtag_reboot_title)).inRoot(isDialog());
+    }
+
+    @Test
+    public void setChecked_isChecked_doesNotShowDialog() {
+        ZygoteShadow.setSupportsMemoryTagging(false);
+        mController.setChecked(false);
+        onView(withText(R.string.memtag_reboot_title)).inRoot(isDialog()).check(doesNotExist());
+    }
+
+    @Test
+    public void setChecked_isUnchecked_doesNotShowDialog() {
+        ZygoteShadow.setSupportsMemoryTagging(true);
+        mController.setChecked(true);
+        onView(withText(R.string.memtag_reboot_title)).inRoot(isDialog()).check(doesNotExist());
+    }
+
+    private static final class TestActivity extends FragmentActivity {
+
+        private static final int CONTAINER_VIEW_ID = 1234;
+
+        @Override
+        protected void onCreate(Bundle bundle) {
+            super.onCreate(bundle);
+
+            FragmentContainerView contentView = new FragmentContainerView(this);
+            contentView.setId(CONTAINER_VIEW_ID);
+            setContentView(contentView);
+        }
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/security/ZygoteShadow.java b/tests/robotests/src/com/android/settings/security/ZygoteShadow.java
new file mode 100644
index 0000000..23b30fa
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/security/ZygoteShadow.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2022 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.settings.security;
+
+import com.android.internal.os.Zygote;
+
+import org.robolectric.annotation.Implementation;
+import org.robolectric.annotation.Implements;
+
+@Implements(Zygote.class)
+public class ZygoteShadow {
+    private static boolean sSupportsMemoryTagging;
+
+    static void setSupportsMemoryTagging(boolean value) {
+        sSupportsMemoryTagging = value;
+    }
+
+    @Implementation
+    public static boolean nativeSupportsMemoryTagging() {
+        return sSupportsMemoryTagging;
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowStorageManager.java b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowStorageManager.java
index 11834b1..fce0498 100644
--- a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowStorageManager.java
+++ b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowStorageManager.java
@@ -16,6 +16,7 @@
 
 package com.android.settings.testutils.shadow;
 
+import android.annotation.NonNull;
 import android.os.storage.DiskInfo;
 import android.os.storage.StorageManager;
 import android.os.storage.VolumeInfo;
@@ -25,6 +26,9 @@
 import org.robolectric.annotation.Implements;
 import org.robolectric.annotation.Resetter;
 
+import java.util.ArrayList;
+import java.util.List;
+
 @Implements(StorageManager.class)
 public class ShadowStorageManager {
 
@@ -40,6 +44,10 @@
         return sIsForgetCalled;
     }
 
+    public @NonNull List<VolumeInfo> getVolumes() {
+        return new ArrayList<VolumeInfo>();
+    }
+
     @Resetter
     public static void reset() {
         sIsUnmountCalled = false;