blob: 36d060b53ba2bf1f88f5b2edd782aa6045fdcb33 [file] [log] [blame]
dcashman5909bcd2014-08-21 15:33:24 -07001/*
2 * Copyright (C) 2014 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
dcashman13eae902014-10-16 10:04:21 -070017package android.cts.security;
dcashman5909bcd2014-08-21 15:33:24 -070018
19import com.android.cts.tradefed.build.CtsBuildHelper;
20import com.android.ddmlib.Log;
21import com.android.ddmlib.Log.LogLevel;
22import com.android.tradefed.build.IBuildInfo;
23import com.android.tradefed.device.ITestDevice;
24import com.android.tradefed.testtype.DeviceTestCase;
25import com.android.tradefed.testtype.IBuildReceiver;
26
27import java.io.BufferedReader;
28import java.io.File;
29import java.io.InputStream;
30import java.io.InputStreamReader;
31import java.io.FileOutputStream;
32import java.lang.String;
33import java.net.URL;
34import java.util.Scanner;
35
36/**
37 * Host-side SELinux tests.
38 *
39 * These tests analyze the policy file in use on the subject device directly or
40 * run as the shell user to evaluate aspects of the state of SELinux on the test
41 * device which otherwise would not be available to a normal apk.
42 */
43public class SELinuxHostTest extends DeviceTestCase {
44
45 /**
46 * A reference to the device under test.
47 */
48 private ITestDevice mDevice;
49
50 @Override
51 protected void setUp() throws Exception {
52 super.setUp();
53 mDevice = getDevice();
54 }
55
56 /**
57 * Tests that all domains in the running policy file are in enforcing mode
58 *
59 * @throws Exception
60 */
61 public void testAllEnforcing() throws Exception {
62
63 /* retrieve the sepolicy-analyze executable from jar */
64 InputStream is = this.getClass().getResourceAsStream("/sepolicy-analyze");
65 File execFile = File.createTempFile("sepolicy-analyze", ".tmp");
66 FileOutputStream os = new FileOutputStream(execFile);
67 int rByte = 0;
68 while ((rByte = is.read()) != -1) {
69 os.write(rByte);
70 }
71 os.flush();
72 os.close();
73 execFile.setExecutable(true);
74
75 /* obtain sepolicy file from running device */
76 File policyFile = File.createTempFile("sepolicy", ".tmp");
77 mDevice.executeAdbCommand("pull", "/sys/fs/selinux/policy", policyFile.getAbsolutePath());
78
79 /* run sepolicy-analyze permissive check on policy file */
80 ProcessBuilder pb = new ProcessBuilder(execFile.getAbsolutePath(), "-p", "-P",
81 policyFile.getAbsolutePath());
82 pb.redirectOutput(ProcessBuilder.Redirect.PIPE);
83 pb.redirectErrorStream(true);
84 Process p = pb.start();
85 p.waitFor();
86 BufferedReader result = new BufferedReader(new InputStreamReader(p.getInputStream()));
87 String line;
88 StringBuilder errorString = new StringBuilder();
89 while ((line = result.readLine()) != null) {
90 errorString.append(line);
91 errorString.append("\n");
92 }
93
94 /* clean up and check condition */
95 execFile.delete();
96 policyFile.delete();
97 assertTrue("The following SELinux domains were found to be in permissive mode:\n"
98 + errorString, errorString.length() == 0);
99 }
100}