dcashman | 9b61575 | 2015-01-07 14:23:11 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 2 | |
| 3 | src_header = """/* |
| 4 | * Copyright (C) 2014 The Android Open Source Project |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | package android.cts.security; |
| 20 | |
Changfei Chen | 178b43b | 2016-12-05 18:13:06 -0800 | [diff] [blame] | 21 | import android.platform.test.annotations.RestrictedBuildTest; |
Aaron Holden | d16ae8f | 2016-11-22 18:44:36 -0800 | [diff] [blame] | 22 | import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper; |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 23 | import com.android.tradefed.build.IBuildInfo; |
| 24 | import com.android.tradefed.device.ITestDevice; |
| 25 | import com.android.tradefed.testtype.DeviceTestCase; |
| 26 | import com.android.tradefed.testtype.IBuildReceiver; |
dcashman | 4371f00 | 2016-03-29 10:42:03 -0700 | [diff] [blame] | 27 | import com.android.tradefed.testtype.IDeviceTest; |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 28 | |
| 29 | import java.io.BufferedReader; |
| 30 | import java.io.File; |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 31 | import java.io.InputStream; |
| 32 | import java.io.InputStreamReader; |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Neverallow Rules SELinux tests. |
| 36 | */ |
dcashman | 4371f00 | 2016-03-29 10:42:03 -0700 | [diff] [blame] | 37 | public class SELinuxNeverallowRulesTest extends DeviceTestCase implements IBuildReceiver, IDeviceTest { |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 38 | private File sepolicyAnalyze; |
| 39 | private File devicePolicyFile; |
| 40 | |
dcashman | 9cf20df | 2016-04-01 11:32:35 -0700 | [diff] [blame] | 41 | private IBuildInfo mBuild; |
| 42 | |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 43 | /** |
| 44 | * A reference to the device under test. |
| 45 | */ |
| 46 | private ITestDevice mDevice; |
| 47 | |
dcashman | 4371f00 | 2016-03-29 10:42:03 -0700 | [diff] [blame] | 48 | /** |
| 49 | * {@inheritDoc} |
| 50 | */ |
| 51 | @Override |
| 52 | public void setBuild(IBuildInfo build) { |
dcashman | 9cf20df | 2016-04-01 11:32:35 -0700 | [diff] [blame] | 53 | mBuild = build; |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 54 | } |
| 55 | |
dcashman | 4371f00 | 2016-03-29 10:42:03 -0700 | [diff] [blame] | 56 | /** |
| 57 | * {@inheritDoc} |
| 58 | */ |
| 59 | @Override |
| 60 | public void setDevice(ITestDevice device) { |
| 61 | super.setDevice(device); |
| 62 | mDevice = device; |
| 63 | } |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 64 | @Override |
| 65 | protected void setUp() throws Exception { |
| 66 | super.setUp(); |
Aaron Holden | d16ae8f | 2016-11-22 18:44:36 -0800 | [diff] [blame] | 67 | CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mBuild); |
| 68 | sepolicyAnalyze = buildHelper.getTestFile("sepolicy-analyze"); |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 69 | sepolicyAnalyze.setExecutable(true); |
| 70 | |
Alex Klyubin | e91509c | 2017-04-14 11:17:19 -0700 | [diff] [blame] | 71 | devicePolicyFile = android.security.cts.SELinuxHostTest.getDevicePolicyFile(mDevice); |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 72 | } |
Alex Klyubin | 9dd67db | 2017-04-06 20:14:43 -0700 | [diff] [blame] | 73 | |
| 74 | private boolean isFullTrebleDevice() throws Exception { |
| 75 | return android.security.cts.SELinuxHostTest.isFullTrebleDevice(mDevice); |
| 76 | } |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 77 | """ |
| 78 | src_body = "" |
| 79 | src_footer = """} |
| 80 | """ |
| 81 | |
| 82 | src_method = """ |
Changfei Chen | 178b43b | 2016-12-05 18:13:06 -0800 | [diff] [blame] | 83 | @RestrictedBuildTest |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 84 | public void testNeverallowRules() throws Exception { |
| 85 | String neverallowRule = "$NEVERALLOW_RULE_HERE$"; |
Alex Klyubin | 9dd67db | 2017-04-06 20:14:43 -0700 | [diff] [blame] | 86 | boolean fullTrebleOnly = $FULL_TREBLE_ONLY_BOOL_HERE$; |
| 87 | |
| 88 | if ((fullTrebleOnly) && (!isFullTrebleDevice())) { |
| 89 | // This test applies only to Treble devices but this device isn't one |
| 90 | return; |
| 91 | } |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 92 | |
| 93 | /* run sepolicy-analyze neverallow check on policy file using given neverallow rules */ |
| 94 | ProcessBuilder pb = new ProcessBuilder(sepolicyAnalyze.getAbsolutePath(), |
Alex Klyubin | cb20eaf | 2017-04-25 13:16:00 -0700 | [diff] [blame] | 95 | devicePolicyFile.getAbsolutePath(), "neverallow", "-w", "-n", |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 96 | neverallowRule); |
| 97 | pb.redirectOutput(ProcessBuilder.Redirect.PIPE); |
| 98 | pb.redirectErrorStream(true); |
| 99 | Process p = pb.start(); |
| 100 | p.waitFor(); |
| 101 | BufferedReader result = new BufferedReader(new InputStreamReader(p.getInputStream())); |
| 102 | String line; |
| 103 | StringBuilder errorString = new StringBuilder(); |
| 104 | while ((line = result.readLine()) != null) { |
| 105 | errorString.append(line); |
| 106 | errorString.append("\\n"); |
| 107 | } |
| 108 | assertTrue("The following errors were encountered when validating the SELinux" |
| 109 | + "neverallow rule:\\n" + neverallowRule + "\\n" + errorString, |
| 110 | errorString.length() == 0); |
| 111 | } |
| 112 | """ |