Home | History | Annotate | Download | only in cts
      1 /*
      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 
     17 package android.security.cts;
     18 
     19 import android.content.Context;
     20 import android.content.res.AssetManager;
     21 import android.platform.test.annotations.SecurityTest;
     22 import android.test.AndroidTestCase;
     23 
     24 import junit.framework.TestCase;
     25 import org.xmlpull.v1.XmlPullParserException;
     26 
     27 import java.io.File;
     28 import java.io.IOException;
     29 import java.io.InputStream;
     30 import java.util.ArrayList;
     31 import java.util.List;
     32 import java.util.Collection;
     33 import java.util.HashMap;
     34 import java.util.Map;
     35 import java.util.HashSet;
     36 
     37 /**
     38  * Verify that the SELinux configuration is sane.
     39  */
     40 @SecurityTest
     41 public class SELinuxTest extends AndroidTestCase {
     42 
     43     static {
     44         System.loadLibrary("ctssecurity_jni");
     45     }
     46 
     47     public void testCTSIsUntrustedApp() throws IOException {
     48         String found = KernelSettingsTest.getFile("/proc/self/attr/current");
     49         String expected = "u:r:untrusted_app:s0";
     50         String msg = "Expected prefix context: \"" + expected + "\"" +
     51                         ", Found: \"" + found + "\"";
     52         assertTrue(msg, found.startsWith(expected));
     53     }
     54 
     55     public void testCTSAppDataContext() throws Exception {
     56         File appDataDir = getContext().getFilesDir();
     57         String found = getFileContext(appDataDir.getAbsolutePath());
     58         String expected = "u:object_r:app_data_file:s0";
     59         String msg = "Expected prefix context: \"" + expected + "\"" +
     60                         ", Found: \"" + found + "\"";
     61         assertTrue(msg, found.startsWith(expected));
     62     }
     63 
     64     public void testFileContexts() throws Exception {
     65         assertEquals(getFileContext("/"), "u:object_r:rootfs:s0");
     66         assertEquals(getFileContext("/dev"), "u:object_r:device:s0");
     67         assertEquals(getFileContext("/dev/socket"), "u:object_r:socket_device:s0");
     68         assertEquals(getFileContext("/dev/binder"), "u:object_r:binder_device:s0");
     69         assertEquals(getFileContext("/system"), "u:object_r:system_file:s0");
     70         assertEquals(getFileContext("/system/bin/app_process"), "u:object_r:zygote_exec:s0");
     71         assertEquals(getFileContext("/data"), "u:object_r:system_data_file:s0");
     72         assertEquals(getFileContext("/data/app"), "u:object_r:apk_data_file:s0");
     73         assertEquals(getFileContext("/data/local/tmp"), "u:object_r:shell_data_file:s0");
     74         assertEquals(getFileContext("/sys"), "u:object_r:sysfs:s0");
     75         File dir = new File("/cache");
     76         if (dir.exists()) {
     77             assertEquals(getFileContext("/cache"), "u:object_r:cache_file:s0");
     78         }
     79     }
     80 
     81     private static final native String getFileContext(String path);
     82 }
     83