1 /*
2  * Copyright (C) 2015 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.config.OptionClass;
20 import com.android.tradefed.device.DeviceNotAvailableException;
21 import com.android.tradefed.device.ITestDevice;
22 import com.android.tradefed.invoker.TestInformation;
23 import com.android.tradefed.log.LogUtil.CLog;
24 import com.android.tradefed.targetprep.BuildError;
25 import com.android.tradefed.targetprep.TargetSetupError;
26 
27 /**
28  * Checks that a device property is as expected
29  */
30 @OptionClass(alias="property-check")
31 public class PropertyCheck extends PreconditionPreparer {
32 
33     @Option(
34         name = "property-name",
35         description = "The name of the property to check",
36         mandatory = true
37     )
38     private String mPropertyName = null;
39 
40     @Option(name = "expected-value", description = "The expected value of the property")
41     private String mExpectedPropertyValue = null;
42 
43     @Option(
44             name = "is-set-only",
45             description = "Whether this value must be set only (don't check value)")
46     private boolean mPropertyValueIsSetOnly = false;
47 
48     @Option(
49             name = "throw-error",
50             description = "Whether to throw an error for an unexpected property value")
51     private boolean mThrowError = false;
52 
53     @Override
run(TestInformation testInfo)54     public void run(TestInformation testInfo)
55             throws TargetSetupError, BuildError, DeviceNotAvailableException {
56         ITestDevice device = testInfo.getDevice();
57         String propertyValue = device.getProperty(mPropertyName);
58 
59         if (mPropertyValueIsSetOnly && mExpectedPropertyValue != null) {
60             throw new IllegalArgumentException(
61                     "is-set-only and " + "expected-value cannot both be set.");
62         } else if (!mPropertyValueIsSetOnly && mExpectedPropertyValue == null) {
63             throw new IllegalArgumentException("is-set-only or expected-value must be set.");
64         }
65 
66         if (mPropertyValueIsSetOnly) {
67             if (propertyValue == null || propertyValue.equals("")) {
68                 String msg =
69                         String.format(
70                                 "Property \"%s\" not found or not set on device", mPropertyName);
71                 // Handle missing property with either exception or warning
72                 if (mThrowError) {
73                     throw new TargetSetupError(msg, device.getDeviceDescriptor());
74                 } else {
75                     logWarning(msg);
76                 }
77             }
78             return;
79         }
80 
81         if (propertyValue == null) {
82             CLog.w(
83                     "Property \"%s\" not found on device, cannot verify value \"%s\" ",
84                     mPropertyName, mExpectedPropertyValue);
85             return;
86         }
87 
88         if (!mExpectedPropertyValue.equalsIgnoreCase(propertyValue)) {
89             String msg = String.format("Expected \"%s\" but found \"%s\" for property: %s",
90                     mExpectedPropertyValue, propertyValue, mPropertyName);
91             // Handle unexpected property value with either exception or warning
92             if(mThrowError) {
93                 throw new TargetSetupError(msg, device.getDeviceDescriptor());
94             } else {
95                 CLog.w(msg);
96             }
97         }
98     }
99 
100 }
101