blob: 313a9232723dbdc941c4e937d0b6db9c7e3df002 [file] [log] [blame]
Julien Desprez5e6aa7e2018-12-06 11:08:43 -08001/*
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 */
16package com.android.tradefed.suite.checker;
17
18import com.android.tradefed.config.Option;
19import com.android.tradefed.config.OptionClass;
20import com.android.tradefed.device.DeviceNotAvailableException;
21import com.android.tradefed.device.ITestDevice;
22import com.android.tradefed.log.LogUtil.CLog;
23import com.android.tradefed.suite.checker.StatusCheckerResult.CheckStatus;
24
25/**
26 * Check if the shell status is as expected before and after a module run. Any changes may
27 * unexpectedly affect test cases.
28 *
29 * <p>There is a command-line option to disable the checker entirely:
30 *
31 * <pre>--skip-system-status-check=com.android.tradefed.suite.checker.ShellStatusChecker
32 * </pre>
33 */
34@OptionClass(alias = "shell-status-checker")
35public class ShellStatusChecker implements ISystemStatusChecker {
36
37 private static final String FAIL_STRING = " Reset failed.";
38
39 /* Option for the expected root state at pre- and post-check. */
40 @Option(
41 name = "expect-root",
42 description =
43 "Controls whether the shell root status is expected to be "
44 + "root ('true') or non-root ('false'). "
45 + "The checker will warn and try to revert the state "
46 + "if not as expected at pre- or post-check."
47 )
48 private boolean mExpectedRoot = false;
49
50 /** {@inheritDoc} */
51 @Override
52 public StatusCheckerResult preExecutionCheck(ITestDevice device)
53 throws DeviceNotAvailableException {
54 StatusCheckerResult result = new StatusCheckerResult(CheckStatus.SUCCESS);
55 if (mExpectedRoot != device.isAdbRoot()) {
56 String message =
57 "This module unexpectedly started in a "
58 + (mExpectedRoot ? "non-root" : "root")
59 + " shell. Leaked from earlier module?";
60 result.setStatus(CheckStatus.FAILED);
61
62 boolean reset;
63 if (mExpectedRoot) {
64 reset = device.enableAdbRoot();
65 } else {
66 reset = device.disableAdbRoot();
67 }
68 if (!reset) {
69 message += FAIL_STRING;
70 }
71 CLog.w(message);
72 result.setErrorMessage(message);
73 }
74
75 return result;
76 }
77
78 /** {@inheritDoc} */
79 @Override
80 public StatusCheckerResult postExecutionCheck(ITestDevice device)
81 throws DeviceNotAvailableException {
82 StatusCheckerResult result = new StatusCheckerResult(CheckStatus.SUCCESS);
83 if (mExpectedRoot != device.isAdbRoot()) {
84 String message =
85 "This module changed shell root status to "
86 + (mExpectedRoot ? "non-root" : "root")
87 + ". Leaked from a test case or setup?";
88 result.setStatus(CheckStatus.FAILED);
89
90 boolean reset;
91 if (mExpectedRoot) {
92 reset = device.enableAdbRoot();
93 } else {
94 reset = device.disableAdbRoot();
95 }
96 if (!reset) {
97 message += FAIL_STRING;
98 }
99 CLog.w(message);
100 result.setErrorMessage(message);
101 }
102
103 return result;
104 }
105}