1 /*
2  * Copyright (C) 2018 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 package com.android.compatibility.common.tradefed.targetprep;
17 
18 import com.android.tradefed.config.Option;
19 import com.android.tradefed.device.DeviceNotAvailableException;
20 import com.android.tradefed.device.ITestDevice;
21 import com.android.tradefed.invoker.TestInformation;
22 import com.android.tradefed.targetprep.BaseTargetPreparer;
23 import com.android.tradefed.targetprep.BuildError;
24 import com.android.tradefed.targetprep.TargetSetupError;
25 
26 /**
27  * Special preparer used to check the build fingerprint of a device against an expected one.
28  *
29  * <p>An "unaltered" fingerprint might be available. Which reflects that the official fingerprint
30  * has been modified for business reason, but we still want to validate the original device is the
31  * same.
32  */
33 public final class BuildFingerPrintPreparer extends BaseTargetPreparer {
34 
35     /**
36      * These 3 options cannot really be injected directly, but are needed to be copied during retry
37      * and sharding.
38      */
39     @Option(name = "expected-fingerprint")
40     private String mExpectedFingerprint = null;
41 
42     @Option(name = "expected-vendor-fingerprint")
43     private String mExpectedVendorFingerprint = null;
44 
45     @Option(name = "unaltered-fingerprint")
46     private String mUnalteredFingerprint = null;
47 
48     private String mFingerprintProperty = "ro.build.fingerprint";
49     private String mVendorFingerprintProperty = "ro.vendor.build.fingerprint";
50 
51     @Override
setUp(TestInformation testInfo)52     public void setUp(TestInformation testInfo)
53             throws TargetSetupError, BuildError, DeviceNotAvailableException {
54         ITestDevice device = testInfo.getDevice();
55         if (mExpectedFingerprint == null) {
56             throw new TargetSetupError("build fingerprint shouldn't be null",
57                     device.getDeviceDescriptor());
58         }
59         try {
60             String compare = mExpectedFingerprint;
61             if (mUnalteredFingerprint != null) {
62                 compare = mUnalteredFingerprint;
63             }
64             String currentBuildFingerprint = device.getProperty(mFingerprintProperty);
65             if (!compare.equals(currentBuildFingerprint)) {
66                 throw new TargetSetupError(
67                         String.format(
68                                 "Device build fingerprint must match '%s'. Found '%s' instead.",
69                                 compare, currentBuildFingerprint),
70                         device.getDeviceDescriptor());
71             }
72             if (mExpectedVendorFingerprint != null) {
73                 String currentBuildVendorFingerprint =
74                         device.getProperty(mVendorFingerprintProperty);
75                 // Replace by empty string if null to do a proper comparison.
76                 if (currentBuildVendorFingerprint == null) {
77                     currentBuildVendorFingerprint = "";
78                 }
79                 if (!mExpectedVendorFingerprint.equals(currentBuildVendorFingerprint)) {
80                     throw new TargetSetupError(
81                             String.format(
82                                     "Device vendor build fingerprint must match '%s'. Found '%s' "
83                                             + "instead.",
84                                     mExpectedVendorFingerprint, currentBuildVendorFingerprint),
85                             device.getDeviceDescriptor());
86                 }
87             }
88         } catch (DeviceNotAvailableException e) {
89             throw new RuntimeException(e);
90         }
91     }
92 
93     /**
94      * Sets the expected fingerprint we are checking against.
95      */
setExpectedFingerprint(String expectedFingerprint)96     public void setExpectedFingerprint(String expectedFingerprint) {
97         mExpectedFingerprint = expectedFingerprint;
98     }
99 
100     /**
101      * Returns the expected fingerprint.
102      */
getExpectedFingerprint()103     public String getExpectedFingerprint() {
104         return mExpectedFingerprint;
105     }
106 
107     /**
108      * Allow to override the base fingerprint property. In some cases, we want to check the
109      * "ro.vendor.build.fingerpint" for example.
110      */
setFingerprintProperty(String property)111     public void setFingerprintProperty(String property) {
112         mFingerprintProperty = property;
113     }
114 
115     /** Sets the unchanged original fingerprint. */
setUnalteredFingerprint(String unalteredFingerprint)116     public void setUnalteredFingerprint(String unalteredFingerprint) {
117         mUnalteredFingerprint = unalteredFingerprint;
118     }
119 
120     /** Set the property value associated with ro.vendor.build.fingerprint */
setExpectedVendorFingerprint(String expectedVendorFingerprint)121     public void setExpectedVendorFingerprint(String expectedVendorFingerprint) {
122         mExpectedVendorFingerprint = expectedVendorFingerprint;
123     }
124 }
125