001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Alexey V. Varlamov
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.fortress;
022:
023: import java.io.File;
024: import java.net.URL;
025: import java.security.AllPermission;
026: import java.security.Permission;
027: import java.security.PermissionCollection;
028: import java.security.Security;
029: import java.security.SecurityPermission;
030: import java.security.UnresolvedPermission;
031: import java.util.Arrays;
032: import java.util.Collection;
033: import java.util.Enumeration;
034: import java.util.HashSet;
035: import java.util.Properties;
036:
037: import org.apache.harmony.security.fortress.PolicyUtils;
038: import junit.framework.TestCase;
039:
040: /**
041: * Tests for <code>PolicyUtils</code> class fields and methods.
042: *
043: */
044:
045: public class PolicyUtilsTest extends TestCase {
046:
047: /** Tests valid expansion of ${key} entries. */
048: public void testExpand() throws Exception {
049: String[] input = new String[] { "${key.1}", "abcd${key.1}",
050: "a ${key.1} b ${$key$}${key.2}", "$key.1", "${}" };
051: String[] output = new String[] { "value1", "abcdvalue1",
052: "a value1 b ${${${${${${value.2", "$key.1", "" };
053: Properties props = new Properties();
054: props.put("key.1", "value1");
055: props.put("key.2", "value.2");
056: props.put("$key$", "${${${${${${");
057: props.put("", "");
058: for (int i = 0; i < output.length; i++) {
059: assertEquals(output[i], PolicyUtils.expand(input[i], props));
060: }
061: }
062:
063: /** Tests ExpansionFailedException for missing keys of ${key} entries. */
064: public void testExpandFailed() throws Exception {
065: try {
066: PolicyUtils.expand("${key.123}", new Properties());
067: fail("Should throw ExpansionFailedException");
068: } catch (PolicyUtils.ExpansionFailedException ok) {
069: }
070: }
071:
072: /** Tests valid URL-specific expansion. */
073: public void testExpandURL() throws Exception {
074: String input = "file:/${my.home}" + File.separator
075: + "lib/extensions/";
076: Properties props = new Properties();
077: String q = File.separator + "drl" + File.separator + "tools1.2";
078: props.put("my.home", q);
079: assertEquals("file://drl/tools1.2/lib/extensions/", PolicyUtils
080: .expandURL(input, props));
081: }
082:
083: /** Tests valid expansion of ${{protocol:data}} entries. */
084: public void testExpandGeneral() throws Exception {
085: String[] input = new String[] { "${{a:b}}",
086: "a ${{self}}${{a: made}}", "${{}}" };
087: String[] output = new String[] { "b", "a this made", "" };
088: PolicyUtils.GeneralExpansionHandler handler = new PolicyUtils.GeneralExpansionHandler() {
089:
090: public String resolve(String protocol, String data) {
091: if ("a".equals(protocol)) {
092: return data;
093: }
094: if ("self".equals(protocol)) {
095: return "this";
096: }
097: if ("".equals(protocol)) {
098: return protocol;
099: }
100: return null;
101: }
102: };
103: for (int i = 0; i < output.length; i++) {
104: assertEquals(output[i], PolicyUtils.expandGeneral(input[i],
105: handler));
106: }
107: }
108:
109: /**
110: * Tests ExpansionFailedException for undefined protocol
111: * of ${{protocol:data}} entries.
112: */
113: public void testExpandGeneralFailed() throws Exception {
114: try {
115: PolicyUtils.expandGeneral("${{c}}",
116: new PolicyUtils.GeneralExpansionHandler() {
117:
118: public String resolve(String protocol,
119: String data)
120: throws PolicyUtils.ExpansionFailedException {
121: throw new PolicyUtils.ExpansionFailedException(
122: "");
123: }
124: });
125: fail("Should throw ExpansionFailedException");
126: } catch (PolicyUtils.ExpansionFailedException ok) {
127: }
128: }
129:
130: /**
131: * Tests positive/negative/invalid/missing values of
132: * "policy.expandProperties" security property.
133: */
134: public void testCanExpandProperties() {
135: final String key = "policy.expandProperties";
136: String OLD = Security.getProperty(key);
137: try {
138: Security.setProperty(key, "true");
139: assertTrue(PolicyUtils.canExpandProperties());
140: Security.setProperty(key, "false");
141: assertFalse(PolicyUtils.canExpandProperties());
142: Security.setProperty(key, "");
143: assertTrue(PolicyUtils.canExpandProperties());
144: Security.setProperty(key, "laejhg");
145: assertTrue(PolicyUtils.canExpandProperties());
146: } finally {
147: Security.setProperty(key, OLD);
148: }
149: }
150:
151: /**
152: * Tests cases of enabled/disabled system URL.
153: */
154: public void testGetPolicyURLs01() throws Throwable {
155: final String KEY_DYNAMIC = "policy.allowSystemProperty";
156: String OLD_DYNAMIC = Security.getProperty(KEY_DYNAMIC);
157:
158: final String KEY = "dsfvdf";
159: Properties arg = new Properties();
160: arg.put(KEY, "http://foo.bar.com");
161: try {
162: Security.setProperty(KEY_DYNAMIC, "true");
163: URL[] result = PolicyUtils.getPolicyURLs(arg, KEY, "");
164: assertNotNull(result);
165: assertEquals(1, result.length);
166: assertEquals(new URL("http://foo.bar.com"), result[0]);
167:
168: Security.setProperty(KEY_DYNAMIC, "false");
169: result = PolicyUtils.getPolicyURLs(arg, KEY, "");
170: assertNotNull(result);
171: assertEquals(0, result.length);
172:
173: Security.setProperty(KEY_DYNAMIC, "");
174: result = PolicyUtils.getPolicyURLs(arg, KEY, "");
175: assertNotNull(result);
176: assertEquals(1, result.length);
177: assertEquals(new URL("http://foo.bar.com"), result[0]);
178: } finally {
179: Security.setProperty(KEY_DYNAMIC, OLD_DYNAMIC);
180: }
181: }
182:
183: /** Tests finding algorithm for numbered locations in security properties. */
184: public void testGetPolicyURLs02() throws Throwable {
185: final String PREFIX = "testGetPolicyURLs02.";
186: String[] OLD = new String[5];
187: for (int i = 0; i < OLD.length; i++) {
188: OLD[i] = Security.getProperty(PREFIX + i);
189: }
190:
191: try {
192: Security.setProperty(PREFIX + 0, "http://foo0.bar.com");
193: Security.setProperty(PREFIX + 1, "http://foo1.bar.com");
194: Security.setProperty(PREFIX + 2, "http://foo2.bar.com");
195: Security.setProperty(PREFIX + 4, "http://foo4.bar.com");
196: URL[] result = PolicyUtils.getPolicyURLs(new Properties(),
197: "", PREFIX);
198: assertNotNull(result);
199: assertEquals(2, result.length);
200: assertEquals(new URL("http://foo1.bar.com"), result[0]);
201: assertEquals(new URL("http://foo2.bar.com"), result[1]);
202:
203: Security.setProperty(PREFIX + 1, "slkjdfhk/svfv*&^");
204: Security.setProperty(PREFIX + 3, "dlkfjvb3lk5jt");
205: result = PolicyUtils.getPolicyURLs(new Properties(), "",
206: PREFIX);
207: assertNotNull(result);
208: assertEquals(2, result.length);
209: assertEquals(new URL("http://foo2.bar.com"), result[0]);
210: assertEquals(new URL("http://foo4.bar.com"), result[1]);
211: } finally {
212: for (int i = 0; i < OLD.length; i++) {
213: Security.setProperty(PREFIX + i, (OLD[i] == null) ? ""
214: : OLD[i]);
215: }
216: }
217: }
218:
219: /**
220: * Tests expansion in system and security URLs.
221: */
222: public void testGetPolicyURLs03() throws Throwable {
223: final String KEY_DYNAMIC = "policy.allowSystemProperty";
224: final String OLD_DYNAMIC = Security.getProperty(KEY_DYNAMIC);
225: final String KEY_EXP = "policy.expandProperties";
226: final String OLD_EXP = Security.getProperty(KEY_EXP);
227: final String PREFIX = "testGetPolicyURLs03.";
228: String[] OLD = new String[5];
229: for (int i = 0; i < OLD.length; i++) {
230: OLD[i] = Security.getProperty(PREFIX + i);
231: }
232:
233: final String KEY = "dsfvdf";
234: Properties arg = new Properties();
235: arg.put(KEY, "file://${foo.path}/${foo.name}");
236: arg.put("foo.path", "path");
237: arg.put("foo.name", "name");
238: arg.put("foo", "acme");
239: Security.setProperty(KEY_DYNAMIC, "true");
240: Security.setProperty(KEY_EXP, "true");
241: Security.setProperty(PREFIX + 1, "http://foo0.${foo}.org");
242: Security.setProperty(PREFIX + 2, "http://${bar}.com");
243: Security.setProperty(PREFIX + 3,
244: "http://foo2.bar.com/${foo.path}/${foo.name}");
245: try {
246:
247: URL[] result = PolicyUtils.getPolicyURLs(arg, KEY, PREFIX);
248: assertNotNull(result);
249: assertEquals(3, result.length);
250: assertEquals(new URL("http://foo0.acme.org"), result[0]);
251: assertEquals(new URL("http://foo2.bar.com/path/name"),
252: result[1]);
253: assertEquals(new URL("file://path/name"), result[2]);
254:
255: //expansion here cannot be switched off
256: Security.setProperty(KEY_EXP, "false");
257: result = PolicyUtils.getPolicyURLs(arg, KEY, PREFIX);
258: assertNotNull(result);
259: assertEquals(3, result.length);
260: assertEquals(new URL("http://foo0.acme.org"), result[0]);
261: assertEquals(new URL("http://foo2.bar.com/path/name"),
262: result[1]);
263: assertEquals(new URL("file://path/name"), result[2]);
264: } finally {
265: Security.setProperty(KEY_DYNAMIC, OLD_DYNAMIC);
266: Security.setProperty(KEY_EXP, OLD_EXP);
267: for (int i = 0; i < OLD.length; i++) {
268: Security.setProperty(PREFIX + i, (OLD[i] == null) ? ""
269: : OLD[i]);
270: }
271: }
272: }
273:
274: /** Tests conversion of null, empty and non-empty heterogeneous collections. */
275: public void testToPermissionCollection() {
276: Permission p1 = new SecurityPermission("abc");
277: Permission p2 = new AllPermission();
278: Collection c1 = Arrays.asList(new Permission[] { p1, p2, });
279:
280: PermissionCollection pc = PolicyUtils
281: .toPermissionCollection(null);
282: assertNotNull(pc);
283: assertFalse(pc.elements().hasMoreElements());
284:
285: pc = PolicyUtils.toPermissionCollection(new HashSet());
286: assertNotNull(pc);
287: assertFalse(pc.elements().hasMoreElements());
288:
289: pc = PolicyUtils.toPermissionCollection(c1);
290: assertNotNull(pc);
291: Enumeration en = pc.elements();
292: Collection c2 = new HashSet();
293: c2.add(en.nextElement());
294: c2.add(en.nextElement());
295: assertFalse(en.hasMoreElements());
296: assertTrue(c2.contains(p1));
297: assertTrue(c2.contains(p2));
298: }
299:
300: public void testInstantiatePermission() throws Throwable {
301: String name = "abc";
302: Permission expected = new SecurityPermission(name);
303: //test valid input
304: assertEquals(expected, PolicyUtils.instantiatePermission(
305: SecurityPermission.class, name, null));
306: assertEquals(expected, PolicyUtils.instantiatePermission(
307: SecurityPermission.class, name, "4t46"));
308:
309: //test invalid class
310: try {
311: PolicyUtils.instantiatePermission(
312: UnresolvedPermission.class, null, null);
313: fail("IllegalArgumentException expected on invalid class argument");
314: } catch (IllegalArgumentException ok) {
315: }
316: }
317:
318: /**
319: * Tests various combinations of arrays:
320: * null/empty/containing null/containing real objects.
321: */
322: public void testMatchSubset() {
323: assertTrue(PolicyUtils.matchSubset(null, null));
324: assertTrue(PolicyUtils.matchSubset(new Object[] {}, null));
325: assertTrue(PolicyUtils.matchSubset(new Object[] { null }, null));
326: assertTrue(PolicyUtils.matchSubset(new Object[] {},
327: new Object[] { null }));
328: assertTrue(PolicyUtils.matchSubset(new Object[] { "1", "2" },
329: new Object[] { "3", "2", "1" }));
330: assertTrue(PolicyUtils.matchSubset(new Object[] { "1", null },
331: new Object[] { "3", "2", "1" }));
332: assertFalse(PolicyUtils.matchSubset(new Object[] { "1", null },
333: new Object[] { "3", "2", }));
334: }
335: }
|