1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tests.mtectrl; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.hamcrest.CoreMatchers.equalTo; 22 import static org.junit.Assume.assumeThat; 23 24 import com.android.tradefed.invoker.TestInformation; 25 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 26 import com.android.tradefed.testtype.junit4.AfterClassWithInfo; 27 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 28 import com.android.tradefed.testtype.junit4.BeforeClassWithInfo; 29 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 // Test the protocol described in 34 // https://source.android.com/docs/security/test/memory-safety/bootloader-support. 35 // This will reboot the device multiple times, which is perfectly normal. 36 37 @RunWith(DeviceJUnit4ClassRunner.class) 38 public class MtectrlEndToEndTest extends BaseHostJUnit4Test { 39 private static String mPreviousState = null; 40 41 @BeforeClassWithInfo setUp(TestInformation testInfo)42 public static void setUp(TestInformation testInfo) throws Exception { 43 assumeThat( 44 testInfo.getDevice().getProperty("ro.arm64.memtag.bootctl_supported"), 45 equalTo("1")); 46 mPreviousState = testInfo.getDevice().getProperty("arm64.memtag.bootctl"); 47 if (mPreviousState == null) { 48 mPreviousState = ""; 49 } 50 } 51 52 @AfterClassWithInfo tearDown(TestInformation testInfo)53 public static void tearDown(TestInformation testInfo) throws Exception { 54 if (mPreviousState != null) { 55 testInfo.getDevice().setProperty("arm64.memtag.bootctl", mPreviousState); 56 testInfo.getDevice().reboot(); 57 } 58 } 59 60 @Test testMemtagOnce()61 public void testMemtagOnce() throws Exception { 62 getDevice().setProperty("arm64.memtag.bootctl", "memtag-once"); 63 getDevice().reboot(); 64 assertThat(getDevice().getProperty("arm64.memtag.bootctl")).isAnyOf("", "none", null); 65 assertThat(getDevice().pullFileContents("/proc/cpuinfo")).contains("mte"); 66 getDevice().reboot(); 67 assertThat(getDevice().getProperty("arm64.memtag.bootctl")).isAnyOf("", "none", null); 68 assertThat(getDevice().pullFileContents("/proc/cpuinfo")).doesNotContain("mte"); 69 } 70 71 @Test testMemtag()72 public void testMemtag() throws Exception { 73 getDevice().setProperty("arm64.memtag.bootctl", "memtag"); 74 getDevice().reboot(); 75 assertThat(getDevice().getProperty("arm64.memtag.bootctl")).isEqualTo("memtag"); 76 assertThat(getDevice().pullFileContents("/proc/cpuinfo")).contains("mte"); 77 getDevice().reboot(); 78 assertThat(getDevice().getProperty("arm64.memtag.bootctl")).isEqualTo("memtag"); 79 assertThat(getDevice().pullFileContents("/proc/cpuinfo")).contains("mte"); 80 } 81 82 @Test testBoth()83 public void testBoth() throws Exception { 84 getDevice().setProperty("arm64.memtag.bootctl", "memtag,memtag-once"); 85 getDevice().reboot(); 86 assertThat(getDevice().getProperty("arm64.memtag.bootctl")).isEqualTo("memtag"); 87 assertThat(getDevice().pullFileContents("/proc/cpuinfo")).contains("mte"); 88 getDevice().reboot(); 89 assertThat(getDevice().getProperty("arm64.memtag.bootctl")).isEqualTo("memtag"); 90 assertThat(getDevice().pullFileContents("/proc/cpuinfo")).contains("mte"); 91 } 92 } 93