1 /*
2  * Copyright (C) 2016 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.compatibility.common.tradefed.targetprep;
18 
19 import com.android.tradefed.build.IBuildInfo;
20 import com.android.tradefed.config.Option;
21 import com.android.tradefed.device.DeviceNotAvailableException;
22 import com.android.tradefed.device.ITestDevice;
23 import com.android.tradefed.invoker.TestInformation;
24 import com.android.tradefed.targetprep.BuildError;
25 import com.android.tradefed.targetprep.ITargetPreparer;
26 import com.android.tradefed.targetprep.TargetSetupError;
27 import com.android.tradefed.util.ArrayUtil;
28 
29 import com.google.api.client.util.Strings;
30 
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 
35 /**
36  * Collects device info. This's a fork of DeviceInfoCollector and is forked in order to simplify the
37  * change deployment process and reduce the deployment time, which are critical for VTS services.
38  */
39 public class VtsDeviceInfoCollector implements ITargetPreparer {
40     // TODO(trong): remove "cts:" prefix, will need a custom ResultReporter.
41     private static final Map<String, String> BUILD_KEYS = new HashMap<>();
42     private static final Map<String, String> BUILD_LEGACY_PROPERTIES = new HashMap<>();
43 
44     // The name of a system property which tells whether to stop properly configured
45     // native servers where properly configured means a server's init.rc is
46     // configured to stop when that property's value is 1.
47     static final String SYSPROP_VTS_NATIVE_SERVER = "vts.native_server.on";
48 
49     @Deprecated
50     @Option(name = "disable-framework", description = "Initialize device by stopping framework.")
51     private boolean mDisableFramework = false;
52 
53     static {
54         BUILD_KEYS.put("cts:build_id", "ro.build.id");
55         BUILD_KEYS.put("cts:build_product", "ro.product.name");
56         BUILD_KEYS.put("cts:build_device", "ro.product.device");
57         BUILD_KEYS.put("cts:build_board", "ro.product.board");
58         BUILD_KEYS.put("cts:build_manufacturer", "ro.product.vendor.manufacturer");
59         BUILD_KEYS.put("cts:build_brand", "ro.product.brand");
60         BUILD_KEYS.put("cts:build_model", "ro.product.vendor.model");
61         BUILD_KEYS.put("cts:build_type", "ro.build.type");
62         BUILD_KEYS.put("cts:build_tags", "ro.build.tags");
63         /**
64          * build_fingerprint is used for certification; build_fingerprint_unaltered is for retry.
65          */
66         BUILD_KEYS.put("cts:build_fingerprint_unaltered", "ro.build.fingerprint");
67         BUILD_KEYS.put("cts:build_abi", "ro.product.cpu.abi");
68         BUILD_KEYS.put("cts:build_abi2", "ro.product.cpu.abi2");
69         BUILD_KEYS.put("cts:build_abis", "ro.product.cpu.abilist");
70         BUILD_KEYS.put("cts:build_abis_32", "ro.product.cpu.abilist32");
71         BUILD_KEYS.put("cts:build_abis_64", "ro.product.cpu.abilist64");
72         BUILD_KEYS.put("cts:build_first_api_level", "ro.product.first_api_level");
73         BUILD_KEYS.put("cts:build_serial", "ro.serialno");
74         BUILD_KEYS.put("cts:build_version_release", "ro.build.version.release");
75         BUILD_KEYS.put("cts:build_version_sdk", "ro.build.version.sdk");
76         BUILD_KEYS.put("cts:build_version_base_os", "ro.build.version.base_os");
77         BUILD_KEYS.put("cts:build_version_security_patch", "ro.build.version.security_patch");
78         BUILD_KEYS.put("cts:build_reference_fingerprint", "ro.build.reference.fingerprint");
79         BUILD_KEYS.put("cts:build_system_fingerprint", "ro.build.fingerprint");
80         BUILD_KEYS.put("cts:build_vendor_fingerprint", "ro.vendor.build.fingerprint");
81         BUILD_KEYS.put("cts:build_vendor_manufacturer", "ro.product.vendor.manufacturer");
82         BUILD_KEYS.put("cts:build_vendor_model", "ro.product.vendor.model");
83 
84         BUILD_LEGACY_PROPERTIES.put(
85                 "ro.product.vendor.manufacturer", "ro.vendor.product.manufacturer");
86         BUILD_LEGACY_PROPERTIES.put("ro.product.vendor.model", "ro.vendor.product.model");
87     }
88 
89     @Override
setUp(TestInformation testInfo)90     public void setUp(TestInformation testInfo)
91             throws TargetSetupError, BuildError, DeviceNotAvailableException {
92         ITestDevice device = testInfo.getDevice();
93         IBuildInfo buildInfo = testInfo.getBuildInfo();
94         for (Entry<String, String> entry : BUILD_KEYS.entrySet()) {
95             String propertyValue = device.getProperty(entry.getValue());
96             if ((propertyValue == null || propertyValue.length() == 0)
97                     && BUILD_LEGACY_PROPERTIES.containsKey(entry.getValue())) {
98                 propertyValue = device.getProperty(BUILD_LEGACY_PROPERTIES.get(entry.getValue()));
99             }
100             buildInfo.addBuildAttribute(entry.getKey(), ArrayUtil.join(",", propertyValue));
101         }
102 
103         // cts:build_fingerprint in the report consists of:
104         // {build_brand}/{build_product}/{build_device}:{build_version_release}/{build_id}/
105         // {build_version_incremental}:{build_type}/{build_tags}
106         //
107         // cts:build_version_incremental should be the same as the {build_version_incremental} in
108         // cts:build_fingerprint. If ODM fingerprint exists, the value is
109         // {ro.odm.build.version.incremental}. Otherwise, it is
110         // {ro.vendor.build.version.incremental}.
111         //
112         // The {build_device} in {ro.odm.build.fingerprint} can be {ro.product.device} or
113         // {ro.product.odm.device}_{ro.boot.product.hardware.sku}. The latter one is rewritten as
114         // {ro.product.odm.device}.
115         String buildFingerprint = device.getProperty("ro.odm.build.fingerprint");
116         String buildVersionIncremental = null;
117         if (!Strings.isNullOrEmpty(buildFingerprint)) {
118             String[] splitBuildFingerprint = buildFingerprint.split("/");
119             if (splitBuildFingerprint.length <= 2) {
120                 throw new TargetSetupError(
121                         "Cannot parse ODM fingerprint: " + buildFingerprint,
122                         device.getDeviceDescriptor());
123             }
124             // Need root to read that property
125             buildVersionIncremental = device.getProperty("ro.odm.build.version.incremental");
126 
127             String odmDevice = device.getProperty("ro.product.odm.device");
128             String sku = device.getProperty("ro.boot.product.hardware.sku");
129             if (!Strings.isNullOrEmpty(odmDevice) && !Strings.isNullOrEmpty(sku)) {
130                 String odmDeviceAndSku = odmDevice + "_" + sku;
131                 if (splitBuildFingerprint[2].startsWith(odmDeviceAndSku + ":")) {
132                     splitBuildFingerprint[2] =
133                             odmDevice
134                                     + splitBuildFingerprint[2].substring(odmDeviceAndSku.length());
135                     buildFingerprint = String.join("/", splitBuildFingerprint);
136                 }
137             }
138         } else {
139             buildFingerprint = device.getProperty("ro.vendor.build.fingerprint");
140             // Need root to read that property
141             buildVersionIncremental = device.getProperty("ro.vendor.build.version.incremental");
142         }
143 
144         buildInfo.addBuildAttribute("cts:build_fingerprint", nullToEmpty(buildFingerprint));
145         buildInfo.addBuildAttribute(
146                 "cts:build_version_incremental", nullToEmpty(buildVersionIncremental));
147     }
148 
nullToEmpty(String value)149     private static String nullToEmpty(String value) {
150         return value == null ? "" : value;
151     }
152 }
153