dcashman | fb28897 | 2014-10-24 16:16:30 -0700 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 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 | |
| 21 | import com.android.cts.tradefed.build.CtsBuildHelper; |
| 22 | import com.android.tradefed.build.IBuildInfo; |
| 23 | import com.android.tradefed.device.ITestDevice; |
| 24 | import com.android.tradefed.testtype.DeviceTestCase; |
| 25 | import com.android.tradefed.testtype.IBuildReceiver; |
| 26 | |
| 27 | import java.io.BufferedReader; |
| 28 | import java.io.File; |
| 29 | import java.io.IOException; |
| 30 | import java.io.InputStream; |
| 31 | import java.io.InputStreamReader; |
| 32 | import java.io.FileOutputStream; |
| 33 | import java.lang.String; |
| 34 | import java.net.URL; |
| 35 | import java.util.Scanner; |
| 36 | |
| 37 | /** |
| 38 | * Neverallow Rules SELinux tests. |
| 39 | */ |
| 40 | public class SELinuxNeverallowRulesTest extends DeviceTestCase { |
| 41 | private File sepolicyAnalyze; |
| 42 | private File devicePolicyFile; |
| 43 | |
| 44 | /** |
| 45 | * A reference to the device under test. |
| 46 | */ |
| 47 | private ITestDevice mDevice; |
| 48 | |
| 49 | private File copyResourceToTempFile(String resName) throws IOException { |
| 50 | InputStream is = this.getClass().getResourceAsStream(resName); |
| 51 | File tempFile = File.createTempFile("SELinuxHostTest", ".tmp"); |
| 52 | FileOutputStream os = new FileOutputStream(tempFile); |
| 53 | int rByte = 0; |
| 54 | while ((rByte = is.read()) != -1) { |
| 55 | os.write(rByte); |
| 56 | } |
| 57 | os.flush(); |
| 58 | os.close(); |
| 59 | tempFile.deleteOnExit(); |
| 60 | return tempFile; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | protected void setUp() throws Exception { |
| 65 | super.setUp(); |
| 66 | mDevice = getDevice(); |
| 67 | |
| 68 | /* retrieve the sepolicy-analyze executable from jar */ |
| 69 | sepolicyAnalyze = copyResourceToTempFile("/sepolicy-analyze"); |
| 70 | sepolicyAnalyze.setExecutable(true); |
| 71 | |
| 72 | /* obtain sepolicy file from running device */ |
| 73 | devicePolicyFile = File.createTempFile("sepolicy", ".tmp"); |
| 74 | devicePolicyFile.deleteOnExit(); |
| 75 | mDevice.executeAdbCommand("pull", "/sys/fs/selinux/policy", |
| 76 | devicePolicyFile.getAbsolutePath()); |
| 77 | } |
| 78 | """ |
| 79 | src_body = "" |
| 80 | src_footer = """} |
| 81 | """ |
| 82 | |
| 83 | src_method = """ |
| 84 | public void testNeverallowRules() throws Exception { |
| 85 | String neverallowRule = "$NEVERALLOW_RULE_HERE$"; |
| 86 | |
| 87 | /* run sepolicy-analyze neverallow check on policy file using given neverallow rules */ |
| 88 | ProcessBuilder pb = new ProcessBuilder(sepolicyAnalyze.getAbsolutePath(), |
| 89 | devicePolicyFile.getAbsolutePath(), "neverallow", "-n", |
| 90 | neverallowRule); |
| 91 | pb.redirectOutput(ProcessBuilder.Redirect.PIPE); |
| 92 | pb.redirectErrorStream(true); |
| 93 | Process p = pb.start(); |
| 94 | p.waitFor(); |
| 95 | BufferedReader result = new BufferedReader(new InputStreamReader(p.getInputStream())); |
| 96 | String line; |
| 97 | StringBuilder errorString = new StringBuilder(); |
| 98 | while ((line = result.readLine()) != null) { |
| 99 | errorString.append(line); |
| 100 | errorString.append("\\n"); |
| 101 | } |
| 102 | assertTrue("The following errors were encountered when validating the SELinux" |
| 103 | + "neverallow rule:\\n" + neverallowRule + "\\n" + errorString, |
| 104 | errorString.length() == 0); |
| 105 | } |
| 106 | """ |