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.microdroid.test.common; 18 19 import static java.util.Objects.requireNonNull; 20 21 /** This class can be used in both host tests and device tests to get the device properties. */ 22 public final class DeviceProperties { 23 24 /** PropertyGetter is used to get the property associated to a given key. */ 25 public interface PropertyGetter { getProperty(String key)26 String getProperty(String key) throws Exception; 27 } 28 29 private static final String KEY_VENDOR_DEVICE = "ro.product.vendor.device"; 30 private static final String KEY_BUILD_TYPE = "ro.build.type"; 31 private static final String KEY_PRODUCT_NAME = "ro.product.name"; 32 private static final String KEY_METRICS_TAG = "debug.hypervisor.metrics_tag"; 33 34 private static final String CUTTLEFISH_DEVICE_PREFIX = "vsoc_"; 35 private static final String CUTTLEFISH_ARM64_DEVICE_PREFIX = "vsoc_arm64"; 36 private static final String GOLDFISH_DEVICE_PREFIX = "emu64"; 37 private static final String GOLDFISH_ARM64_DEVICE_PREFIX = "emu64a"; 38 private static final String USER_BUILD_TYPE = "user"; 39 private static final String HWASAN_SUFFIX = "_hwasan"; 40 41 private final PropertyGetter mPropertyGetter; 42 DeviceProperties(PropertyGetter propertyGetter)43 private DeviceProperties(PropertyGetter propertyGetter) { 44 mPropertyGetter = requireNonNull(propertyGetter); 45 } 46 47 /** Creates a new instance of {@link DeviceProperties}. */ create(PropertyGetter propertyGetter)48 public static DeviceProperties create(PropertyGetter propertyGetter) { 49 return new DeviceProperties(propertyGetter); 50 } 51 52 /** 53 * @return whether the device is a cuttlefish device. 54 */ isCuttlefish()55 public boolean isCuttlefish() { 56 String vendorDeviceName = getProperty(KEY_VENDOR_DEVICE); 57 return vendorDeviceName != null && vendorDeviceName.startsWith(CUTTLEFISH_DEVICE_PREFIX); 58 } 59 60 /** 61 * @return whether the device is a cuttlefish device running on 64 bit Arm. 62 */ isCuttlefishArm64()63 public boolean isCuttlefishArm64() { 64 String vendorDeviceName = getProperty(KEY_VENDOR_DEVICE); 65 return vendorDeviceName != null 66 && vendorDeviceName.startsWith(CUTTLEFISH_ARM64_DEVICE_PREFIX); 67 } 68 69 /** 70 * @return whether the device is a cuttlefish device. 71 */ isGoldfish()72 public boolean isGoldfish() { 73 String vendorDeviceName = getProperty(KEY_VENDOR_DEVICE); 74 return vendorDeviceName != null && vendorDeviceName.startsWith(GOLDFISH_DEVICE_PREFIX); 75 } 76 77 /** 78 * @return whether the device is a cuttlefish device running on 64 bit Arm. 79 */ isGoldfishArm64()80 public boolean isGoldfishArm64() { 81 String vendorDeviceName = getProperty(KEY_VENDOR_DEVICE); 82 return vendorDeviceName != null 83 && vendorDeviceName.startsWith(GOLDFISH_ARM64_DEVICE_PREFIX); 84 } 85 86 /** 87 * @return whether the build is HWASAN. 88 */ isHwasan()89 public boolean isHwasan() { 90 String productName = getProperty(KEY_PRODUCT_NAME); 91 return productName != null && productName.contains(HWASAN_SUFFIX); 92 } 93 94 /** 95 * @return whether the device is user build. 96 */ isUserBuild()97 public boolean isUserBuild() { 98 return USER_BUILD_TYPE.equals(getProperty(KEY_BUILD_TYPE)); 99 } 100 getMetricsTag()101 public String getMetricsTag() { 102 return getProperty(KEY_METRICS_TAG); 103 } 104 getProperty(String key)105 private String getProperty(String key) { 106 try { 107 return mPropertyGetter.getProperty(key); 108 } catch (Exception e) { 109 throw new IllegalArgumentException("Cannot get property for the key: " + key, e); 110 } 111 } 112 } 113