summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/tests/security/AccessControllerTest.java
blob: e031b5e025674de005744faea9ca543e02762afb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package tests.security;

import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.BasicPermission;
import java.security.CodeSource;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;

import junit.framework.TestCase;

public class AccessControllerTest extends TestCase {

    private static void setProtectionDomain(Class<?> c, ProtectionDomain pd){
        Field fields[] = Class.class.getDeclaredFields();
        for(Field f : fields){
            if("pd".equals(f.getName())){
                f.setAccessible(true);
                try {
                    f.set(c, pd);
                } catch (IllegalArgumentException e) {
                    fail("Protection domain could not be set");
                } catch (IllegalAccessException e) {
                    fail("Protection domain could not be set");
                }
                break;
            }
        }
    }

    SecurityManager old;
    TestPermission p;
    CodeSource codeSource;
    PermissionCollection c0, c1, c2;

    @Override
    protected void setUp() throws Exception {
        old = System.getSecurityManager();
        codeSource = null;
        p = new TestPermission();
        c0 = p.newPermissionCollection();
        c1 = p.newPermissionCollection();
        c2 = p.newPermissionCollection();
        super.setUp();
    }

    public void test_do_privileged2() {
        // add TestPermission to T0, T1, T2
        c0.add(p);
        c1.add(p);
        c2.add(p);
        setProtectionDomain(T0.class, new ProtectionDomain(codeSource, c0));
        setProtectionDomain(T1.class, new ProtectionDomain(codeSource, c1));
        setProtectionDomain(T2.class, new ProtectionDomain(codeSource, c2));
    }

    static class T0 {
        static String f0(){
            return T1.f1();
        }
        static String f0_priv(){
            return T1.f1_priv();
        }
    }

    static class T1 {
        static String f1(){
            return T2.f2();
        }
        static String f1_priv(){
            return AccessController.doPrivileged(
                new PrivilegedAction<String>(){
                    public String run() {
                        return T2.f2();
                    }
                }
            );
        }
    }

    static class T2 {
        static String f2(){
            SecurityManager s = System.getSecurityManager();
            assertNotNull(s);
            s.checkPermission(new TestPermission());
            return "ok";
        }
    }

    static class TestPermission extends BasicPermission {
        private static final long serialVersionUID = 1L;

        public TestPermission(){ super("TestPermission"); }

        @Override
        public boolean implies(Permission permission) {
            return permission instanceof TestPermission;
        }
    }

}