1c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalleypackage android.os;
2c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley
3c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalleyimport android.os.Process;
4c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalleyimport android.os.SELinux;
5c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalleyimport android.test.AndroidTestCase;
6c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalleyimport static junit.framework.Assert.assertEquals;
7c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley
8c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalleypublic class SELinuxTest extends AndroidTestCase {
9c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley
10c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley    public void testgetFileCon() {
11c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley        if(SELinux.isSELinuxEnabled() == false)
12c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley            return;
13c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley
14c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley        String ctx = SELinux.getFileContext("/system/bin/toolbox");
15c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley        assertEquals(ctx, "u:object_r:system_file:s0");
16c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley    }
17c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley
18c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley    public void testgetCon() {
19c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley        if(SELinux.isSELinuxEnabled() == false)
20c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley            return;
21c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley
22c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley        String mycon = SELinux.getContext();
23c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley        assertEquals(mycon, "u:r:untrusted_app:s0:c33");
24c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley    }
25c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley
26c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley    public void testgetPidCon() {
27c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley        if(SELinux.isSELinuxEnabled() == false)
28c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley            return;
29c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley
30c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley        String mycon = SELinux.getPidContext(Process.myPid());
31c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley        assertEquals(mycon, "u:r:untrusted_app:s0:c33");
32c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley    }
33c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley
34c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley    public void testcheckSELinuxAccess() {
35c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley        if(SELinux.isSELinuxEnabled() == false)
36c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley            return;
37c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley
38c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley        String mycon = SELinux.getContext();
39c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley        boolean ret;
40c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley        ret = SELinux.checkSELinuxAccess(mycon, mycon, "process", "fork");
41c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley        assertEquals(ret,"true");
42c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley        ret = SELinux.checkSELinuxAccess(mycon, mycon, "memprotect", "mmap_zero");
43c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley        assertEquals(ret,"true");
44c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley    }
45c07fca3831baf4d812dd724f506b4ed23dcc39e0Stephen Smalley}
46