1 | /*
|
---|
2 | * Copyright 2003-2004 The Apache Software Foundation
|
---|
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 |
|
---|
18 | package org.apache.tools.ant.types;
|
---|
19 |
|
---|
20 | import junit.framework.TestCase;
|
---|
21 |
|
---|
22 | import org.apache.tools.ant.ExitException;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * JUnit 3 testcases for org.apache.tools.ant.types.Permissions.
|
---|
26 | *
|
---|
27 | */
|
---|
28 | public class PermissionsTest extends TestCase {
|
---|
29 |
|
---|
30 | Permissions perms;
|
---|
31 |
|
---|
32 | public PermissionsTest(String name) {
|
---|
33 | super(name);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public void setUp() {
|
---|
37 | perms = new Permissions();
|
---|
38 | Permissions.Permission perm = new Permissions.Permission();
|
---|
39 | // Grant extra permissions to read and write the user.* properties and read to the
|
---|
40 | // java.home property
|
---|
41 | perm.setActions("read, write");
|
---|
42 | perm.setName("user.*");
|
---|
43 | perm.setClass("java.util.PropertyPermission");
|
---|
44 | perms.addConfiguredGrant(perm);
|
---|
45 |
|
---|
46 | perm = new Permissions.Permission();
|
---|
47 | perm.setActions("read");
|
---|
48 | perm.setName("java.home");
|
---|
49 | perm.setClass("java.util.PropertyPermission");
|
---|
50 | perms.addConfiguredGrant(perm);
|
---|
51 |
|
---|
52 | perm = new Permissions.Permission();
|
---|
53 | perm.setActions("read");
|
---|
54 | perm.setName("file.encoding");
|
---|
55 | perm.setClass("java.util.PropertyPermission");
|
---|
56 | perms.addConfiguredGrant(perm);
|
---|
57 |
|
---|
58 | // Revoke permission to write user.home (granted above via user.*), still able to read though.
|
---|
59 | // and the default granted permission to read os.name.
|
---|
60 | perm = new Permissions.Permission();
|
---|
61 | perm.setActions("write");
|
---|
62 | perm.setName("user.home");
|
---|
63 | perm.setClass("java.util.PropertyPermission");
|
---|
64 | perms.addConfiguredRevoke(perm);
|
---|
65 |
|
---|
66 | perm = new Permissions.Permission();
|
---|
67 | perm.setActions("read");
|
---|
68 | perm.setName("os.*");
|
---|
69 | perm.setClass("java.util.PropertyPermission");
|
---|
70 | perms.addConfiguredRevoke(perm);
|
---|
71 | }
|
---|
72 |
|
---|
73 | /** Tests a permission that is granted per default. */
|
---|
74 | public void testDefaultGranted() {
|
---|
75 | perms.setSecurityManager();
|
---|
76 | try {
|
---|
77 | String s = System.getProperty("line.separator");
|
---|
78 | } finally {
|
---|
79 | perms.restoreSecurityManager();
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | /** Tests a permission that has been granted later via wildcard. */
|
---|
84 | public void testGranted() {
|
---|
85 | perms.setSecurityManager();
|
---|
86 | try {
|
---|
87 | String s = System.getProperty("user.name");
|
---|
88 | System.setProperty("user.name", s);
|
---|
89 | } finally {
|
---|
90 | perms.restoreSecurityManager();
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | /** Tests a permission that has been granted and revoked later. */
|
---|
95 | public void testGrantedAndRevoked() {
|
---|
96 | perms.setSecurityManager();
|
---|
97 | try {
|
---|
98 | String s = System.getProperty("user.home");
|
---|
99 | System.setProperty("user.home", s);
|
---|
100 | fail("Could perform an action that should have been forbidden.");
|
---|
101 | } catch (SecurityException e){
|
---|
102 | // Was expected, test passes
|
---|
103 | } finally {
|
---|
104 | perms.restoreSecurityManager();
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | /** Tests a permission that is granted as per default but revoked later via wildcard. */
|
---|
109 | public void testDefaultRevoked() {
|
---|
110 | perms.setSecurityManager();
|
---|
111 | try {
|
---|
112 | System.getProperty("os.name");
|
---|
113 | fail("Could perform an action that should have been forbidden.");
|
---|
114 | } catch (SecurityException e){
|
---|
115 | // Was expected, test passes
|
---|
116 | } finally {
|
---|
117 | perms.restoreSecurityManager();
|
---|
118 | }
|
---|
119 | }
|
---|
120 | /** Tests a permission that has not been granted or revoked. */
|
---|
121 | public void testOther() {
|
---|
122 | String ls = System.getProperty("line.separator");
|
---|
123 | perms.setSecurityManager();
|
---|
124 | try {
|
---|
125 | String s = System.setProperty("line.separator",ls);
|
---|
126 | fail("Could perform an action that should have been forbidden.");
|
---|
127 | } catch (SecurityException e){
|
---|
128 | // Was expected, test passes
|
---|
129 | } finally {
|
---|
130 | perms.restoreSecurityManager();
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | /** Tests an exit condition. */
|
---|
135 | public void testExit() {
|
---|
136 | perms.setSecurityManager();
|
---|
137 | try {
|
---|
138 | System.out.println("If this is the last line on standard out the testExit f.a.i.l.e.d");
|
---|
139 | System.exit(3);
|
---|
140 | fail("Totaly impossible that this fail is ever executed. Please let me know if it is!");
|
---|
141 | } catch (ExitException e) {
|
---|
142 | if (e.getStatus() != 3) {
|
---|
143 | fail("Received wrong exit status in Exit Exception.");
|
---|
144 | }
|
---|
145 | System.out.println("testExit successfull.");
|
---|
146 | } finally {
|
---|
147 | perms.restoreSecurityManager();
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | public void tearDown() {
|
---|
153 | }
|
---|
154 |
|
---|
155 | }
|
---|