Julien Desprez | 125891a | 2019-02-22 13:52:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.tradefed.suite.checker; |
| 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.suite.checker.StatusCheckerResult.CheckStatus; |
| 22 | |
| 23 | /** Status checker that ensures the status of Selinux. */ |
| 24 | public class EnforcedSeLinuxChecker implements ISystemStatusChecker { |
| 25 | |
| 26 | private static final String ENFORCING_STRING = "enforcing"; |
| 27 | |
| 28 | @Option( |
| 29 | name = "expect-enforced", |
| 30 | description = "Controls whether or not Selinux is enforced between modules." |
| 31 | ) |
| 32 | private boolean mExpectedEnforced = false; |
| 33 | |
| 34 | /** {@inheritDoc} */ |
| 35 | @Override |
| 36 | public StatusCheckerResult postExecutionCheck(ITestDevice device) |
| 37 | throws DeviceNotAvailableException { |
| 38 | StatusCheckerResult result = new StatusCheckerResult(CheckStatus.SUCCESS); |
| 39 | if (mExpectedEnforced && !isEnforced(device)) { |
| 40 | result.setStatus(CheckStatus.FAILED); |
| 41 | result.setErrorMessage("Device is in permissive mode while enforced was expected."); |
| 42 | setEnforced(device, true); |
| 43 | } else if (!mExpectedEnforced && isEnforced(device)) { |
| 44 | result.setStatus(CheckStatus.FAILED); |
| 45 | result.setErrorMessage("Device is in enforced mode while permissive was expected."); |
| 46 | setEnforced(device, false); |
| 47 | } |
| 48 | return result; |
| 49 | } |
| 50 | |
| 51 | private boolean isEnforced(ITestDevice device) throws DeviceNotAvailableException { |
| 52 | String result = device.executeShellCommand("getenforce"); |
| 53 | if (ENFORCING_STRING.equals(result.toLowerCase().trim())) { |
| 54 | return true; |
| 55 | } |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | private void setEnforced(ITestDevice device, boolean enforced) |
| 60 | throws DeviceNotAvailableException { |
| 61 | String enforce = enforced ? "1" : "0"; |
Naga Venkata Durga Ashok Mutyala | 1b46c0a | 2019-11-07 14:50:24 +0530 | [diff] [blame] | 62 | device.executeShellCommand("su root setenforce " + enforce); |
Julien Desprez | 125891a | 2019-02-22 13:52:23 -0800 | [diff] [blame] | 63 | } |
| 64 | } |